Fawkes API  Fawkes Development Version
motor.cpp
1 /***************************************************************************
2  * motor.cpp - Plugin for controling a model through a simulated motor
3  *
4  * Created: Wed Jan 29 16:10:17 2014
5  * Copyright 2014 Frederik Zwilling
6  ****************************************************************************/
7 
8 /* This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Library General Public License for more details.
17  *
18  * Read the full text in the LICENSE.GPL file in the doc directory.
19  */
20 
21 #include <math.h>
22 
23 #include "motor.h"
24 
25 using namespace gazebo;
26 
27 // Register this plugin to make it available in the simulator
28 GZ_REGISTER_MODEL_PLUGIN(Motor)
29 
31 {
32 }
33 
35 {
36  printf("Destructing Motor Plugin!\n");
37 }
38 
39 /** on loading of the plugin
40  * @param _parent Parent Model
41  */
42 void Motor::Load(physics::ModelPtr _parent, sdf::ElementPtr /*_sdf*/)
43 {
44  // Store the pointer to the model
45  this->model_ = _parent;
46 
47  //get the model-name
48  this->name_ = model_->GetName();
49  printf("Loading Motor Plugin of model %s\n", name_.c_str());
50 
51  // Listen to the update event. This event is broadcast every
52  // simulation iteration.
53  this->update_connection_ = event::Events::ConnectWorldUpdateBegin(boost::bind(&Motor::OnUpdate, this, _1));
54 
55  //Create the communication Node for communication with fawkes
56  this->node_ = transport::NodePtr(new transport::Node());
57  //the namespace is set to the model name!
58  this->node_->Init(model_->GetWorld()->GetName()+"/"+name_);
59 
60 
61  //initialize movement commands:
62  vx_ = 0.0;
63  vy_ = 0.0;
64  vomega_ = 0.0;
65 
66  //create subscriber
67  this->motor_move_sub_ = this->node_->Subscribe(std::string("~/RobotinoSim/MotorMove/"), &Motor::on_motor_move_msg, this);
68 }
69 
70 /** Called by the world update start event
71  */
72 void Motor::OnUpdate(const common::UpdateInfo & /*_info*/)
73 {
74  //Apply movement command
75  float x,y;
76  float yaw = this->model_->GetWorldPose().rot.GetAsEuler().z;
77  //foward part
78  x = cos(yaw) * vx_;
79  y = sin(yaw) * vx_;
80  //sideways part
81  x += cos(yaw + 3.1415926f / 2) * vy_;
82  y += sin(yaw + 3.1415926f / 2) * vy_;
83  // Apply velocity to the model.
84  this->model_->SetLinearVel(math::Vector3(x, y, 0));
85  this->model_->SetAngularVel(math::Vector3(0, 0, vomega_));
86 }
87 
88 /** on Gazebo reset
89  */
91 {
92 }
93 
94 /** Functions for recieving Messages (registerd via suscribers)
95  * @param msg message
96  */
97 void Motor::on_motor_move_msg(ConstVector3dPtr &msg)
98 {
99  //printf("Got MotorMove Msg!!! %f %f %f\n", msg->x(), msg->y(), msg->z());
100  //Transform relative motion into ablosulte motion
101  vx_ = msg->x();
102  vy_ = msg->y();
103  vomega_ = msg->z();
104 }
virtual void Reset()
on Gazebo reset
Definition: motor.cpp:90
Definition: gps.h:30
Motor plugin for Gazebo.
Definition: motor.h:36
virtual void Load(physics::ModelPtr _parent, sdf::ElementPtr)
on loading of the plugin
Definition: motor.cpp:42
virtual void OnUpdate(const common::UpdateInfo &)
Called by the world update start event.
Definition: motor.cpp:72
~Motor()
Destructor.
Definition: motor.cpp:34