xarm_ros_client.cpp
Go to the documentation of this file.
1 /* Copyright 2018 UFACTORY Inc. All Rights Reserved.
2  *
3  * Software License Agreement (BSD License)
4  *
5  * Author: Jason Peng <jason@ufactory.cc>
6  ============================================================================*/
7 #include <xarm_ros_client.h>
8 
9 namespace xarm_api{
10 
13 
15 {
16  nh_ = nh;
17  std::string client_ns = nh_.getNamespace() + "/";
18  ros::service::waitForService(client_ns+"motion_ctrl");
19  ros::service::waitForService(client_ns+"set_state");
20  ros::service::waitForService(client_ns+"set_mode");
21  ros::service::waitForService(client_ns+"move_servoj");
22 
23  motion_ctrl_client_ = nh_.serviceClient<xarm_msgs::SetAxis>("motion_ctrl");
24  set_mode_client_ = nh_.serviceClient<xarm_msgs::SetInt16>("set_mode");
25  set_state_client_ = nh_.serviceClient<xarm_msgs::SetInt16>("set_state");
26  set_tcp_offset_client_ = nh_.serviceClient<xarm_msgs::TCPOffset>("set_tcp_offset");
27  set_load_client_ = nh_.serviceClient<xarm_msgs::SetLoad>("set_load");
28  clear_err_client_ = nh_.serviceClient<xarm_msgs::ClearErr>("clear_err");
29  get_err_client_ = nh_.serviceClient<xarm_msgs::GetErr>("get_err");
30  go_home_client_ = nh_.serviceClient<xarm_msgs::Move>("go_home");
31  move_lineb_client_ = nh_.serviceClient<xarm_msgs::Move>("move_lineb");
32  move_line_client_ = nh_.serviceClient<xarm_msgs::Move>("move_line");
33  move_joint_client_ = nh_.serviceClient<xarm_msgs::Move>("move_joint");
34  move_servoj_client_ = nh_.serviceClient<xarm_msgs::Move>("move_servoj",true); // persistent connection for servoj
35  move_servo_cart_client_ = nh_.serviceClient<xarm_msgs::Move>("move_servo_cart",true); // persistent connection for servo_cartesian
36 
37  //xarm gripper:
38  gripper_move_client_ = nh_.serviceClient<xarm_msgs::GripperMove>("gripper_move");
39  gripper_config_client_ = nh_.serviceClient<xarm_msgs::GripperConfig>("gripper_config");
40  gripper_state_client_ = nh_.serviceClient<xarm_msgs::GripperState>("gripper_state");
41 
42  //tool modbus:
43  config_modbus_client_ = nh_.serviceClient<xarm_msgs::ConfigToolModbus>("config_tool_modbus");
44  send_modbus_client_ = nh_.serviceClient<xarm_msgs::SetToolModbus>("set_tool_modbus");
45 
46  // velocity control
47  velo_move_joint_client_ = nh_.serviceClient<xarm_msgs::MoveVelo>("velo_move_joint");
48  velo_move_line_client_ = nh_.serviceClient<xarm_msgs::MoveVelo>("velo_move_line");
49 }
50 
52 {
53  set_axis_srv_.request.id = 8;
54  set_axis_srv_.request.data = en;
56  {
57  ROS_INFO("%s\n", set_axis_srv_.response.message.c_str());
58  return set_axis_srv_.response.ret;
59  }
60  else
61  {
62  ROS_ERROR("Failed to call service motion_ctrl");
63  return 1;
64  }
65 
66 }
67 
68 int XArmROSClient::setState(short state)
69 {
70  set_int16_srv_.request.data = state;
72  {
73  ROS_INFO("%s\n", set_int16_srv_.response.message.c_str());
74  return set_int16_srv_.response.ret;
75  }
76  else
77  {
78  ROS_ERROR("Failed to call service set_state");
79  return 1;
80  }
81 }
82 
83 int XArmROSClient::setMode(short mode)
84 {
85  set_int16_srv_.request.data = mode;
87  {
88  ROS_INFO("%s\n", set_int16_srv_.response.message.c_str());
89  return set_int16_srv_.response.ret;
90  }
91  else
92  {
93  ROS_ERROR("Failed to call service set_mode");
94  return 1;
95  }
96 
97 }
98 
100 {
102  {
103  ROS_INFO("%s\n", clear_err_srv_.response.message.c_str());
104  return clear_err_srv_.response.ret;
105  }
106  else
107  {
108  ROS_ERROR("Failed to call service clear_err");
109  return 1;
110  }
111 
112 }
113 
115 {
117  {
118  ROS_INFO("%s\n", get_err_srv_.response.message.c_str());
119  return get_err_srv_.response.err;
120  }
121  else
122  {
123  ROS_ERROR("Failed to call service get_err");
124  return 1;
125  }
126 
127 }
128 
129 int XArmROSClient::setServoJ(const std::vector<float>& joint_cmd)
130 {
131  servoj_msg_.request.mvvelo = 0;
132  servoj_msg_.request.mvacc = 0;
133  servoj_msg_.request.mvtime = 0;
134  servoj_msg_.request.pose = joint_cmd;
135 
137  {
138  // ROS_INFO("%s\n", servoj_msg_.response.message.c_str());
139  return servoj_msg_.response.ret;
140  }
141  else
142  {
143  ROS_ERROR("Failed to call service move_servoj");
144  return 1;
145  }
146 }
147 
148 int XArmROSClient::setServoCartisian(const std::vector<float>& cart_cmd)
149 {
150  servo_cart_msg_.request.mvvelo = 0;
151  servo_cart_msg_.request.mvacc = 0;
152  servo_cart_msg_.request.mvtime = 0;
153  servo_cart_msg_.request.pose = cart_cmd;
154 
155 
157  {
158  // ROS_INFO("%s\n", servo_cart_msg_.response.message.c_str());
159  return servo_cart_msg_.response.ret;
160  }
161  else
162  {
163  ROS_ERROR("Failed to call service move_servo_cart");
164  return 1;
165  }
166 }
167 
168 int XArmROSClient::setTCPOffset(const std::vector<float>& tcp_offset)
169 {
170  if(tcp_offset.size() != 6)
171  {
172  ROS_ERROR("Set tcp offset service parameter should be 6-element Cartesian offset!");
173  return 1;
174  }
175 
176  offset_srv_.request.x = tcp_offset[0];
177  offset_srv_.request.y = tcp_offset[1];
178  offset_srv_.request.z = tcp_offset[2];
179  offset_srv_.request.roll = tcp_offset[3];
180  offset_srv_.request.pitch = tcp_offset[4];
181  offset_srv_.request.yaw = tcp_offset[5];
182 
184  {
185  return offset_srv_.response.ret;
186  }
187  else
188  {
189  ROS_ERROR("Failed to call service set_tcp_offset");
190  return 1;
191  }
192 
193 }
194 
195 int XArmROSClient::setLoad(float mass, const std::vector<float>& center_of_mass)
196 {
197  set_load_srv_.request.mass = mass;
198  set_load_srv_.request.xc = center_of_mass[0];
199  set_load_srv_.request.yc = center_of_mass[1];
200  set_load_srv_.request.zc = center_of_mass[2];
201 
203  {
204  return set_load_srv_.response.ret;
205  }
206  else
207  {
208  ROS_ERROR("Failed to call service set_load");
209  return 1;
210  }
211 }
212 
213 int XArmROSClient::goHome(float jnt_vel_rad, float jnt_acc_rad)
214 {
215  move_srv_.request.mvvelo = jnt_vel_rad;
216  move_srv_.request.mvacc = jnt_acc_rad;
217  move_srv_.request.mvtime = 0;
218 
220  {
221  return move_srv_.response.ret;
222  }
223  else
224  {
225  ROS_ERROR("Failed to call service go_home");
226  return 1;
227  }
228 }
229 
230 int XArmROSClient::moveJoint(const std::vector<float>& joint_cmd, float jnt_vel_rad, float jnt_acc_rad)
231 {
232  move_srv_.request.mvvelo = jnt_vel_rad;
233  move_srv_.request.mvacc = jnt_acc_rad;
234  move_srv_.request.mvtime = 0;
235  move_srv_.request.pose = joint_cmd;
236 
238  {
239  return move_srv_.response.ret;
240  }
241  else
242  {
243  ROS_ERROR("Failed to call service move_joint");
244  return 1;
245  }
246 }
247 
248 int XArmROSClient::moveLine(const std::vector<float>& cart_cmd, float cart_vel_mm, float cart_acc_mm)
249 {
250  move_srv_.request.mvvelo = cart_vel_mm;
251  move_srv_.request.mvacc = cart_acc_mm;
252  move_srv_.request.mvtime = 0;
253  move_srv_.request.pose = cart_cmd;
254 
256  {
257  return move_srv_.response.ret;
258  }
259  else
260  {
261  ROS_ERROR("Failed to call service move_line");
262  return 1;
263  }
264 }
265 
266 int XArmROSClient::moveLineB(int num_of_pnts, const std::vector<float> cart_cmds[], float cart_vel_mm, float cart_acc_mm, float radii)
267 {
268  move_srv_.request.mvvelo = cart_vel_mm;
269  move_srv_.request.mvacc = cart_acc_mm;
270  move_srv_.request.mvtime = 0;
271  move_srv_.request.mvradii = radii;
272 
273  for(int i=0; i<num_of_pnts; i++)
274  {
275  move_srv_.request.pose = cart_cmds[i];
276 
278  {
279  if(move_srv_.response.ret)
280  return 1; // move_lineb() returns non-zero value.
281  }
282  else
283  {
284  ROS_ERROR("Failed to call service move_lineb");
285  return 1;
286  }
287  }
288 
289  return 0;
290 }
291 
292 int XArmROSClient::config_tool_modbus(int baud_rate, int time_out_ms)
293 {
294  cfg_modbus_msg_.request.baud_rate = baud_rate;
295  cfg_modbus_msg_.request.timeout_ms = time_out_ms;
297  {
298  return cfg_modbus_msg_.response.ret;
299  }
300  else
301  {
302  ROS_ERROR("Failed to call service config_tool_modbus");
303  return 1;
304  }
305 }
306 
307 int XArmROSClient::send_tool_modbus(unsigned char* data, int send_len, unsigned char* recv_data, int recv_len)
308 {
309  for(int i=0; i<send_len; i++)
310  {
311  set_modbus_msg_.request.send_data.push_back(data[i]);
312  }
313 
314  set_modbus_msg_.request.respond_len = recv_len;
315 
317  {
318  if(recv_len)
319  {
320  for(int j=0; j<recv_len; j++)
321  {
322  recv_data[j] = set_modbus_msg_.response.respond_data[j];
323  }
324  }
325 
326  set_modbus_msg_.request.send_data.clear();
327  set_modbus_msg_.response.respond_data.clear();
328 
329  return 0;
330  }
331  else
332  {
333  ROS_ERROR("Failed to call service send_tool_modbus");
334  set_modbus_msg_.request.send_data.clear();
335  set_modbus_msg_.response.respond_data.clear();
336  return 1;
337  }
338 }
339 
341 {
342  gripper_move_msg_.request.pulse_pos = pulse;
344  {
345  ROS_INFO("gripper_move: %f\n", pulse);
346  return gripper_move_msg_.response.ret;
347  }
348  else
349  {
350  ROS_ERROR("Failed to call service gripper_move");
351  return 1;
352  }
353 
354 }
355 
356 int XArmROSClient::gripperConfig(float pulse_vel)
357 {
358  gripper_config_msg_.request.pulse_vel = pulse_vel;
360  {
361  // ROS_INFO("gripper_vel: %f\n", pulse_vel);
362  return gripper_config_msg_.response.ret;
363  }
364  else
365  {
366  ROS_ERROR("Failed to call service gripper_move");
367  return 1;
368  }
369 
370 }
371 
372 int XArmROSClient::getGripperState(float *curr_pulse, int *curr_err)
373 {
375  {
376  *curr_pulse = gripper_state_msg_.response.curr_pos;
377  *curr_err = gripper_state_msg_.response.err_code;
378  return 0;
379  }
380  else
381  {
382  ROS_ERROR("Failed to call service gripper_move");
383  return 1;
384  }
385 
386 }
387 
388 int XArmROSClient::veloMoveJoint(const std::vector<float>& jnt_v, bool is_sync)
389 {
390  move_velo_srv_.request.velocities = jnt_v;
391  move_velo_srv_.request.jnt_sync = is_sync ? 1 : 0;
392 
394  {
395  return move_velo_srv_.response.ret;
396  }
397  else
398  {
399  ROS_ERROR("Failed to call service velo_move_joint");
400  return 1;
401  }
402 }
403 
404 int XArmROSClient::veloMoveLine(const std::vector<float>& line_v, bool is_tool_coord)
405 {
406  move_velo_srv_.request.velocities = line_v;
407  move_velo_srv_.request.coord = is_tool_coord ? 1 : 0;
408 
410  {
411  return move_velo_srv_.response.ret;
412  }
413  else
414  {
415  ROS_ERROR("Failed to call service velo_move_line");
416  return 1;
417  }
418 }
419 
420 }// namespace xarm_api
int setState(short state)
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
ros::ServiceClient move_joint_client_
xarm_msgs::SetToolModbus set_modbus_msg_
int setServoCartisian(const std::vector< float > &cart_cmd)
xarm_msgs::ConfigToolModbus cfg_modbus_msg_
ros::ServiceClient move_line_client_
ros::ServiceClient set_state_client_
xarm_msgs::SetAxis set_axis_srv_
ros::ServiceClient move_servoj_client_
ros::ServiceClient velo_move_line_client_
ros::ServiceClient motion_ctrl_client_
xarm_msgs::MoveVelo move_velo_srv_
bool call(MReq &req, MRes &res)
ros::ServiceClient config_modbus_client_
xarm_msgs::Move servo_cart_msg_
int veloMoveLine(const std::vector< float > &line_v, bool is_tool_coord=false)
xarm_msgs::SetInt16 set_int16_srv_
xarm_msgs::GripperState gripper_state_msg_
xarm_msgs::GetErr get_err_srv_
int moveJoint(const std::vector< float > &joint_cmd, float jnt_vel_rad, float jnt_acc_rad=15)
ros::ServiceClient gripper_state_client_
xarm_msgs::ClearErr clear_err_srv_
int config_tool_modbus(int baud_rate, int time_out_ms)
ros::ServiceClient move_lineb_client_
int gripperConfig(float pulse_vel)
xarm_msgs::Move move_srv_
xarm_msgs::TCPOffset offset_srv_
#define ROS_INFO(...)
ros::ServiceClient set_load_client_
ros::ServiceClient gripper_config_client_
const std::string & getNamespace() const
xarm_msgs::Move servoj_msg_
xarm_msgs::SetLoad set_load_srv_
ros::ServiceClient set_mode_client_
int veloMoveJoint(const std::vector< float > &jnt_v, bool is_sync=true)
ros::ServiceClient move_servo_cart_client_
int goHome(float jnt_vel_rad, float jnt_acc_rad=15)
ros::ServiceClient gripper_move_client_
int send_tool_modbus(unsigned char *data, int send_len, unsigned char *recv_data=NULL, int recv_len=0)
int moveLineB(int num_of_pnts, const std::vector< float > cart_cmds[], float cart_vel_mm, float cart_acc_mm=500, float radii=0)
xarm_msgs::GripperMove gripper_move_msg_
int setLoad(float mass, const std::vector< float > &center_of_mass)
int getGripperState(float *curr_pulse, int *curr_err)
ros::ServiceClient send_modbus_client_
void init(ros::NodeHandle &nh)
ros::ServiceClient set_tcp_offset_client_
ros::ServiceClient clear_err_client_
int setTCPOffset(const std::vector< float > &tcp_offset)
#define ROS_ERROR(...)
int gripperMove(float pulse)
ros::ServiceClient go_home_client_
ros::ServiceClient get_err_client_
ros::ServiceClient velo_move_joint_client_
int moveLine(const std::vector< float > &cart_cmd, float cart_vel_mm, float cart_acc_mm=500)
ROSCPP_DECL bool waitForService(const std::string &service_name, int32_t timeout)
int setServoJ(const std::vector< float > &joint_cmd)
xarm_msgs::GripperConfig gripper_config_msg_


xarm_api
Author(s):
autogenerated on Sat May 8 2021 02:51:23