Source code for pneumatics_animation.ValveControlProtocol
"""\
ValveControlProtocol.py : application-specific class to manage serial communication with an Arduino ValveControl sketch.
Copyright (c) 2015-2017, Garth Zeglin. All rights reserved. Licensed under the terms
of the BSD 3-clause license.
"""
#================================================================
from __future__ import print_function
import time
from ArduinoProtocol import ArduinoProtocol
#================================================================
[docs]class ValveControlProtocol(ArduinoProtocol):
"""Example class to communicate with the ValveControlASCII Arduino sketch.
"""
def __init__(self, **kwargs ):
# initialize the parent class
ArduinoProtocol.__init__(self, **kwargs)
# define attributes to hold sensor and status values
self.awake_time = 0 #: The clock time of the most recent Arduino awake message.
self.joints = dict() #: Dictionary of most recent joint status data.
return
[docs] def message_received(self, tokens):
"""Application-specific message processing to parse lists of tokens received
from the Arduino sketch. This implementation just stores values in
instance attributes. This method overrides the null implementation in
the parent class.
:param tokens: list of string tokens
"""
# Detect wakeup.
if len(tokens) == 1:
if tokens[0] == 'awake':
print("Arduino awake.")
self.awake_time = time.time()
return
# Process the default numerical sensor status.
elif len(tokens) == 6:
self.joints[0] = {'raw': int(tokens[0]), 'position': float(tokens[1]), 'pwm': float(tokens[2])};
self.joints[1] = {'raw': int(tokens[3]), 'position': float(tokens[4]), 'pwm': float(tokens[5])};
return
################################################################
[docs] def set_flow( self, flow, extend=1, retract=2, verbose=False):
"""Issue an open-loop set of flow commands for a valve pair attached to an actuator.
Returns immediately. No return value.
:param flow: net flow from -100 full retraction rate to 100 full extension rate
:param extend: channel number for extension valve pair
:param retract: channel number for retract valve pair
:param verbose: flag to enable printed debugging output
:return: None
"""
if flow > 0:
ext_command = "speed %d %d" % (extend, flow)
ret_command = "speed %d %d" % (retract, -100)
elif flow == 0:
ext_command = "speed %d %d" % (extend, 0)
ret_command = "speed %d %d" % (retract, 0)
else:
ext_command = "speed %d %d" % (extend, -100)
ret_command = "speed %d %d" % (retract, -flow)
if verbose:
print ("Issuing %s, %s" % (ext_command, ret_command))
self.send_message_string(ext_command)
self.send_message_string(ret_command)
return
################################################################