arduino-2020-06-30-2320.py
01234567890123456789012345678901234567890123456789012345678901234567890123456789









4647484950515253545556575859606162636465   6667686970717273747576777879808182838485








680681682683684685686687688689690691692693694695696697698699700701702  703704  705706  707708  709710711712                   713714715    716                                         717718719720721722723724725726727728729730731732733                734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763   764765766767768769770771772773   774775776777778779780781782783784785786787788789790791792 793794795796    797798799800801   802803804805806807808809810811812813814815816817818819820821











                            <----SKIPPED LINES---->





  ARDUINO_ROLLING_LOG = WEBSERVER_PATH + ARDUINO_ROLLING_LOG
  ROLLING_SERIALS_LOG = WEBSERVER_PATH + ROLLING_SERIALS_LOG

CONNECTION_FLAG_BLUETOOTH = 1
CONNECTION_FLAG_USB = 2
CONNECTION_FLAG_SIMULATED = 3
RASPBERRY_PI = psutil.sys.platform.title() == 'Linux'

SN_SERVO = '5583834303435111C1A0'
SERVO_CONNECTION = (CONNECTION_FLAG_BLUETOOTH, (2, '98:D3:11:FC:42:16', 1))

SN_REMOTE = '75835343130351802272'
REMOTE_CONNECTION = (CONNECTION_FLAG_BLUETOOTH, (1, '98:D3:91:FD:B3:C9', 1))

LASER_OFF = (False, False, False)
LASER_ALL = (True, True, True)
LASER_RED = (True, False, False)
LASER_GREEN = (False, True, False)
LASER_BLUE = (False, False, True)




if SIMULATE_ARDUINO:
  SERVO_CONNECTION = (
      CONNECTION_FLAG_SIMULATED, (SERVO_SIMULATED_IN, SERVO_SIMULATED_OUT))
  REMOTE_CONNECTION = (
      CONNECTION_FLAG_SIMULATED, (REMOTE_SIMULATED_IN, REMOTE_SIMULATED_OUT))

KEY_NOT_PRESENT_STRING = 'N/A'

DISP_LAST_FLIGHT_NUMB_ORIG_DEST = 0
DISP_LAST_FLIGHT_AZIMUTH_ELEVATION = 1
DISP_FLIGHT_COUNT_LAST_SEEN = 2
DISP_RADIO_RANGE = 3
DISPLAY_MODE_NAMES = [
    'LAST_FLIGHT_NUMB_ORIG_DEST', 'LAST_FLIGHT_AZIMUTH_ELEVATION',
    'FLIGHT_COUNT_LAST_SEEN', 'RADIO_RANGE']

WRITE_DELAY_TIME = 0.2  # write to arduino every n seconds
READ_DELAY_TIME_SERVO = 1  # read from arduino every n seconds
READ_DELAY_TIME_REMOTE = 0.1  # read from arduino every n seconds




                            <----SKIPPED LINES---->




  return value


def InitialMessageValues(q):
  """Initializes the arduino main processes with values from messageboard."""
  v = DrainQueue(q)
  if v:
    return v
  return {}, {}, {}, {}


def SetLoggingGlobals(configuration):
  """Sets the logging globals based on values in the config file."""
  global VERBOSE
  VERBOSE = 'arduino_verbose' in configuration

  global LOG_SERIALS
  LOG_SERIALS = 'arduino_log_serials' in configuration


def ServoTestOrdinal(link):
  """Point laser at each of 0, 90, 180, 270 and hold with different colors."""
  link.Write((0, 0, *LASER_ALL, False))


  time.sleep(1)
  link.Write((90, 0, *LASER_RED, False))


  time.sleep(1)
  link.Write((180, 0, *LASER_GREEN, False))


  time.sleep(1)
  link.Write((270, 0, *LASER_BLUE, False))


  time.sleep(1)


