RS30X.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # coding:utf-8
3 
4 """
5 RS30X.py
6 
7 Copyright (c) 2017 Shota Hirama <brast351-github@yahoo.co.jp>
8 Copyright (c) 2017 Daisuke Sato <tiryoh@gmail.com>
9 
10 Permission is hereby granted, free of charge, to any person obtaining a copy
11 of this software and associated documentation files (the "Software"), to deal
12 in the Software without restriction, including without limitation the rights
13 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 copies of the Software, and to permit persons to whom the Software is
15 furnished to do so, subject to the following conditions:
16 
17 The above copyright notice and this permission notice shall be included in all
18 copies or substantial portions of the Software.
19 
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 SOFTWARE.
27 """
28 
29 import serial
30 import struct
31 
32 
33 class RS304MD(object):
34  def __init__(self, port = "/dev/ttyUSB0"):
35  PORT = port
36  BAUDRATE = 115200
37  BYTESIZE = serial.EIGHTBITS
38  PARITY = serial.PARITY_NONE
39  STOPBIT = serial.STOPBITS_ONE
40  TIMEOUT = 1
41  self.ser = serial.Serial(PORT, BAUDRATE, BYTESIZE, PARITY, STOPBIT, TIMEOUT)
42 
43  def __del__(self):
44  self.ser.close()
45 
46  def __bytecreateid(self, id):
47  return [0xFA, 0xAF, id]
48 
49  def __checksum(self, checklist):
50  sum = 0
51  for i in range(2, len(checklist)):
52  sum ^= checklist[i]
53  checklist.append(sum)
54  return checklist
55 
56  def __write(self, servolist):
57  self.ser.write("".join(map(chr, servolist)))
58 
59  def __requestStatus(self, servo_id):
60  a = self.__bytecreateid(servo_id)
61  a.extend([0x09, 0x00, 0x00, 0x01])
62  self.__write(self.__checksum(a))
63 
64  def __flash(self, servo_id):
65  a = self.__bytecreateid(servo_id)
66  a.extend([0x40, 0xFF, 0x00, 0x00])
67  self.__write(self.__checksum(a))
68 
69  def setAngle(self, servo_id, set_angle):
70  angle = max(-150.0, min(150.0, set_angle))
71  angle = int(angle * 10)
72  a = self.__bytecreateid(servo_id)
73  a.extend([0x00, 0x1E, 0x02, 0x01, (angle & 0xFF), (angle & 0xFF00) >> 8])
74  self.__write(self.__checksum(a))
75 
76  def setAngleInTime(self, servo_id, set_angle, set_goal_time):
77  angle = max(-150.0, min(150.0, set_angle))
78  angle = int(angle * 10)
79  goal_time = int(set_goal_time * 100)
80  a = self.__bytecreateid(servo_id)
81  a.extend([0x00, 0x1E, 0x04, 0x01, angle & 0xFF, (angle & 0xFF00) >> 8, goal_time & 0xFF, (goal_time & 0xFF00) >> 8])
82  self.__write(self.__checksum(a))
83 
84  def setTorque(self, servo_id, onoff):
85  a = self.__bytecreateid(servo_id)
86  a.extend([0x00, 0x24, 0x01, 0x01, int(onoff)])
87  self.__write(self.__checksum(a))
88 
89  def setBreak(self, servo_id, onoff):
90  if onoff == 1 : self.setTorque(servo_id, 2)
91  else : self.setTorque(servo_id, onoff)
92 
93  def setServoId(self, servo_id, dest):
94  a = self.__bytecreateid(servo_id)
95  a.extend([0x00, 0x04, 0x01, 0x01, dest])
96  self.__write(self.__checksum(a))
97  self.__flash(servo_id)
98 
99  def setMaxTorque(self, servo_id, max_torque):
100  a = self.__bytecreateid(servo_id)
101  a.extend([0x00, 0x23, 0x01, 0x01, int(max_torque)])
102  self.__write(self.__checksum(a))
103  self.__flash(servo_id)
104 
105  def readAngle(self, servo_id):
106  self.__requestStatus(servo_id)
107  b = self.ser.read(26)[7:9]
108  return struct.unpack("<h", b)[0] / 10.0
109 
110  def readTime(self, servo_id):
111  self.__requestStatus(servo_id)
112  b = self.ser.read(26)[9:11]
113  return struct.unpack("<h", b)[0] * 10
114 
115  def readSpeed(self, servo_id):
116  self.__requestStatus(servo_id)
117  b = self.ser.read(26)[11:13]
118  return struct.unpack("<h", b)[0]
119 
120  def readCurrent(self, servo_id):
121  self.__requestStatus(servo_id)
122  b = self.ser.read(26)[13:15]
123  return struct.unpack("<h", b)[0]
124 
125  def readTemperature(self, servo_id):
126  self.__requestStatus(servo_id)
127  b = self.ser.read(26)[15:17]
128  return struct.unpack("<h", b)[0]
129 
130  def readVoltage(self, servo_id):
131  self.__requestStatus(servo_id)
132  b = self.ser.read(26)[17:19]
133  return struct.unpack("<h", b)[0] * 10
134 
135  def readTorqueStatus(self, servo_id):
136  a = self.__bytecreateid(servo_id)
137  a.extend([0x0F, 0x24, 0x02, 0x00])
138  self.__write(self.__checksum(a))
139  b = self.ser.read(9)[7:9]
140  return struct.unpack("<h", b)[0]
141 
142  def reboot(self, servo_id):
143  a = self.__bytecreateid(servo_id)
144  a.extend([0x20, 0xFF, 0x00, 0x00])
145  self.__write(self.__checksum(a))
146 
def readSpeed(self, servo_id)
Definition: RS30X.py:115
def __flash(self, servo_id)
Definition: RS30X.py:64
def __requestStatus(self, servo_id)
Definition: RS30X.py:59
def setAngleInTime(self, servo_id, set_angle, set_goal_time)
Definition: RS30X.py:76
def reboot(self, servo_id)
Definition: RS30X.py:142
def readCurrent(self, servo_id)
Definition: RS30X.py:120
def readTemperature(self, servo_id)
Definition: RS30X.py:125
def readTorqueStatus(self, servo_id)
Definition: RS30X.py:135
def readVoltage(self, servo_id)
Definition: RS30X.py:130
def __write(self, servolist)
Definition: RS30X.py:56
def __checksum(self, checklist)
Definition: RS30X.py:49
def __init__(self, port="/dev/ttyUSB0")
Definition: RS30X.py:34
def setAngle(self, servo_id, set_angle)
Definition: RS30X.py:69
def setBreak(self, servo_id, onoff)
Definition: RS30X.py:89
def setTorque(self, servo_id, onoff)
Definition: RS30X.py:84
def setServoId(self, servo_id, dest)
Definition: RS30X.py:93
def readAngle(self, servo_id)
Definition: RS30X.py:105
def readTime(self, servo_id)
Definition: RS30X.py:110
def setMaxTorque(self, servo_id, max_torque)
Definition: RS30X.py:99
def __bytecreateid(self, id)
Definition: RS30X.py:46


futaba_serial_servo
Author(s): Daisuke Sato
autogenerated on Mon Jun 10 2019 14:27:10