robot_driver.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2016 SoftBank Robotics Europe
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 
18 // NAOqi Headers
19 #include <qi/application.hpp>
20 
22 
23 static std::string getROSIP(std::string network_interface)
24 {
25  if (network_interface.empty())
26  network_interface = "eth0";
27 
28  typedef std::map< std::string, std::vector<std::string> > Map_IP;
29  Map_IP map_ip = static_cast<Map_IP>(qi::os::hostIPAddrs());
30  if ( map_ip.find(network_interface) == map_ip.end() ) {
31  std::cerr << "Could not find network interface named " << network_interface << ", possible interfaces are ... ";
32  for (Map_IP::iterator it=map_ip.begin(); it!=map_ip.end(); ++it) std::cerr << it->first << " ";
33  std::cerr << std::endl;
34  exit(1);
35  }
36 
37  static const std::string ip = map_ip[network_interface][0];
38  return ip;
39 }
40 
41 static void setMasterURINet( const std::string& uri, const std::string& network_interface )
42 {
43  setenv("ROS_MASTER_URI", uri.c_str(), 1);
44 
45  std::string my_master = "__master="+uri;
46  std::map< std::string, std::string > remap;
47  remap["__master"] = uri;
48  remap["__ip"] = getROSIP(network_interface);
49  // init ros without a sigint-handler in order to shutdown correctly by naoqi
50  const char* ns_env = std::getenv("ROS_NAMESPACE");
51 
52  ros::init( remap, (ns_env==NULL)?(std::string("naoqi_dcm_driver")):("") , ros::init_options::NoSigintHandler );
53 }
54 
55 int main(int argc, char** argv)
56 {
57  // Need this to for SOAP serialization of floats to work
58  setlocale(LC_NUMERIC, "C");
59 
60  //start a session
61  qi::Application app(argc, argv);
62 
63  ros::init(argc, argv, "naoqi_dcm_driver");
64 
65  ros::NodeHandle nh("~");
66  if(!ros::master::check())
67  {
68  ROS_ERROR("Could not contact master!\nQuitting... ");
69  return -1;
70  }
71 
72  // Load Params from Parameter Server
73  int pport = 9559;
74  std::string pip = "127.0.0.1";
75  std::string roscore_ip = "127.0.0.1";
76  std::string network_interface = "eth0";
77  nh.getParam("RobotIP", pip);
78  nh.getParam("RobotPort", pport);
79  nh.getParam("DriverBrokerIP", roscore_ip);
80  nh.getParam("network_interface", network_interface);
81  setMasterURINet( "http://"+roscore_ip+":11311", network_interface);
82 
83  //create a session
84  qi::SessionPtr session = qi::makeSession();
85  try
86  {
87  std::stringstream strstr;
88  strstr << "tcp://" << pip << ":" << pport;
89  ROS_INFO_STREAM("Connecting to " << pip << ":" << pport);
90  session->connect(strstr.str()).wait();
91  }
92  catch(const std::exception &e)
93  {
94  ROS_ERROR("Cannot connect to session, %s", e.what());
95  session->close();
96  return -1;
97  }
98 
99  if (!session->connected)
100  {
101  ROS_ERROR("Cannot connect to session");
102  session->close();
103  return -1;
104  }
105 
106  // Deal with ALBrokerManager singleton (add your broker into NAOqi)
107  boost::shared_ptr<Robot> robot = boost::make_shared<Robot>(session);
108 
109  // stop ALTouch service to prevent the robot shaking
110  try
111  {
112  qi::AnyObject touch_proxy = session->service("ALTouch");
113  touch_proxy.call<void>("exit");
114  ROS_INFO_STREAM("Naoqi Touch service is shut down");
115  }
116  catch (const std::exception& e)
117  {
118  ROS_DEBUG("Did not stop ALTouch: %s", e.what());
119  }
120 
121  // stop AutonomousLife service to prevent the robot shaking
122  try
123  {
124  qi::AnyObject life_proxy = session->service("ALAutonomousLife");
125  if (life_proxy.call<std::string>("getState") != "disabled")
126  {
127  life_proxy.call<void>("setState", "disabled");
128  ROS_INFO_STREAM("Shutting down Naoqi AutonomousLife ...");
129  ros::Duration(2.0).sleep();
130  }
131  }
132  catch (const std::exception& e)
133  {
134  ROS_DEBUG("Did not stop AutonomousLife: %s", e.what());
135  }
136 
137  session->registerService("naoqi_dcm_driver", robot);
138  ros::Duration(0.1).sleep();
139 
140  if (!robot->connect())
141  {
142  session->close();
143  return 0;
144  }
145 
146  // Run the spinner in a separate thread to prevent lockups
148  spinner.start();
149 
150  // Run the main Loop
151  robot->run();
152 
153  //release stiffness and stop correctly
154  robot->stopService();
155 
156  //close the session
157  session->close();
158  spinner.stop();
159 
160  return 0;
161 }
app
ROSCPP_DECL bool check()
static std::string getROSIP(std::string network_interface)
bool sleep() const
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
int main(int argc, char **argv)
void spinner()
ROSCPP_DECL std::string remap(const std::string &name)
void wait(int seconds)
#define ROS_INFO_STREAM(args)
bool getParam(const std::string &key, std::string &s) const
static void setMasterURINet(const std::string &uri, const std::string &network_interface)
#define ROS_ERROR(...)
#define ROS_DEBUG(...)


naoqi_dcm_driver
Author(s): Konstantinos Chatzilygeroudis , Mikael Arguedas , Karsten Knese , Natalia Lyubova
autogenerated on Thu Jul 25 2019 03:49:27