def ServoTestSweep(link, altitude=45):



















  """Sweep red laser around 360 degrees."""
  for azimuth in range(0, 360, 10):
    link.Write((azimuth, altitude, *LASER_RED, False))




    time.sleep(WRITE_DELAY_TIME)











































def ServoMain(to_arduino_q, to_parent_q, shutdown):
  """Main servo controller for projecting the plane position on a hemisphere.

  Takes the latest flight from the to_arduino_q and converts that to the current
  azimuth and altitude of the plane on a hemisphere.
  """
  sys.stderr = open(messageboard.STDERR_FILE, 'a')

  Log('Process started with process id %d' % os.getpid())

  # Ensures that the child can exit if the parent exits unexpectedly
  # docs.python.org/2/library/multiprocessing.html
  # #multiprocessing.Queue.cancel_join_thread
  to_arduino_q.cancel_join_thread()
  to_parent_q.cancel_join_thread()

















  # write_format: azimuth, altitude, R, G, & B intensity
  # read heartbeat: millis
  link = Serial(
      *SERVO_CONNECTION, read_timeout=60,
      error_pin=messageboard.GPIO_ERROR_ARDUINO_SERVO_CONNECTION,
      to_parent_q=to_parent_q,
      read_format='l', write_format='ff????', name='Servo')
  link.Open()

  last_flight = {}
  last_angles = (0, 0)
  flight, json_desc_dict, configuration, additional_attr = InitialMessageValues(
      to_arduino_q)
  next_read = 0
  next_write = 0
  now = GetNow(json_desc_dict, additional_attr)

  while not shutdown.value:
    SetLoggingGlobals(configuration)
    if not to_arduino_q.empty():
      flight, json_desc_dict, configuration, additional_attr = to_arduino_q.get(
          block=False)

      if 'test_servos_ordinal' in configuration:
        messageboard.RemoveSetting(configuration, 'test_servos_ordinal')
        ServoTestOrdinal(link)
      elif 'test_servos_sweep' in configuration:
        messageboard.RemoveSetting(configuration, 'test_servos_sweep')
        ServoTestSweep(link)




      new_flight = DifferentFlights(flight, last_flight)
      if new_flight:
        Log('Flight changed from %s to %s' % (
            messageboard.DisplayFlightNumber(last_flight),
            messageboard.DisplayFlightNumber(flight)
        ), ser=link)

        # Turn off laser so line isn't traced while it moves to new position
        link.Write((*last_angles, *LASER_OFF, False))




      last_flight = flight

    if time.time() >= next_read:
      heartbeat = link.Read()  # simple ack message sent by servos
      next_read = time.time() + READ_DELAY_TIME_SERVO
      if heartbeat and VERBOSE:
        Log('Heartbeat read by Servo: %s' % str(heartbeat))

    now = GetNow(json_desc_dict, additional_attr)

    current_angles = AzimuthAltitude(flight, now)
    if current_angles and time.time() > next_write:
      if current_angles[1] >= configuration['minimum_altitude_servo_tracking']:
        if VERBOSE:
          Log('Flight #: %s current_angles: %s' % (
              messageboard.DisplayFlightNumber(flight), str(current_angles)))
        laser_rgb = LaserRGBFlight(flight)
        link.Write((*current_angles, *laser_rgb, False))

        last_angles = current_angles

      else:
        link.Write((*last_angles, *LASER_OFF, False))





      next_write = time.time() + WRITE_DELAY_TIME

  # One final write telling Arduino to do a software reset
  link.Write((*last_angles, *LASER_OFF, True))



  link.Close(SHUTDOWN_TEXT)


def LaserRGBFlight(flight):
  """Based on flight attributes, set the laser."""
  # Possible assignment based on:
  #   - ascending / descending / level
  #   - to SFO / from SFO / other
  #   - big plane / med plane / small plane
  #   - low alt / med alt / high alt
  #   - low speed / med speed / high speed
  #   - rare destination / common destination
  aircraft_length = messageboard.AircraftLength(flight)

  if aircraft_length > 50:
    return LASER_RED
  if aircraft_length > 30:
    return LASER_GREEN
  return LASER_BLUE





                            <----SKIPPED LINES---->





