arduino-2020-06-04-0021.py
01234567890123456789012345678901234567890123456789012345678901234567890123456789









176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218








642643644645646647648649650651652653654655656657658659660661662   663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698  699700701702703704705706707708709710711712713714715716717718











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




    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
      self.to_parent_q.put(('pin', (self.error_pin, True)))

    self.start_time = time.time()

  def __str__(self):
    return '%s @ %s opened @ %s' % (
        self.name, str(self.connection_tuple),
        messageboard.EpochDisplayTime(self.start_time))

  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)
      self.__simulated_reads__ = lines

      # clear out file so that shell tail -f process can continue to point to same file
      with open(self.connection_tuple[1], 'w') as f:
        f.write('')

      if self.error_pin:




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





  while not shutdown.value:
    timing.append((time.time(), 2))
    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_RGB_OFF))
        time.sleep(1)
        link.Write((180, 0, *LASER_RGB_OFF))
        time.sleep(1)
        link.Write((270, 0, *LASER_RGB_OFF))

      timing.append((time.time(), 3))
      new_flight = DifferentFlights(flight, last_flight)
      if new_flight:
        Log('Flight changed: %s' % 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
      timing.append((time.time(), 4))

    if time.time() >= next_read:
      link.Read()  # simple ack message sent by servos
      next_read = time.time() + READ_DELAY_TIME
      timing.append((time.time(), 5))

    now = GetNow(json_desc_dict, additional_attr)
    timing.append((time.time(), 6))

    current_angles = AzimuthAltitude(flight, now)
    timing.append((time.time(), 7))
    if current_angles and time.time() > next_write:
      if current_angles[1] >= configuration['minimum_altitude_servo_tracking']:
        Log('Flight #: %s current_angles: %s' % (
            messageboard.DisplayFlightNumber(flight), str(current_angles)))
        messageboard.LogTimes(timing)
        flight_present = True
        laser_rgb = LaserRGBFlight(flight)
        timing = [(time.time(), 0)]
        link.Write((*current_angles, *laser_rgb))
        last_angles = current_angles
        timing.append((time.time(), 1))

      # flight no longer tracked; send one more command to turn off lasers
      elif flight_present:
        timing = [(time.time(), -1)]
        link.Write((*last_angles, *LASER_RGB_OFF))
        flight_present = False

      next_write = time.time() + WRITE_DELAY_TIME



  link.Close(SHUTDOWN_TEXT)


LASER_RGB_OFF = (0, 0, 0)
def LaserRGBFlight(flight):
  """Based on flight attributes, set the laser."""
  if not flight:
    return LASER_RGB_OFF
  return 1, 0, 0


def DifferentFlights(f1, f2):
  """True if flights same except for persistent path; False if they differ.

  We cannot simply check if two flights are identical by checking equality of the dicts,
  because a few attributes are updated after the flight is first found:
  - the persistent_path is kept current
  - cached_* attributes may be updated
  - the insights are generated after flight first enqueued




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





01234567890123456789012345678901234567890123456789012345678901234567890123456789









176177178179180181182183184185186187188189190191192193194195196  197198199200201202203204205206207208209210211212213214215216








640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721











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




    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
      self.to_parent_q.put(('pin', (self.error_pin, True)))

    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)
      self.__simulated_reads__ = lines

      # clear out file so that shell tail -f process can continue to point to same file
      with open(self.connection_tuple[1], 'w') as f:
        f.write('')

      if self.error_pin:




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





  while not shutdown.value:
    timing.append((time.time(), 2))
    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_RGB_OFF))
        time.sleep(1)
        link.Write((180, 0, *LASER_RGB_OFF))
        time.sleep(1)
        link.Write((270, 0, *LASER_RGB_OFF))

      timing.append((time.time(), 3))
      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
      timing.append((time.time(), 4))

    if time.time() >= next_read:
      link.Read()  # simple ack message sent by servos
      next_read = time.time() + READ_DELAY_TIME
      timing.append((time.time(), 5))

    now = GetNow(json_desc_dict, additional_attr)
    timing.append((time.time(), 6))

    current_angles = AzimuthAltitude(flight, now)
    timing.append((time.time(), 7))
    if current_angles and time.time() > next_write:
      if current_angles[1] >= configuration['minimum_altitude_servo_tracking']:
        Log('Flight #: %s current_angles: %s' % (
            messageboard.DisplayFlightNumber(flight), str(current_angles)))
        messageboard.LogTimes(timing)
        flight_present = True
        laser_rgb = LaserRGBFlight(flight)
        timing = [(time.time(), 0)]
        link.Write((*current_angles, *laser_rgb))
        last_angles = current_angles
        timing.append((time.time(), 1))

      # flight no longer tracked; send one more command to turn off lasers
      elif flight_present:
        timing = [(time.time(), -1)]
        link.Write((*last_angles, *LASER_RGB_OFF))
        flight_present = False

      next_write = time.time() + WRITE_DELAY_TIME
    else:
      timing = timing = [(time.time(), -2)]

  link.Close(SHUTDOWN_TEXT)


LASER_RGB_OFF = (0, 0, 0)
def LaserRGBFlight(flight):
  """Based on flight attributes, set the laser."""
  if not flight:
    return LASER_RGB_OFF
  return 1, 0, 0


def DifferentFlights(f1, f2):
  """True if flights same except for persistent path; False if they differ.

  We cannot simply check if two flights are identical by checking equality of the dicts,
  because a few attributes are updated after the flight is first found:
  - the persistent_path is kept current
  - cached_* attributes may be updated
  - the insights are generated after flight first enqueued




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