class Cobot: """ Class used to read and write data from/to the COM-to-USB port. This class is used in the Cobot project (= a project consisting of writing a driver in C for the Jobot robot and implementing a communication channel between a SunSpot and the microcontroller of the Jobot). """ def __init__(self): """ Initialise the serial connection to the Cobot robot. """ self.serial = Serial(0) self.serial.setParity(PARITY_NONE) self.serial.setByteSize(EIGHTBITS) self.serial.setStopbits(STOPBITS_ONE) self.serial.setBaudrate(9600) self.msg_count = 0 print 'i Initialised serial connection "%s".' % self.serial.portstr # Read welcome header self.readline() # Enable debug mode. This is useful if the Jobot sends additional # information about the input/output pin states. This additional # information is sent, when the Jobot's source code is compiled with # DEBUG_MODE directive is enabled. See also the "DEBUG_MODE" directive # in the source file "ex_led.c". self.debug_mode = 0 # Read handshake status information or, if DEBUG_MODE is enabled in # the Jobot's source code, input/output pin states. self.readline() self.readline() self.readline() self.readline() def __delete__(self): """ Disconnect the serial connnection to the Jobot. """ print 'i Destroying serial connection "%s".' % self.serial.portstr self.serial.close() def readline(self): """ Read a single newline terminated line from the serial connection. """ print '%d < %s' % (self.msg_count, self.serial.readline()[:-1]) self.msg_count += 1 def write(self, msg): """ Write a message through the serial connection to the Jobot. """ print '%d > %s' % (self.msg_count, msg) self.serial.write(msg) self.msg_count += 1 def set_output(self, state): """ Set output pin states (PIN_D4 and PIN_D5) of the Jobot. """ if not state in (0, 1, 2, 3): raise CobotException('Output pin state should be in {0, 1, 2, 3}.') # Set output pin states self.write(str(state)) # Read both output pin states self.readline() self.readline() # Read both input pin states self.readline() self.readline()
from time import sleep from serial import Serial id = 255 s = Serial("/dev/ttyUSB0", 19200) s.setStopbits(2) def clear(): while s.inWaiting(): s.read() def write(cmd): s.write("{0}:{1}\n".format(id, cmd)) def read(cmd): write(cmd) buf = "_" while buf[-1] != "\n": buf += s.read() clear() return buf[1:] def dribbler(sp = 90): while 1: sleep(1) write("dm{0}".format(sp)) def ball_sense():
class Hemisson: def __init__(self): """ Initialise the serial connection to the Hemisson robot. """ # Initialise serial connection on /dev/ttyUSB0 self.serial = Serial(0) self.serial.setParity(PARITY_NONE) self.serial.setByteSize(EIGHTBITS) self.serial.setStopbits(STOPBITS_ONE) self.serial.setBaudrate(115200) # Connection established, read welcome message from Hemisson. print 'i Initialised serial connection "%s".' % self.serial.portstr self.serial.write(chr(254)+'\r') self.readline() self.readline() self.readline() def __delete__(self): """ Disconnect the serial connnection to the Hemisson robot. """ print 'i Destroying serial connection "%s".' % self.serial.portstr self.write(chr(8)) self.serial.close() def beep(self, state): """ Generates a continuous beep, depending on state (0 = Off, 1 = On). """ if state not in (0, 1): raise HemissionException('Beep state should be 0 (Off) or 1 (On).') self.write('H,%d' % state) self.readline() def drive(self): """ Drive forward (setting both wheel's drive speed to '2'). """ self.set_speed(2) def get_switches(self): """ Read the current status of the four top switches. Possible values are 0 (= robot's right handside) and 1 (= robot's left handside). The first value is the value of the first switch from the front of the robot. """ self.write('I') self.readline() def set_speed(self, left, right=None): """ Set driving speed of left and right wheel. If only the left wheel drive speed is given, the right wheel's drive speed is set to the left wheel's drive speed. """ if right == None: right = left if not( -9 <= left <= 9 and -9 <= right <= 9): raise HemissonException( 'Keep the wheel drive speed value between -9 and 9.') self.write('D,%d,%d' % (left, right)) self.readline() def stop(self): """ Stop moving forward (setting both wheel's drive speed to zero). """ self.write('D,0,0') self.readline() def readline(self): """ Read a single newline terminated line from the serial connection. """ print '< %s' % self.serial.readline()[:-1] def remote_version(self): """ Display version of the HemiOS running on the connected Hemisson robot. """ self.write('B') self.readline() def reset(self): """ Reset the robot's processor as if the On/Off switch is cycled. """ self.serial.write('Z') self.readline() def write(self, msg): """ Write a message through the serial connection to the Hemisson robot. """ print '> %s\\n\\r' % msg self.serial.write('%s \n\r' % msg)