Permalink
Browse files

first commit - existing code

  • Loading branch information...
roboalchemist committed May 2, 2015
1 parent 10470aa commit 78e1081d00e0277a57975cd9a99f214b7f4f5794
Showing with 3,063 additions and 0 deletions.
  1. +249 −0 Comms/SerialComms.py
  2. BIN Comms/SerialComms.pyc
  3. +203 −0 Controllers/Servotor32.py
  4. BIN Controllers/Servotor32.pyc
  5. +1,418 −0 GUI.py
  6. BIN GUI.pyc
  7. BIN Images/Arduino.png
  8. BIN Images/Foot_Guide_300.jpg
  9. BIN Images/Head_Guide_300.jpg
  10. BIN Images/Hip_Guide_300.jpg
  11. BIN Images/RecBtn.png
  12. BIN Images/Refresh.png
  13. BIN Images/StopBtn.png
  14. BIN Images/Thigh_Guide_300.jpg
  15. BIN Images/greenBut.png
  16. BIN Images/greenBut2.png
  17. BIN Images/redBut.png
  18. BIN Images/redBut2.png
  19. BIN Images/robot_300.jpg
  20. +104 −0 PoMoCo.py
  21. +47 −0 PoMoCoModule.py
  22. BIN PoMoCoModule.pyc
  23. +9 −0 Robots/Hexy V1/Moves/BellyFlop.py
  24. BIN Robots/Hexy V1/Moves/BellyFlop.pyc
  25. +39 −0 Robots/Hexy V1/Moves/Dance.py
  26. BIN Robots/Hexy V1/Moves/Dance.pyc
  27. +34 −0 Robots/Hexy V1/Moves/GetUp.py
  28. BIN Robots/Hexy V1/Moves/GetUp.pyc
  29. +27 −0 Robots/Hexy V1/Moves/LeanBack.py
  30. BIN Robots/Hexy V1/Moves/LeanBack.pyc
  31. +37 −0 Robots/Hexy V1/Moves/MoveBackward.py
  32. BIN Robots/Hexy V1/Moves/MoveBackward.pyc
  33. +37 −0 Robots/Hexy V1/Moves/MoveForward.py
  34. BIN Robots/Hexy V1/Moves/MoveForward.pyc
  35. +12 −0 Robots/Hexy V1/Moves/Point.py
  36. BIN Robots/Hexy V1/Moves/Point.pyc
  37. +33 −0 Robots/Hexy V1/Moves/Reset.py
  38. BIN Robots/Hexy V1/Moves/Reset.pyc
  39. +38 −0 Robots/Hexy V1/Moves/RotateLeft.py
  40. BIN Robots/Hexy V1/Moves/RotateLeft.pyc
  41. +39 −0 Robots/Hexy V1/Moves/RotateRight.py
  42. BIN Robots/Hexy V1/Moves/RotateRight.pyc
  43. +4 −0 Robots/Hexy V1/Moves/SetZero.py
  44. BIN Robots/Hexy V1/Moves/SetZero.pyc
  45. BIN Robots/Hexy V1/Moves/Test.pyc
  46. +11 −0 Robots/Hexy V1/Moves/TiltBackward.py
  47. BIN Robots/Hexy V1/Moves/TiltBackward.pyc
  48. +11 −0 Robots/Hexy V1/Moves/TiltForward.py
  49. BIN Robots/Hexy V1/Moves/TiltForward.pyc
  50. +9 −0 Robots/Hexy V1/Moves/TiltLeft.py
  51. BIN Robots/Hexy V1/Moves/TiltLeft.pyc
  52. +23 −0 Robots/Hexy V1/Moves/TiltNone.py
  53. BIN Robots/Hexy V1/Moves/TiltNone.pyc
  54. +9 −0 Robots/Hexy V1/Moves/TiltRight.py
  55. BIN Robots/Hexy V1/Moves/TiltRight.pyc
  56. +17 −0 Robots/Hexy V1/Moves/Typing.py
  57. BIN Robots/Hexy V1/Moves/Typing.pyc
  58. +23 −0 Robots/Hexy V1/Moves/Wave.py
  59. BIN Robots/Hexy V1/Moves/Wave.pyc
  60. +10 −0 Robots/Hexy V1/robot.inf
  61. BIN Robots/Hexy V1/robot.jpg
  62. +300 −0 Robots/Hexy V1/robot.py
  63. BIN Robots/Hexy V1/robot.pyc
  64. +320 −0 Robots/Hexy V1/servos.inf
