arduino-2020-07-01-0055.py
01234567890123456789012345678901234567890123456789012345678901234567890123456789









744745746747748749750751752753754755756757758759760761762763 764   765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794        795796797798799800801802803804805806807808809810811812813814








865866867868869870871872873874875876877878879880881882883884885  886  887888889 890891892893   894895 896897898899900901902903904905906907908909910911912913914915916











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





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 = (




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




            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




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





01234567890123456789012345678901234567890123456789012345678901234567890123456789









744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826








877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907 908909910911912913914915916917918919920921922923924925926927928929930931932933934935936











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





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):
  """Creates a dictionary of messages for servo arduino.

  All values are optional; if not provided, the cached value last sent to the
  Arduino is used.
  """
  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 HexColorToRGBTuple(hex_color):
  """Converts i.e.: #329a43 to (50, 154, 67)."""
  r = hex_color[1:3]
  g = hex_color[3:5]
  b = hex_color[5:7]
  return (int(r, 16), int(g, 16), int(b, 16))


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 = (




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




            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 time.time() > next_write:
      if (current_angles and
          current_angles[1] >=
          configuration['minimum_altitude_servo_tracking'] and
          configuration['servo_mode'] in ('laser_only', 'both')):

        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, led=LED_OFF)

      elif configuration['servo_mode'] == 'laser_only':
        message_dict = GenerateServoMessage(laser=LASER_OFF)

      else:
        rgb_tuple = HexColorToRGBTuple(configuration['led_color'])
        message_dict = GenerateServoMessage(laser=LASER_OFF, led=rgb_tuple)

      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




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