external_registration.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2015 Aldebaran
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 
19 #include <qi/applicationsession.hpp>
20 #include <qi/anymodule.hpp>
22 
23 #include <boost/program_options.hpp>
24 
25 #include "naoqi_env.hpp"
27 
28 #if LIBQI_VERSION >= 29
29 #include "driver_authenticator.hpp"
30 #endif
31 
32 
33 int main(int argc, char** argv)
34 {
35  const std::string no_password = "no_password";
36  std::string protocol = "tcp://";
37 
38  /* launch naoqi service */
39  qi::ApplicationSession app(argc, argv);
40  /* In case you launch via roslaunch/rosrun we remove the ros args */
41  std::vector<std::string> args_out;
42  ros::removeROSArgs( argc, argv, args_out );
43 
44  namespace po = boost::program_options;
45  po::options_description desc("Options");
46  desc.add_options()
47  ("help,h", "print help message")
48  ("nao_ip", po::value<std::string>(), "the ip of the robot")
49  ("nao_port", po::value<int>(), "the ip of the robot")
50  ("user,u", po::value<std::string>(), "the user profile on the robot, nao by default")
51  ("password,p", po::value<std::string>(), "the password of the robot")
52  ("roscore_ip,r", po::value<std::string>(), "set the ip of the roscore to use")
53  ("network_interface,i", po::value<std::string>()->default_value("eth0"), "set the network interface over which to connect")
54  ("namespace,n", po::value<std::string>()->default_value("naoqi_driver_node"), "set an explicit namespace in case ROS namespace variables cannot be used");
55 
56  po::variables_map vm;
57  try
58  {
59  po::store( po::parse_command_line(argc, argv, desc), vm );
60  }
61  catch (boost::program_options::invalid_command_line_syntax& e)
62  {
63  std::cout << "error " << e.what() << std::endl;
64  throw ros::Exception(e.what());
65  }
66  catch (boost::program_options::unknown_option& e)
67  {
68  std::cout << "error 2 " << e.what() << std::endl;
69  throw ros::Exception(e.what());
70  }
71 
72  if( vm.count("help") )
73  {
74  std::cout << "This is the help message for the ROS-Driver C++ bridge" << std::endl << desc << std::endl;
75  exit(0);
76  }
77 
78  if (vm["password"].as<std::string>().compare(no_password) != 0) {
79 #if LIBQI_VERSION>=29
80  protocol = "tcps://";
82  factory->user = vm["user"].as<std::string>();
83  factory->pass = vm["password"].as<std::string>();
84  app.session()->setClientAuthenticatorFactory(qi::ClientAuthenticatorFactoryPtr(factory));
85 #else
86  std::cout << BOLDRED
87  << "No need to set a password"
88  << RESETCOLOR
89  << std::endl;
90 #endif
91  }
92 
93  qi::Url url(protocol + vm["nao_ip"].as<std::string>() + ":" + std::to_string(vm["nao_port"].as<int>()));
94  qi::Future<void> connection = app.session()->connect(url);
95 
96  if (connection.hasError()) {
97  std::cout << BOLDRED << connection.error() << RESETCOLOR << std::endl;
98  return 0;
99  }
100 
101  boost::shared_ptr<naoqi::Driver> bs = boost::make_shared<naoqi::Driver>(app.session(), vm["namespace"].as<std::string>());
102  app.session()->registerService("ROS-Driver", bs);
103 
104  // set ros paramters directly upfront if available
105  if ( vm.count("roscore_ip") )
106  {
107  std::string roscore_ip = vm["roscore_ip"].as<std::string>();
108  std::string network_interface = vm["network_interface"].as<std::string>();
109 
110  std::cout << BOLDYELLOW << "using ip address: "
111  << BOLDCYAN << roscore_ip << " @ " << network_interface << RESETCOLOR << std::endl;
112  bs->init();
113  bs->setMasterURINet( "http://"+roscore_ip+":11311", network_interface);
114  }
115  else
116  {
117  std::cout << BOLDRED << "No ip address given. Run qicli call to set the master uri" << RESETCOLOR << std::endl;
118  bs->init();
119  }
120 
121  std::cout << BOLDYELLOW << "naoqi_driver initialized" << RESETCOLOR << std::endl;
122  app.run();
123  bs->stopService();
124  app.session()->close();
125  return 0;
126 }
app
#define BOLDRED
Definition: tools.hpp:25
#define RESETCOLOR
Definition: tools.hpp:22
int main(int argc, char **argv)
#define BOLDCYAN
Definition: tools.hpp:28
#define BOLDYELLOW
Definition: tools.hpp:27
ROSCPP_DECL void removeROSArgs(int argc, const char *const *argv, V_string &args_out)


naoqi_driver
Author(s): Karsten Knese
autogenerated on Sat Jul 1 2023 02:53:24