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 #ifndef ORO_ROS_PROTOCOL_ID
00041   #define ORO_ROS_PROTOCOL_ID 3
00042 #endif
00043 
00044 #include <ros/names.h>
00045 
00046 namespace gazebo
00047 {
00048 
00049 class RTTPlugin::RTTOSInitializer
00050 {
00051 public:
00052   RTTOSInitializer() {
00053     __os_init(0, 0);
00054     RTT::Logger::Instance()->setLogLevel(RTT::Logger::Info);
00055     RTT::os::TimeService::Instance()->enableSystemClock(false);
00056   }
00057 
00058   ~RTTOSInitializer() {
00059     __os_exit();
00060   }
00061 
00062   static boost::shared_ptr<RTTOSInitializer> Instance() {
00063     boost::shared_ptr<RTTOSInitializer> ptr;
00064     if (weak_ptr.expired()) {
00065       ptr.reset(new RTTOSInitializer());
00066       weak_ptr = ptr;
00067     } else {
00068       ptr = weak_ptr.lock();
00069     }
00070     return ptr;
00071   }
00072 
00073 private:
00074   static boost::weak_ptr<RTTPlugin::RTTOSInitializer> weak_ptr;
00075 };
00076 boost::weak_ptr<RTTPlugin::RTTOSInitializer> RTTPlugin::RTTOSInitializer::weak_ptr;
00077 
00078 RTTPlugin::RTTPlugin()
00079   : rttOsInitializer(RTTOSInitializer::Instance())
00080 {
00081 }
00082 
00083 RTTPlugin::~RTTPlugin()
00084 {
00085   if (taskContext) {
00086     taskContext->stop();
00087     taskContext->cleanup();
00088   }
00089 }
00090 
00091 void RTTPlugin::Load(physics::ModelPtr _parent, sdf::ElementPtr _sdf)
00092 {
00093   // Store the pointer to the model
00094   this->model = _parent;
00095 
00096   // Store the pointer to the world
00097   this->world = _parent->GetWorld();
00098 
00099   // Load RTT component packages and libraries
00100   RTT::ComponentLoader::Instance()->import("rtt_rosnode", "");
00101   if (_sdf->HasElement("import")) {
00102     RTT::ComponentLoader::Instance()->import(_sdf->GetValueString("import"), "");
00103   }
00104   if (_sdf->HasElement("library")) {
00105     RTT::ComponentLoader::Instance()->loadLibrary(_sdf->GetValueString("library"));
00106   }
00107 
00108   // Create an instance of the component
00109   std::string component = _sdf->GetValueString("component");
00110   std::string name = _sdf->GetValueString("name");
00111   if (name.empty()) name = component;
00112 
00113   taskContext.reset(RTT::ComponentLoader::Instance()->loadComponent(name, component));
00114   if (!taskContext) {
00115     gzerr << "Could not create a TaskContext for component " << component << std::endl;
00116     return;
00117   }
00118 
00119   // set Activity
00120   GazeboActivity *activity = new GazeboActivity(name);
00121   activity->Load(world, _sdf);
00122   taskContext->setActivity(activity);
00123 
00124   // load configuration settings from marshalling
00125 #ifdef PLUGINS_ENABLE_MARSHALLING
00126   if (_sdf->HasElement("configuration")) {
00127     taskContext->getProvider<RTT::Marshalling>("marshalling")->loadProperties(_sdf->GetValueString("configuration"));
00128   }
00129 #endif
00130 
00131   // set TaskContext's properties
00132   sdf::ElementPtr property = _sdf->GetElement("property");
00133   while(property) {
00134     std::string name = property->GetValueString("name");
00135     RTT::base::PropertyBase *prop = taskContext->getProperty(name);
00136     if (!prop) {
00137       gzwarn << "Component '" << taskContext->getName() << "' has no property named '" << name << "'" << std::endl;
00138       property = property->GetNextElement("property");
00139       continue;
00140     }
00141 
00142     if (prop->getType() == "string")
00143       RTT::Property<std::string>(prop).set(property->GetValueString());
00144     else if (prop->getType() == "double")
00145       RTT::Property<double>(prop).set(property->GetValueDouble());
00146     else if (prop->getType() == "float")
00147       RTT::Property<float>(prop).set(property->GetValueFloat());
00148     else if (prop->getType() == "int")
00149       RTT::Property<int>(prop).set(property->GetValueInt());
00150     else if (prop->getType() == "uint")
00151       RTT::Property<uint>(prop).set(property->GetValueUInt());
00152     else if (prop->getType() == "char")
00153       RTT::Property<char>(prop).set(property->GetValueChar());
00154     else if (prop->getType() == "bool")
00155       RTT::Property<bool>(prop).set(property->GetValueBool());
00156     else
00157       gzerr << "Property " << name << " has an unknown type. Will not use it." << std::endl;
00158 
00159     property = property->GetNextElement("property");
00160   }
00161 
00162   // configure the TaskContext
00163   if (!taskContext->configure()) {
00164     gzerr << "Failed to configure TaskContext " << taskContext->getName() << std::endl;
00165     return;
00166   }
00167 
00168   // get robot namespace
00169   std::string robotNamespace = _sdf->GetValueString("robotNamespace");
00170 
00171   // create Streams
00172   sdf::ElementPtr port = _sdf->GetElement("port");
00173   while(port) {
00174     std::string name = port->GetValueString("name");
00175     RTT::base::PortInterface *port_interface = taskContext->getPort(name);
00176     if (!port_interface) {
00177       gzwarn << "Component '" << taskContext->getName() << "' has no port named '" << name << "'" << std::endl;
00178       port = port->GetNextElement("port");
00179       continue;
00180     }
00181 
00182     RTT::ConnPolicy conn_policy;
00183     conn_policy.transport = ORO_ROS_PROTOCOL_ID;
00184 
00185     if (port->HasAttribute("topic") || port->HasElement("topic"))
00186       conn_policy.name_id = port->GetValueString("topic");
00187     else
00188       conn_policy.name_id = name;
00189     if (port->HasAttribute("queue_size") || port->HasElement("queue_size"))
00190       conn_policy.size = boost::lexical_cast<int>(port->GetValueString("queue_size"));
00191     conn_policy.type = conn_policy.size > 1 ? RTT::ConnPolicy::BUFFER : RTT::ConnPolicy::DATA;
00192 
00193     conn_policy.name_id = ros::names::resolve(robotNamespace, conn_policy.name_id, false);
00194     port_interface->createStream(conn_policy);
00195     port = port->GetNextElement("port");
00196   }
00197 }
00198 
00199 void RTTPlugin::Init()
00200 {
00201   if (!taskContext) return;
00202   taskContext->start();
00203 }
00204 
00205 void RTTPlugin::Reset()
00206 {
00207   if (!taskContext) return;
00208   taskContext->stop();
00209   Init();
00210 }
00211 
00212 // Register this plugin with the simulator
00213 GZ_REGISTER_MODEL_PLUGIN(RTTPlugin)
00214 
00215 } // namespace gazebo


gazebo_rtt_plugin
Author(s): Johannes Meyer
autogenerated on Mon Oct 6 2014 00:23:23