arduino-2020-06-12-1353.py
01234567890123456789012345678901234567890123456789012345678901234567890123456789









414243444546474849505152535455565758596061 626364656667686970717273747576777879808182838485








178179180181182183184185186187188189190191192193194195196197198 199200201202203204205206207208209210211212213214215216217218








615616617618619620621622623624625626627628629630631632633634635 636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715








119711981199120012011202120312041205120612071208120912101211121212131214121512161217 12181219122012211222122312241225122612271228122912301231123212331234123512361237











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




  REMOTE_SIMULATED_OUT = MESSAGEBOARD_PATH + REMOTE_SIMULATED_OUT
  REMOTE_SIMULATED_IN = MESSAGEBOARD_PATH + REMOTE_SIMULATED_IN
  REMOTE_DISPLAY_MODE = MESSAGEBOARD_PATH + REMOTE_DISPLAY_MODE

  ARDUINO_ROLLING_LOG = WEBSERVER_PATH + ARDUINO_ROLLING_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))  # with MOSFET
#SERVO_CONNECTION = (CONNECTION_FLAG_BLUETOOTH, (3, '98:D3:91:FD:B1:8F', 1))  # no MOSFET

SN_REMOTE = '75835343130351802272'
# directly connected to Serial2
# connected thru MOSFET to Serial1
REMOTE_CONNECTION = (CONNECTION_FLAG_BLUETOOTH, (1, '98:D3:91:FD:B3:C9', 1))

LASER_RGB_OFF = (False, False, False)

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 = 0.1  # read from arduino every n seconds

HISTOGRAM_TYPES = (




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




    self.write_format = write_format
    self.read_timeout = read_timeout
    self.error_pin = error_pin
    self.to_parent_q = to_parent_q
    self.last_read = 0
    self.last_receipt = 0
    self.reset_flag = True
    self.link = None
    self.name = name

    self.__simulated_reads__ = None

    if self.connection_type == CONNECTION_FLAG_BLUETOOTH:
      self.open_function = OpenBluetooth
    elif self.connection_type == CONNECTION_FLAG_USB:
      self.open_function = OpenUSB
    elif self.connection_type == CONNECTION_FLAG_SIMULATED:
      self.open_function = None

    if self.error_pin:  # Error turned on when main initiated; turned off when connected
      error_message = 'Process %s (%s) initialized into error state' % (os.getpid(), str(self.name))

      Log(error_message)
      self.to_parent_q.put(('pin', (self.error_pin, True, error_message)))

    self.start_time = time.time()

  def __str__(self):
    return self.name

  def Open(self):
    """Opens an instantiated serial connection for reading and writing."""
    if self.connection_type == CONNECTION_FLAG_SIMULATED:
      lines = []
      if os.path.exists(self.connection_tuple[0]):
        with open(self.connection_tuple[0], 'r') as f:
          for line in f:
            if line.strip():
              lines.append(eval(line))  # pylint: disable=W0123
      else:
        Log('File %s does not exist for simulated commands to Arudino'
            % self.connection_tuple[0], self.link)




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






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=7,  TODO: maybe without this read timeout, we won't get the correlated BT failures

      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:
    if not to_arduino_q.empty():
      flight, json_desc_dict, configuration, additional_attr = to_arduino_q.get(
          block=False)

      if 'test_servos' in configuration:
        messageboard.RemoveSetting(configuration, 'test_servos')
        link.Write((0, 0, *LASER_RGB_OFF))
        time.sleep(1)
        link.Write((90, 0, *LASER_RED))
        time.sleep(1)
        link.Write((180, 0, *LASER_GREEN))
        time.sleep(1)
        link.Write((270, 0, *LASER_BLUE))
        time.sleep(1)

      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 that line isn't traced while it moves to new position
        link.Write((*last_angles, *LASER_RGB_OFF))

      last_flight = flight

    if time.time() >= next_read:
      heartbeat = link.Read()  # simple ack message sent by servos
      next_read = time.time() + READ_DELAY_TIME
      if heartbeat and VERBOSE:
        Log(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))
        last_angles = current_angles

      else:
        link.Write((*last_angles, *LASER_RGB_OFF))

      next_write = time.time() + WRITE_DELAY_TIME

  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:




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




      ('setting_max_altitude',        'L'),  # 4 bytes
      ('setting_on_time',             'H'),  # 2 bytes
      ('setting_off_time',            'H'),  # 2 bytes
      ('setting_delay',               'H'),  # 2 bytes
      ('line1',                       '9s'), # 9 bytes; 8 character plus terminator
      ('line2',                       '9s'), # 9 bytes; 8 character plus terminator
      ('line1_dec_mask',              'H'),  # 2 bytes
      ('line2_dec_mask',              'H'),  # 2 bytes
      ('display_mode',                'H'),  # 2 bytes
      ('last_flight_available',       '?'),  # 1 byte
  )
  #pylint: enable = bad-whitespace
  read_keys, unused_read_format_tuple, read_format_string = SplitFormat(read_config)
  write_keys, write_format_tuple, write_format_string = SplitFormat(write_config)

  values_d = {}
  low_batt = False
  to_parent_q.put(('pin', (messageboard.GPIO_ERROR_BATTERY_CHARGE, low_batt)))

  link = Serial(
      *REMOTE_CONNECTION, #read_timeout=7, TODO: maybe without this read timeout, we won't get the correlated BT failures

      error_pin=messageboard.GPIO_ERROR_ARDUINO_REMOTE_CONNECTION, to_parent_q=to_parent_q,
      read_format=read_format_string, write_format=write_format_string, name='Remote')
  link.Open()

  # Read in the saved display mode, if it exists
  display_mode = messageboard.ReadFile(REMOTE_DISPLAY_MODE, log_exception=False)
  if not display_mode:
    display_mode = DISP_LAST_FLIGHT_NUMB_ORIG_DEST
  else:
    display_mode = int(display_mode)

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

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




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





