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


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