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 <algorithm>
00030 #include <assert.h>
00031
00032 #include <gazebo_plugins/gazebo_ros_step_world_state.h>
00033
00034 #include <gazebo/Global.hh>
00035 #include <gazebo/XMLConfig.hh>
00036 #include <gazebo/gazebo.h>
00037 #include <gazebo/GazeboError.hh>
00038 #include <gazebo/ControllerFactory.hh>
00039
00040 #include <boost/thread.hpp>
00041 #include <boost/bind.hpp>
00042
00043 using namespace gazebo;
00044
00045 GZ_REGISTER_DYNAMIC_CONTROLLER("gazebo_ros_step_world_state", GazeboRosStepWorldState);
00046
00048
00049 GazeboRosStepWorldState::GazeboRosStepWorldState(Entity *parent)
00050 : Controller(parent)
00051 {
00052 this->parent_model_ = dynamic_cast<Model*>(this->parent);
00053
00054 if (!this->parent_model_)
00055 gzthrow("GazeboMechanismControl controller requires a Model as its parent");
00056
00057 Param::Begin(&this->parameters);
00058 this->robotNamespaceP = new ParamT<std::string>("robotNamespace", "/", 0);
00059 this->topicNameP = new ParamT<std::string>("topicName", "", 1);
00060 this->frameNameP = new ParamT<std::string>("frameName", "base_link", 0);
00061 Param::End();
00062
00063 this->all_bodies.clear();
00064 this->models.clear();
00065 }
00066
00068
00069 GazeboRosStepWorldState::~GazeboRosStepWorldState()
00070 {
00071 delete this->robotNamespaceP;
00072 delete this->topicNameP;
00073 delete this->frameNameP;
00074 delete this->rosnode_;
00075 }
00076
00078
00079 void GazeboRosStepWorldState::LoadChild(XMLConfigNode *node)
00080 {
00081 this->robotNamespaceP->Load(node);
00082 this->robotNamespace = this->robotNamespaceP->GetValue();
00083
00084 if (!ros::isInitialized())
00085 {
00086 int argc = 0;
00087 char** argv = NULL;
00088 ros::init(argc,argv,"gazebo",ros::init_options::NoSigintHandler|ros::init_options::AnonymousName);
00089 }
00090 this->rosnode_ = new ros::NodeHandle(this->robotNamespace);
00091
00092 this->topicNameP->Load(node);
00093 this->topicName = this->topicNameP->GetValue();
00094 this->frameNameP->Load(node);
00095 this->frameName = this->frameNameP->GetValue();
00096
00097
00098 ros::SubscribeOptions so = ros::SubscribeOptions::create<gazebo_plugins::WorldState>(
00099 this->topicName,1,
00100 boost::bind( &GazeboRosStepWorldState::WorldStateCallback,this,_1),
00101 ros::VoidPtr(), &this->queue_);
00102 this->sub_ = this->rosnode_->subscribe(so);
00103
00104 }
00105
00107
00108 void GazeboRosStepWorldState::WorldStateCallback(const gazebo_plugins::WorldStateConstPtr& worldStateMsg)
00109 {
00110 ROS_DEBUG("received state message");
00111
00112
00113
00114 gazebo::Simulator::Instance()->SetSimTime(worldStateMsg->header.stamp.toSec());
00115
00116 int object_count = worldStateMsg->name.size();
00117
00118 for (int count = 0; count < object_count; count++)
00119 {
00120 boost::recursive_mutex::scoped_lock lock(*gazebo::Simulator::Instance()->GetMRMutex());
00121 std::map<std::string,gazebo::Body*>::iterator body = this->all_bodies.find(worldStateMsg->name[count]);
00122 if (body == this->all_bodies.end())
00123 {
00124
00125 }
00126 else
00127 {
00128
00129 Vector3 pos;
00130 Quatern rot;
00131 pos.x = worldStateMsg->pose[count].position.x;
00132 pos.y = worldStateMsg->pose[count].position.y;
00133 pos.z = worldStateMsg->pose[count].position.z;
00134 rot.x = worldStateMsg->pose[count].orientation.x;
00135 rot.y = worldStateMsg->pose[count].orientation.y;
00136 rot.z = worldStateMsg->pose[count].orientation.z;
00137 rot.u = worldStateMsg->pose[count].orientation.w;
00138 body->second->SetWorldPose(Pose3d(pos,rot));
00139 }
00140 }
00141
00142 }
00143
00145
00146 void GazeboRosStepWorldState::InitChild()
00147 {
00148
00149 this->callback_queue_thread_ = boost::thread( boost::bind( &GazeboRosStepWorldState::QueueThread,this ) );
00150 }
00151
00153
00154 void GazeboRosStepWorldState::UpdateChild()
00155 {
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167 if (this->models.size() != gazebo::World::Instance()->GetModels().size())
00168 {
00169 this->models = gazebo::World::Instance()->GetModels();
00170
00171
00172 for (std::vector<gazebo::Model*>::iterator miter = this->models.begin(); miter != this->models.end(); miter++)
00173 {
00174
00175 const std::vector<gazebo::Entity*> entities = (*miter)->GetChildren();
00176
00177 std::vector<Entity*>::const_iterator eiter;
00178 for (eiter=entities.begin(); eiter!=entities.end(); eiter++)
00179 {
00180 gazebo::Body* body = dynamic_cast<gazebo::Body*>(*eiter);
00181 if (body)
00182 this->all_bodies.insert(make_pair(body->GetName(),body));
00183 }
00184 }
00185 }
00186
00187
00188 }
00189
00191
00192 void GazeboRosStepWorldState::FiniChild()
00193 {
00194
00195 this->queue_.clear();
00196 this->queue_.disable();
00197 this->rosnode_->shutdown();
00198 this->callback_queue_thread_.join();
00199 }
00200
00201
00203
00204 void GazeboRosStepWorldState::QueueThread()
00205 {
00206 static const double timeout = 0.01;
00207
00208 while (this->rosnode_->ok())
00209 {
00210 this->queue_.callAvailable(ros::WallDuration(timeout));
00211 }
00212 }
00213
00214