01234567890123456789012345678901234567890123456789012345678901234567890123456789









41424344454647484950515253545556575859606162636465 6667686970717273747576777879808182838485








178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219








616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717








119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240











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




  REMOTE_SIMULATED_OUT = MESSAGEBOARD_PATH + REMOTE_SIMULATED_OUT
  REMOTE_SIMULATED_IN = MESSAGEBOARD_PATH + REMOTE_SIMULATED_IN
  REMOTE_DISPLAY_MODE = MESSAGEBOARD_PATH + REMOTE_DISPLAY_MODE

  ARDUINO_ROLLING_LOG = WEBSERVER_PATH + ARDUINO_ROLLING_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))  # with MOSFET
#SERVO_CONNECTION = (CONNECTION_FLAG_BLUETOOTH, (3, '98:D3:91:FD:B1:8F', 1))  # no MOSFET

SN_REMOTE = '75835343130351802272'
# directly connected to Serial2
# connected thru MOSFET to Serial1
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 = 0.1  # read from arduino every n seconds

HISTOGRAM_TYPES = (




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




    self.write_format = write_format
    self.read_timeout = read_timeout
    self.error_pin = error_pin
    self.to_parent_q = to_parent_q
    self.last_read = 0
    self.last_receipt = 0
    self.reset_flag = True
    self.link = None
    self.name = name

    self.__simulated_reads__ = None

    if self.connection_type == CONNECTION_FLAG_BLUETOOTH:
      self.open_function = OpenBluetooth
    elif self.connection_type == CONNECTION_FLAG_USB:
      self.open_function = OpenUSB
    elif self.connection_type == CONNECTION_FLAG_SIMULATED:
      self.open_function = None

    if self.error_pin:  # Error turned on when main initiated; turned off when connected
      error_message = 'Process %s (%s) initialized into error state' % (
          os.getpid(), str(self.name))
      Log(error_message)
      self.to_parent_q.put(('pin', (self.error_pin, True, error_message)))

    self.start_time = time.time()

  def __str__(self):
    return self.name

  def Open(self):
    """Opens an instantiated serial connection for reading and writing."""
    if self.connection_type == CONNECTION_FLAG_SIMULATED:
      lines = []
      if os.path.exists(self.connection_tuple[0]):
        with open(self.connection_tuple[0], 'r') as f:
          for line in f:
            if line.strip():
              lines.append(eval(line))  # pylint: disable=W0123
      else:
        Log('File %s does not exist for simulated commands to Arudino'
            % self.connection_tuple[0], self.link)




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






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(
      #TODO: maybe without this read timeout, we won't get the correlated BT failures
      *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:
    if not to_arduino_q.empty():
      flight, json_desc_dict, configuration, additional_attr = to_arduino_q.get(
          block=False)

      if 'test_servos' in configuration:
        messageboard.RemoveSetting(configuration, 'test_servos')
        link.Write((0, 0, *LASER_ALL))
        time.sleep(1)
        link.Write((90, 0, *LASER_RED))
        time.sleep(1)
        link.Write((180, 0, *LASER_GREEN))
        time.sleep(1)
        link.Write((270, 0, *LASER_BLUE))
        time.sleep(1)

      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 that line isn't traced while it moves to new position
        link.Write((*last_angles, *LASER_OFF))

      last_flight = flight

    if time.time() >= next_read:
      heartbeat = link.Read()  # simple ack message sent by servos
      next_read = time.time() + READ_DELAY_TIME
      if heartbeat and VERBOSE:
        Log(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))
        last_angles = current_angles

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

      next_write = time.time() + WRITE_DELAY_TIME

  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:




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




      ('setting_max_altitude',        'L'),  # 4 bytes
      ('setting_on_time',             'H'),  # 2 bytes
      ('setting_off_time',            'H'),  # 2 bytes
      ('setting_delay',               'H'),  # 2 bytes
      ('line1',                       '9s'), # 9 bytes; 8 character plus terminator
      ('line2',                       '9s'), # 9 bytes; 8 character plus terminator
      ('line1_dec_mask',              'H'),  # 2 bytes
      ('line2_dec_mask',              'H'),  # 2 bytes
      ('display_mode',                'H'),  # 2 bytes
      ('last_flight_available',       '?'),  # 1 byte
  )
  #pylint: enable = bad-whitespace
  read_keys, unused_read_format_tuple, read_format_string = SplitFormat(read_config)
  write_keys, write_format_tuple, write_format_string = SplitFormat(write_config)

  values_d = {}
  low_batt = False
  to_parent_q.put(('pin', (messageboard.GPIO_ERROR_BATTERY_CHARGE, low_batt)))

  link = Serial(
      # TODO: maybe without this read timeout, we won't get the correlated BT failures
      *REMOTE_CONNECTION, read_timeout=60,
      error_pin=messageboard.GPIO_ERROR_ARDUINO_REMOTE_CONNECTION, to_parent_q=to_parent_q,
      read_format=read_format_string, write_format=write_format_string, name='Remote')
  link.Open()

  # Read in the saved display mode, if it exists
  display_mode = messageboard.ReadFile(REMOTE_DISPLAY_MODE, log_exception=False)
  if not display_mode:
    display_mode = DISP_LAST_FLIGHT_NUMB_ORIG_DEST
  else:
    display_mode = int(display_mode)

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

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




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