move_test.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: waylon <weile.wang@ufactory.cc>
6  Jason <jason@ufactory.cc>
7  ============================================================================*/
8 #include "ros/ros.h"
9 #include <xarm_driver.h>
10 
11 int go_home_test(xarm_msgs::Move srv, ros::ServiceClient client)
12 {
13  srv.request.mvvelo = 20.0 / 57.0;
14  srv.request.mvacc = 1000;
15  srv.request.mvtime = 0;
16  if(client.call(srv))
17  {
18  ROS_INFO("%s\n", srv.response.message.c_str());
19  }
20  else
21  {
22  ROS_ERROR("Failed to call service go home");
23  return 1;
24  }
25  return 0;
26 }
27 
28 int servoj_test(xarm_msgs::Move srv, ros::ServiceClient client)
29 {
30  std::vector<float> joint[2] = {{0, 0, 0, 0, 0, 0, 0},
31  {0, -0.3, 0, 0, 0, 0, 0}};
32 
33  srv.request.mvvelo = 0;
34  srv.request.mvacc = 0;
35  srv.request.mvtime = 0;
36  for (int i = 0; i < 2; i++)
37  {
38  srv.request.pose = joint[i];
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(2).sleep(); //2s
49  }
50  return 0;
51 }
52 
53 int move_lineb_test(xarm_msgs::Move srv, ros::ServiceClient client)
54 {
55  std::vector<float> pose[5] = { {300, 0, 100, -3.14, 0, 0},
56  {300, 100, 100, -3.14, 0, 0},
57  {400, 100, 100, -3.14, 0, 0},
58  {400, -100, 100, -3.14, 0, 0},
59  {300, -100, 100, -3.14, 0, 0}};
60  srv.request.mvvelo = 100;
61  srv.request.mvacc = 1000;
62  srv.request.mvtime = 0;
63  srv.request.mvradii = 20;
64 
65  for(int k=0; k<3; k++)
66  {
67  for(int i = 0; i < 5; i++)
68  {
69  srv.request.pose = pose[i];
70  if(client.call(srv))
71  {
72  ROS_INFO("%s\n", srv.response.message.c_str());
73  }
74  else
75  {
76  ROS_ERROR("Failed to call service move_lineb");
77  return 1;
78  }
79  }
80  }
81  return 0;
82 }
83 
84 int main(int argc, char **argv)
85 {
86  ros::init(argc, argv, "xarm_move_test");
87  ros::NodeHandle nh;
88 
89  nh.setParam("/xarm/wait_for_finish", true); // return after motion service finish
90  ros::Publisher sleep_pub_ = nh.advertise<std_msgs::Float32>("/xarm/sleep_sec", 1);
91  ros::ServiceClient motion_ctrl_client_ = nh.serviceClient<xarm_msgs::SetAxis>("/xarm/motion_ctrl");
92  ros::ServiceClient set_mode_client_ = nh.serviceClient<xarm_msgs::SetInt16>("/xarm/set_mode");
93  ros::ServiceClient set_state_client_ = nh.serviceClient<xarm_msgs::SetInt16>("/xarm/set_state");
94  ros::ServiceClient go_home_client_ = nh.serviceClient<xarm_msgs::Move>("/xarm/go_home");
95  ros::ServiceClient move_lineb_client_ = nh.serviceClient<xarm_msgs::Move>("/xarm/move_lineb");
96  ros::ServiceClient move_servoj_client_ = nh.serviceClient<xarm_msgs::Move>("/xarm/move_servoj");
97 
98  xarm_msgs::SetAxis set_axis_srv_;
99  xarm_msgs::SetInt16 set_int16_srv_;
100  xarm_msgs::Move move_srv_;
101 
102  set_axis_srv_.request.id = 8;
103  set_axis_srv_.request.data = 1;
104 
105  if(motion_ctrl_client_.call(set_axis_srv_))
106  {
107  ROS_INFO("%s\n", set_axis_srv_.response.message.c_str());
108  }
109  else
110  {
111  ROS_ERROR("Failed to call service motion_ctrl");
112  return 1;
113  }
114 
115  set_int16_srv_.request.data = 0;
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  set_int16_srv_.request.data = 0;
127  if(set_state_client_.call(set_int16_srv_))
128  {
129  ROS_INFO("%s\n", set_int16_srv_.response.message.c_str());
130  }
131  else
132  {
133  ROS_ERROR("Failed to call service set_state");
134  return 1;
135  }
136 
137  if(go_home_test(move_srv_, go_home_client_) == 1)
138  return 1;
139 
140  // MOVE_LINEB need some additional configurations: wait=False, sleep before sending commands
141  nh.setParam("/xarm/wait_for_finish", false); // This configuration is CRITICAL for move_lineb!
142  std_msgs::Float32 sleep_msg;
143  sleep_msg.data = 1.0;
144  sleep_pub_.publish(sleep_msg);
145  if(move_lineb_test(move_srv_, move_lineb_client_) == 1)
146  return 1;
147  nh.setParam("/xarm/wait_for_finish", true);
148 
149 }
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
void publish(const boost::shared_ptr< M > &message) const
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 main(int argc, char **argv)
Definition: move_test.cpp:84
int go_home_test(xarm_msgs::Move srv, ros::ServiceClient client)
Definition: move_test.cpp:11
#define ROS_INFO(...)
int move_lineb_test(xarm_msgs::Move srv, ros::ServiceClient client)
Definition: move_test.cpp:53
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
int servoj_test(xarm_msgs::Move srv, ros::ServiceClient client)
Definition: move_test.cpp:28
#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