dynamixel_controller.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright 2016 ROBOTIS CO., LTD.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *******************************************************************************/
16 
17 /* Authors: Taehun Lim (Darby) */
18 
20 
21 using namespace dynamixel;
22 
23 double mapd(double x, double in_min, double in_max, double out_min, double out_max)
24 {
25  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
26 }
27 
29  :node_handle_(""),
30  priv_node_handle_("~")
31 {
32  robot_name_ = priv_node_handle_.param<std::string>("robot_name", "open_manipulator");
33 
34  std::string device_name = priv_node_handle_.param<std::string>("device_name", "/dev/ttyUSB0");
35  uint32_t dxl_baud_rate = priv_node_handle_.param<int>("baud_rate", 1000000);
36  protocol_version_ = priv_node_handle_.param<float>("protocol_version", 2.0);
37 
38  joint_mode_ = priv_node_handle_.param<std::string>("joint_controller", "position_mode");
39 
40  joint_id_.push_back(priv_node_handle_.param<int>("joint1_id", 1));
41  joint_id_.push_back(priv_node_handle_.param<int>("joint2_id", 2));
42  joint_id_.push_back(priv_node_handle_.param<int>("joint3_id", 3));
43  joint_id_.push_back(priv_node_handle_.param<int>("joint4_id", 4));
44 
45  gripper_mode_ = priv_node_handle_.param<std::string>("gripper_controller", "current_mode");
46 
47  gripper_id_.push_back(priv_node_handle_.param<int>("gripper_id", 5));
48 
51 
52  joint_controller_->begin(device_name.c_str(), dxl_baud_rate);
53  gripper_controller_->begin(device_name.c_str(), dxl_baud_rate);
54 
56 
57  initPublisher();
59 
60  ROS_INFO("open_manipulator_dynamixel_controller : Init OK!");
61 }
62 
64 {
65  for (uint8_t num = 0; num < JOINT_NUM; num++)
66  joint_controller_->itemWrite(joint_id_.at(num), "Torque_Enable", false);
67 
68  gripper_controller_->itemWrite(gripper_id_.at(0), "Torque_Enable", false);
69 
70  ros::shutdown();
71 }
72 
74 {
75  joint_states_pub_ = node_handle_.advertise<sensor_msgs::JointState>(robot_name_ + "/joint_states", 10);
76 }
77 
79 {
82 }
83 
85 {
86  uint16_t get_model_number;
87  for (uint8_t index = 0; index < JOINT_NUM; index++)
88  {
89 
90  if (joint_controller_->ping(joint_id_.at(index), &get_model_number) != true)
91  {
92  ROS_ERROR("Not found Joints, Please check id and baud rate");
93 
94  ros::shutdown();
95  return;
96  }
97  }
98 
99  if (gripper_controller_->ping(gripper_id_.at(0), &get_model_number) != true)
100  {
101  ROS_ERROR("Not found Grippers, Please check id and baud rate");
102 
103  ros::shutdown();
104  return;
105  }
106 
108  setSyncFunction();
109 }
110 
112 {
113  if (joint_mode_ == "position_mode")
114  {
115  for (uint8_t num = 0; num < JOINT_NUM; num++)
117  }
118  else if (joint_mode_ == "current_mode")
119  {
120  for (uint8_t num = 0; num < JOINT_NUM; num++)
122  }
123  else
124  {
125  for (uint8_t num = 0; num < JOINT_NUM; num++)
127  }
128 
129  if (gripper_mode_ == "position_mode")
131  else if (gripper_mode_ == "current_mode" && protocol_version_ == 2.0)
133  else
135 }
136 
138 {
139  joint_controller_->addSyncWrite("Goal_Position");
140 
141  if (protocol_version_ == 2.0)
142  {
143  joint_controller_->addSyncRead("Present_Position");
144  joint_controller_->addSyncRead("Present_Velocity");
145  }
146 }
147 
149 {
150  int32_t* get_joint_present_position = NULL;
151 
152  if (protocol_version_ == 2.0)
153  get_joint_present_position = joint_controller_->syncRead("Present_Position");
154  else if (protocol_version_ == 1.0)
155  {
156  for (int index = 0; index < JOINT_NUM; index++)
157  get_joint_present_position[index] = joint_controller_->itemRead(joint_id_.at(index), "Present_Position");
158  }
159 
160  int32_t get_gripper_present_position = gripper_controller_->itemRead(gripper_id_.at(0), "Present_Position");
161  int32_t present_position[DXL_NUM] = {0, };
162 
163  for (int index = 0; index < JOINT_NUM; index++)
164  present_position[index] = get_joint_present_position[index];
165 
166  present_position[DXL_NUM-1] = get_gripper_present_position;
167 
168  for (int index = 0; index < JOINT_NUM; index++)
169  {
170  value[index] = joint_controller_->convertValue2Radian(joint_id_.at(index), present_position[index]);
171  }
172 
173  value[DXL_NUM-1] = gripper_controller_->convertValue2Radian(gripper_id_.at(0), present_position[DXL_NUM-1]);
174 }
175 
177 {
178  int32_t* get_joint_present_velocity = NULL;
179 
180  if (protocol_version_ == 2.0)
181  get_joint_present_velocity = joint_controller_->syncRead("Present_Velocity");
182  else if (protocol_version_ == 1.0)
183  {
184  for (int index = 0; index < JOINT_NUM; index++)
185  get_joint_present_velocity[index] = joint_controller_->itemRead(joint_id_.at(index), "Present_Velocity");
186  }
187 
188  int32_t get_gripper_present_velocity = gripper_controller_->itemRead(gripper_id_.at(0), "Present_Velocity");
189  int32_t present_velocity[DXL_NUM] = {0, };
190 
191  for (int index = 0; index < JOINT_NUM; index++)
192  present_velocity[index] = get_joint_present_velocity[index];
193 
194  present_velocity[DXL_NUM-1] = get_gripper_present_velocity;
195 
196  for (int index = 0; index < JOINT_NUM; index++)
197  {
198  value[index] = joint_controller_->convertValue2Velocity(joint_id_.at(index), present_velocity[index]);
199  }
200 
201  value[DXL_NUM-1] = gripper_controller_->convertValue2Velocity(gripper_id_.at(0), present_velocity[DXL_NUM-1]);
202 }
203 
205 {
206  sensor_msgs::JointState joint_state;
207 
208  float joint_states_pos[JOINT_NUM + PALM_NUM] = {0.0, };
209  float joint_states_vel[JOINT_NUM + PALM_NUM] = {0.0, };
210  float joint_states_eff[JOINT_NUM + PALM_NUM] = {0.0, };
211 
212  double get_joint_position[JOINT_NUM + GRIPPER_NUM] = {0.0, };
213  double get_joint_velocity[JOINT_NUM + GRIPPER_NUM] = {0.0, };
214 
215  readPosition(get_joint_position);
216  readVelocity(get_joint_velocity);
217 
218  joint_state.header.frame_id = "world";
219  joint_state.header.stamp = ros::Time::now();
220 
221  joint_state.name.push_back("joint1");
222  joint_state.name.push_back("joint2");
223  joint_state.name.push_back("joint3");
224  joint_state.name.push_back("joint4");
225  joint_state.name.push_back("grip_joint");
226  joint_state.name.push_back("grip_joint_sub");
227 
228  joint_states_pos[0] = get_joint_position[0];
229  joint_states_pos[1] = get_joint_position[1];
230  joint_states_pos[2] = get_joint_position[2];
231  joint_states_pos[3] = get_joint_position[3];
232  joint_states_pos[4] = mapd(get_joint_position[4], 0.90, -0.80, -0.01, 0.01);
233  joint_states_pos[5] = joint_states_pos[4];
234 
235  joint_states_vel[0] = get_joint_velocity[0];
236  joint_states_vel[1] = get_joint_velocity[1];
237  joint_states_vel[2] = get_joint_velocity[2];
238  joint_states_vel[3] = get_joint_velocity[3];
239  joint_states_vel[4] = get_joint_velocity[4];
240  joint_states_vel[5] = joint_states_vel[4];
241 
242  for (int index = 0; index < JOINT_NUM + PALM_NUM; index++)
243  {
244  joint_state.position.push_back(joint_states_pos[index]);
245  joint_state.velocity.push_back(joint_states_vel[index]);
246  joint_state.effort.push_back(joint_states_eff[index]);
247  }
248 
249  joint_states_pub_.publish(joint_state);
250 }
251 
252 void DynamixelController::goalJointPositionCallback(const sensor_msgs::JointState::ConstPtr &msg)
253 {
254  double goal_joint_position[JOINT_NUM] = {0.0, 0.0, 0.0, 0.0};
255 
256  for (int index = 0; index < JOINT_NUM; index++)
257  goal_joint_position[index] = msg->position.at(index);
258 
259  int32_t goal_position[JOINT_NUM] = {0, };
260 
261  for (int index = 0; index < JOINT_NUM; index++)
262  {
263  goal_position[index] = joint_controller_->convertRadian2Value(joint_id_.at(index), goal_joint_position[index]);
264  }
265 
266  joint_controller_->syncWrite("Goal_Position", goal_position);
267 }
268 
269 void DynamixelController::goalGripperPositionCallback(const sensor_msgs::JointState::ConstPtr &msg)
270 {
271  double goal_gripper_position = msg->position[0];
272  goal_gripper_position = mapd(goal_gripper_position, -0.01, 0.01, 0.90, -0.80);
273 
274  gripper_controller_->itemWrite(gripper_id_.at(0), "Goal_Position", gripper_controller_->convertRadian2Value(gripper_id_.at(0), goal_gripper_position));
275 }
276 
278 {
279  // Read & Publish Dynamixel position
281 }
282 
283 int main(int argc, char **argv)
284 {
285  // Init ROS node
286  ros::init(argc, argv, "open_manipulator_dynamixel_controller");
287  DynamixelController dynamixel_controller;
288  ros::Rate loop_rate(ITERATION_FREQUENCY);
289 
290  while (ros::ok())
291  {
292  dynamixel_controller.control_loop();
293  ros::spinOnce();
294  loop_rate.sleep();
295  }
296 
297  return 0;
298 }
bool ping(uint8_t id, uint16_t *get_model_number=0)
#define DXL_NUM
double mapd(double x, double in_min, double in_max, double out_min, double out_max)
bool currentMode(uint8_t id, uint8_t cur=50)
bool syncWrite(const char *item_name, int32_t *value)
void publish(const boost::shared_ptr< M > &message) const
std::vector< uint8_t > joint_id_
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
#define JOINT_NUM
#define GRIPPER_NUM
void addSyncRead(const char *item_name)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
int32_t convertRadian2Value(uint8_t id, float radian)
int32_t itemRead(uint8_t id, const char *item_name)
#define ITERATION_FREQUENCY
bool itemWrite(uint8_t id, const char *item_name, int32_t value)
void goalGripperPositionCallback(const sensor_msgs::JointState::ConstPtr &msg)
bool begin(const char *device_name="/dev/ttyUSB0", uint32_t baud_rate=57600)
int main(int argc, char **argv)
#define ROS_INFO(...)
bool param(const std::string &param_name, T &param_val, const T &default_val) const
float convertValue2Radian(uint8_t id, int32_t value)
ROSCPP_DECL bool ok()
bool jointMode(uint8_t id, uint16_t vel=0, uint16_t acc=0)
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
DynamixelWorkbench * joint_controller_
#define PALM_NUM
bool sleep()
int32_t * syncRead(const char *item_name)
std::vector< uint8_t > gripper_id_
static Time now()
ROSCPP_DECL void shutdown()
DynamixelWorkbench * gripper_controller_
void goalJointPositionCallback(const sensor_msgs::JointState::ConstPtr &msg)
ROSCPP_DECL void spinOnce()
float convertValue2Velocity(uint8_t id, int32_t value)
void addSyncWrite(const char *item_name)
#define ROS_ERROR(...)


open_manipulator_dynamixel_ctrl
Author(s): Darby Lim
autogenerated on Sat Jun 2 2018 02:43:30