Go to the documentation of this file.00001 #include <iostream>
00002 #include <stdio.h>
00003
00004 #include <boost/bind.hpp>
00005 #include <gazebo/gazebo.hh>
00006 #include <gazebo/physics/physics.hh>
00007 #include <gazebo/common/common.hh>
00008 #include <gazebo/common/Time.hh>
00009
00010 #include <ros/ros.h>
00011 #include <ros/callback_queue.h>
00012 #include <ros/advertise_options.h>
00013 #include <ros/subscribe_options.h>
00014
00015 #include <geometry_msgs/Vector3.h>
00016 #include <std_msgs/String.h>
00017 #include <tf/transform_broadcaster.h>
00018
00019 #include "PubQueue.h"
00020
00021
00022 namespace gazebo
00023 {
00024 class PubTf : public ModelPlugin
00025 {
00026
00027 public:
00028
00029 void Load(physics::ModelPtr _parent, sdf::ElementPtr _sdf)
00030 {
00031
00032 this->model = _parent;
00033
00034
00035 this->obj_name = "";
00036 if (_sdf->HasElement("objname")) {
00037 this->obj_name = _sdf->Get<std::string>("objname");
00038 }
00039 this->link_name = "root";
00040 if (_sdf->HasElement("linkname")) {
00041 this->link_name = _sdf->Get<std::string>("linkname");
00042 }
00043
00044
00045 this->link = this->model->GetLink(this->link_name);
00046 if(!this->link) {
00047 gzerr << "Root link are not found. (link_name is "<< this->link_name << ")" << std::endl;
00048 return;
00049 }
00050 world = this->model->GetWorld();
00051
00052
00053 if (!ros::isInitialized()) {
00054 gzerr << "A ROS node for Gazebo has not been initialized, unable to load plugin. "
00055 << "Load the Gazebo system plugin 'libgazebo_ros_api_plugin.so' in the gazebo_ros package)";
00056 return;
00057 }
00058
00059 this->rosNode = new ros::NodeHandle("");
00060
00061 this->deferredLoadThread = boost::thread(boost::bind(&PubTf::DeferredLoad, this));
00062 }
00063
00064 void DeferredLoad() {
00065
00066 this->pmq.startServiceThread();
00067
00068
00069 this->updateConnection = event::Events::ConnectWorldUpdateBegin(boost::bind(&PubTf::OnUpdate, this, _1));
00070
00071 gzmsg << "PubTfPlugin was loaded !" << std::endl;
00072 }
00073
00074
00075 void OnUpdate(const common::UpdateInfo & )
00076 {
00077 common::Time curTime = this->world->GetSimTime();
00078
00079
00080 this->PublishTf(curTime);
00081 }
00082
00083
00084 void PublishTf(const common::Time &_curTime)
00085 {
00086 math::Pose pose;
00087 tf::Transform transform;
00088
00089
00090 pose = this->link->GetWorldPose();
00091 transform.setOrigin(tf::Vector3(pose.pos.x, pose.pos.y, pose.pos.z));
00092 tf::Quaternion q(pose.rot.x, pose.rot.y, pose.rot.z, pose.rot.w);
00093 transform.setRotation(q);
00094
00095 this->br.sendTransform(tf::StampedTransform(transform, ros::Time(_curTime.sec, _curTime.nsec), "gazebo_world", this->link_name));
00096 }
00097
00098 private:
00099 physics::ModelPtr model;
00100 physics::WorldPtr world;
00101 std::string obj_name;
00102 std::string link_name;
00103 physics::LinkPtr link;
00104 event::ConnectionPtr updateConnection;
00105
00106 ros::NodeHandle* rosNode;
00107 PubMultiQueue pmq;
00108 boost::thread deferredLoadThread;
00109
00110 tf::TransformBroadcaster br;
00111 };
00112
00113 GZ_REGISTER_MODEL_PLUGIN(PubTf)
00114 }