01234567890123456789012345678901234567890123456789012345678901234567890123456789









46474849505152535455565758596061626364656667686970717273747576777879808182838485868788








683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835 836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925











                            <----SKIPPED LINES---->





  ARDUINO_ROLLING_LOG = WEBSERVER_PATH + ARDUINO_ROLLING_LOG
  ROLLING_SERIALS_LOG = WEBSERVER_PATH + ROLLING_SERIALS_LOG

CONNECTION_FLAG_BLUETOOTH = 1
CONNECTION_FLAG_USB = 2
CONNECTION_FLAG_SIMULATED = 3
RASPBERRY_PI = psutil.sys.platform.title() == 'Linux'

SN_SERVO = '5583834303435111C1A0'
SERVO_CONNECTION = (CONNECTION_FLAG_BLUETOOTH, (2, '98:D3:11:FC:42:16', 1))

SN_REMOTE = '75835343130351802272'
REMOTE_CONNECTION = (CONNECTION_FLAG_BLUETOOTH, (1, '98:D3:91:FD:B3:C9', 1))

LASER_OFF = (False, False, False)
LASER_ALL = (True, True, True)
LASER_RED = (True, False, False)
LASER_GREEN = (False, True, False)
LASER_BLUE = (False, False, True)

LED_OFF = (0, 0, 0)
MAX_PWM = 255

if SIMULATE_ARDUINO:
  SERVO_CONNECTION = (
      CONNECTION_FLAG_SIMULATED, (SERVO_SIMULATED_IN, SERVO_SIMULATED_OUT))
  REMOTE_CONNECTION = (
      CONNECTION_FLAG_SIMULATED, (REMOTE_SIMULATED_IN, REMOTE_SIMULATED_OUT))

KEY_NOT_PRESENT_STRING = 'N/A'

DISP_LAST_FLIGHT_NUMB_ORIG_DEST = 0
DISP_LAST_FLIGHT_AZIMUTH_ELEVATION = 1
DISP_FLIGHT_COUNT_LAST_SEEN = 2
DISP_RADIO_RANGE = 3
DISPLAY_MODE_NAMES = [
    'LAST_FLIGHT_NUMB_ORIG_DEST', 'LAST_FLIGHT_AZIMUTH_ELEVATION',
    'FLIGHT_COUNT_LAST_SEEN', 'RADIO_RANGE']

WRITE_DELAY_TIME = 0.2  # write to arduino every n seconds
READ_DELAY_TIME_SERVO = 1  # read from arduino every n seconds
READ_DELAY_TIME_REMOTE = 0.1  # read from arduino every n seconds




                            <----SKIPPED LINES---->




  return value


def InitialMessageValues(q):
  """Initializes the arduino main processes with values from messageboard."""
  v = DrainQueue(q)
  if v:
    return v
  return {}, {}, {}, {}


def SetLoggingGlobals(configuration):
  """Sets the logging globals based on values in the config file."""
  global VERBOSE
  VERBOSE = 'arduino_verbose' in configuration

  global LOG_SERIALS
  LOG_SERIALS = 'arduino_log_serials' in configuration


def ServoTestOrdinal(link, write_keys, write_format_tuple):
  """Point laser at each of 0, 90, 180, 270 and hold with different colors."""

  message_dict = GenerateServoMessage(laser=LASER_ALL, angles=(0, 0))
  link.Write(DictToValueTuple(message_dict, write_keys, write_format_tuple))
  time.sleep(1)

  message_dict = GenerateServoMessage(laser=LASER_RED, angles=(90, 0))
  link.Write(DictToValueTuple(message_dict, write_keys, write_format_tuple))
  time.sleep(1)

  message_dict = GenerateServoMessage(laser=LASER_GREEN, angles=(180, 0))
  link.Write(DictToValueTuple(message_dict, write_keys, write_format_tuple))
  time.sleep(1)

  message_dict = GenerateServoMessage(laser=LASER_BLUE, angles=(270, 0))
  link.Write(DictToValueTuple(message_dict, write_keys, write_format_tuple))
  time.sleep(1)


