servo_cartesian_test.cpp
Go to the documentation of this file.
1 /* Copyright 2020 UFACTORY Inc. All Rights Reserved.
2  *
3  * Software License Agreement (BSD License)
4  *
5  * Author: jason <jason@ufactory.cc>
6  ============================================================================*/
7 #include "ros/ros.h"
8 #include <xarm_driver.h>
9 
10 int go_home_test(xarm_msgs::Move &srv, ros::ServiceClient &client)
11 {
12  srv.request.mvvelo = 20.0 / 57.0;
13  srv.request.mvacc = 1000;
14  srv.request.mvtime = 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 go home");
22  return 1;
23  }
24  return 0;
25 }
26 
27 int servo_cart_test(xarm_msgs::Move &srv, ros::ServiceClient &client)
28 {
29  std::vector<float> inc = {0.3, 0, 0, 0, 0, 0};
30 
31  srv.request.mvvelo = 0;
32  srv.request.mvacc = 0;
33  srv.request.mvtime = 1;
34 
35  srv.request.pose = inc;
36 
37  for (int i = 0; i < 500; i++)
38  {
39  if(client.call(srv))
40  {
41  ROS_INFO("%s\n", srv.response.message.c_str());
42  }
43  else
44  {
45  ROS_ERROR("Failed to call service move_servoj");
46  return 1;
47  }
48  ros::Duration(0.01).sleep(); //10ms
49 
50  }
51  return 0;
52 }
53 
54 
55 int main(int argc, char **argv)
56 {
57  ros::init(argc, argv, "xarm_move_test");
58  ros::NodeHandle nh;
59  nh.setParam("/xarm/wait_for_finish", true); // return after motion service finish
60  ros::ServiceClient motion_ctrl_client_ = nh.serviceClient<xarm_msgs::SetAxis>("/xarm/motion_ctrl");
61  ros::ServiceClient set_mode_client_ = nh.serviceClient<xarm_msgs::SetInt16>("/xarm/set_mode");
62  ros::ServiceClient set_state_client_ = nh.serviceClient<xarm_msgs::SetInt16>("/xarm/set_state");
63  ros::ServiceClient go_home_client_ = nh.serviceClient<xarm_msgs::Move>("/xarm/go_home");
64  ros::ServiceClient servo_cart_client_ = nh.serviceClient<xarm_msgs::Move>("/xarm/move_servo_cart");
65 
66  xarm_msgs::SetAxis set_axis_srv_;
67  xarm_msgs::SetInt16 set_int16_srv_;
68  xarm_msgs::Move move_srv_;
69 
70 
71  // STEP 1: Motion Enable
72  set_axis_srv_.request.id = 8;
73  set_axis_srv_.request.data = 1;
74 
75  if(motion_ctrl_client_.call(set_axis_srv_))
76  {
77  ROS_INFO("%s\n", set_axis_srv_.response.message.c_str());
78  }
79  else
80  {
81  ROS_ERROR("Failed to call service motion_ctrl");
82  return 1;
83  }
84 
85  // STEP 2: Set Mode to 0
86  set_int16_srv_.request.data = 0;
87  if(set_mode_client_.call(set_int16_srv_))
88  {
89  ROS_INFO("%s\n", set_int16_srv_.response.message.c_str());
90  }
91  else
92  {
93  ROS_ERROR("Failed to call service set_mode");
94  return 1;
95  }
96 
97  // STEP 3: Set State to 0
98  set_int16_srv_.request.data = 0;
99  if(set_state_client_.call(set_int16_srv_))
100  {
101  ROS_INFO("%s\n", set_int16_srv_.response.message.c_str());
102  }
103  else
104  {
105  ROS_ERROR("Failed to call service set_state");
106  return 1;
107  }
108 
109  // STEP 4: Go to Home Position
110  if(go_home_test(move_srv_, go_home_client_) == 1)
111  return 1;
112 
113 
114  // STEP 5: Set Mode to 1
115  set_int16_srv_.request.data = 1;
116  if(set_mode_client_.call(set_int16_srv_))
117  {
118  ROS_INFO("%s\n", set_int16_srv_.response.message.c_str());
119  }
120  else
121  {
122  ROS_ERROR("Failed to call service set_mode");
123  return 1;
124  }
125 
126  // STEP 6: Set state to 0
127  set_int16_srv_.request.data = 0;
128  if(set_state_client_.call(set_int16_srv_))
129  {
130  ROS_INFO("%s\n", set_int16_srv_.response.message.c_str());
131  }
132  else
133  {
134  ROS_ERROR("Failed to call service set_state");
135  return 1;
136  }
137 
138  // STEP 7: Call SERVO_CARTESIAN service in a loop
139  return servo_cart_test(move_srv_, servo_cart_client_);
140 
141 }
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
bool sleep() const
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
bool call(MReq &req, MRes &res)
int go_home_test(xarm_msgs::Move &srv, ros::ServiceClient &client)
int main(int argc, char **argv)
#define ROS_INFO(...)
int servo_cart_test(xarm_msgs::Move &srv, ros::ServiceClient &client)
#define ROS_ERROR(...)
void setParam(const std::string &key, const XmlRpc::XmlRpcValue &v) const


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