ros_plugin.cpp
Go to the documentation of this file.
1 /*
2  * (C) 2010, Ruben Smits, ruben.smits@mech.kuleuven.be
3  * Department of Mechanical Engineering,
4  * Katholieke Universiteit Leuven, Belgium.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the copyright holder nor the names of its contributors
15  * may be used to endorse or promote products derived from this software
16  * without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <rtt/plugin/Plugin.hpp>
33 #include <rtt/TaskContext.hpp>
34 #include <rtt/Activity.hpp>
36 #include <rtt/Logger.hpp>
37 #include <rtt/os/startstop.h>
38 #include <ros/ros.h>
39 
40 using namespace RTT;
41 
42 static void loadROSService()
43 {
45 
46  ros->addOperation("getNodeName", &ros::this_node::getName)
47  .doc("Return full name of ROS node.");
48  ros->addOperation("getNamespace", &ros::this_node::getNamespace)
49  .doc("Return ROS node namespace.");
50 }
51 
52 extern "C" {
54 
55  // Initialize ROS if necessary
56  if (!ros::isInitialized()) {
57  log(Info) << "Initializing ROS node in Orocos plugin..." << endlog();
58 
59  int argc = __os_main_argc();
60  char ** argv = __os_main_argv();
61 
62  // copy the argv array of C strings into a std::vector<char *>
63  // Rationale: ros::init(int &argc, char **argv) removes some of the
64  // command line arguments and rearranges the remaining ones in the argv
65  // vector.
66  // See https://github.com/orocos/rtt_ros_integration/issues/54
67  std::vector<char *> argvv(argv, argv + argc);
68  assert(argvv.size() == argc);
69  ros::init(argc, argvv.data(), "rtt", ros::init_options::AnonymousName);
70  argvv.resize(argc);
71 
72  if (ros::master::check()) {
73  ros::start();
74  } else {
75  log(Warning) << "'roscore' is not running: no ROS functions will be available." << endlog();
76  ros::shutdown();
77  return true;
78  }
79 
80  // Register new operations in global ros Service
82  }
83 
84  // Defaults the number of threads to the number of CPUs available on the machine
85  int thread_count = 0;
86  // get number of spinners from parameter server, if available
87  ros::param::get("~spinner_threads", thread_count);
88 
89  // Create an asynchronous spinner to handle the default callback queue
90  static ros::AsyncSpinner spinner(thread_count); // Use thread_count threads
91 
92  // TODO: Check spinner.canStart() to suppress errors / warnings once it's incorporated into ROS
93  spinner.start();
94  log(Info) << "ROS node spinner started (" << thread_count << " " << (thread_count > 1 ? "threads" : "thread") << ")." << endlog();
95 
96  return true;
97  }
98 
99  std::string getRTTPluginName () {
100  return "rosnode";
101  }
102 
103  std::string getRTTTargetName () {
104  return OROCOS_TARGET_NAME;
105  }
106 }
ROSCPP_DECL bool check()
static void loadROSService()
Definition: ros_plugin.cpp:42
ROSCPP_DECL void start()
std::string getRTTTargetName()
Definition: ros_plugin.cpp:103
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:53
std::string getRTTPluginName()
Definition: ros_plugin.cpp:99
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 Mon May 10 2021 02:44:53