read_write_node.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 #*******************************************************************************
4 # Copyright 2021 ROBOTIS CO., LTD.
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #*******************************************************************************
18 
19 #*******************************************************************************
20 # This example is written for DYNAMIXEL X(excluding XL-320) and MX(2.0) series with U2D2.
21 # For other series, please refer to the product eManual and modify the Control Table addresses and other definitions.
22 # To test this example, please follow the commands below.
23 #
24 # Open terminal #1
25 # $ roscore
26 #
27 # Open terminal #2
28 # $ rosrun dynamixel_sdk_examples read_write_node.py
29 #
30 # Open terminal #3 (run one of below commands at a time)
31 # $ rostopic pub -1 /set_position dynamixel_sdk_examples/SetPosition "{id: 1, position: 0}"
32 # $ rostopic pub -1 /set_position dynamixel_sdk_examples/SetPosition "{id: 1, position: 1000}"
33 # $ rosservice call /get_position "id: 1"
34 #
35 # Author: Will Son
36 #******************************************************************************/
37 
38 import os
39 import rospy
40 from dynamixel_sdk import *
41 from dynamixel_sdk_examples.srv import *
42 from dynamixel_sdk_examples.msg import *
43 
44 if os.name == 'nt':
45  import msvcrt
46  def getch():
47  return msvcrt.getch().decode()
48 else:
49  import sys, tty, termios
50  fd = sys.stdin.fileno()
51  old_settings = termios.tcgetattr(fd)
52  def getch():
53  try:
54  tty.setraw(sys.stdin.fileno())
55  ch = sys.stdin.read(1)
56  finally:
57  termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
58  return ch
59 
60 # Control table address
61 ADDR_TORQUE_ENABLE = 64 # Control table address is different in Dynamixel model
62 ADDR_GOAL_POSITION = 116
63 ADDR_PRESENT_POSITION = 132
64 
65 # Protocol version
66 PROTOCOL_VERSION = 2.0 # See which protocol version is used in the Dynamixel
67 
68 # Default setting
69 DXL_ID = 1 # Dynamixel ID : 1
70 BAUDRATE = 57600 # Dynamixel default baudrate : 57600
71 DEVICENAME = '/dev/ttyUSB0' # Check which port is being used on your controller
72  # ex) Windows: "COM1" Linux: "/dev/ttyUSB0" Mac: "/dev/tty.usbserial-*"
73 
74 TORQUE_ENABLE = 1 # Value for enabling the torque
75 TORQUE_DISABLE = 0 # Value for disabling the torque
76 DXL_MINIMUM_POSITION_VALUE = 0 # Dynamixel will rotate between this value
77 DXL_MAXIMUM_POSITION_VALUE = 1000 # and this value (note that the Dynamixel would not move when the position value is out of movable range. Check e-manual about the range of the Dynamixel you use.)
78 DXL_MOVING_STATUS_THRESHOLD = 20 # Dynamixel moving status threshold
79 
80 portHandler = PortHandler(DEVICENAME)
81 packetHandler = PacketHandler(PROTOCOL_VERSION)
82 
84  print("Set Goal Position of ID %s = %s" % (data.id, data.position))
85  dxl_comm_result, dxl_error = packetHandler.write4ByteTxRx(portHandler, data.id, ADDR_GOAL_POSITION, data.position)
86 
87 def get_present_pos(req):
88  dxl_present_position, dxl_comm_result, dxl_error = packetHandler.read4ByteTxRx(portHandler, req.id, ADDR_PRESENT_POSITION)
89  print("Present Position of ID %s = %s" % (req.id, dxl_present_position))
90  return dxl_present_position
91 
93  rospy.init_node('read_write_py_node')
94  rospy.Subscriber('set_position', SetPosition, set_goal_pos_callback)
95  rospy.Service('get_position', GetPosition, get_present_pos)
96  rospy.spin()
97 
98 def main():
99  # Open port
100  try:
101  portHandler.openPort()
102  print("Succeeded to open the port")
103  except:
104  print("Failed to open the port")
105  print("Press any key to terminate...")
106  getch()
107  quit()
108 
109  # Set port baudrate
110  try:
111  portHandler.setBaudRate(BAUDRATE)
112  print("Succeeded to change the baudrate")
113  except:
114  print("Failed to change the baudrate")
115  print("Press any key to terminate...")
116  getch()
117  quit()
118 
119  # Enable Dynamixel Torque
120  dxl_comm_result, dxl_error = packetHandler.write1ByteTxRx(portHandler, DXL_ID, ADDR_TORQUE_ENABLE, TORQUE_ENABLE)
121  if dxl_comm_result != COMM_SUCCESS:
122  print("%s" % packetHandler.getTxRxResult(dxl_comm_result))
123  print("Press any key to terminate...")
124  getch()
125  quit()
126  elif dxl_error != 0:
127  print("%s" % packetHandler.getRxPacketError(dxl_error))
128  print("Press any key to terminate...")
129  getch()
130  quit()
131  else:
132  print("DYNAMIXEL has been successfully connected")
133 
134  print("Ready to get & set Position.")
135 
137 
138 
139 if __name__ == '__main__':
140  main()
def set_goal_pos_callback(data)
def get_present_pos(req)


dynamixel_sdk_examples
Author(s): zerom
autogenerated on Fri Apr 16 2021 02:25:58