01234567890123456789012345678901234567890123456789012345678901234567890123456789
211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390 391392393394395396397398399400 401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443 460461462463464465466467468469470471472473474475476477478479480 481482483484485486487488489490491492493494495496497498499500501502503504505 635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675 12141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254 |
<----SKIPPED LINES---->
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:
self.to_parent_q.put(('pin', (self.error_pin, False)))
return
self.link = self.open_function(
self.connection_tuple, baud=self.baud, timeout=self.open_timeout)
if self.error_pin:
self.to_parent_q.put(('pin', (self.error_pin, False)))
self.last_read = time.time()
self.last_receipt = time.time()
self.reset_flag = True
def Reopen(self, log_message=None):
"""Closes and reopens a link, optionally logging a message."""
if self.connection_type == CONNECTION_FLAG_SIMULATED:
raise NotImplementedError('Not implemented for simulations')
self.link = ReopenConnection(
self.open_function, self.link, self.connection_tuple,
baud=self.baud, timeout=self.open_timeout, log_message=log_message)
if self.error_pin:
self.to_parent_q.put(('pin', (self.error_pin, False)))
self.reset_flag = True
self.last_read = time.time()
self.last_receipt = time.time()
def Close(self, close_message):
"""Closes an open serial connection."""
if self.connection_type == CONNECTION_FLAG_SIMULATED:
return
self.link.close()
if self.error_pin:
self.to_parent_q.put(('pin', (self.error_pin, True, close_message)))
Log(close_message, self)
def Available(self):
"""Calls self.link.available()."""
if self.connection_type == CONNECTION_FLAG_SIMULATED:
raise NotImplementedError('Not implemented for simulations')
<----SKIPPED LINES---->
flag = self.reset_flag
self.reset_flag = False
return flag
def RunCommand(cmd, sleep_seconds=1, log=True):
"""Runs shell command, checking if it completed (perhaps with errors) within timeout."""
conn = subprocess.Popen(cmd, shell=True)
time.sleep(sleep_seconds)
conn.poll()
if conn.returncode is None:
Log('ERROR: %s did not complete within %d seconds'
% (cmd, sleep_seconds))
sys.exit()
if log:
Log('%s completed' % cmd)
def OpenBluetooth(connection_tuple, baud=9600, timeout=5):
"""Attempts to open bluetooth for a number of attempts, exiting program on fail.
This may fail due to a missing /dev/rfcomm# entry and inability to create new one (or
an existing one bound to same device number but pointing to a different serial
connection), expectation of a handshake but lack of receipt of one, or timeouts
in general before receipt.
Args:
connection_tuple: A 3-tuple of the rfcomm_device number (i.e.: the 1 in /dev/rfcomm1),
mac_address of the bluetooth radio, and the bluetooth channel; all must be provided.
baud: speed of the connection.
timeout: seconds after a connection is established that this polls for a heartbeat
before failing; a value of zero indicates no handshake needed.
Returns:
An open pySerialTransfer.SerialTransfer link.
"""
rfcomm_device, bt_mac_address, channel = connection_tuple
attempts = 5 # max number of attempts to make a connection
attempt = 1
link = None
dev = '/dev/rfcomm%d' % rfcomm_device
desc = '%s @ %s' % (bt_mac_address, dev)
if not os.path.exists(dev):
RunCommand('sudo rfcomm bind %d %s %d' % (rfcomm_device, bt_mac_address, channel))
while attempt <= attempts:
link = pySerialTransfer.SerialTransfer(dev, baud)
# We think we made a connection; lets see if we can get a receipt
start = time.time()
if not timeout:
return link
try:
while time.time() - start < timeout:
b = ReadBytes(link)
time.sleep(0.1) # avoid a tight loop soaking up all serial / RPi resources
if b:
Log('Handshake received at %s by receipt of bytes %s' % (desc, b))
return link
Log('No handshake received at %s after %d seconds on attempt %d'
% (desc, timeout, attempt))
except OSError as e:
Log('Handshake error with %s on attempt %d: %s' % (desc, attempt, e))
attempt += 1
Log('ERROR: Failed to connect to %s after %d attempts' % (bt_mac_address, attempt - 1))
sys.exit()
def OpenUSB(connection_tuple=('arduino', None), baud=9600, timeout=5):
"""Attempts to open USB for a number of seconds, exiting program on fail.
This may fail due to lack of a plugged-in serial device matching the given definition.
Args:
connection_tuple: A 2-tuple of the manufacturer and the serial number of the expected
device to connect with; either one may be missing (by providing a None value).
baud: speed of the connection.
timeout: seconds polling for the device matching the connection_tuple before fail
on timeout.
Raises:
serial.SerialException: raised if no serial matching given attributes found
<----SKIPPED LINES---->
port_sn = port.serial_number
match_sn = not sn or port_sn == sn
if match_mfg and match_sn:
arduino_port = port.device
break
link = None
if arduino_port:
link = pySerialTransfer.SerialTransfer(arduino_port, baud=baud)
time.sleep(2) # Need to give Arduino time before it is ready to receive
else: # no USB-based matching port found
raise serial.SerialException(
'ERROR: No USB port found for mfg %s and sn %s' % (manufacturer, sn))
return link
def ReopenConnection(
open_function, link, connection_tuple, baud=9600, timeout=3, log_message=None):
"""Close and reopen the serial."""
if log_message:
Log(log_message)
link.close()
link = open_function(connection_tuple, baud=baud, timeout=timeout)
return link
def Write(link, values, format_string):
"""Sends the encapsulated string command on an open pySerialTransfer."""
packed_bytes = struct.pack(format_string, *values)
for n, b in enumerate(packed_bytes):
link.txBuff[n] = b
link.send(len(packed_bytes))
def ReadBytes(link):
"""Reads the bytes at the link, returning a byte object."""
read_bytes = []
try:
if link.available():
if link.status < 0:
raise serial.SerialException('ERROR: %s' % link.status)
read_bytes = []
<----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_ordinal' in configuration:
messageboard.RemoveSetting(configuration, 'test_servos_ordinal')
<----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)
<----SKIPPED LINES---->
|
01234567890123456789012345678901234567890123456789012345678901234567890123456789
211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445 462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508 638639640641642643644645646647648649650651652653654655656657 658659660661662663664665666667668669670671672673674675676677 12161217121812191220122112221223122412251226122712281229123012311232123312341235 12361237123812391240124112421243124412451246124712481249125012511252125312541255 |
<----SKIPPED LINES---->
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:
self.to_parent_q.put(('pin', (self.error_pin, False)))
return
self.link = self.open_function(
self.connection_tuple, name=self.name, baud=self.baud, timeout=self.open_timeout)
if self.error_pin:
self.to_parent_q.put(('pin', (self.error_pin, False)))
self.last_read = time.time()
self.last_receipt = time.time()
self.reset_flag = True
def Reopen(self, log_message=None):
"""Closes and reopens a link, optionally logging a message."""
if self.connection_type == CONNECTION_FLAG_SIMULATED:
raise NotImplementedError('Not implemented for simulations')
self.link = ReopenConnection(
self.open_function, self.link, self.connection_tuple,
name=self.name, baud=self.baud, timeout=self.open_timeout, log_message=log_message)
if self.error_pin:
self.to_parent_q.put(('pin', (self.error_pin, False)))
self.reset_flag = True
self.last_read = time.time()
self.last_receipt = time.time()
def Close(self, close_message):
"""Closes an open serial connection."""
if self.connection_type == CONNECTION_FLAG_SIMULATED:
return
self.link.close()
if self.error_pin:
self.to_parent_q.put(('pin', (self.error_pin, True, close_message)))
Log(close_message, self)
def Available(self):
"""Calls self.link.available()."""
if self.connection_type == CONNECTION_FLAG_SIMULATED:
raise NotImplementedError('Not implemented for simulations')
<----SKIPPED LINES---->
flag = self.reset_flag
self.reset_flag = False
return flag
def RunCommand(cmd, sleep_seconds=1, log=True):
"""Runs shell command, checking if it completed (perhaps with errors) within timeout."""
conn = subprocess.Popen(cmd, shell=True)
time.sleep(sleep_seconds)
conn.poll()
if conn.returncode is None:
Log('ERROR: %s did not complete within %d seconds'
% (cmd, sleep_seconds))
sys.exit()
if log:
Log('%s completed' % cmd)
def OpenBluetooth(connection_tuple, baud=9600, timeout=5, name=None):
"""Attempts to open bluetooth for a number of attempts, exiting program on fail.
This may fail due to a missing /dev/rfcomm# entry and inability to create new one (or
an existing one bound to same device number but pointing to a different serial
connection), expectation of a handshake but lack of receipt of one, or timeouts
in general before receipt.
Args:
connection_tuple: A 3-tuple of the rfcomm_device number (i.e.: the 1 in /dev/rfcomm1),
mac_address of the bluetooth radio, and the bluetooth channel; all must be provided.
baud: speed of the connection.
timeout: seconds after a connection is established that this polls for a heartbeat
before failing; a value of zero indicates no handshake needed.
name: string name of the connection to display in error logging.
Returns:
An open pySerialTransfer.SerialTransfer link.
"""
rfcomm_device, bt_mac_address, channel = connection_tuple
attempts = 5 # max number of attempts to make a connection
attempt = 1
link = None
dev = '/dev/rfcomm%d' % rfcomm_device
if not name:
name = '%s @ %s' % (bt_mac_address, dev)
if not os.path.exists(dev):
RunCommand('sudo rfcomm bind %d %s %d' % (rfcomm_device, bt_mac_address, channel))
while attempt <= attempts:
link = pySerialTransfer.SerialTransfer(dev, baud)
# We think we made a connection; lets see if we can get a receipt
start = time.time()
if not timeout:
return link
try:
while time.time() - start < timeout:
b = ReadBytes(link)
time.sleep(0.1) # avoid a tight loop soaking up all serial / RPi resources
if b:
Log('Handshake received at %s by receipt of bytes %s' % (name, b))
return link
Log('No handshake received at %s after %d seconds on attempt %d'
% (name, timeout, attempt))
except OSError as e:
Log('Handshake error with %s on attempt %d: %s' % (name, attempt, e))
attempt += 1
Log('ERROR: Failed to connect to %s after %d attempts' % (bt_mac_address, attempt - 1))
sys.exit()
def OpenUSB(connection_tuple=('arduino', None), baud=9600, timeout=5):
"""Attempts to open USB for a number of seconds, exiting program on fail.
This may fail due to lack of a plugged-in serial device matching the given definition.
Args:
connection_tuple: A 2-tuple of the manufacturer and the serial number of the expected
device to connect with; either one may be missing (by providing a None value).
baud: speed of the connection.
timeout: seconds polling for the device matching the connection_tuple before fail
on timeout.
Raises:
serial.SerialException: raised if no serial matching given attributes found
<----SKIPPED LINES---->
port_sn = port.serial_number
match_sn = not sn or port_sn == sn
if match_mfg and match_sn:
arduino_port = port.device
break
link = None
if arduino_port:
link = pySerialTransfer.SerialTransfer(arduino_port, baud=baud)
time.sleep(2) # Need to give Arduino time before it is ready to receive
else: # no USB-based matching port found
raise serial.SerialException(
'ERROR: No USB port found for mfg %s and sn %s' % (manufacturer, sn))
return link
def ReopenConnection(
open_function, link, connection_tuple,
name=None, baud=9600, timeout=3, log_message=None):
"""Close and reopen the serial."""
if log_message:
Log(log_message)
link.close()
link = open_function(connection_tuple, name=name, baud=baud, timeout=timeout)
return link
def Write(link, values, format_string):
"""Sends the encapsulated string command on an open pySerialTransfer."""
packed_bytes = struct.pack(format_string, *values)
for n, b in enumerate(packed_bytes):
link.txBuff[n] = b
link.send(len(packed_bytes))
def ReadBytes(link):
"""Reads the bytes at the link, returning a byte object."""
read_bytes = []
try:
if link.available():
if link.status < 0:
raise serial.SerialException('ERROR: %s' % link.status)
read_bytes = []
<----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=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_ordinal' in configuration:
messageboard.RemoveSetting(configuration, 'test_servos_ordinal')
<----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=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)
<----SKIPPED LINES---->
|