-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBioloid.py
executable file
·127 lines (99 loc) · 3.88 KB
/
Bioloid.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import serial;
import os, sys, math;
## This class handles all low level communication with the Bioloid computer. It uses the pyserial module (see above "import serial")
## to connect to the Bioloid computer via the serial port and send it the raw command to move servos to appropriate pulse widths.
class Bioloid():
def __init__(self, port=None, baud=57600):
self.botConnected = False;
self.botConnectPort = port;
self.botConnectBaud = baud;
if(self.openBotConnection()):
self.botConnected = True;
def __del__(self):
self.closeBotConnection();
def openBotConnection(self):
try:
banner = '';
if(self.botConnectPort != None):
# If we were passed a port to connect to then use it
self.serialPort = serial.Serial(self.botConnectPort, self.botConnectBaud, timeout=.25);
self.writeToBot("scan");
x = self.readFromBot(1024);
while(len(x) > 0):
banner += x;
x = self.readFromBot(1024);
else:
# If we weren't given any ports then we better start scanning through ports looking for the bioloids
testPorts = [];
if(os.name == 'nt'): ## This is for windows
for i in range(1,6):
testPorts.append("COM%d" % (i));
else: ## This is for Linux
for i in range(6):
testPorts.append("/dev/ttyUSB%d" % (i));
# Run through the ports and try connecting and sending the "scan" bioloid command.
# "scan" seemed to be the safest (non-volatile) command to send to see if we get a response.
# We then look for the word "Dynamixel" somewhere in the response and see if it is successful.
for i in testPorts:
try:
self.botConnectPort = i;
print "TRYING PORT ", self.botConnectPort;
self.serialPort = serial.Serial(self.botConnectPort, self.botConnectBaud, timeout=.25);
# Send the "scan" command
self.writeToBot("scan");
# Read from the bot 1024 characters at a time appending to banner
x = self.readFromBot(1024);
while(len(x) > 0):
banner += x;
x = self.readFromBot(1024);
# Look for the word "Dynamixel"
if(banner.find("Dynamixel") >= 0):
print banner, "\n";
print "Found a Dynamixel port at :", self.botConnectPort;
break;
except serial.SerialException:
# Pyserial must have thrown an exception so the port is fail
print "SERIAL FAILED ON ", self.botConnectPort;
self.botConnectPort = None;
if(self.botConnectPort == None):
# If no ports were found from the scanning return false
return False;
except serial.SerialException, exception:
# Pyserial must have thrown an exception so return False
return False;
# Assume we're good, return True
return True;
def closeBotConnection(self):
# Close the serial port connection to the bioloid computer
try:
if(self.serialPort.isOpen()):
# Reset Bot?
self.serialPort.close();
except: # Exception, exception:
return False;
return True;
def writeToBot(self, cmd):
# This method sends a raw command to the Bioloid computer (tacks on a \r)
#print "<- ", cmd, "\n";
cmd += "\r";
self.serialPort.write(cmd);
def readFromBot(self, bytes):
# This method reads a certain amount of bytes from the Bioloid computer
r = '';
r = self.serialPort.read(bytes);
#print "-> ", r, "\n";
return r;
def moveServoTo(self, servoID, pulseWidth, time):
# This method moves a particular servo on the chain of Bioloid servos hooked up
# to the Bioloid computer (CM-5) to a given pulseWidth in a given amount of time.
# Note the sequence of raw instructions to move a servo to a pulse width is as follows
#
# cid servoID
# go pulseWidth time
#
# where servoID, pulseWidth, and time are all integers. Be sure to read the response from
# the bioloid computer too in between instructions.
self.writeToBot("cid %d" % (servoID));
self.readFromBot(255);
self.writeToBot("go %d %d" % (pulseWidth, time));
self.readFromBot(255);