def update(): global outputLabel cont.update() buttons1 = 0x0 buttons2 = 0x0 # going to eight would include the start button; however, it seems that when 0x80 (only the start button) is sent # the arduino lags for a second or two and reports false values for buttons for i in range(0, cont.getNumButtons()): if(cont.getButton(i)): if(cont.getValueForButton(i) <= 0xFF): buttons1 += cont.getValueForButton(i) else: buttons2 += cont.getValueForButton(i) >> 8 outbound.write("STR") # sends a signal to tell that this is the start of data outbound.write(chr(buttons1)) # writes the buttons first outbound.write(chr(buttons2)) outbound.write(str(int(cont.getPrimaryX()))) # casts the floats to ints, then to strings for simple parsing outbound.write(" ") outbound.write(str(int(cont.getPrimaryY()))) outbound.write(" ") outbound.write(str(int(cont.getSecondaryX()))) outbound.write(" ") outbound.write(str(int(cont.getSecondaryY()))) outbound.write(" ") outbound.write(str(int(cont.getTriggers()))) outbound.write(" ") counter = 10 proceed = False while True and counter > 0: counter -= 1 if outbound.readable(): if 'S' == outbound.read(1): if 'T' == outbound.read(1): if 'R' == outbound.read(1): proceed = True break if(proceed): linesToRead = int(outbound.read(3)) # allows for up to 999 lines to be read... for i in range(0, linesToRead): outputLabel['text'] = outbound.readline().rstrip() outputLabel.after(100, update)
cont.update() buttons1 = 0x0 buttons2 = 0x0 # going to eight would include the start button; however, it seems that when 0x80 (only the start button) is sent # the arduino lags for a second or two and reports false values for buttons for i in range(0, cont.getNumButtons()): if(cont.getButton(i)): if(cont.getValueForButton(i) <= 0xFF): buttons1 += cont.getValueForButton(i) else: buttons2 += cont.getValueForButton(i) >> 8 outbound.write("STR") # sends a signal to tell that this is the start of data outbound.write(chr(buttons1)) # writes the buttons first outbound.write(chr(buttons2)) outbound.write(str(int(cont.getPrimaryX()))) # casts the floats to ints, then to strings for simple parsing outbound.write(" ") outbound.write(str(int(cont.getPrimaryY()))) outbound.write(" ") outbound.write(str(int(cont.getSecondaryX()))) outbound.write(" ") outbound.write(str(int(cont.getSecondaryY()))) outbound.write(" ") outbound.write(str(int(cont.getTriggers()))) outbound.write(" ") time.sleep(0.1) # allows for chirps of information rather than a stream
def update(): cont.update() buttons1 = 0x0 buttons2 = 0x0 # going to eight would include the start button; however, it seems that when 0x80 (only the start button) is sent # the arduino lags for a second or two and reports false values for buttons for i in range(0, cont.getNumButtons()): if cont.getButton(i): if cont.getValueForButton(i) <= 0xFF: buttons1 += cont.getValueForButton(i) else: buttons2 += cont.getValueForButton(i) >> 8 outbound.write("STR") # sends a signal to tell that this is the start of data outbound.write(chr(buttons1)) # writes the buttons first outbound.write(chr(buttons2)) outbound.write(str(int(cont.getPrimaryX()))) # casts the floats to ints, then to strings for simple parsing outbound.write(" ") outbound.write(str(int(cont.getPrimaryY()))) outbound.write(" ") outbound.write(str(int(cont.getSecondaryX()))) outbound.write(" ") outbound.write(str(int(cont.getSecondaryY()))) outbound.write(" ") outbound.write(str(int(cont.getTriggers()))) outbound.write(" ") counter = 10 proceed = False pressureOut["bg"] = "red" voltageOut["bg"] = "red" tempOut["bg"] = "red" accelOut["bg"] = "red" depthOut["bg"] = "red" pressureLabel["bg"] = "red" voltageLabel["bg"] = "red" tempLabel["bg"] = "red" accelLabel["bg"] = "red" depthLabel["bg"] = "red" while True and counter > 0: counter -= 1 if outbound.readable(): if "S" == outbound.read(1): if "T" == outbound.read(1): if "R" == outbound.read(1): proceed = True break if proceed: linesToRead = int(outbound.read(3)) # allows for up to 999 lines to be read... for i in range(0, linesToRead // 2): label = outbound.readline().rstrip().lstrip() if label == "PSR": pressureOut["text"] = outbound.readline().rstrip() pressureOut["bg"] = "green" pressureLabel["bg"] = "green" elif label == "VLT": voltageOut["text"] = outbound.readline().rstrip() voltageOut["bg"] = "green" voltageLabel["bg"] = "green" elif label == "TMP": tempOut["text"] = outbound.readline().rstrip() tempOut["bg"] = "green" tempLabel["bg"] = "green" elif label == "ACL": accelOut["text"] = outbound.readline().rstrip() + "\n" accelOut["text"] += outbound.readline().rstrip() + "\n" accelOut["text"] += outbound.readline().rstrip() accelOut["bg"] = "green" accelLabel["bg"] = "green" elif label == "DPT": depthOut["text"] = outbound.readline().rstrip() depthOut["bg"] = "green" depthLabel["bg"] = "green" else: print "unknown datatype:", label print "data:", outbound.readline().rstrip() root.after(100, update)
# Convert the 0-1 range into a value in the right range. return(rightMin + (valueScaled * rightSpan)) if __name__ == '__main__': controller.init() motorSerial = serial.Serial('/dev/ttyUSB0', 9600, timeout=0.1) xbox = controller.getController() while(True): leftSpeed = 0 rightSpeed = 0 buttons = controller.getButtons(xbox) triggers = controller.getTriggers(xbox) leftSpeed += translate(triggers["LT"], -1, 1, 0, 250) rightSpeed += translate(triggers["LT"], -1, 1, 0, 250) leftSpeed -= translate(triggers["RT"], -1, 1, 0, 250) rightSpeed -= translate(triggers["RT"], -1, 1, 0, 250) if(buttons["LB"] == 1): leftSpeed /= 1.5 rightSpeed *= 1.5 if(buttons["RB"] == 1): leftSpeed *= 1.5 rightSpeed /= 1.5
bytesize=serial.EIGHTBITS ) print "connect the controller" cont.update() while not cont.isConnected(): cont.update() print "controller connected" while(1): cont.update() buttons = 0x0 for i in range(0, 8): if cont.getButton(i): buttons += (1 << i) #ser.print("STR")#should I send an ok signal to start the communication? how long does that take? ser.write(chr(buttons)) ser.write(str(int(cont.getPrimaryX()))) ser.write(" ") ser.write(str(int(cont.getPrimaryY()))) ser.write(" ") ser.write(str(int(cont.getSecondaryX()))) ser.write(" ") ser.write(str(int(cont.getSecondaryY()))) ser.write(" ") ser.write(str(int(cont.getTriggers()))) ser.write(" ") time.sleep(0.1) #ser.write("END")#should tis be replied to? or just not sent?
def update(): cont.update() buttons1 = 0x0 buttons2 = 0x0 # going to eight would include the start button; however, it seems that when 0x80 (only the start button) is sent # the arduino lags for a second or two and reports false values for buttons for i in range(0, cont.getNumButtons()): if(cont.getButton(i)): if(cont.getValueForButton(i) <= 0xFF): buttons1 += cont.getValueForButton(i) else: buttons2 += cont.getValueForButton(i) >> 8 outbound.write("STR") # sends a signal to tell that this is the start of data outbound.write(chr(buttons1)) # writes the buttons first outbound.write(chr(buttons2)) outbound.write(str(int(cont.getPrimaryX()))) # casts the floats to ints, then to strings for simple parsing outbound.write(" ") outbound.write(str(int(cont.getPrimaryY()))) outbound.write(" ") outbound.write(str(int(cont.getSecondaryX()))) outbound.write(" ") outbound.write(str(int(cont.getSecondaryY()))) outbound.write(" ") outbound.write(str(int(cont.getTriggers()))) outbound.write(" ") counter = 10 proceed = False pressureOut['bg'] = "red" voltageOut['bg'] = "red" tempOut['bg'] = "red" accelOut['bg'] = "red" depthOut['bg'] = "red" pressureLabel['bg'] = "red" voltageLabel['bg'] = "red" tempLabel['bg'] = "red" accelLabel['bg'] = "red" depthLabel['bg'] = "red" while True and counter > 0: counter -= 1 if outbound.readable(): if 'S' == outbound.read(1): if 'T' == outbound.read(1): if 'R' == outbound.read(1): proceed = True break if(proceed): linesToRead = int(outbound.read(3)) # allows for up to 999 lines to be read... for i in range(0, linesToRead // 2): label = outbound.readline().rstrip().lstrip() if(label == "PSR"): pressureOut['text'] = outbound.readline().rstrip() pressureOut['bg'] = "green" pressureLabel['bg'] = "green" elif(label == "VLT"): voltageOut['text'] = outbound.readline().rstrip() voltageOut['bg'] = "green" voltageLabel['bg'] = "green" elif(label == "TMP"): tempOut['text'] = outbound.readline().rstrip() tempOut['bg'] = "green" tempLabel['bg'] = "green" elif(label == "ACL"): accelOut['text'] = outbound.readline().rstrip() + '\n' accelOut['text'] += outbound.readline().rstrip() + '\n' accelOut['text'] += outbound.readline().rstrip() accelOut['bg'] = "green" accelLabel['bg'] = "green" elif(label == "DPT"): depthOut['text'] = outbound.readline().rstrip() depthOut['bg'] = "green" depthLabel['bg'] = "green" else: print "unknown datatype:", label print "data:", outbound.readline().rstrip() root.after(100, update)
else: outbound.write(str(int(cont.getPrimaryX()))) outbound.write(" ") outbound.write(str(int(cont.getPrimaryY()))) #outbound.write(" ") #outbound.write(str(int(cont.getPrimaryY()))) outbound.write(" ") outbound.write(str(int(cont.getSecondaryX()))) outbound.write(" ") outbound.write(str(int(cont.getSecondaryY()))) outbound.write(" ") #outbound.write(str(int(cont.getTriggers()))) if half_m: outbound.write(str(int(cont.getTriggers()) / 2)) else: outbound.write(str(int(cont.getTriggers()))) outbound.write(" ") except: print "Crashed while reading controller input" pass counter = 10 proceed = False got = '' #shows in red the old data in case of lost connection. textwrite(200, 90, (str(pressure) + " mbars"), 255, 10, 10) textwrite(200, 110, (str(current) + " amps"), 255, 10, 10)
p_factor = 1 try: outbound.write("STR") # sends a signal to tell that this is the start of data outbound.write(chr(buttons1))# writes the buttons first outbound.write(chr(buttons2)) outbound.write(str(int(cont.getPrimaryX() * p_factor)))# casts the floats to ints, then to strings for simple parsing outbound.write(" ") outbound.write(str(int(cont.getPrimaryY() * p_factor))) outbound.write(" ") outbound.write(str(int(cont.getSecondaryX() * p_factor))) outbound.write(" ") outbound.write(str(int(cont.getSecondaryY() * p_factor))) outbound.write(" ") outbound.write(str(int(cont.getTriggers() * p_factor))) outbound.write(" ") except serial.serialutil.SerialException: no_serial = True except: print "Crashed while sending controller input" got = '' #shows in red the old data in case of lost connection. textwrite(200, 90, (str(pressure) + " mbars"), 255, 10, 10) textwrite(200, 110, (str(current) + " amps"), 255, 10, 10) textwrite(200, 130, (str(temperature) + " degrees C"), 255, 10, 10) textwrite(200, 150, (str(depth) + " feet"), 255, 10, 10) textwrite(200, 170, str(ypr), 255, 10, 10)