-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommCommunicator.py
92 lines (72 loc) · 2.76 KB
/
CommCommunicator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
'''
Created on Nov 13, 2014
This is the class that controls the TQ10X drivers.
@author: John Lorenz
'''
import serial
from time import sleep
BAUD_RATE = 9600
class CommCommunicator(object):
def __init__(self, devices):
self.devices = devices
self.ser = None
def open(self):
'''Open the serial connection to the TQ10X driver'''
print 'opening serial communication'
for device in self.devices:
try:
self.ser = serial.Serial(device,
BAUD_RATE,
bytesize=serial.EIGHTBITS,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
timeout=.1)
self.ser.write('#1\r')
sleep(1)
self.ser.write('1R\r')
sleep(1)
print 'writing "1R"'
response = self.ser.read(10)
print "read: "+str(response)+"\n"
if "*" in str(response):
print 'response indicates TQ10X drivers, moving forward'
break
else:
print 'response does not indicate TQ10x, next port'
except Exception:
print 'encountered exception'
pass
if self.ser is None:
raise Exception
sleep(2)
commands = ["#1", "1OSA1", "2OSA1", "V1", "A1", "D0", "INE1"]
self.sendCommands(commands)
def close(self):
self.ser.close()
self.ser = None
def sendCommands(self, commands):
for s in commands:
self.ser.write(s+'\r')
print self.ser.read(10)
return True
def moveArm(self, encoderCounts, direction, speed):
commands = ["S", "A5", "V"+str(speed), "D"+str(encoderCounts), "G"]
for i in range(0, len(commands)):
commands[i] = str(direction)+commands[i];
commandsSuccessful = self.sendCommands(commands);
sleep(4);
return commandsSuccessful;
if __name__ == '__main__':
USB_devices = ['/dev/ttyUSB0', '/dev/ttyUSB1', '/dev/ttyUSB2',
'/dev/ttyUSB3', '/dev/ttyACM0', '/dev/ttyACM1', '/dev/ttyACM2']
CC = CommCommunicator(USB_devices)
CC.open()
c = '1'
# Used for just sending communication to the TQ10X drivers
# To use, just run this script. Any command typed will be send directly
# to the drivers. For example, typing 1S will tell the vertical driver to
# stop movement
while c!=0:
c = raw_input('Command: ')
s = c.split()
CC.sendCommands(s)