dsr_service_modbus_simple.cpp
Go to the documentation of this file.
1 /*
2  * [c++ example simple] modbus 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 
12 #include "dsr_util.h"
13 #include "dsr_robot.h"
14 
15 using namespace std;
16 using namespace DSR_Robot;
17 
18 //----- set tartget robot----------------------------------------------------
19 string ROBOT_ID = "dsr01";
20 string ROBOT_MODEL = "m1013";
21 void SET_ROBOT(string id, string model) {ROBOT_ID = id; ROBOT_MODEL= model;}
22 //---------------------------------------------------------------------------
23 
25 
26 static void thread_subscriber()
27 {
28  ros::NodeHandlePtr node = boost::make_shared<ros::NodeHandle>();
31  ros::MultiThreadedSpinner spinner(2);
32  spinner.spin();
33 }
34 
35 void SigHandler(int sig)
36 {
37  // Do some custom action.
38  // For example, publish a stop message to some other nodes.
39 
40  // All the default sigint handler does is call shutdown()
41  ROS_INFO("shutdown time!");
42  ROS_INFO("shutdown time!");
43  ROS_INFO("shutdown time!");
44  //ros::ServiceClient srvMoveStop = nh.serviceClient<dsr_msgs::MoveStop>("/dsr01m1013/motion/move_stop");
45 
46  ros::NodeHandlePtr node = boost::make_shared<ros::NodeHandle>();
47  ros::Publisher pubRobotStop = node->advertise<dsr_msgs::RobotStop>("/"+ROBOT_ID +ROBOT_MODEL+"/stop",100);
48 
49  dsr_msgs::RobotStop msg;
50 
51  msg.stop_mode = STOP_TYPE_QUICK;
52  pubRobotStop.publish(msg);
53 
54  ros::shutdown();
55 }
56 
57 int main(int argc, char** argv)
58 {
59  //----- set target robot ---------------
60  string my_robot_id = "dsr01";
61  string my_robot_model = "m1013";
62  if(1 == argc){
63  ROS_INFO("default arguments: dsr01 m1013");
64  }
65  else{
66  if(3 != argc){
67  ROS_ERROR("invalid arguments: <ns> <model> (ex) dsr01 m1013");
68  exit(1);
69  }
70  for (int i = 1; i < argc; i++){
71  printf("argv[%d] = %s\n", i, argv[i]);
72  }
73  my_robot_id = argv[1];
74  my_robot_model = argv[2];
75  }
76  //std::cout << "my_robot_id= " << my_robot_id << ", my_robot_model= " << my_robot_model << endl;
77  SET_ROBOT(my_robot_id, my_robot_model);
78 
79  //----- init ROS ----------------------
80  ros::init(argc, argv, "dsr_service_test_cpp", ros::init_options::NoSigintHandler);
81  ros::NodeHandle nh("~");
82  // Override the default ros sigint handler.
83  // This must be set after the first NodeHandle is created.
84  signal(SIGINT, SigHandler);
85  ros::Publisher pubRobotStop = nh.advertise<dsr_msgs::RobotStop>("/"+ROBOT_ID +ROBOT_MODEL+"/stop",10);
87 
88  //----- create DsrRobot ---------------
89  CDsrRobot robot(nh, my_robot_id, my_robot_model);
90 
91  // run subscriber thread (for monitoring)
92  boost::thread thread_sub(thread_subscriber);
93 
95  robot.config_create_modbus("di1", "192.168.137.50", 502, MODBUS_REGISTER_TYPE_DISCRETE_INPUTS, 3);
96  robot.config_create_modbus("do1", "192.168.137.50", 502, MODBUS_REGISTER_TYPE_COILS, 3);
97  robot.config_create_modbus("ro1", "192.168.137.50", 502, MODBUS_REGISTER_TYPE_HOLDING_REGISTER, 7);
98  robot.config_create_modbus("ro2", "192.168.137.50", 502, MODBUS_REGISTER_TYPE_HOLDING_REGISTER, 6);
99 
100  while(ros::ok())
101  {
102  robot.set_modbus_output("ro2", 80);
103  robot.set_modbus_output("do1", 1);
104  if(robot.get_modbus_input("di1") == 1){
105  robot.set_modbus_output("ro1", 30);
106  }
107  }
108  thread_sub.join();
109  // wait the second thread to finish
110  ROS_INFO("dsr_service_test finished !!!!!!!!!!!!!!!!!!!!!");
111  return 0;
112 }
void SigHandler(int sig)
static void thread_subscriber()
string ROBOT_MODEL
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
#define ROS_INFO(...)
ROSCPP_DECL bool ok()
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
void SET_ROBOT(string id, string model)
string ROBOT_ID
ROSCPP_DECL void shutdown()
#define ROS_ERROR(...)
int main(int argc, char **argv)


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