dsr_service_drl_basic.cpp
Go to the documentation of this file.
1 /*
2  * [c++ example basic] DRL(Doosan Robot Language) 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 drl_pause()
75 {
76  ros::NodeHandlePtr node = boost::make_shared<ros::NodeHandle>();
77  ros::ServiceClient srvDrlPause = node->serviceClient<dsr_msgs::DrlPause>("/"+ROBOT_ID +ROBOT_MODEL+"/drl/drl_pause");
78  dsr_msgs::DrlPause srv;
79 
80  if(srvDrlPause.call(srv))
81  {
82  //ROS_INFO("receive srv, srv.response.success: %ld\n", (long int)srv.response.success);
83  return (srv.response.success);
84  }
85  else
86  {
87  ROS_ERROR("Failed to call service dr_control_service : drl_pause\n");
88  ros::shutdown();
89  return -1;
90  }
91  return 0;
92 }
93 
95 {
96  ros::NodeHandlePtr node = boost::make_shared<ros::NodeHandle>();
97  ros::ServiceClient srvDrlResume = node->serviceClient<dsr_msgs::DrlResume>("/"+ROBOT_ID +ROBOT_MODEL+"/drl/drl_resume");
98  dsr_msgs::DrlResume srv;
99 
100  if(srvDrlResume.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 : drl_resume\n");
108  ros::shutdown();
109  return -1;
110  }
111  return 0;
112 }
113 
114 int drl_start(int nRobotSystem,
115  string strCode)
116 {
117  ros::NodeHandlePtr node = boost::make_shared<ros::NodeHandle>();
118  ros::ServiceClient srvDrlStart = node->serviceClient<dsr_msgs::DrlStart>("/"+ROBOT_ID +ROBOT_MODEL+"/drl/drl_start");
119  dsr_msgs::DrlStart srv;
120 
121  srv.request.robotSystem = nRobotSystem;
122  srv.request.code = strCode;
123 
124  if(srvDrlStart.call(srv))
125  {
126  //ROS_INFO("receive srv, srv.response.success: %ld\n", (long int)srv.response.success);
127  return (srv.response.success);
128  }
129  else
130  {
131  ROS_ERROR("Failed to call service dr_control_service : drl_start\n");
132  ros::shutdown();
133  return -1;
134  }
135  return 0;
136 }
137 
138 int drl_stop(int nStopMode = STOP_TYPE_QUICK)
139 {
140  ros::NodeHandlePtr node = boost::make_shared<ros::NodeHandle>();
141  ros::ServiceClient srvDrlStop = node->serviceClient<dsr_msgs::DrlStop>("/"+ROBOT_ID +ROBOT_MODEL+"/drl/drl_stop");
142  dsr_msgs::DrlStop srv;
143 
144  srv.request.stop_mode = nStopMode;
145 
146  if(srvDrlStop.call(srv))
147  {
148  //ROS_INFO("receive srv, srv.response.success: %ld\n", (long int)srv.response.success);
149  return (srv.response.success);
150  }
151  else
152  {
153  ROS_ERROR("Failed to call service dr_control_service : drl_stop\n");
154  ros::shutdown();
155  return -1;
156  }
157  return 0;
158 }
159 
161 
163 {
164  ros::NodeHandlePtr node = boost::make_shared<ros::NodeHandle>();
165  ros::MultiThreadedSpinner spinner(2);
166  spinner.spin();
167 }
168 
169 void SigHandler(int sig)
170 {
171  // Do some custom action.
172  // For example, publish a stop message to some other nodes.
173 
174  // All the default sigint handler does is call shutdown()
175  ROS_INFO("shutdown time! sig=%d",sig);
176  ROS_INFO("shutdown time! sig=%d",sig);
177  ROS_INFO("shutdown time! sig=%d",sig);
178  //ros::ServiceClient srvMoveStop = nh.serviceClient<dsr_msgs::MoveStop>("/dsr01m1013/motion/move_stop");
179 
180  ros::NodeHandlePtr node = boost::make_shared<ros::NodeHandle>();
181  ros::Publisher pubRobotStop = node->advertise<dsr_msgs::RobotStop>("/"+ROBOT_ID +ROBOT_MODEL+"/stop",100);
182 
183  dsr_msgs::RobotStop msg;
184 
185  msg.stop_mode = STOP_TYPE_QUICK;
186  pubRobotStop.publish(msg);
187 
188  ros::shutdown();
189 }
190 
191 int main(int argc, char** argv)
192 {
193  //----- set target robot ---------------
194  string my_robot_id = "dsr01";
195  string my_robot_model = "m1013";
196  if(1 == argc){
197  ROS_INFO("default arguments: dsr01 m1013");
198  }
199  else{
200  if(3 != argc){
201  ROS_ERROR("invalid arguments: <ns> <model> (ex) dsr01 m1013");
202  exit(1);
203  }
204  for (int i = 1; i < argc; i++){
205  printf("argv[%d] = %s\n", i, argv[i]);
206  }
207  my_robot_id = argv[1];
208  my_robot_model = argv[2];
209  }
210  //std::cout << "my_robot_id= " << my_robot_id << ", my_robot_model= " << my_robot_model << endl;
211  SET_ROBOT(my_robot_id, my_robot_model);
212 
213  //----- init ROS ----------------------
214  int rate_sub = 1; // 1 Hz = 1 sec
215  int nPubRate = 100; // 100 Hz (10ms)
216  int i=0, nRes=0;
217  ros::init(argc, argv, "dsr_service_drl_basic_cpp", ros::init_options::NoSigintHandler);
218  ros::NodeHandle nh("~");
219  // Override the default ros sigint handler.
220  // This must be set after the first NodeHandle is created.
221  signal(SIGINT, SigHandler);
222  ros::Publisher pubRobotStop = nh.advertise<dsr_msgs::RobotStop>("/"+ROBOT_ID +ROBOT_MODEL+"/stop",10);
223 
224  // spawn another thread
225  boost::thread thread_sub(thread_subscriber);
226 
227 
229  std::string drlCodeMove = "set_velj(50)\nset_accj(50)\nmovej([0,0,90,0,90,0])\n";
230  std::string drlCodeReset = "movej([0,0,0,0,0,0])\n";
231 
232  std::string mode;
233  int _rate;
234  nh.getParam("/dsr/mode", mode);
235  ROS_INFO("current mode is : %s", mode.c_str());
236  if(mode == "real"){
237  drl_start(ROBOT_SYSTEM_REAL, drlCodeMove + drlCodeReset);
238  }
239  else{
240  drl_start(ROBOT_SYSTEM_VIRTUAL, drlCodeMove + drlCodeReset);
241  }
242 
243  while(ros::ok())
244  {
245  }
246 
247  ros::shutdown();
248 
249  ROS_INFO("dsr_service_drl_basic_cpp finished !!!!!!!!!!!!!!!!!!!!!");
250  return 0;
251 }
void SET_ROBOT(string id, string model)
void SigHandler(int sig)
int drl_pause()
int drl_start(int nRobotSystem, string strCode)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
string ROBOT_MODEL
int main(int argc, char **argv)
int drl_resume()
#define ROS_INFO(...)
string ROBOT_ID
ROSCPP_DECL bool ok()
void thread_subscriber()
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
bool getParam(const std::string &key, std::string &s) const
ROSCPP_DECL void shutdown()
int drl_stop(int nStopMode=STOP_TYPE_QUICK)
#define ROS_ERROR(...)


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