Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
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
00094 this->model = _parent;
00095
00096
00097 this->world = _parent->GetWorld();
00098
00099
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
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
00120 GazeboActivity *activity = new GazeboActivity(name);
00121 activity->Load(world, _sdf);
00122 taskContext->setActivity(activity);
00123
00124
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
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
00163 if (!taskContext->configure()) {
00164 gzerr << "Failed to configure TaskContext " << taskContext->getName() << std::endl;
00165 return;
00166 }
00167
00168
00169 std::string robotNamespace = _sdf->GetValueString("robotNamespace");
00170
00171
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
00213 GZ_REGISTER_MODEL_PLUGIN(RTTPlugin)
00214
00215 }