01234567890123456789012345678901234567890123456789012345678901234567890123456789
703704705706707708709710711712713714715716717718719720721722723724725726727728729730 731732733734735736737738739740741742 743744745746747748749750751752753754755756757758759760761762 796797798799800801802803804805806807808809810811812813814815816817818 819820821822823824825826827828829830831832833834835836837838 868869870871872873874875876877878879880881882883884885886887 888 889 890891892893894895896897898899900901902903904905906907908909 |
<----SKIPPED LINES---->
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."""
def SendLEDMessage(red, green, blue):
message_dict = GenerateServoMessage(led=(int(red), int(green), int(blue)))
link.Write(DictToValueTuple(message_dict, write_keys, write_format_tuple))
time.sleep(0.2)
SendLEDMessage(MAX_PWM, MAX_PWM, MAX_PWM)
for intensity in range(0, 255, 50):
SendLEDMessage(intensity, 0, 0)
for intensity in range(0, 255, 50):
SendLEDMessage(0, intensity, 0)
for intensity in range(0, 255, 50):
SendLEDMessage(0, 0, intensity)
SendLEDMessage(0, 0, 0)
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,
<----SKIPPED LINES---->
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 Gamma(c, gamma):
"""Converts a desired brightness 0..255 to a gamma-corrected PWM 0..255."""
# Based on https://hackaday.com/2016/08/23/
# rgb-leds-how-to-master-gamma-and-hue-for-perfect-brightness/
return round(((c / MAX_PWM) ** gamma) * MAX_PWM)
def GammaRGB(rgb, gamma):
"""Gamma converts an RGB tuple."""
return Gamma(rgb[0], gamma), Gamma(rgb[1], gamma), Gamma(rgb[2], gamma)
def GetGamma(configuration):
"""Returns gamma stored in configuration file."""
return configuration['servo_rgb_gamma'] / 10 # gamma saved as whole number
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
<----SKIPPED LINES---->
flight, json_desc_dict, configuration, additional_attr = InitialMessageValues(
to_arduino_q)
next_read = 0
next_write = 0
now = GetNow(json_desc_dict, additional_attr)
gamma = GetGamma(configuration)
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)
gamma = GetGamma(configuration)
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
<----SKIPPED LINES---->
|
01234567890123456789012345678901234567890123456789012345678901234567890123456789
703704705706707708709710711712713714715716717718719720721722723724725 726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775 809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854 884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930 |
<----SKIPPED LINES---->
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 SendLEDTestMessage(
link, write_keys, write_format_tuple, red, green, blue, delay=0.2):
"""Send LED colors to servo Arduino with a delay."""
message_dict = GenerateServoMessage(led=(int(red), int(green), int(blue)))
link.Write(DictToValueTuple(message_dict, write_keys, write_format_tuple))
time.sleep(delay)
def ServoTestRgb(link, write_keys, write_format_tuple):
"""Cycle thru the LED colors without gamma correction."""
comms_args = link, write_keys, write_format_tuple
SendLEDTestMessage(*comms_args, MAX_PWM, MAX_PWM, MAX_PWM)
for intensity in range(0, 255, 50):
SendLEDTestMessage(*comms_args, intensity, 0, 0)
for intensity in range(0, 255, 50):
SendLEDTestMessage(*comms_args, 0, intensity, 0)
for intensity in range(0, 255, 50):
SendLEDTestMessage(*comms_args, 0, 0, intensity)
SendLEDTestMessage(*comms_args, 0, 0, 0)
def ServoTestDim(link, write_keys, write_format_tuple, rgb, gamma):
"""Dim LEDs at selected color from 100% to 0% with gamma correction."""
comms_args = link, write_keys, write_format_tuple
for intensity in range(0, 101, 5):
b = intensity / 100
SendLEDTestMessage(*comms_args, *GammaRGB(rgb, gamma, brightness=b))
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,
<----SKIPPED LINES---->
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 Gamma(c, gamma):
"""Converts a desired brightness 0..255 to a gamma-corrected PWM 0..255."""
# Based on https://hackaday.com/2016/08/23/
# rgb-leds-how-to-master-gamma-and-hue-for-perfect-brightness/
return round(((c / MAX_PWM) ** gamma) * MAX_PWM)
def GammaRGB(rgb, gamma, brightness=1.0):
"""Gamma converts an RGB tuple at a selected brightness 0%..100%."""
r = Gamma(rgb[0], gamma) * brightness
g = Gamma(rgb[1], gamma) * brightness
b = Gamma(rgb[2], gamma) * brightness
return r, g, b
def GetGamma(configuration):
"""Returns gamma stored in configuration file."""
return configuration['servo_rgb_gamma'] / 10 # gamma saved as whole number
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
<----SKIPPED LINES---->
flight, json_desc_dict, configuration, additional_attr = InitialMessageValues(
to_arduino_q)
next_read = 0
next_write = 0
now = GetNow(json_desc_dict, additional_attr)
gamma = GetGamma(configuration)
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_rgb')
ServoTestRgb(link, write_keys, write_format_tuple)
elif 'test_servos_dim' in configuration:
messageboard.RemoveSetting(configuration, 'test_servos_dim')
ServoTestDim(
link, write_keys, write_format_tuple,
HexColorToRGBTuple(configuration['led_color']), gamma)
gamma = GetGamma(configuration)
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
<----SKIPPED LINES---->
|