move_model_test.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2011 Nate Koenig & Andrew Howard
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  *
00016 */
00017 #include <boost/bind.hpp>
00018 #include "physics/physics.hh"
00019 #include "transport/Node.hh"
00020 #include "transport/TransportTypes.hh"
00021 #include "msgs/MessageTypes.hh"
00022 #include "common/Time.hh"
00023 #include "common/Plugin.hh"
00024 #include "common/Events.hh"
00025 
00026 namespace gazebo
00027 {
00028   class MoveModelTest : public ModelPlugin
00029   {
00030     public: void Load( physics::ModelPtr _parent, sdf::ElementPtr _sdf )
00031     {
00032       // Get then name of the parent model
00033       //std::string modelName = _sdf->GetParent()->GetValueString("name");
00034 
00035       // Get the world name.
00036       this->world = _parent->GetWorld();
00037 
00038       // Get a pointer to the model
00039       //this->model = this->world->GetModel(modelName);
00040       this->model = _parent;
00041 
00042       // Error message if the model couldn't be found
00043       if (!this->model)
00044         gzerr << "Unable to get parent model\n";
00045 
00046       // Listen to the update event. This event is broadcast every
00047       // simulation iteration.
00048       this->updateConnection = event::Events::ConnectWorldUpdateBegin(
00049           boost::bind(&MoveModelTest::OnUpdate, this));
00050       gzdbg << "plugin model name: " << this->model->GetName() << "\n";
00051 
00052 
00053       this->node = transport::NodePtr(new transport::Node());
00054       this->node->Init(this->world->GetName());
00055       this->statsSub = this->node->Subscribe("~/world_stats", &MoveModelTest::OnStats, this);
00056 
00057     }
00058 
00059     // Called by the world update start event
00060     public: void OnUpdate()
00061     {
00062       // do something on update
00063       this->simTime  = this->world->GetSimTime();
00064 
00065       math::Pose orig_pose = this->model->GetWorldPose();
00066 
00067       math::Pose new_pose = orig_pose;
00068       //new_pose.pos.x = -1.0 + 0.5*sin(0.01*this->simTime.Double());
00069       new_pose.rot.SetFromEuler(math::Vector3(0,0,this->simTime.Double()));
00070 
00071       //if (this->simTime.Double() > 20.0)
00072         this->model->SetWorldPose( new_pose );
00073 
00074       gzdbg << "plugin simTime [" << this->simTime.Double() << "] update new_pose [" << new_pose << "] orig pose [" << orig_pose << "]\n";
00075     }
00076 
00077     public: void OnStats( const boost::shared_ptr<msgs::WorldStatistics const> &_msg)
00078     {
00079       this->simTime  = msgs::Convert( _msg->sim_time() );
00080 
00081     }
00082 
00083     // Pointer to the model
00084     private: physics::ModelPtr model;
00085 
00086     // Pointer to the update event connection
00087     private: event::ConnectionPtr updateConnection;
00088 
00089     // subscribe to world stats
00090     private: transport::NodePtr node;
00091     private: transport::SubscriberPtr statsSub;
00092     private: common::Time simTime;
00093     private: physics::WorldPtr world;
00094 
00095   };
00096 
00097   // Register this plugin with the simulator
00098   GZ_REGISTER_MODEL_PLUGIN(MoveModelTest)
00099 }


gazebo_plugins
Author(s): Sachin Chitta, Stu Glaser, John Hsu
autogenerated on Mon Oct 6 2014 12:15:44