pick_and_place_tcp_server.cpp
Go to the documentation of this file.
1 //
2 // Created by tom on 20/09/16.
3 //
4 
5 #include <ros/ros.h>
6 #include <boost/asio.hpp>
7 #include <std_srvs/Trigger.h>
8 #include <boost/smart_ptr.hpp>
9 
10 
11 #define MAX_LEN 128
12 using boost::asio::ip::tcp;
13 
15 
16 std_srvs::Trigger::Response pickAndPlace(ros::ServiceClient &pickAndPlaceClient);
17 
18 void recover(ros::ServiceClient &pickAndPlaceClient, socket_ptr &client);
19 
20 void changeColor(std::string colorName);
21 
22 int main(int argc, char** argv) {
23  ros::init(argc, argv, "pick_and_place_server");
24 
25  ros::NodeHandle nodeHandle;
27  spinner.start();
28  ros::ServiceClient pickAndPlaceClient = nodeHandle.serviceClient<std_srvs::Trigger>("pick_go");
29  pickAndPlaceClient.waitForExistence();
30 
31  boost::asio::io_service io_service;
32  tcp::acceptor server(io_service, tcp::endpoint(tcp::v4(), 5001));
33  socket_ptr client(new tcp::socket(io_service));
34 
35 
36  ROS_INFO("[%s]: Server up and waiting for client to connect....", ros::this_node::getName().c_str());
37 
38  server.accept(*client);
39  ROS_INFO("[%s]: Got new connection", ros::this_node::getName().c_str());
40  std_srvs::Trigger::Response pickAndPlaceRes = pickAndPlace(pickAndPlaceClient);
41 
42 
43  if (pickAndPlaceRes.success) {
44  boost::asio::write(*client, boost::asio::buffer("go\n", 3));
45  } else {
46  recover(pickAndPlaceClient, client);
47  }
48  while(ros::ok()) {
49  char data[MAX_LEN] = {'\0'};
50 
51  boost::asio::read(*client, boost::asio::buffer(data, 3));
52  if (std::strcmp(data, "go\n") == 0) {
53  if (pickAndPlace(pickAndPlaceClient).success) {
54  boost::asio::write(*client, boost::asio::buffer("go\n", 3));
55  } else {
56  recover(pickAndPlaceClient, client);
57  }
58  } else {
59  ROS_WARN("[%s]: Unknown syntax: %s", ros::this_node::getName().c_str(), data);
60  }
61  }
62 
63 
64 
65  return 0;
66 }
67 
68 void recover(ros::ServiceClient &pickAndPlaceClient, socket_ptr &client) {
69  ROS_WARN("[%s]: plan failed type 'r' to re-plan or 'q' to quit", ros::this_node::getName().c_str());
70  char choice;
71  do {
72  std::cin >> choice;
73  if(choice == 'r') {
74  if (pickAndPlace(pickAndPlaceClient).success) {
75  write(*client, boost::asio::buffer("go\n", 3));
76  break;
77  }
78  } else if(choice == 'q') {
79  ros::shutdown();
80  break;
81  }
82  } while(ros::ok());
83 }
84 
85 
86 std_srvs::Trigger::Response pickAndPlace(ros::ServiceClient &pickAndPlaceClient) {
87  std_srvs::Trigger::Request pickAndPlaceReq;
88  std_srvs::Trigger::Response pickAndPlaceRes;
89 
90  if (pickAndPlaceClient.call(pickAndPlaceReq, pickAndPlaceRes)) {
91  ROS_INFO_STREAM(pickAndPlaceRes);
92  }
93  return pickAndPlaceRes;
94 }
95 
96 
97 void changeColor(std::string colorName) {
98  std::string baseCmd = "rosrun dynamic_reconfigure dynparam load /find_object_node `rospack find robotican_demos`/config/";
99  if(colorName == "red") {
100 
101  FILE *process = popen((baseCmd + "red_object.yaml").c_str(), "r");
102  ros::Duration(1.5).sleep();
103  if(process != 0) {
104  ROS_INFO("[%s]: Change to red", ros::this_node::getName().c_str());
105  }
106  }
107  else if(colorName == "green") {
108  FILE *process = popen((baseCmd + "green_object.yaml").c_str(), "r");
109  ros::Duration(1.5).sleep();
110  if(process != 0) {
111  ROS_INFO("[%s]: Change to green", ros::this_node::getName().c_str());
112  }
113  } else if(colorName == "blue") {
114  FILE *process = popen((baseCmd + "blue_object.yaml").c_str(), "r");
115  ros::Duration(1.5).sleep();
116  if(process != 0) {
117  ROS_INFO("[%s]: Change to blue", ros::this_node::getName().c_str());
118  }
119  }
120 }
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
std_srvs::Trigger::Response pickAndPlace(ros::ServiceClient &pickAndPlaceClient)
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)
ROSCPP_DECL const std::string & getName()
int main(int argc, char **argv)
#define ROS_WARN(...)
void spinner()
bool waitForExistence(ros::Duration timeout=ros::Duration(-1))
#define ROS_INFO(...)
#define MAX_LEN
ROSCPP_DECL bool ok()
boost::shared_ptr< tcp::socket > socket_ptr
void recover(ros::ServiceClient &pickAndPlaceClient, socket_ptr &client)
#define ROS_INFO_STREAM(args)
ROSCPP_DECL void shutdown()
void changeColor(std::string colorName)


robotican_common
Author(s):
autogenerated on Wed Jan 3 2018 03:48:33