def ServoTestLED(link, write_keys, write_format_tuple):
  """Cycle thru the LED colors."""
  message_dict = GenerateServoMessage(led=(MAX_PWM, MAX_PWM, MAX_PWM))
  link.Write(DictToValueTuple(message_dict, write_keys, write_format_tuple))
  time.sleep(1)

  message_dict = GenerateServoMessage(led=(MAX_PWM, 0, 0))
  link.Write(DictToValueTuple(message_dict, write_keys, write_format_tuple))
  time.sleep(1)

  message_dict = GenerateServoMessage(led=(0, MAX_PWM, 0))
  link.Write(DictToValueTuple(message_dict, write_keys, write_format_tuple))
  time.sleep(1)

  message_dict = GenerateServoMessage(led=(0, 0, MAX_PWM))
  link.Write(DictToValueTuple(message_dict, write_keys, write_format_tuple))
  time.sleep(1)


def ServoTestSweep(link, write_keys, write_format_tuple, altitude=45):
  """Sweep red laser around 360 degrees."""
  for azimuth in range(0, 360, 10):

    message_dict = GenerateServoMessage(
        laser=LASER_RED, angles=(azimuth, altitude))
    link.Write(DictToValueTuple(message_dict, write_keys, write_format_tuple))

    time.sleep(WRITE_DELAY_TIME)


last_angles = (0, 0)
last_laser = LASER_OFF
last_led = LED_OFF
def GenerateServoMessage(
    angles=None,
    laser=None,
    led=None,
    reset=False):

  if angles:  # if angles provided, update the cache
    global last_angles
    last_angles = angles
  else:  # if angles not provided, use last-provided angles
    angles = last_angles

  if laser:
    global last_laser
    last_laser = laser
  else:
    laser = last_laser

  if led:
    global last_led
    last_led = led
  else:
    led = last_led

  d = {}
  d['azimuth'] = angles[0]
  d['altitude'] = angles[1]
  d['laser_red'] = laser[0]
  d['laser_green'] = laser[1]
  d['laser_blue'] = laser[2]
  d['led_red'] = led[0]
  d['led_green'] = led[1]
  d['led_blue'] = led[2]
  d['arduino_reset'] = reset

  return d


