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 #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
00096 this->model = _parent;
00097
00098
00099 this->world = _parent->GetWorld();
00100
00101
00102 if (_sdf->HasElement("import")) {
00103 std::string package = _sdf->GetElement("import")->Get<std::string>();
00104
00105 bool success = false;
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
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
00140 GazeboActivity *activity = new GazeboActivity(name);
00141 activity->Load(world, _sdf);
00142 taskContext->setActivity(activity);
00143
00144
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
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
00189 if (!taskContext->configure()) {
00190 gzerr << "Failed to configure TaskContext " << taskContext->getName() << std::endl;
00191 return;
00192 }
00193
00194
00195 std::string robotNamespace;
00196 if (_sdf->HasElement("robotNamespace"))
00197 robotNamespace = _sdf->GetElement("robotNamespace")->Get<std::string>();
00198
00199
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
00241 GZ_REGISTER_MODEL_PLUGIN(RTTPlugin)
00242
00243 }