ros_plugin.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002   tag: Ruben Smits  Tue Nov 16 09:19:02 CET 2010  ros_plugin.cpp
00003 
00004                         ros_plugin.cpp -  description
00005                            -------------------
00006     begin                : Tue November 16 2010
00007     copyright            : (C) 2010 Ruben Smits
00008     email                : first.last@mech.kuleuven.be
00009 
00010  ***************************************************************************
00011  *   This library is free software; you can redistribute it and/or         *
00012  *   modify it under the terms of the GNU Lesser General Public            *
00013  *   License as published by the Free Software Foundation; either          *
00014  *   version 2.1 of the License, or (at your option) any later version.    *
00015  *                                                                         *
00016  *   This library is distributed in the hope that it will be useful,       *
00017  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00018  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00019  *   Lesser General Public License for more details.                       *
00020  *                                                                         *
00021  *   You should have received a copy of the GNU Lesser General Public      *
00022  *   License along with this library; if not, write to the Free Software   *
00023  *   Foundation, Inc., 59 Temple Place,                                    *
00024  *   Suite 330, Boston, MA  02111-1307  USA                                *
00025  *                                                                         *
00026  ***************************************************************************/
00027 
00028 
00029 #include <rtt/plugin/Plugin.hpp>
00030 #include <rtt/TaskContext.hpp>
00031 #include <rtt/Activity.hpp>
00032 #include <rtt/Logger.hpp>
00033 #include <rtt/os/startstop.h>
00034 #include <ros/ros.h>
00035 
00036 using namespace RTT;
00037 
00038 extern "C" {
00039   bool loadRTTPlugin(RTT::TaskContext* c){
00040 
00041     // Initialize ROS if necessary
00042     if (!ros::isInitialized()) {
00043       log(Info) << "Initializing ROS node in Orocos plugin..." << endlog();
00044 
00045       int argc = __os_main_argc();
00046       char ** argv = __os_main_argv();
00047 
00048       // copy the argv array of C strings into a std::vector<char *>
00049       // Rationale: ros::init(int &argc, char **argv) removes some of the
00050       // command line arguments and rearranges the remaining ones in the argv
00051       // vector.
00052       // See https://github.com/orocos/rtt_ros_integration/issues/54
00053       std::vector<char *> argvv(argv, argv + argc);
00054       assert(argvv.size() == argc);
00055       ros::init(argc, argvv.data(), "rtt", ros::init_options::AnonymousName);
00056       argvv.resize(argc);
00057 
00058       if (ros::master::check()) {
00059         ros::start();
00060       } else {
00061         log(Warning) << "'roscore' is not running: no ROS functions will be available." << endlog();
00062         ros::shutdown();
00063         return true;
00064       }
00065     }
00066 
00067     // get number of spinners from parameter server, if available
00068     int thread_count = 1;
00069     ros::param::get("~spinner_threads", thread_count);
00070 
00071     // Create an asynchronous spinner to handle the default callback queue 
00072     static ros::AsyncSpinner spinner(thread_count); // Use thread_count threads
00073 
00074     // TODO: Check spinner.canStart() to suppress errors / warnings once it's incorporated into ROS
00075     spinner.start();
00076     log(Info) << "ROS node spinner started (" << thread_count << " " << (thread_count > 1 ? "threads" : "thread") << ")." << endlog();
00077 
00078     return true;
00079   }
00080 
00081   std::string getRTTPluginName () {
00082     return "rosnode";
00083   }
00084 
00085   std::string getRTTTargetName () {
00086     return OROCOS_TARGET_NAME;
00087   }
00088 }
00089 


rtt_rosnode
Author(s): Ruben Smits
autogenerated on Mon Apr 3 2017 03:34:26