@@ -0,0 +1,249 @@
import sys, os, time, serial, threading
import multiprocessing, Queue
# chose an implementation, depending on os
if os.name == 'nt': #sys.platform == 'win32':
from serial.tools.list_ports_windows import *
elif os.name == 'posix':
from serial.tools.list_ports_posix import *
else:
raise ImportError("Sorry: no implementation for your platform ('%s') available" % (os.name,))
import PoMoCoModule
class SerialLink(PoMoCoModule.Node):
def __init__(self):
super(SerialLink, self).__init__()
threading.Thread.__init__(self)
self.moduleType = "comms"
self.baud_rate = 9600
self.ports = None
self.ser = None
self.connected = False
self.connectedPort = None
self.timeout_period = 8
self.serial_incoming = []
self.last_received = ""
self.buffer = ""
self.debug = True
self.autoConnStr = ""
self.port_priority = ["VID:PID=2341:8036",
"USB",
"BT",
"",
]
self.priority_desc = ["Servotor32 (VID/PID matched)",
"USB Serial device",
"Windows Bluetooth Serial device",
"Unknown Type Serial Port",
]
self.moduleType = 'comms'
PoMoCoModule.Node.modules[self.moduleType] = self.inNoteQueue
self.start()
def __del__(self):
self.ser.close()
def run(self):
#main thread after init
startTime = time.clock()
while True:
#every 1 second(s) poll the available serial ports
if time.clock()-startTime >= 1.0:
self.portRefresh()
startTime = time.clock()
try:
note = self.inNoteQueue.get(block=False)
self.processNote(note)
except Queue.Empty: # nothing in the Queue
#process the serial buffer
self.readIncomingSerial()
#process incoming serial messages
self.processSerial()
time.sleep(0) # keeps infinite loop from hogging all the CPU
def portRefresh(self):
#check the existing ports and see if any new ones have popped up
#notify the GUI of any changes
self.debug = False
oldPorts = []
if self.ports:
oldPorts = self.ports[:]
self.scanForPorts()
if self.ports != oldPorts:
if self.debug: print "difference!"
portList = ""
for port in self.ports:
portList+=port['name']+","
if len(portList) > 0:
portList = portList[:-1] #remove that last comma
self.writeAndSendNote("SetPortList", portList, "controller")
def processNote(self, note):
#process an incoming PoMoCo message
#print self.moduleType,"Received Note:",note.sender,"->",note.receiver,"-",note.type,":",note.message
if note.type == "RequestConnectPort":
connectPort = note.message
self.connect(connectPort)
if note.type == "RequestAutoConnect":
self.autoConnStr = note.message
self.autoConnect()
if note.type == "SendMessage":
self.sendSerial(note.message)
if note.type == "RequestPortList":
print "comms got port list request"
self.scanForPorts()
portList = ""
for port in self.ports:
portList+=port['name']+","
if len(portList) > 0:
portList = portList[:-1] #remove that last comma
print note.sender
self.writeAndSendNote("SetPortList", portList, note.sender)
def processSerial(self):
# transfer the serial stack to the string
while len(self.serial_incoming) > 0:
print "From Controller:", self.serial_incoming.pop()
# send the string stack to the controller for processing
def readIncomingSerial(self):
#process incoming serial messages
if self.connected and self.ser:
try:
self.buffer = self.buffer + self.ser.read(self.ser.inWaiting())
except IOError as detail:
print "Serial connection unexpectedly terminated:",detail
self.ser = None
self.connected = False
self.writeAndSendNote("SetFirmwareV","?","controller")
self.writeAndSendNote("SetConnectionState","inactive","controller")
if '\n' in self.buffer:
lines = self.buffer.split('\n') # Guaranteed to have at least 2 entries
last_received = lines[-2]
self.serial_incoming.append(last_received)
#If the Arduino sends lots of empty lines, you'll lose the
#last filled line, so you could make the above statement conditional
#like so: if lines[-2]: last_received = lines[-2]
self.buffer = lines[-1]
return True
return False
def sendSerial(self, toSend):
if self.ser:
#print "sending "+str(self.ser.name)+": '"+str(toSend.strip('\n'))+"'"
pass
else:
#print "sending (not connected): '"+str(toSend.strip('\n'))+"'"
pass
if self.ser:
if self.ser.writable:
self.ser.write(str(toSend))
return True
return False
def scanForPorts(self):
#scans for and sorts ports
unsorted_ports = []
for portPath, desc, hwid in comports():
portPath = portPath.replace("/dev/cu.","/dev/tty.",1)
port = {"name":portPath, "desc":desc, "hwid":hwid}
unsorted_ports.append(port)
priority_val = 0
sorted_ports = []
while (len(unsorted_ports) > 0) and (priority_val < len(self.port_priority)):
for port in unsorted_ports:
# see if the string is part of the description
if self.port_priority[priority_val] in port['hwid']:
#print self.port_priority[priority_val],"in", port['hwid']
if port not in sorted_ports:
sorted_ports.append(port)
else:
pass
#print self.port_priority[priority_val],"not in", port['hwid']
priority_val += 1
if len(sorted_ports) > 0:
self.ports = sorted_ports
if self.debug:
print "Available Serial Ports:"
print "-"*20
for port in self.ports:
if self.debug:
print "Name:",port['name']
print "Description:",port['desc']
print "Hardware ID:",port['hwid']
print "-"*20
else:
if self.debug:
print "No serial ports available!"
print "Are the drivers installed?"
print "Device powered/plugged in?"
print "Bluetooth paired?"
self.ports = None
def autoConnect(self):
#automatically connect to the "best" available port
if self.connected:
print "Already connected"
return True
self.scanForPorts()
if self.ports:
for port in self.ports:
print "trying", port['name']
self.connect(port['name'])
if self.connected:
return True
return None
def connect(self, portName):
#connect to a specifically named port
self.connected = False
self.ser = None
result = ""
try:
self.ser = serial.Serial(portName, baudrate=self.baud_rate, timeout=self.timeout_period, writeTimeout=self.timeout_period)
if not self.ser.isOpen():
print "Connection failed:",portName,"has already been opened by another process"
self.ser = None
return False
self.ser.flush()
time.sleep(0.1)
self.sendSerial('V\n')
time.sleep(1)
result = self.ser.readline()
if self.autoConnStr in result:
print "Connected to",portName
firmware = result.rstrip('\n\r')
print "Firmware Version:",firmware
self.writeAndSendNote("SetFirmwareV",firmware,"controller")
self.ser.flush()
self.connected = True
self.writeAndSendNote("SetConnectionState","active","controller")
else:
self.ser = None
print "Device not on",portName
except serial.serialutil.SerialException as detail:
print "Serial Exception:",portName,",",sys.exc_info()
self.ser = None
except:
print "Connection failed:",portName,",",sys.exc_info()
self.ser = None
if __name__ == '__main__':
serialObject = Serial()
serialObject.autoConnect()
serialObject.sendSerial('C\n')
serialObject.sendSerial('K\n')
serialObject.sendSerial('V\n')
BIN +6.9 KB Comms/SerialComms.pyc
Binary file not shown.
Oops, something went wrong.

0 comments on commit 78e1081

Please sign in to comment.