def ServoMain(to_arduino_q, to_parent_q, shutdown):
  """Main servo controller for projecting the plane position on a hemisphere.

  Takes the latest flight from the to_arduino_q and converts that to the current
  azimuth and altitude of the plane on a hemisphere.
  """
  sys.stderr = open(messageboard.STDERR_FILE, 'a')

  Log('Process started with process id %d' % os.getpid())

  # Ensures that the child can exit if the parent exits unexpectedly
  # docs.python.org/2/library/multiprocessing.html
  # #multiprocessing.Queue.cancel_join_thread
  to_arduino_q.cancel_join_thread()
  to_parent_q.cancel_join_thread()

  #pylint: disable = bad-whitespace
  write_config = (
      ('azimuth',       'f'),  # 4 bytes
      ('altitude',      'f'),  # 4 bytes
      ('laser_red',     '?'),  # 1 byte
      ('laser_green',   '?'),  # 1 byte
      ('laser_blue',    '?'),  # 1 byte
      ('led_red',       'H'),  # 2 bytes
      ('led_green',     'H'),  # 2 bytes
      ('led_blue',      'H'),  # 2 bytes
      ('arduino_reset', '?'),  # 1 byte
  )
  #pylint: enable = bad-whitespace
  write_keys, write_format_tuple, write_format_string = SplitFormat(
      write_config)

  # write_format: azimuth, altitude, R, G, & B intensity
  # read heartbeat: millis
  link = Serial(
      *SERVO_CONNECTION, read_timeout=60,
      error_pin=messageboard.GPIO_ERROR_ARDUINO_SERVO_CONNECTION,
      to_parent_q=to_parent_q,
      read_format='l', write_format=write_format_string, name='Servo')
  link.Open()

  last_flight = {}

  flight, json_desc_dict, configuration, additional_attr = InitialMessageValues(
      to_arduino_q)
  next_read = 0
  next_write = 0
  now = GetNow(json_desc_dict, additional_attr)

  while not shutdown.value:
    SetLoggingGlobals(configuration)
    if not to_arduino_q.empty():
      flight, json_desc_dict, configuration, additional_attr = to_arduino_q.get(
          block=False)

      if 'test_servos_ordinal' in configuration:
        messageboard.RemoveSetting(configuration, 'test_servos_ordinal')
        ServoTestOrdinal(link, write_keys, write_format_tuple)
      elif 'test_servos_sweep' in configuration:
        messageboard.RemoveSetting(configuration, 'test_servos_sweep')
        ServoTestSweep(link, write_keys, write_format_tuple)
      elif 'test_servos_led' in configuration:
        messageboard.RemoveSetting(configuration, 'test_servos_led')
        ServoTestLED(link, write_keys, write_format_tuple)

      new_flight = DifferentFlights(flight, last_flight)
      if new_flight:
        Log('Flight changed from %s to %s' % (
            messageboard.DisplayFlightNumber(last_flight),
            messageboard.DisplayFlightNumber(flight)
        ), ser=link)

        # Turn off laser so line isn't traced while it moves to new position
        message_dict = GenerateServoMessage(laser=LASER_OFF)
        message_tuple = DictToValueTuple(
            message_dict, write_keys, write_format_tuple)
        link.Write(message_tuple)

      last_flight = flight

    if time.time() >= next_read:
      heartbeat = link.Read()  # simple ack message sent by servos
      next_read = time.time() + READ_DELAY_TIME_SERVO
      if heartbeat and VERBOSE:
        Log('Heartbeat read by Servo: %s' % str(heartbeat))

    now = GetNow(json_desc_dict, additional_attr)

    current_angles = AzimuthAltitude(flight, now)
    if current_angles and time.time() > next_write:
      if current_angles[1] >= configuration['minimum_altitude_servo_tracking']:
        if VERBOSE:
          Log('Flight #: %s current_angles: %s' % (
              messageboard.DisplayFlightNumber(flight), str(current_angles)))
        laser_rgb = LaserRGBFlight(flight)

        message_dict = GenerateServoMessage(
            laser=laser_rgb, angles=current_angles)

      else:
        message_dict = GenerateServoMessage(laser=LASER_OFF)

      message_tuple = DictToValueTuple(
          message_dict, write_keys, write_format_tuple)
      link.Write(message_tuple)

      next_write = time.time() + WRITE_DELAY_TIME

  # One final write telling Arduino to do a software reset
  message_dict = GenerateServoMessage(laser=LASER_OFF, reset=True)
  message_tuple = DictToValueTuple(message_dict, write_keys, write_format_tuple)
  link.Write(message_tuple)

  link.Close(SHUTDOWN_TEXT)


def LaserRGBFlight(flight):
  """Based on flight attributes, set the laser."""
  # Possible assignment based on:
  #   - ascending / descending / level
  #   - to SFO / from SFO / other
  #   - big plane / med plane / small plane
  #   - low alt / med alt / high alt
  #   - low speed / med speed / high speed
  #   - rare destination / common destination
  aircraft_length = messageboard.AircraftLength(flight)

  if aircraft_length > 50:
    return LASER_RED
  if aircraft_length > 30:
    return LASER_GREEN
  return LASER_BLUE





                            <----SKIPPED LINES---->