test_velo_move.cpp
Go to the documentation of this file.
1 /* Copyright 2021 UFACTORY Inc. All Rights Reserved.
2  *
3  * Software License Agreement (BSD License)
4  *
5  * Author: Jason <jason@ufactory.cc>
6  Vinman <vinman@ufactory.cc>
7  ============================================================================*/
8 #include "ros/ros.h"
9 #include <xarm_driver.h>
10 
11 int motion_enable(bool enable, xarm_msgs::SetAxis srv, ros::ServiceClient client)
12 {
13  srv.request.id = 8;
14  srv.request.data = enable ? 1 : 0;
15  if(client.call(srv))
16  {
17  ROS_INFO("%s\n", srv.response.message.c_str());
18  }
19  else
20  {
21  ROS_ERROR("Failed to call service motion_ctrl");
22  return 1;
23  }
24  return 0;
25 }
26 
27 int switch_mode(int mode, xarm_msgs::SetInt16 srv, ros::ServiceClient set_mode_client, ros::ServiceClient set_state_client)
28 {
29  srv.request.data = mode;
30  if(set_mode_client.call(srv))
31  {
32  ROS_INFO("%s\n", srv.response.message.c_str());
33  }
34  else
35  {
36  ROS_ERROR("Failed to call service set_mode");
37  return 1;
38  }
39 
40  srv.request.data = 0;
41  if(set_state_client.call(srv))
42  {
43  ROS_INFO("%s\n", srv.response.message.c_str());
44  }
45  else
46  {
47  ROS_ERROR("Failed to call service set_state");
48  return 1;
49  }
50  return 0;
51 }
52 
53 int velo_move_test(xarm_msgs::MoveVelo srv, ros::ServiceClient client)
54 {
55  if(client.call(srv))
56  {
57  ROS_INFO("%s\n", srv.response.message.c_str());
58  }
59  else
60  {
61  ROS_ERROR("Failed to call service velo move");
62  return 1;
63  }
64  return 0;
65 }
66 
67 
68 int main(int argc, char **argv)
69 {
70  ros::init(argc, argv, "xarm_move_test");
71  ros::NodeHandle nh;
72 
73  ros::ServiceClient motion_ctrl_client_ = nh.serviceClient<xarm_msgs::SetAxis>("/xarm/motion_ctrl");
74  ros::ServiceClient set_mode_client_ = nh.serviceClient<xarm_msgs::SetInt16>("/xarm/set_mode");
75  ros::ServiceClient set_state_client_ = nh.serviceClient<xarm_msgs::SetInt16>("/xarm/set_state");
76  ros::ServiceClient velo_move_joint_client_ = nh.serviceClient<xarm_msgs::MoveVelo>("/xarm/velo_move_joint");
77  ros::ServiceClient velo_move_line_client_ = nh.serviceClient<xarm_msgs::MoveVelo>("/xarm/velo_move_line");
78 
79  xarm_msgs::SetAxis set_axis_srv_;
80  xarm_msgs::SetInt16 set_int16_srv_;
81  xarm_msgs::MoveVelo move_joint_velo_srv_;
82  xarm_msgs::MoveVelo move_line_velo_srv_;
83 
84  if(motion_enable(true, set_axis_srv_, motion_ctrl_client_) == 1)
85  return 1;
86 
87  std::vector<float> jnt_v = { 1, 0, 0, 0, 0, 0, 0 };
88  std::vector<float> line_v = { 100, 0, 0, 0, 0, 0};
89  move_joint_velo_srv_.request.velocities = jnt_v;
90  move_joint_velo_srv_.request.jnt_sync = 1;
91  move_line_velo_srv_.request.velocities = line_v;
92  move_line_velo_srv_.request.coord = 0;
93 
94  // velocity move joint
95  if(switch_mode(4, set_int16_srv_, set_mode_client_, set_state_client_) == 1)
96  return 1;
97  if(velo_move_test(move_joint_velo_srv_, velo_move_joint_client_) == 1)
98  return 1;
99  sleep_milliseconds(3000); // sleep 3s
100  move_joint_velo_srv_.request.velocities[0] = -1;
101  if(velo_move_test(move_joint_velo_srv_, velo_move_joint_client_) == 1)
102  return 1;
103  sleep_milliseconds(3100); // sleep 3s
104  move_joint_velo_srv_.request.velocities[0] = 0;
105  if(velo_move_test(move_joint_velo_srv_, velo_move_joint_client_) == 1)
106  return 1;
107 
108  // velocity move line
109  if(switch_mode(5, set_int16_srv_, set_mode_client_, set_state_client_) == 1)
110  return 1;
111  if(velo_move_test(move_line_velo_srv_, velo_move_line_client_) == 1)
112  return 1;
113  sleep_milliseconds(3000); // sleep 3s
114  move_line_velo_srv_.request.velocities[0] = -100;
115  if(velo_move_test(move_line_velo_srv_, velo_move_line_client_) == 1)
116  return 1;
117  sleep_milliseconds(3000); // sleep 3s
118  move_line_velo_srv_.request.velocities[0] = 0;
119  if(velo_move_test(move_line_velo_srv_, velo_move_line_client_) == 1)
120  return 1;
121 }
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
void sleep_milliseconds(unsigned long milliseconds)
Definition: uxbus_cmd.h:24
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
bool call(MReq &req, MRes &res)
int main(int argc, char **argv)
int switch_mode(int mode, xarm_msgs::SetInt16 srv, ros::ServiceClient set_mode_client, ros::ServiceClient set_state_client)
#define ROS_INFO(...)
int velo_move_test(xarm_msgs::MoveVelo srv, ros::ServiceClient client)
int motion_enable(bool enable, xarm_msgs::SetAxis srv, ros::ServiceClient client)
#define ROS_ERROR(...)


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