This repository has been archived by the owner on Jan 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ax12.py
executable file
·289 lines (258 loc) · 9.4 KB
/
ax12.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
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
from time import sleep
from serial import Serial
import RPi.GPIO as GPIO
import sys
import time
class Ax12:
'''
Ax12 Servo's class
This class allows you to control the settings and move the Ax12 servo's
Written for the Raspberry Pi 3 and for python3 (Python2 does not work)
This class is not thread safe, and should only be used from the main thread
'''
PORT = "/dev/ttyS0"
BAUDRATE = 1000000
TIME_OUT= 0.01
AX_GOAL_LENGTH = 5
AX_POS_LENGTH = 4
#AX_VOLT_LENGTH = 4
AX_AL_LENGTH = 7
AX_PRESENT_VOLTAGE = 42
AX_BYTE_READ = 1
AX_READ_DATA = 2
AX_WRITE_DATA = 3
AX_GOAL_POSITION_L = 30
AX_PRESENT_POSITION_L = 38
AX_CW_ANGLE_LIMIT_L = 6
AX_START = 255
AX_REG_WRITE = 4
AX_INT_READ = 2
TX_DELAY_TIME =0.0002
#raspberry pi constants
RPI_DIRECTION_PIN = 18
RPI_DIRECTION_TX = GPIO.HIGH
RPI_DIRECTION_RX = GPIO.LOW
RPI_DIRECTION_SWITCH_DELAY = 0.0001
#static variables
port = None
gpioSet = False
class timeoutError(Exception) : pass
def __init__(self):
if(Ax12.port == None):
Ax12.port = Serial( port=Ax12.PORT,baudrate=Ax12.BAUDRATE,timeout=Ax12.TIME_OUT)
# Ax12.port.flush()
if(not Ax12.gpioSet):
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(Ax12.RPI_DIRECTION_PIN, GPIO.OUT)
Ax12.gpioSet = True
Ax12.port.flush()
self.direction(Ax12.RPI_DIRECTION_RX)
def direction(self,d):
'''
Sets the pin and sleeps
@param d, the pin you wish to set
'''
GPIO.output(Ax12.RPI_DIRECTION_PIN,d)
sleep(Ax12.RPI_DIRECTION_SWITCH_DELAY)
def readData(self,d):
'''
Reads the data
@param d, reserved parameter
@return a number
@except time out exception
'''
self.direction(Ax12.RPI_DIRECTION_RX)
bytesToRead = 5 #Ax12.port.inWaiting()
reply = Ax12.port.read(bytesToRead)
try:
assert reply[0] == 0xFF
except:
e = "servo : " + str(d) + " timed out"
raise Ax12.timeoutError(e)
try:
length = reply[3] - 2
error = reply[4]
if(error != 0):
return -error
elif(length == 0):
return error
else:
if(length > 1):
reply = Ax12.port.read(2)
return (reply[1] << 8) + (reply[0] << 0)
else:
reply = Ax12.port.read(1)
return reply[0]
finally:
Ax12.port.flushInput()
time.sleep(0.002)
def setAngleLimit(self, id, cwLimit, ccwLimit):
'''
Set's the limit of an angle
@param id the id of the servo
@param CWLimit the new limit of clockwise
@param CCWlimit the new counter clockwise
@return bool did it succeed?
@except time out exception
'''
outData = bytearray([
Ax12.AX_START,Ax12.AX_START, id,
Ax12.AX_AL_LENGTH, Ax12.AX_WRITE_DATA,
Ax12.AX_CW_ANGLE_LIMIT_L, cwLimit&0xff, cwLimit >> 8, ccwLimit&0xff, ccwLimit>>8])
checksum = ~sum(outData[2:])&0xff
outData.append(checksum)
self.direction(Ax12.RPI_DIRECTION_TX)
Ax12.port.write(outData)
sleep(Ax12.TX_DELAY_TIME)
return self.readData(id)
'''
pings an servo
@param id The servo id you wish to ping
@return bool did the ping succeed?
@except Time out exception
'''
def ping(self,id):
PING = 1
outData = bytearray([Ax12.AX_START, Ax12.AX_START, id, Ax12.AX_READ_DATA, PING])
checksum = ~sum(outData[2:]) & 0xff
outData.append(checksum)
self.direction(Ax12.RPI_DIRECTION_TX)
Ax12.port.write(outData)
sleep(Ax12.TX_DELAY_TIME)
return self.readData(id)
'''
Checks the servo's and returns a list of available servo's
@param minValue the starting value you wish to learn
@param maxValue the endvalue you wish to learn
@param verbose Do you wish to print the status of servo's?
@return list of id's of available servo's
'''
def learnServos(self,minValue, maxValue, printCallBack=lambda x: x):
servoList = []
for i in range(minValue, maxValue + 1):
try:
temp = self.ping(i)
servoList.append(i)
printCallBack("Found servo #" + str(i))
time.sleep(0.1)
except Exception as detail:
printCallBack("Error pinging servo #" + str(i) + '; ' + str(detail))
pass
return servoList
def readPosition(self, id):
'''
Reads the position of the specified servo
@Param id the id of the servo
@return the position of specified servo
@except Time out exception
'''
outData = bytearray([
Ax12.AX_START, Ax12.AX_START, id,
Ax12.AX_POS_LENGTH, Ax12.AX_READ_DATA, Ax12.AX_PRESENT_POSITION_L,
Ax12.AX_INT_READ])
checksum = ~sum(outData[2:]) & 0xff
outData.append(checksum)
self.direction(Ax12.RPI_DIRECTION_TX)
Ax12.port.write(outData)
sleep(Ax12.TX_DELAY_TIME)
return self.readData(id)
def readTemperature(self,id):
'''
Reads the temperature from the specified servo
@Param id the id of the servo
@return returns the temperature
@except An time out exception
'''
TEMP_LENGTH = 4
PRESENT_TEMPERATURE = 43
outData = bytearray([
Ax12.AX_START,Ax12.AX_START,id, TEMP_LENGTH,Ax12.AX_READ_DATA, PRESENT_TEMPERATURE, Ax12.AX_BYTE_READ])
checksum = ~sum(outData[2:]) & 0xff
outData.append(checksum)
self.direction(Ax12.RPI_DIRECTION_TX)
Ax12.port.write(outData)
sleep(Ax12.TX_DELAY_TIME)
return self.readData(id)
def moveSpeed(self, id, position, speed):
'''
Moves the specified servo with the specified speed
@param id the servo id
@param position The position to move to
@param speed The movement speed
@return succesfull? if the servo was succesfull
@IO exception Timeout exception
'''
Ax12.port.flushInput()
AX_GOAL_SP_LENGTH = 7
AX_GOAL_POSITION_L = 30
outData = bytearray([Ax12.AX_START, Ax12.AX_START, id,
AX_GOAL_SP_LENGTH, Ax12.AX_WRITE_DATA,
AX_GOAL_POSITION_L, position&0xff,
position>>8, speed&0xff,speed>>8])
checksum = ~sum(outData[2:])&0xff
outData.append(checksum)
self.direction(Ax12.RPI_DIRECTION_TX)
Ax12.port.write(outData)
sleep(Ax12.TX_DELAY_TIME)
return self.readData(id)
def setShutdownAlarm(self,id,alarm):
Ax12.port.flushInput()
SHUTDOWN_ALARM_LENGTH = 4
SHUTDOWN_ALARM = 18
outData = bytearray([
Ax12.AX_START, Ax12.AX_START, id,
SHUTDOWN_ALARM_LENGTH, Ax12.AX_WRITE_DATA,
SHUTDOWN_ALARM, alarm
])
checksum = ~sum(outData[2:])&0xff
outData.append(checksum)
self.direction(Ax12.RPI_DIRECTION_TX)
Ax12.port.write(outData)
sleep(Ax12.TX_DELAY_TIME)
return self.readData(id)
def readVoltage(self, id):
'''
Reads the voltage of the specified servo
@param id The servo
@return voltage The voltage it returns
@IO exception An time out exception when a time-out occurs
'''
Ax12.port.flushInput()
VOLT_LENGTH = 4
outData = bytearray([
Ax12.AX_START, Ax12.AX_START, id,
VOLT_LENGTH, Ax12.AX_READ_DATA,
Ax12.AX_PRESENT_VOLTAGE, Ax12.AX_BYTE_READ])
checksum = ~sum(outData[2:]) & 0xff
outData.append(checksum)
self.direction(Ax12.RPI_DIRECTION_TX)
Ax12.port.write(outData)
sleep(Ax12.TX_DELAY_TIME)
return self.readData(id) / 10
def shutdown(self):
'''
Closes the serial port
And cleans up the mess
This should be called when the program exits
'''
Ax12.port.close()
Ax12.gpioSet = False
def move(self, id, position):
'''
Moves the specified servo to the specified position as fast as possible
@param id The servo id
@param position The position to move to
@return Succes? returns if the movement was succesfull
@return IO exception An exception when the read takes to long(time-out)
'''
outData = bytearray([
Ax12.AX_START, Ax12.AX_START, id,
Ax12.AX_GOAL_LENGTH, Ax12.AX_WRITE_DATA,
Ax12.AX_GOAL_POSITION_L, position & 0xff, position >> 8])
checksum = ~sum(outData[2:]) & 0xff
outData.append(checksum)
self.direction(Ax12.RPI_DIRECTION_TX)
Ax12.port.write(outData)
sleep(Ax12.TX_DELAY_TIME)
return self.readData(id)