33 import serial, time, sys, threading
35 from struct
import unpack, pack
54 def __init__(self, port="/dev/ttyUSB0", baud=115200, timeout=0.1, open_port=True):
55 self.
_mutex = threading._allocate_lock()
56 self.
_ser = serial.Serial()
59 self.
_ser.baudrate = baud
60 self.
_ser.timeout = timeout
71 except serial.SerialException
as e:
79 except serial.SerialException
as e:
90 def getPacket(self, mode, id=-1, leng=-1, error=-1, params = None):
93 except Exception
as e:
121 return self.
getPacket(6, id, leng, ord(d), list())
123 return self.
getPacket(5, id, leng, ord(d), list())
125 params.append(ord(d))
126 if len(params) + 2 == leng:
127 return self.
getPacket(6, id, leng, error, params)
129 return self.
getPacket(5, id, leng, error, params)
131 checksum = id + leng + error + sum(params) + ord(d)
132 if checksum % 256 != 255:
149 def execute(self, index, ins, params, ret=True):
153 self.
_ser.flushInput()
154 except Exception
as e:
156 length = 2 + len(params)
157 checksum = 255 - ((index + length + ins + sum(params))%256)
162 packet.append(length)
182 def read(self, index, start, length):
183 values = self.
execute(index, AX_READ_DATA, [start, length])
198 def write(self, index, start, values):
199 self.
execute(index, AX_WRITE_DATA, [start] + values)
212 length = len(output) + 4
213 lbytes = len(values[0])-1
216 self.
_ser.flushInput()
223 packet.append(length)
224 packet.append(AX_SYNC_WRITE)
230 checksum = 255 - ((254 + length + AX_SYNC_WRITE + start + lbytes + sum(output))%256)
244 return self.
execute(0xFE, AX_SYNC_READ, [start, length] + servos )
254 return self.
write(index, P_BAUD_RATE, [baud, ])
263 return int(self.
read(index, P_RETURN_LEVEL, 1)[0])
275 return self.
write(index, P_RETURN_LEVEL, [value])
283 return self.
write(index, P_TORQUE_ENABLE, [1])
291 return self.
write(index, P_TORQUE_ENABLE, [0])
301 return self.
write(index, P_LED, [value])
311 return self.
write(index, P_GOAL_POSITION_L, [value%256, value>>8])
321 return self.
write(index, P_GOAL_SPEED_L, [value%256, value>>8])
329 values = self.
read(index, P_PRESENT_POSITION_L, 2)
331 return int(values[0]) + (int(values[1])<<8)
341 values = self.
read(index, P_PRESENT_SPEED_L, 2)
343 return int(values[0]) + (int(values[1])<<8)
353 values = self.
read(index, P_GOAL_SPEED_L, 2)
355 return int(values[0]) + (int(values[1])<<8)
366 return int(self.
read(index, P_PRESENT_VOLTAGE, 1)[0])/10.0
377 return int(self.
read(index, P_PRESENT_TEMPERATURE, 1)[0])
388 d = self.
read(index, P_MOVING, 1)[0]
397 self.
write(index, P_CCW_ANGLE_LIMIT_L, [0,0])
408 resolution = (2 ** resolution) - 1
409 self.
write(index, P_CCW_ANGLE_LIMIT_L, [resolution%256,resolution>>8])
430 self.
write(index, P_GOAL_SPEED_L, [speed%256, speed>>8])
434 self.
write(index, P_GOAL_SPEED_L, [speed%256, speed>>8])
465 REG_DIGITAL_OUT0 = 47
481 return sum(val[i] << (i * 8)
for i
in range(leng))
498 if x & (2**(index%8)):
513 if index > 31:
return -1
514 if value == 0
and direction > 0:
516 elif value > 0
and direction > 0:
518 elif value > 0
and direction == 0:
533 if index > 7:
return -1
534 if value != 0
and (value < 500
or value > 2500):
535 print(
"ArbotiX Error: Servo value out of range:", value)
537 self.
write(253, self._SERVO_BASE + 2*index, [value%256, value>>8])