dsr_service_io_basic.cpp
Go to the documentation of this file.
1 /*
2  * [c++ example basic] I/O test for doosan robot
3  * Author: Jin Hyuk Gong (jinhyuk.gong@doosan.com)
4  *
5  * Copyright (c) 2019 Doosan Robotics
6  * Use of this source code is governed by the BSD, see LICENSE
7 */
8 
9 #include <ros/ros.h>
10 #include <signal.h>
11 #include <boost/thread/thread.hpp>
12 #include <cstdlib>
13 #include <array>
14 #include <vector>
15 #include <algorithm>
16 #include <string>
17 #include <iostream>
18 
19 #include <dsr_msgs/RobotState.h>
20 #include <dsr_msgs/RobotStop.h>
21 
22 #include <dsr_msgs/MoveJoint.h>
23 #include <dsr_msgs/MoveLine.h>
24 #include <dsr_msgs/MoveJointx.h>
25 #include <dsr_msgs/MoveCircle.h>
26 #include <dsr_msgs/MoveSplineJoint.h>
27 #include <dsr_msgs/MoveSplineTask.h>
28 #include <dsr_msgs/MoveBlending.h>
29 #include <dsr_msgs/MoveSpiral.h>
30 #include <dsr_msgs/MovePeriodic.h>
31 #include <dsr_msgs/MoveWait.h>
32 
33 #include <dsr_msgs/ConfigCreateTcp.h>
34 #include <dsr_msgs/ConfigDeleteTcp.h>
35 #include <dsr_msgs/GetCurrentTcp.h>
36 #include <dsr_msgs/SetCurrentTcp.h>
37 
38 #include <dsr_msgs/SetCurrentTool.h>
39 #include <dsr_msgs/GetCurrentTool.h>
40 #include <dsr_msgs/ConfigCreateTool.h>
41 #include <dsr_msgs/ConfigDeleteTool.h>
42 
43 #include <dsr_msgs/SetCtrlBoxDigitalOutput.h>
44 #include <dsr_msgs/GetCtrlBoxDigitalInput.h>
45 #include <dsr_msgs/SetToolDigitalOutput.h>
46 #include <dsr_msgs/GetToolDigitalInput.h>
47 #include <dsr_msgs/SetCtrlBoxAnalogOutput.h>
48 #include <dsr_msgs/GetCtrlBoxAnalogInput.h>
49 #include <dsr_msgs/SetCtrlBoxAnalogOutputType.h>
50 #include <dsr_msgs/SetCtrlBoxAnalogInputType.h>
51 
52 #include <dsr_msgs/SetModbusOutput.h>
53 #include <dsr_msgs/GetModbusInput.h>
54 #include <dsr_msgs/ConfigCreateModbus.h>
55 #include <dsr_msgs/ConfigDeleteModbus.h>
56 
57 #include <dsr_msgs/DrlPause.h>
58 #include <dsr_msgs/DrlStart.h>
59 #include <dsr_msgs/DrlStop.h>
60 #include <dsr_msgs/DrlResume.h>
61 
62 #include "DRFL.h"
63 #include "DRFC.h"
64 #include "DRFS.h"
65 
66 using namespace std;
67 
68 //----- set tartget robot----------------------------------------------------
69 string ROBOT_ID = "dsr01";
70 string ROBOT_MODEL = "m1013";
71 void SET_ROBOT(string id, string model) {ROBOT_ID = id; ROBOT_MODEL= model;}
72 //---------------------------------------------------------------------------
73 
74 int movej(float fTargetPos[NUM_JOINT],
75  float fTargetVel,
76  float fTargetAcc,
77  float fTargetTime = 0.f,
78  int nMoveMode = MOVE_MODE_ABSOLUTE,
79  float fBlendingRadius = 0.f,
80  int nBlendingType = BLENDING_SPEED_TYPE_DUPLICATE,
81  int nSyncType = 0)
82 {
83 
84  //ros::ServiceClient srvMoveJoint = nh.serviceClient<dsr_msgs::MoveJoint>("/dsr01m1013/motion/move_joint");
85  ros::NodeHandlePtr node = boost::make_shared<ros::NodeHandle>();
86  ros::ServiceClient srvMoveJoint = node->serviceClient<dsr_msgs::MoveJoint>("/"+ROBOT_ID +ROBOT_MODEL+"/motion/move_joint");
87 
88  dsr_msgs::MoveJoint srv;
89 
90  for(int i=0; i<NUM_JOINT; i++)
91  srv.request.pos[i] = fTargetPos[i];
92  srv.request.vel = fTargetVel;
93  srv.request.acc = fTargetAcc;
94  srv.request.time = fTargetTime;
95  srv.request.mode = nMoveMode;
96  srv.request.radius = fBlendingRadius;
97  srv.request.blendType = nBlendingType;
98  srv.request.syncType = nSyncType;
99 
100  if(srvMoveJoint.call(srv))
101  {
102  //ROS_INFO("receive srv, srv.response.success: %ld\n", (long int)srv.response.success);
103  return (srv.response.success);
104  }
105  else
106  {
107  ROS_ERROR("Failed to call service dr_control_service : move_joint\n");
108  ros::shutdown();
109  return -1;
110  }
111 
112  return 0;
113 }
114 
115 int set_digital_output(int nGpioIndex,
116  bool bGpioValue)
117 {
118  ros::NodeHandlePtr node = boost::make_shared<ros::NodeHandle>();
119  ros::ServiceClient srvSetCtrlBoxDigitalOutput = node->serviceClient<dsr_msgs::SetCtrlBoxDigitalOutput>("/"+ROBOT_ID +ROBOT_MODEL+"/io/set_digital_output");
120  dsr_msgs::SetCtrlBoxDigitalOutput srv;
121 
122  srv.request.index = nGpioIndex;
123  srv.request.value = bGpioValue;
124 
125  if(srvSetCtrlBoxDigitalOutput.call(srv))
126  {
127  //ROS_INFO("receive srv, srv.response.success: %ld\n", (long int)srv.response.success);
128  return (srv.response.success);
129  }
130  else
131  {
132  ROS_ERROR("Failed to call service dr_control_service : set_digital_output\n");
133  ros::shutdown();
134  return -1;
135  }
136  return 0;
137 }
138 
139 int get_digital_input(int nGpioIndex)
140 {
141  ros::NodeHandlePtr node = boost::make_shared<ros::NodeHandle>();
142  ros::ServiceClient srvGetCtrlBoxDigitalInput = node->serviceClient<dsr_msgs::GetCtrlBoxDigitalInput>("/"+ROBOT_ID +ROBOT_MODEL+"/io/get_digital_input");
143  dsr_msgs::GetCtrlBoxDigitalInput srv;
144 
145  srv.request.index = nGpioIndex;
146 
147  if(srvGetCtrlBoxDigitalInput.call(srv))
148  {
149  //ROS_INFO("receive srv, srv.response.success: %ld\n", (long int)srv.response.value);
150  return (srv.response.value);
151  }
152  else
153  {
154  ROS_ERROR("Failed to call service dr_control_service : get_digital_input\n");
155  ros::shutdown();
156  return -1;
157  }
158  return 0;
159 }
160 
161 int set_tool_digital_output(int nGpioIndex,
162  bool bGpioValue)
163 {
164  ros::NodeHandlePtr node = boost::make_shared<ros::NodeHandle>();
165  ros::ServiceClient srvSetToolDigitalOutput = node->serviceClient<dsr_msgs::SetToolDigitalOutput>("/"+ROBOT_ID +ROBOT_MODEL+"/io/set_tool_digital_output");
166  dsr_msgs::SetToolDigitalOutput srv;
167 
168  srv.request.index = nGpioIndex;
169  srv.request.value = bGpioValue;
170 
171  if(srvSetToolDigitalOutput.call(srv))
172  {
173  //ROS_INFO("receive srv, srv.response.success: %ld\n", (long int)srv.response.success);
174  return (srv.response.success);
175  }
176  else
177  {
178  ROS_ERROR("Failed to call service dr_control_service : set_tool_digital_output\n");
179  ros::shutdown();
180  return -1;
181  }
182  return 0;
183 }
184 
185 int get_tool_digital_input(int nGpioIndex)
186 {
187  ros::NodeHandlePtr node = boost::make_shared<ros::NodeHandle>();
188  ros::ServiceClient srvGetToolDigitalInput = node->serviceClient<dsr_msgs::GetToolDigitalInput>("/"+ROBOT_ID +ROBOT_MODEL+"/io/get_tool_digital_input");
189  dsr_msgs::GetToolDigitalInput srv;
190 
191  srv.request.index = nGpioIndex;
192 
193  if(srvGetToolDigitalInput.call(srv))
194  {
195  //ROS_INFO("receive srv, srv.response.success: %ld\n", (long int)srv.response.value);
196  return (srv.response.value);
197  }
198  else
199  {
200  ROS_ERROR("Failed to call service dr_control_service : get_tool_digital_input\n");
201  ros::shutdown();
202  return -1;
203  }
204  return 0;
205 }
206 
207 int set_analog_output(int nGpioChannel,
208  float fGpioValue)
209 {
210  ros::NodeHandlePtr node = boost::make_shared<ros::NodeHandle>();
211  ros::ServiceClient srvSetCtrlBoxAnalogOutput = node->serviceClient<dsr_msgs::SetCtrlBoxAnalogOutput>("/"+ROBOT_ID +ROBOT_MODEL+"/io/set_analog_output");
212  dsr_msgs::SetCtrlBoxAnalogOutput srv;
213 
214  srv.request.channel = nGpioChannel;
215  srv.request.value = fGpioValue;
216 
217  if(srvSetCtrlBoxAnalogOutput.call(srv))
218  {
219  //ROS_INFO("receive srv, srv.response.success: %ld\n", (long int)srv.response.success);
220  return (srv.response.success);
221  }
222  else
223  {
224  ROS_ERROR("Failed to call service dr_control_service : set_analog_output\n");
225  ros::shutdown();
226  return -1;
227  }
228  return 0;
229 }
230 
231 int get_analog_input(int nGpioChannel)
232 {
233  ros::NodeHandlePtr node = boost::make_shared<ros::NodeHandle>();
234  ros::ServiceClient srvGetCtrlBoxAnalogInput = node->serviceClient<dsr_msgs::GetCtrlBoxDigitalInput>("/"+ROBOT_ID +ROBOT_MODEL+"/io/get_analog_input");
235  dsr_msgs::GetCtrlBoxAnalogInput srv;
236 
237  srv.request.channel = nGpioChannel;
238 
239  if(srvGetCtrlBoxAnalogInput.call(srv))
240  {
241  //ROS_INFO("receive srv, srv.response.success: %ld\n", (long int)srv.response.value);
242  return (srv.response.value);
243  }
244  else
245  {
246  ROS_ERROR("Failed to call service dr_control_service : get_analog_input\n");
247  ros::shutdown();
248  return -1;
249  }
250  return 0;
251 }
252 
253 int set_analog_output_type(int nGpioChannel,
254  int nGpioMode)
255 {
256  ros::NodeHandlePtr node = boost::make_shared<ros::NodeHandle>();
257  ros::ServiceClient srvSetCtrlBoxAnalogOutputType = node->serviceClient<dsr_msgs::SetCtrlBoxAnalogOutputType>("/"+ROBOT_ID +ROBOT_MODEL+"/io/set_analog_output_type");
258  dsr_msgs::SetCtrlBoxAnalogOutputType srv;
259 
260  srv.request.channel = nGpioChannel;
261  srv.request.mode = nGpioMode;
262 
263  if(srvSetCtrlBoxAnalogOutputType.call(srv))
264  {
265  //ROS_INFO("receive srv, srv.response.success: %ld\n", (long int)srv.response.success);
266  return (srv.response.success);
267  }
268  else
269  {
270  ROS_ERROR("Failed to call service dr_control_service : set_analog_output_type\n");
271  ros::shutdown();
272  return -1;
273  }
274  return 0;
275 }
276 
277 int set_analog_input_type(int nGpioChannel,
278  int nGpioMode)
279 {
280  ros::NodeHandlePtr node = boost::make_shared<ros::NodeHandle>();
281  ros::ServiceClient srvSetCtrlBoxAnalogInputType = node->serviceClient<dsr_msgs::SetCtrlBoxAnalogInputType>("/"+ROBOT_ID +ROBOT_MODEL+"/io/set_analog_input_type");
282  dsr_msgs::SetCtrlBoxAnalogInputType srv;
283 
284  srv.request.channel = nGpioChannel;
285  srv.request.mode = nGpioMode;
286 
287  if(srvSetCtrlBoxAnalogInputType.call(srv))
288  {
289  //ROS_INFO("receive srv, srv.response.success: %ld\n", (long int)srv.response.success);
290  return (srv.response.success);
291  }
292  else
293  {
294  ROS_ERROR("Failed to call service dr_control_service : set_analog_input_type\n");
295  ros::shutdown();
296  return -1;
297  }
298  return 0;
299 }
300 
302 {
303  ros::NodeHandlePtr node = boost::make_shared<ros::NodeHandle>();
304  ros::MultiThreadedSpinner spinner(2);
305  spinner.spin();
306 }
307 
308 void SigHandler(int sig)
309 {
310  // Do some custom action.
311  // For example, publish a stop message to some other nodes.
312 
313  // All the default sigint handler does is call shutdown()
314  ROS_INFO("shutdown time! sig=%d",sig);
315  ROS_INFO("shutdown time! sig=%d",sig);
316  ROS_INFO("shutdown time! sig=%d",sig);
317  //ros::ServiceClient srvMoveStop = nh.serviceClient<dsr_msgs::MoveStop>("/dsr01m1013/motion/move_stop");
318 
319  ros::NodeHandlePtr node = boost::make_shared<ros::NodeHandle>();
320  ros::Publisher pubRobotStop = node->advertise<dsr_msgs::RobotStop>("/"+ROBOT_ID +ROBOT_MODEL+"/stop",100);
321 
322  dsr_msgs::RobotStop msg;
323 
324  msg.stop_mode = STOP_TYPE_QUICK;
325  pubRobotStop.publish(msg);
326 
327  ros::shutdown();
328 }
329 
330 int main(int argc, char** argv)
331 {
332  //----- set target robot ---------------
333  string my_robot_id = "dsr01";
334  string my_robot_model = "m1013";
335  if(1 == argc){
336  ROS_INFO("default arguments: dsr01 m1013");
337  }
338  else{
339  if(3 != argc){
340  ROS_ERROR("invalid arguments: <ns> <model> (ex) dsr01 m1013");
341  exit(1);
342  }
343  for (int i = 1; i < argc; i++){
344  printf("argv[%d] = %s\n", i, argv[i]);
345  }
346  my_robot_id = argv[1];
347  my_robot_model = argv[2];
348  }
349  //std::cout << "my_robot_id= " << my_robot_id << ", my_robot_model= " << my_robot_model << endl;
350  SET_ROBOT(my_robot_id, my_robot_model);
351 
352  //----- init ROS ----------------------
353  int rate_sub = 1; // 1 Hz = 1 sec
354  int nPubRate = 100; // 100 Hz (10ms)
355  int i=0, nRes=0;
356  ros::init(argc, argv, "dsr_service_io_basic_cpp", ros::init_options::NoSigintHandler);
357  ros::NodeHandle nh("~");
358  // Override the default ros sigint handler.
359  // This must be set after the first NodeHandle is created.
360  signal(SIGINT, SigHandler);
361  ros::Publisher pubRobotStop = nh.advertise<dsr_msgs::RobotStop>("/"+ROBOT_ID +ROBOT_MODEL+"/stop",10);
362 
363  // spawn another thread
364  boost::thread thread_sub(thread_subscriber);
365 
366  /*
367  if(argc !=3)
368  {
369  ROS_INFO("cmd : rosrun dsr_control dsr_control_service arg0 arg1");
370  ROS_INFO("arg0: double number, arg1: double number");
371  return 1;
372  }
373  */
374 
376  set_digital_output(2, true);
377  if(get_digital_input(2) == 1){
378  set_digital_output(11, true);
379  }
380 
381  while(ros::ok())
382  {
383  }
384 
385  ros::shutdown();
386  // wait the second thread to finish
387  //thread_sub.join();
388  ROS_INFO("dsr_service_test finished !!!!!!!!!!!!!!!!!!!!!");
389  return 0;
390 }
int set_analog_output(int nGpioChannel, float fGpioValue)
string ROBOT_ID
int get_digital_input(int nGpioIndex)
string ROBOT_MODEL
int set_digital_output(int nGpioIndex, bool bGpioValue)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
int main(int argc, char **argv)
void thread_subscriber()
void SET_ROBOT(string id, string model)
int set_analog_input_type(int nGpioChannel, int nGpioMode)
#define ROS_INFO(...)
ROSCPP_DECL bool ok()
int get_tool_digital_input(int nGpioIndex)
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
int get_analog_input(int nGpioChannel)
int set_analog_output_type(int nGpioChannel, int nGpioMode)
int set_tool_digital_output(int nGpioIndex, bool bGpioValue)
ROSCPP_DECL void shutdown()
int movej(float fTargetPos[NUM_JOINT], float fTargetVel, float fTargetAcc, float fTargetTime=0.f, int nMoveMode=MOVE_MODE_ABSOLUTE, float fBlendingRadius=0.f, int nBlendingType=BLENDING_SPEED_TYPE_DUPLICATE, int nSyncType=0)
#define ROS_ERROR(...)
void SigHandler(int sig)


cpp
Author(s): Kab Kyoum Kim , Jin Hyuk Gong , Jeongwoo Lee
autogenerated on Sat May 18 2019 02:32:53