read_write_node.cpp
Go to the documentation of this file.
1 // Copyright 2020 ROBOTIS CO., LTD.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 /*******************************************************************************
16  * This example is written for DYNAMIXEL X(excluding XL-320) and MX(2.0) series with U2D2.
17  * For other series, please refer to the product eManual and modify the Control Table addresses and other definitions.
18  * To test this example, please follow the commands below.
19  *
20  * Open terminal #1
21  * $ roscore
22  *
23  * Open terminal #2
24  * $ rosrun dynamixel_sdk_examples read_write_node
25  *
26  * Open terminal #3 (run one of below commands at a time)
27  * $ rostopic pub -1 /set_position dynamixel_sdk_examples/SetPosition "{id: 1, position: 0}"
28  * $ rostopic pub -1 /set_position dynamixel_sdk_examples/SetPosition "{id: 1, position: 1000}"
29  * $ rosservice call /get_position "id: 1"
30  * $ rostopic pub -1 /set_position dynamixel_sdk_examples/SetPosition "{id: 2, position: 0}"
31  * $ rostopic pub -1 /set_position dynamixel_sdk_examples/SetPosition "{id: 2, position: 1000}"
32  * $ rosservice call /get_position "id: 2"
33  *
34  * Author: Zerom
35 *******************************************************************************/
36 
37 #include <ros/ros.h>
38 
39 #include "std_msgs/String.h"
40 #include "dynamixel_sdk_examples/GetPosition.h"
41 #include "dynamixel_sdk_examples/SetPosition.h"
43 
44 using namespace dynamixel;
45 
46 // Control table address
47 #define ADDR_TORQUE_ENABLE 64
48 #define ADDR_GOAL_POSITION 116
49 #define ADDR_PRESENT_POSITION 132
50 
51 // Protocol version
52 #define PROTOCOL_VERSION 2.0 // Default Protocol version of DYNAMIXEL X series.
53 
54 // Default setting
55 #define DXL1_ID 1 // DXL1 ID
56 #define DXL2_ID 2 // DXL2 ID
57 #define BAUDRATE 57600 // Default Baudrate of DYNAMIXEL X series
58 #define DEVICE_NAME "/dev/ttyUSB0" // [Linux] To find assigned port, use "$ ls /dev/ttyUSB*" command
59 
62 
64  dynamixel_sdk_examples::GetPosition::Request & req,
65  dynamixel_sdk_examples::GetPosition::Response & res)
66 {
67  uint8_t dxl_error = 0;
68  int dxl_comm_result = COMM_TX_FAIL;
69 
70  // Position Value of X series is 4 byte data. For AX & MX(1.0) use 2 byte data(int16_t) for the Position Value.
71  int32_t position = 0;
72 
73  // Read Present Position (length : 4 bytes) and Convert uint32 -> int32
74  // When reading 2 byte data from AX / MX(1.0), use read2ByteTxRx() instead.
75  dxl_comm_result = packetHandler->read4ByteTxRx(
76  portHandler, (uint8_t)req.id, ADDR_PRESENT_POSITION, (uint32_t *)&position, &dxl_error);
77  if (dxl_comm_result == COMM_SUCCESS) {
78  ROS_INFO("getPosition : [ID:%d] -> [POSITION:%d]", req.id, position);
79  res.position = position;
80  return true;
81  } else {
82  ROS_INFO("Failed to get position! Result: %d", dxl_comm_result);
83  return false;
84  }
85 }
86 
87 void setPositionCallback(const dynamixel_sdk_examples::SetPosition::ConstPtr & msg)
88 {
89  uint8_t dxl_error = 0;
90  int dxl_comm_result = COMM_TX_FAIL;
91 
92  // Position Value of X series is 4 byte data. For AX & MX(1.0) use 2 byte data(uint16_t) for the Position Value.
93  uint32_t position = (unsigned int)msg->position; // Convert int32 -> uint32
94 
95  // Write Goal Position (length : 4 bytes)
96  // When writing 2 byte data to AX / MX(1.0), use write2ByteTxRx() instead.
97  dxl_comm_result = packetHandler->write4ByteTxRx(
98  portHandler, (uint8_t)msg->id, ADDR_GOAL_POSITION, position, &dxl_error);
99  if (dxl_comm_result == COMM_SUCCESS) {
100  ROS_INFO("setPosition : [ID:%d] [POSITION:%d]", msg->id, msg->position);
101  } else {
102  ROS_ERROR("Failed to set position! Result: %d", dxl_comm_result);
103  }
104 }
105 
106 int main(int argc, char ** argv)
107 {
108  uint8_t dxl_error = 0;
109  int dxl_comm_result = COMM_TX_FAIL;
110 
113 
114  if (!portHandler->openPort()) {
115  ROS_ERROR("Failed to open the port!");
116  return -1;
117  }
118 
119  if (!portHandler->setBaudRate(BAUDRATE)) {
120  ROS_ERROR("Failed to set the baudrate!");
121  return -1;
122  }
123 
124  dxl_comm_result = packetHandler->write1ByteTxRx(
125  portHandler, DXL1_ID, ADDR_TORQUE_ENABLE, 1, &dxl_error);
126  if (dxl_comm_result != COMM_SUCCESS) {
127  ROS_ERROR("Failed to enable torque for Dynamixel ID %d", DXL1_ID);
128  return -1;
129  }
130 
131  dxl_comm_result = packetHandler->write1ByteTxRx(
132  portHandler, DXL2_ID, ADDR_TORQUE_ENABLE, 1, &dxl_error);
133  if (dxl_comm_result != COMM_SUCCESS) {
134  ROS_ERROR("Failed to enable torque for Dynamixel ID %d", DXL2_ID);
135  return -1;
136  }
137 
138  ros::init(argc, argv, "read_write_node");
139  ros::NodeHandle nh;
140  ros::ServiceServer get_position_srv = nh.advertiseService("/get_position", getPresentPositionCallback);
141  ros::Subscriber set_position_sub = nh.subscribe("/set_position", 10, setPositionCallback);
142  ros::spin();
143 
144  portHandler->closePort();
145  return 0;
146 }
static PortHandler * getPortHandler(const char *port_name)
#define ADDR_GOAL_POSITION
virtual bool openPort()=0
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
PacketHandler * packetHandler
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
ServiceServer advertiseService(const std::string &service, bool(T::*srv_func)(MReq &, MRes &), T *obj)
#define ADDR_TORQUE_ENABLE
int COMM_SUCCESS
PortHandler * portHandler
virtual void closePort()=0
#define DXL2_ID
#define PROTOCOL_VERSION
int main(int argc, char **argv)
bool getPresentPositionCallback(dynamixel_sdk_examples::GetPosition::Request &req, dynamixel_sdk_examples::GetPosition::Response &res)
#define ROS_INFO(...)
virtual int write1ByteTxRx(PortHandler *port, uint8_t id, uint16_t address, uint8_t data, uint8_t *error=0)=0
ROSCPP_DECL void spin()
static PacketHandler * getPacketHandler(float protocol_version=2.0)
#define DEVICE_NAME
#define ADDR_PRESENT_POSITION
#define BAUDRATE
void setPositionCallback(const dynamixel_sdk_examples::SetPosition::ConstPtr &msg)
virtual int write4ByteTxRx(PortHandler *port, uint8_t id, uint16_t address, uint32_t data, uint8_t *error=0)=0
virtual int read4ByteTxRx(PortHandler *port, uint8_t id, uint16_t address, uint32_t *data, uint8_t *error=0)=0
virtual bool setBaudRate(const int baudrate)=0
#define DXL1_ID
#define ROS_ERROR(...)
int COMM_TX_FAIL


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