gazebo_rtt_plugin.cpp
Go to the documentation of this file.
00001 //=================================================================================================
00002 // Copyright (c) 2012, Johannes Meyer, TU Darmstadt
00003 // All rights reserved.
00004 
00005 // Redistribution and use in source and binary forms, with or without
00006 // modification, are permitted provided that the following conditions are met:
00007 //     * Redistributions of source code must retain the above copyright
00008 //       notice, this list of conditions and the following disclaimer.
00009 //     * Redistributions in binary form must reproduce the above copyright
00010 //       notice, this list of conditions and the following disclaimer in the
00011 //       documentation and/or other materials provided with the distribution.
00012 //     * Neither the name of the Flight Systems and Automatic Control group,
00013 //       TU Darmstadt, nor the names of its contributors may be used to
00014 //       endorse or promote products derived from this software without
00015 //       specific prior written permission.
00016 
00017 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00018 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00019 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00020 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
00021 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00022 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00023 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00024 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00026 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027 //=================================================================================================
00028 
00029 #include <gazebo_rtt_plugin/gazebo_rtt_plugin.h>
00030 #include <gazebo_rtt_plugin/gazebo_activity.h>
00031 
00032 #include <rtt/deployment/ComponentLoader.hpp>
00033 #include <rtt/os/main.h>
00034 
00035 #include <rtt/os/targets/gnulinux-config.h>
00036 #ifdef PLUGINS_ENABLE_MARSHALLING
00037   #include <rtt/marsh/Marshalling.hpp>
00038 #endif
00039 
00040 #include <rtt/internal/GlobalService.hpp>
00041 
00042 #ifndef ORO_ROS_PROTOCOL_ID
00043   #define ORO_ROS_PROTOCOL_ID 3
00044 #endif
00045 
00046 #include <ros/names.h>
00047 
00048 namespace gazebo
00049 {
00050 
00051 class RTTPlugin::RTTOSInitializer
00052 {
00053 public:
00054   RTTOSInitializer() {
00055     __os_init(0, 0);
00056     RTT::Logger::Instance()->setLogLevel(RTT::Logger::Info);
00057     RTT::os::TimeService::Instance()->enableSystemClock(false);
00058   }
00059 
00060   ~RTTOSInitializer() {
00061     __os_exit();
00062   }
00063 
00064   static boost::shared_ptr<RTTOSInitializer> Instance() {
00065     boost::shared_ptr<RTTOSInitializer> ptr;
00066     if (weak_ptr.expired()) {
00067       ptr.reset(new RTTOSInitializer());
00068       weak_ptr = ptr;
00069     } else {
00070       ptr = weak_ptr.lock();
00071     }
00072     return ptr;
00073   }
00074 
00075 private:
00076   static boost::weak_ptr<RTTPlugin::RTTOSInitializer> weak_ptr;
00077 };
00078 boost::weak_ptr<RTTPlugin::RTTOSInitializer> RTTPlugin::RTTOSInitializer::weak_ptr;
00079 
00080 RTTPlugin::RTTPlugin()
00081   : rttOsInitializer(RTTOSInitializer::Instance())
00082 {
00083 }
00084 
00085 RTTPlugin::~RTTPlugin()
00086 {
00087   if (taskContext) {
00088     taskContext->stop();
00089     taskContext->cleanup();
00090   }
00091 }
00092 
00093 void RTTPlugin::Load(physics::ModelPtr _parent, sdf::ElementPtr _sdf)
00094 {
00095   // Store the pointer to the model
00096   this->model = _parent;
00097 
00098   // Store the pointer to the world
00099   this->world = _parent->GetWorld();
00100 
00101   // Load RTT component packages and libraries
00102   if (_sdf->HasElement("import")) {
00103     std::string package = _sdf->GetElement("import")->Get<std::string>();
00104 
00105     bool success = false; /* RTT::ComponentLoader::Instance()->import(package, ""); */
00106     if (!success) {
00107       if (!RTT::internal::GlobalService::Instance()->hasService("ros")) {
00108         RTT::ComponentLoader::Instance()->import("rtt_ros", "");
00109       }
00110       RTT::ServicePtr ros_service = RTT::internal::GlobalService::Instance()->getService("ros");
00111 
00112       if (ros_service) {
00113         RTT::OperationCaller<bool(std::string)> import = ros_service->getOperation("import");
00114         if (import.ready()) success = import(package);
00115       }
00116     }
00117 
00118     if (!success) {
00119       gzerr << "Could not import ROS package " << package << " using the ros.import service!" << std::endl;
00120     }
00121   }
00122   if (_sdf->HasElement("library")) {
00123     RTT::ComponentLoader::Instance()->loadLibrary(_sdf->GetElement("library")->Get<std::string>());
00124   }
00125 
00126   // Create an instance of the component
00127   std::string component;
00128   if (_sdf->HasElement("component")) component = _sdf->GetElement("component")->Get<std::string>();
00129   std::string name;
00130   if (_sdf->HasElement("name")) name = _sdf->GetElement("name")->Get<std::string>();
00131   if (name.empty()) name = component;
00132 
00133   taskContext.reset(RTT::ComponentLoader::Instance()->loadComponent(name, component));
00134   if (!taskContext) {
00135     gzerr << "Could not create a TaskContext for component " << component << std::endl;
00136     return;
00137   }
00138 
00139   // set Activity
00140   GazeboActivity *activity = new GazeboActivity(name);
00141   activity->Load(world, _sdf);
00142   taskContext->setActivity(activity);
00143 
00144   // load configuration settings from marshalling
00145 #ifdef PLUGINS_ENABLE_MARSHALLING
00146   if (_sdf->HasElement("configuration")) {
00147     boost::shared_ptr<RTT::Marshalling> marshalling = taskContext->getProvider<RTT::Marshalling>("marshalling");
00148     if (marshalling)
00149       marshalling->loadProperties(_sdf->GetElement("configuration")->Get<std::string>());
00150     else
00151       gzerr << "Could not load component configuration from " << _sdf->GetElement("configuration")->Get<std::string>() << std::endl;
00152   }
00153 #endif
00154 
00155   // set TaskContext's properties
00156   if (_sdf->HasElement("property")) {
00157     sdf::ElementPtr property = _sdf->GetElement("property");
00158     while(property) {
00159       std::string name = property->Get<std::string>("name");
00160       RTT::base::PropertyBase *prop = taskContext->getProperty(name);
00161       if (!prop) {
00162         gzwarn << "Component '" << taskContext->getName() << "' has no property named '" << name << "'" << std::endl;
00163         property = property->GetNextElement("property");
00164         continue;
00165       }
00166 
00167       if (prop->getType() == "string")
00168         RTT::Property<std::string>(prop).set(property->Get<std::string>());
00169       else if (prop->getType() == "double")
00170         RTT::Property<double>(prop).set(property->Get<double>());
00171       else if (prop->getType() == "float")
00172         RTT::Property<float>(prop).set(property->Get<float>());
00173       else if (prop->getType() == "int")
00174         RTT::Property<int>(prop).set(property->Get<int>());
00175       else if (prop->getType() == "uint")
00176         RTT::Property<uint>(prop).set(property->Get<uint>());
00177       else if (prop->getType() == "char")
00178         RTT::Property<char>(prop).set(property->Get<char>());
00179       else if (prop->getType() == "bool")
00180         RTT::Property<bool>(prop).set(property->Get<bool>());
00181       else
00182         gzerr << "Property " << name << " has an unknown type. Will not use it." << std::endl;
00183 
00184       property = property->GetNextElement("property");
00185     }
00186   }
00187 
00188   // configure the TaskContext
00189   if (!taskContext->configure()) {
00190     gzerr << "Failed to configure TaskContext " << taskContext->getName() << std::endl;
00191     return;
00192   }
00193 
00194   // get robot namespace
00195   std::string robotNamespace;
00196   if (_sdf->HasElement("robotNamespace"))
00197     robotNamespace = _sdf->GetElement("robotNamespace")->Get<std::string>();
00198 
00199   // create Streams
00200   sdf::ElementPtr port = _sdf->GetElement("port");
00201   while(port) {
00202     std::string name = port->Get<std::string>("name");
00203     RTT::base::PortInterface *port_interface = taskContext->getPort(name);
00204     if (!port_interface) {
00205       gzwarn << "Component '" << taskContext->getName() << "' has no port named '" << name << "'" << std::endl;
00206       port = port->GetNextElement("port");
00207       continue;
00208     }
00209 
00210     RTT::ConnPolicy conn_policy;
00211     conn_policy.transport = ORO_ROS_PROTOCOL_ID;
00212 
00213     if (port->HasAttribute("topic") || port->HasElement("topic"))
00214       conn_policy.name_id = port->Get<std::string>("topic");
00215     else
00216       conn_policy.name_id = name;
00217     if (port->HasAttribute("queue_size") || port->HasElement("queue_size"))
00218       conn_policy.size = port->Get<int>("queue_size");
00219     conn_policy.type = conn_policy.size > 1 ? RTT::ConnPolicy::buffer(0).type : RTT::ConnPolicy::data().type;
00220 
00221     conn_policy.name_id = ros::names::resolve(robotNamespace, conn_policy.name_id, false);
00222     port_interface->createStream(conn_policy);
00223     port = port->GetNextElement("port");
00224   }
00225 }
00226 
00227 void RTTPlugin::Init()
00228 {
00229   if (!taskContext) return;
00230   taskContext->start();
00231 }
00232 
00233 void RTTPlugin::Reset()
00234 {
00235   if (!taskContext) return;
00236   taskContext->stop();
00237   Init();
00238 }
00239 
00240 // Register this plugin with the simulator
00241 GZ_REGISTER_MODEL_PLUGIN(RTTPlugin)
00242 
00243 } // namespace gazebo


gazebo_rtt_plugin
Author(s): Johannes Meyer
autogenerated on Thu Mar 27 2014 00:38:43