ros_plugin.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Ruben Smits Tue Nov 16 09:19:02 CET 2010 ros_plugin.cpp
3 
4  ros_plugin.cpp - description
5  -------------------
6  begin : Tue November 16 2010
7  copyright : (C) 2010 Ruben Smits
8  email : first.last@mech.kuleuven.be
9 
10  ***************************************************************************
11  * This library is free software; you can redistribute it and/or *
12  * modify it under the terms of the GNU Lesser General Public *
13  * License as published by the Free Software Foundation; either *
14  * version 2.1 of the License, or (at your option) any later version. *
15  * *
16  * This library is distributed in the hope that it will be useful, *
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
19  * Lesser General Public License for more details. *
20  * *
21  * You should have received a copy of the GNU Lesser General Public *
22  * License along with this library; if not, write to the Free Software *
23  * Foundation, Inc., 59 Temple Place, *
24  * Suite 330, Boston, MA 02111-1307 USA *
25  * *
26  ***************************************************************************/
27 
28 
29 #include <rtt/plugin/Plugin.hpp>
30 #include <rtt/TaskContext.hpp>
31 #include <rtt/Activity.hpp>
33 #include <rtt/Logger.hpp>
34 #include <rtt/os/startstop.h>
35 #include <ros/ros.h>
36 
37 using namespace RTT;
38 
39 static void loadROSService()
40 {
42 
43  ros->addOperation("getNodeName", &ros::this_node::getName)
44  .doc("Return full name of ROS node.");
45  ros->addOperation("getNamespace", &ros::this_node::getNamespace)
46  .doc("Return ROS node namespace.");
47 }
48 
49 extern "C" {
51 
52  // Initialize ROS if necessary
53  if (!ros::isInitialized()) {
54  log(Info) << "Initializing ROS node in Orocos plugin..." << endlog();
55 
56  int argc = __os_main_argc();
57  char ** argv = __os_main_argv();
58 
59  // copy the argv array of C strings into a std::vector<char *>
60  // Rationale: ros::init(int &argc, char **argv) removes some of the
61  // command line arguments and rearranges the remaining ones in the argv
62  // vector.
63  // See https://github.com/orocos/rtt_ros_integration/issues/54
64  std::vector<char *> argvv(argv, argv + argc);
65  assert(argvv.size() == argc);
66  ros::init(argc, argvv.data(), "rtt", ros::init_options::AnonymousName);
67  argvv.resize(argc);
68 
69  if (ros::master::check()) {
70  ros::start();
71  } else {
72  log(Warning) << "'roscore' is not running: no ROS functions will be available." << endlog();
73  ros::shutdown();
74  return true;
75  }
76 
77  // Register new operations in global ros Service
79  }
80 
81  // Defaults the number of threads to the number of CPUs available on the machine
82  int thread_count = 0;
83  // get number of spinners from parameter server, if available
84  ros::param::get("~spinner_threads", thread_count);
85 
86  // Create an asynchronous spinner to handle the default callback queue
87  static ros::AsyncSpinner spinner(thread_count); // Use thread_count threads
88 
89  // TODO: Check spinner.canStart() to suppress errors / warnings once it's incorporated into ROS
90  spinner.start();
91  log(Info) << "ROS node spinner started (" << thread_count << " " << (thread_count > 1 ? "threads" : "thread") << ")." << endlog();
92 
93  return true;
94  }
95 
96  std::string getRTTPluginName () {
97  return "rosnode";
98  }
99 
100  std::string getRTTTargetName () {
101  return OROCOS_TARGET_NAME;
102  }
103 }
104 
ROSCPP_DECL bool check()
static void loadROSService()
Definition: ros_plugin.cpp:39
ROSCPP_DECL void start()
std::string getRTTTargetName()
Definition: ros_plugin.cpp:100
ROSCPP_DECL bool isInitialized()
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
ROSCPP_DECL const std::string & getName()
int __os_main_argc(void)
char ** __os_main_argv(void)
ROSCPP_DECL const std::string & getNamespace()
ROSCPP_DECL bool get(const std::string &key, std::string &s)
bool loadRTTPlugin(RTT::TaskContext *c)
Definition: ros_plugin.cpp:50
std::string getRTTPluginName()
Definition: ros_plugin.cpp:96
ROSCPP_DECL void shutdown()
static Logger & log()
static Logger::LogFunction endlog()
static RTT_API Service::shared_ptr Instance()


rtt_rosnode
Author(s): Ruben Smits
autogenerated on Sat Jun 8 2019 18:05:03