robot_driver.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2016 SoftBank Robotics Europe
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  *
00016 */
00017 
00018 // NAOqi Headers
00019 #include <qi/application.hpp>
00020 
00021 #include "naoqi_dcm_driver/robot.hpp"
00022 
00023 static std::string getROSIP(std::string network_interface)
00024 {
00025   if (network_interface.empty())
00026     network_interface = "eth0";
00027 
00028   typedef std::map< std::string, std::vector<std::string> > Map_IP;
00029   Map_IP map_ip = static_cast<Map_IP>(qi::os::hostIPAddrs());
00030   if ( map_ip.find(network_interface) == map_ip.end() ) {
00031     std::cerr << "Could not find network interface named " << network_interface << ", possible interfaces are ... ";
00032     for (Map_IP::iterator it=map_ip.begin(); it!=map_ip.end(); ++it) std::cerr << it->first <<  " ";
00033     std::cerr << std::endl;
00034     exit(1);
00035   }
00036 
00037   static const std::string ip = map_ip[network_interface][0];
00038   return ip;
00039 }
00040 
00041 static void setMasterURINet( const std::string& uri, const std::string& network_interface )
00042 {
00043   setenv("ROS_MASTER_URI", uri.c_str(), 1);
00044 
00045   std::string my_master = "__master="+uri;
00046   std::map< std::string, std::string > remap;
00047   remap["__master"] = uri;
00048   remap["__ip"] = getROSIP(network_interface);
00049   // init ros without a sigint-handler in order to shutdown correctly by naoqi
00050   const char* ns_env = std::getenv("ROS_NAMESPACE");
00051 
00052   ros::init( remap, (ns_env==NULL)?(std::string("naoqi_dcm_driver")):("") , ros::init_options::NoSigintHandler );
00053 }
00054 
00055 int main(int argc, char** argv)
00056 {
00057   // Need this to for SOAP serialization of floats to work
00058   setlocale(LC_NUMERIC, "C");
00059 
00060   //start a session
00061   qi::Application app(argc, argv);
00062 
00063   ros::init(argc, argv, "naoqi_dcm_driver");
00064 
00065   ros::NodeHandle nh("~");
00066   if(!ros::master::check())
00067   {
00068     ROS_ERROR("Could not contact master!\nQuitting... ");
00069     return -1;
00070   }
00071 
00072   // Load Params from Parameter Server
00073   int pport = 9559;
00074   std::string pip = "127.0.0.1";
00075   std::string roscore_ip = "127.0.0.1";
00076   std::string network_interface = "eth0";
00077   nh.getParam("RobotIP", pip);
00078   nh.getParam("RobotPort", pport);
00079   nh.getParam("DriverBrokerIP", roscore_ip);
00080   nh.getParam("network_interface", network_interface);
00081   setMasterURINet( "http://"+roscore_ip+":11311", network_interface);
00082 
00083   //create a session
00084   qi::SessionPtr session = qi::makeSession();
00085   try
00086   {
00087     std::stringstream strstr;
00088     strstr << "tcp://" << pip << ":" << pport;
00089     ROS_INFO_STREAM("Connecting to " << pip << ":" << pport);
00090     session->connect(strstr.str()).wait();
00091   }
00092   catch(const std::exception &e)
00093   {
00094     ROS_ERROR("Cannot connect to session, %s", e.what());
00095     session->close();
00096     return -1;
00097   }
00098 
00099   if (!session->connected)
00100   {
00101     ROS_ERROR("Cannot connect to session");
00102     session->close();
00103     return -1;
00104   }
00105 
00106   // Deal with ALBrokerManager singleton (add your broker into NAOqi)
00107   boost::shared_ptr<Robot> robot = boost::make_shared<Robot>(session);
00108 
00109   // stop ALTouch service to prevent the robot shaking
00110   try
00111   {
00112     qi::AnyObject touch_proxy = session->service("ALTouch");
00113     touch_proxy.call<void>("exit");
00114     ROS_INFO_STREAM("Naoqi Touch service is shut down");
00115   }
00116   catch (const std::exception& e)
00117   {
00118     ROS_DEBUG("Did not stop ALTouch: %s", e.what());
00119   }
00120 
00121   // stop AutonomousLife service to prevent the robot shaking
00122   try
00123   {
00124     qi::AnyObject life_proxy = session->service("ALAutonomousLife");
00125     if (life_proxy.call<std::string>("getState") != "disabled")
00126     {
00127       life_proxy.call<void>("setState", "disabled");
00128       ROS_INFO_STREAM("Shutting down Naoqi AutonomousLife ...");
00129       ros::Duration(2.0).sleep();
00130     }
00131   }
00132   catch (const std::exception& e)
00133   {
00134     ROS_DEBUG("Did not stop AutonomousLife: %s", e.what());
00135   }
00136 
00137   session->registerService("naoqi_dcm_driver", robot);
00138   ros::Duration(0.1).sleep();
00139 
00140   if (!robot->connect())
00141   {
00142     session->close();
00143     return 0;
00144   }
00145 
00146   // Run the spinner in a separate thread to prevent lockups
00147   ros::AsyncSpinner spinner(1);
00148   spinner.start();
00149 
00150   // Run the main Loop
00151   robot->run();
00152 
00153   //release stiffness and stop correctly
00154   robot->stopService();
00155 
00156   //close the session
00157   session->close();
00158   spinner.stop();
00159 
00160   return 0;
00161 }


naoqi_dcm_driver
Author(s): Konstantinos Chatzilygeroudis , Mikael Arguedas , Karsten Knese , Natalia Lyubova
autogenerated on Thu Jun 6 2019 20:50:50