def bumpWheelDrop(index): # requests sensor packet data connection.write(142) # wait for command to be sent sleep() # request the bump sensor packet connection.write(7) sleep() data = connection.read(1) # interbret data as byte byte = struct.unpack('B', data)[0] # right bump sensor bumpRight = bool(byte & 0x01) bumpLeft = bool(byte & 0x02) wheelDropRight = bool(byte & 0x04) wheelDropLeft = bool(byte & 0x08) # return bumpRight if (index == 0): return bumpRight elif (index == 1): return bumpLeft elif (index == 2): return wheelDropRight elif (index == 3): return wheelDropLeft elif (index == 4): return bool(byte != 0) else: return byte
def distanceRead(): connection.write(142) sleep() connection.write(19) sleep() data = connection.read(1) # print data byte = struct.unpack('i', data)[0] print byte
def angleRead(): connection.write(142) # packet 20 is angle sleep() connection.write(20) # returns signed 2 bytes sleep() data = connection.read(1) # print data byte = struct.unpack('i', data)[0] print byte
def cliffRead(): # declare array to hold results from cliffArray cliffArr = [0,0,0,0,0] counter = 0 for i in range(9,14): # requests sensor data connection.write(142) #requests each cliff sensor connection.write(i) data = connection.read(1) byte = struct.unpack('B', data)[0] cliffArr[counter] = byte counter += 1 return cliffArr
def readButton(): # requests sensor packet data. connection.write(142) # waits 0.015 seconds for the code to process. sleep() # requests button packet data. # should read 1 byte which is returned. connection.write(18) # waits 0.015 seconds for the code to process. sleep() # reads data from the buttons. data = connection.read(1) # unpacks the data read to byte. byte = struct.unpack('B' ,data)[0] return byte