-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathax.py
More file actions
executable file
·426 lines (335 loc) · 15.7 KB
/
ax.py
File metadata and controls
executable file
·426 lines (335 loc) · 15.7 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
'''
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more detailport.
You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/
Use it at your own risk. Assume that I have no idea what I am doing.
Low level script to control AX 12 servos from Robotiport. Based on original script
by Michael Ferguson. Found here:
http://forumport.trossenroboticport.com/tutorials/how-to-diy-128/controlling-ax-12-servos-3275/
The scipt was slightly modified to fix errors and
ported to allow running on Raspberry pi.
The script assumes that it is connected to the servo through a buffer
such as a 74HC125. The communication direction is controlled through
a GPIO pin.
If a Raspberry Pi is being used then the startup config needs to be modified to
set the UART crystal to allow 1Mbps transfer. The TTY attached to the com
port needs to be disabled as well. Make sure to set the baud rate of
the TTY to 1Mbps
-Jesse Merritt
'''
import serial # we need to import the pySerial stuff to use
import time # time module for sleeps and such
import RPi.GPIO as GPIO # GPIO control module
import axReg
registers = axReg.AXRegisters
# important AX-12 constants
AX_SYNC_WRITE = 0x83
AX_RESET = 6
AX_ACTION = 5
AX_REG_WRITE = 4
AX_WRITE_DATA = 3
AX_READ_DATA = 2 # Corrected to 02. Was 04
AX_PING = 1
BROADCASTID = 0xFE
port = serial.Serial() # create a serial port object
port.baudrate = 1000000 # baud rate, in bits/second
port.port = "/dev/ttyAMA0" # this is whatever port your are using
port.timeout = 0.5
port.open()
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM) # Set GPIO mode
tx = GPIO.LOW # Low for TX mode
rx = GPIO.HIGH # High for RX mode
directionPin = 18 # GPIO pin connected to Enable pins on buffer
GPIO.setup(directionPin, GPIO.OUT) # Configure pin for output
disableDirPin = False
connectedServos = []
# Error lookup dictionary for bit masking
dictErrors = { 1 : "Input Voltage",
2 : "Angle Limit",
4 : "Overheating",
8 : "Range",
16 : "Checksum",
32 : "Overload",
64 : "Instruction"
}
# Custom error class to report AX servo errors
class axError(Exception) : pass
# Unspecified Error
class generalError(Exception) : pass
# Servo timeout
class timeoutError(Exception) : pass
def direction(d):
''' Set direction of data. Either rx or tx'''
if not disableDirPin :
GPIO.output(directionPin, d) # set the pin
#time.sleep(0.000050) # sleep for 50uS to allow things to settle. Decreases checksum errors
time.sleep(0.0005)
def ping(index) :
direction(tx) # Set to TX mode
port.flushInput() # flush any garbage in the buffer
checksum = 255 - ((index + AX_PING + 2)%256) # calculate the checksum
outData = chr(0xFF)+chr(0xFF)+chr(index)+chr(0x02)+chr(AX_PING)+chr(checksum) # build a string with the first part
port.write(outData) # write it out of the serial port
getStatus(index)
def getStatus(index) :
vals = list() # build empty list
direction(rx) # configure for RX mode
try :
h1 = port.read() # 0xff, used to make sure the servo responds
assert ord(h1) == 0xFF # Make sure the header byte is right
except :
e = "Timeout on servo " + str(index)
raise timeoutError(e) # raise a timeout error
try :
h2 = port.read() # 0xff, not used for anything
origin = port.read() # Origin ID
length = ord(port.read()) - 1 # Length of data payload
error = ord(port.read()) # read the error data, was being tossed
while length > 0: # read the data payload in a while loop
vals.append(ord(port.read())) # append the payload to the list
length = length - 1 # decrement the counter
if error != 0 : # If the error is not 0 then bail with an error code
e = "Error from servo: " + dictErrors[error] + ' (code ' + hex(error) + ')' # build an error string with the AX error code
raise axError(e) # raise the error
#if rlength == 1: # If it's just a single byte then return it
# return vals[0]
return vals # It's more than one byte so return the list
except Exception, detail:
raise axError(detail)
def reset(index) :
'''
Reset servo to factory default settings.
THIS WILL DESTROY ALL SETTINGS ON THE SERVO!!!!
'''
direction(tx) # set the direction to be transmit
length = 2 # configure length
checksum = 255-((index+length+AX_RESET)%256) # calculate checksum, same as ~(sum(data))
port.write(chr(0xFF)+chr(0xFF)+chr(index)+chr(length)+chr(AX_RESET)) # Write the first part of the protocol
port.write(chr(checksum)) # write the checksum
direction(rx) # Switch back to RX mode
def sync_write(indexes, reg, values):
'''
Synchronized write for multiple servos
'''
direction(tx)
length = (len(values[0]) + 1) * len(indexes) + 4
indexSum = sum(indexes)
valuesSum = 0
for i in range(0, len(values)) :
valuesSum += sum(values[i])
checksum = 255-((indexSum+length+AX_SYNC_WRITE+reg+valuesSum)%256) # calculate checksum, same as ~(sum(data))
port.write((chr(0xFF)+chr(0xFF)+chr(BROADCASTID)+chr(length)+chr(AX_REG_WRITE)+chr(reg) + chr(len(values[0]))))
for i in range(0, len(indexes)):
port.write(chr(indexes[i]))
for val in values[i] :
port.write(chr(val))
port.write(chr(checksum))
def reg_write(index, reg, values) :
''' Set register values but don't act on them'''
direction(tx) # set the direction to be transmit
length = 3 + len(values) # configure length
checksum = 255-((index+length+AX_REG_WRITE+reg+sum(values))%256) # calculate checksum, same as ~(sum(data))
port.write(chr(0xFF)+chr(0xFF)+chr(index)+chr(length)+chr(AX_REG_WRITE)+chr(reg)) # Write the first part of the protocol
for val in values: # actually writes the data payload
port.write(chr(val)) # chr(val) sends the binary data rather than ASCII
port.write(chr(checksum)) # write the checksum
direction(rx) # Switch back to RX mode
def action(index) :
''' Act on set values'''
direction(tx) # set the direction to be transmit
length = 2 # configure length
checksum = 255-((index+length+AX_ACTION)%256) # calculate checksum, same as ~(sum(data))
port.write(chr(0xFF)+chr(0xFF)+chr(index)+chr(length)+chr(AX_ACTION)) # Write the first part of the protocol
port.write(chr(checksum)) # write the checksum
direction(rx) # Switch back to RX mode
def setReg(index, reg,values): # Corrected first arg to 'index'
''' Set register values'''
direction(tx) # set the direction to be transmit
length = 3 + len(values) # configure length
checksum = 255-((index+length+AX_WRITE_DATA+reg+sum(values))%256) # calculate checksum, same as ~(sum(data))
port.write(chr(0xFF)+chr(0xFF)+chr(index)+chr(length)+chr(AX_WRITE_DATA)+chr(reg)) # Write the first part of the protocol
for val in values: # actually writes the data payload
port.write(chr(val)) # chr(val) sends the binary data rather than ASCII
port.write(chr(checksum)) # write the checksum
direction(rx) # Switch back to RX mode
def getReg(index, regstart, rlength):
direction(tx) # Set to TX mode
port.flushInput() # flush any garbage in the buffer
checksum = 255 - ((6 + index + regstart + rlength)%256) # calculate the checksum
outData = chr(0xFF)+chr(0xFF)+chr(index)+chr(0x04)+chr(AX_READ_DATA)+chr(regstart)+chr(rlength)+chr(checksum) # build a string with the first part
port.write(outData) # write it out of the serial port
vals = list() # build empty list
direction(rx) # configure for RX mode
try :
h1 = port.read() # 0xff, used to make sure the servo responds
assert ord(h1) == 0xFF # Make sure the header byte is right
except :
e = "Timeout on servo " + str(index)
raise timeoutError(e) # raise a timeout error
try :
h2 = port.read() # 0xff, not used for anything
origin = port.read() # Origin ID
length = ord(port.read()) - 1 # Length of data payload
error = ord(port.read()) # read the error data, was being tossed
while length > 0: # read the data payload in a while loop
vals.append(ord(port.read())) # append the payload to the list
length = length - 1 # decrement the counter
if error != 0 : # If the error is not 0 then bail with an error code
e = "Error from servo: " + dictErrors[error] + ' (code ' + hex(error) + ')' # build an error string with the AX error code
raise axError(e) # raise the error
if rlength == 1: # If it's just a single byte then return it
return vals[0]
return vals # It's more than one byte so return the list
except Exception, detail:
raise axError(detail)
def getPose2(indexes) :
'''
Steps through the Servos in in the list a gets their positions.
Returns a dictionary of positions with the servo ID's as keys
'''
pose = {} # Empty dictionary to hold the positions
for i in indexes :
raw = getReg(i, 36, 2)[0:2] # read the register on the servo
pose[i] = (raw[0] | (raw[1] << 8)) # add it to the dictionary
return pose
def getPose(indexes) :
'''
Steps through the Servos in in the list a gets their positions.
Returns a list of positions that correspond to the servos
'''
pose = [] # empty list to hold the pose
for i in indexes :
raw = getReg(i, 36, 2)[0:2] # Read the current position
pose.append(raw[0] | (raw[1] << 8)) # Append it to the list
return pose
def groupMove2(servoDict) :
''' Move a group of servos to specified positions. Uses Dictionary'''
for i in servoDict.keys() : # step through the keys
if Arguments.verbose : print "Group Move2", i , "to Pos:", servoDict[i]
reg_write(i, 30, ((servoDict[i]%256, servoDict[i]>>8))) # Write the data to the servo, using reg_write
action(BROADCASTID) # tell all servos to move to the written positions
def groupMove(indexes, positions) :
''' Move a group of servos to specified positions'''
for i in range(0, len(indexes)):
if Arguments.verbose : print "Group Move", indexes[i], "to Pos:", positions[i]
reg_write(indexes[i], 30, ((positions[i]%256, positions[i]>>8)))
action(BROADCASTID) # tell all servos to perform the action
def relax(indexes) :
'''
Turn off toque for list of servos
'''
for i in indexes: # Step through the servos
if Arguments.verbose : print "Relaxing servo", i
setReg(i, 24, [0]) # Turn off torque
def setposition(index, position) :
setReg(index,30,((position%256),(position>>8))) # Moves servo to specified position
def learnServos(minValue=1, maxValue=32, timeout=0.25, verbose=False) :
'''
Step through the possible servos and ping them
Add the found servos to a list and return it
'''
oldTimeout = port.timeout # Save the original timeout
port.timeout = timeout # set timeout to something fast
servoList = [] # Init an empty list
for i in range(minValue, maxValue + 1) : # loop through possible servos
try :
temp = ping(i) # Request a pingt packet
servoList.append(i) # No errors happened so we assume it's a good ID
if verbose: print "Found servo #" + str(i)
time.sleep(.1) # A wee bit of sleep to keep from pounding the bus
except Exception, detail:
if verbose : print "Error pinging servo #" + str(i) + ': ' + str(detail)
pass
port.timeout = oldTimeout # set the timeout to the original
return servoList
def playPose() :
'''
Open a file and move the servos to specified positions in a group move
'''
infile=open(Arguments.playpose, 'r') # Open the file
poseDict = {} # Dictionary to hold poses and positions
if Arguments.verbose : print "Reading pose from", Arguments.playpose
for line in infile.readlines() : # Read the file and step through it
servo = int(line.split(':')[0]) # Servo is first
position = int(line.split(':')[1]) # Position is second
poseDict[servo]=position # add the servo to the Dictionary
groupMove2(poseDict)
def writePose() :
'''
Read the servos and save the positions to a file
'''
of = open(Arguments.savepose, 'w') # open the output file
pose = getPose2(connectedServos) # get the positions
if Arguments.verbose :
print "Servo Positions"
print "---------------"
for key in pose.keys(): # step through the keys, writing to the file
if Arguments.verbose : print "Servo " + str(key), pose[key]
of.write(str(key) + ':' + str(pose[key]) + '\n') # Write to the file
if Arguments.verbose :
print "Wrote pose to " + Arguments.savepose
print
of.close() # close the file
def processArgs() :
'''
Process the arguments after parsing
'''
global connectedServos
if Arguments.port : # specify a different serial port
port.port = Arguments.port
if Arguments.nodir : # allow the DIR pin to be disabled
global disableDirPin
disableDirPin = True
if Arguments.baud :
port.baudrate = int(Arguments.baud)
if Arguments.timeout :
port.timeout = int(Arguments.timeout)
if Arguments.learn : # Learn the connected servos
connectedServos = learnServos(int(Arguments.servomin), int(Arguments.servomax), verbose=Arguments.verbose)
if Arguments.servos : # Servos were specified in arguments
connectedServos = map(int, Arguments.servos.split(','))
if Arguments.savepose : # Write the current pose to a file
writePose()
if Arguments.playpose : # Play an old pose from a file
playPose()
if Arguments.relax : # Relax all the servos (for posing)
relax(connectedServos)
if Arguments.center :
positions = []
for i in connectedServos: positions.append(512)
groupMove(connectedServos, positions)
if Arguments.reset :
if Arguments.verbose :
print "Reset servo " + Arguments.reset
reset(int(Arguments.reset))
if Arguments.id :
original, new = Arguments.id.split(',')
setReg(int(original), 0x03, [int(new)])
def parseArgs() :
'''
Parse the command line arguments
'''
parser = argparse.ArgumentParser(description="Rudementary command parser for manipulating AX12 servos")
parser.add_argument('--servomin', default=0, action='store', help='Specify minimum servo')
parser.add_argument('--servomax', default=32, action='store', help='Specify maximum servo')
parser.add_argument('--learn', action='store_true', help='Automatically discover attached servos')
parser.add_argument('--verbose', action='store_true', help='Show output in verbose mode')
parser.add_argument('--savepose', action='store', help='Read the servos and save the positions to a file')
parser.add_argument('--servos', action='store', help='Specify particular servos. Lists must be comma seperated with no spaces')
parser.add_argument('--playpose', action='store', help='Move the servos to positions specified in pose')
parser.add_argument('--relax', action='store_true', help='Relax the servos')
parser.add_argument('--center', action='store_true', help='Mover to center position')
parser.add_argument('--nodir', action='store_true', help='Disable use of direction pin')
parser.add_argument('--port', action='store', help="Specify different serial port ie '/dev/ttyACM0")
parser.add_argument('--baud', action='store', help="Specify baud rate for the serial port")
parser.add_argument('--timeout', action='store', help="Specify timeout for the serial port")
parser.add_argument('--reset', action='store', help='Reset servo to factory defaults')
parser.add_argument('--id', action='store', help='Change ID of single servo')
parser.parse_args(namespace=Arguments)
class Arguments(object) : # dummy class for command line arguments
pass
if __name__ == '__main__' : # Running as a standalone, read args and run the commands
import argparse
parseArgs()
processArgs()