gazebo_ros_magnetic.cpp
Go to the documentation of this file.
1 //=================================================================================================
2 // Copyright (c) 2012, Johannes Meyer, TU Darmstadt
3 // All rights reserved.
4 
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above copyright
10 // notice, this list of conditions and the following disclaimer in the
11 // documentation and/or other materials provided with the distribution.
12 // * Neither the name of the Flight Systems and Automatic Control group,
13 // TU Darmstadt, nor the names of its contributors may be used to
14 // endorse or promote products derived from this software without
15 // specific prior written permission.
16 
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 //=================================================================================================
28 
30 #include <gazebo/common/Events.hh>
31 #include <gazebo/physics/physics.hh>
32 
33 static const double DEFAULT_MAGNITUDE = 1.0;
34 static const double DEFAULT_REFERENCE_HEADING = 0.0;
35 static const double DEFAULT_DECLINATION = 0.0;
36 static const double DEFAULT_INCLINATION = 60.0;
37 
38 namespace gazebo {
39 
41 {
42 }
43 
45 // Destructor
47 {
49 
51 
53  delete node_handle_;
54 }
55 
57 // Load the controller
58 void GazeboRosMagnetic::Load(physics::ModelPtr _model, sdf::ElementPtr _sdf)
59 {
60  world = _model->GetWorld();
61 
62  // load parameters
63  if (_sdf->HasElement("robotNamespace"))
64  namespace_ = _sdf->GetElement("robotNamespace")->GetValue()->GetAsString();
65  else
66  namespace_.clear();
67 
68  if (!_sdf->HasElement("topicName"))
69  topic_ = "magnetic";
70  else
71  topic_ = _sdf->GetElement("topicName")->Get<std::string>();
72 
73 
74  if (_sdf->HasElement("bodyName"))
75  {
76  link_name_ = _sdf->GetElement("bodyName")->GetValue()->GetAsString();
77  link = _model->GetLink(link_name_);
78  }
79  else
80  {
81  link = _model->GetLink();
82  link_name_ = link->GetName();
83  }
84 
85  if (!link)
86  {
87  ROS_FATAL("GazeboRosMagnetic plugin error: bodyName: %s does not exist\n", link_name_.c_str());
88  return;
89  }
90 
91  // default parameters
95  declination_ = DEFAULT_DECLINATION * M_PI/180.0;
96  inclination_ = DEFAULT_INCLINATION * M_PI/180.0;
97 
98  if (_sdf->HasElement("frameId"))
99  frame_id_ = _sdf->GetElement("frameId")->GetValue()->GetAsString();
100 
101  if (_sdf->HasElement("magnitude"))
102  _sdf->GetElement("magnitude")->GetValue()->Get(magnitude_);
103 
104  if (_sdf->HasElement("referenceHeading"))
105  if (_sdf->GetElement("referenceHeading")->GetValue()->Get(reference_heading_))
106  reference_heading_ *= M_PI/180.0;
107 
108  if (_sdf->HasElement("declination"))
109  if (_sdf->GetElement("declination")->GetValue()->Get(declination_))
110  declination_ *= M_PI/180.0;
111 
112  if (_sdf->HasElement("inclination"))
113  if (_sdf->GetElement("inclination")->GetValue()->Get(inclination_))
114  inclination_ *= M_PI/180.0;
115 
116  // Note: Gazebo uses NorthWestUp coordinate system, heading and declination are compass headings
117  magnetic_field_.header.frame_id = frame_id_;
118 #if (GAZEBO_MAJOR_VERSION >= 8)
122 #else
126 #endif
127 
128  sensor_model_.Load(_sdf);
129 
130  // start ros node
131  if (!ros::isInitialized())
132  {
133  int argc = 0;
134  char** argv = NULL;
136  }
137 
139  publisher_ = node_handle_->advertise<geometry_msgs::Vector3Stamped>(topic_, 1);
140 
141  // setup dynamic_reconfigure server
142  dynamic_reconfigure_server_.reset(new dynamic_reconfigure::Server<SensorModelConfig>(ros::NodeHandle(*node_handle_, topic_)));
144 
145  Reset();
146 
147  // connect Update function
148  updateTimer.Load(world, _sdf);
150 }
151 
153 {
154  updateTimer.Reset();
156 }
157 
159 // Update the controller
161 {
162 #if (GAZEBO_MAJOR_VERSION >= 8)
163  common::Time sim_time = world->SimTime();
164  double dt = updateTimer.getTimeSinceLastUpdate().Double();
165 
166  ignition::math::Pose3d pose = link->WorldPose();
167  ignition::math::Vector3d field = sensor_model_(pose.Rot().RotateVectorReverse(magnetic_field_world_), dt);
168 
169  magnetic_field_.header.stamp = ros::Time(sim_time.sec, sim_time.nsec);
170  magnetic_field_.vector.x = field.X();
171  magnetic_field_.vector.y = field.Y();
172  magnetic_field_.vector.z = field.Z();
173 #else
174  common::Time sim_time = world->GetSimTime();
175  double dt = updateTimer.getTimeSinceLastUpdate().Double();
176 
177  math::Pose pose = link->GetWorldPose();
178  math::Vector3 field = sensor_model_(pose.rot.RotateVectorReverse(magnetic_field_world_), dt);
179 
180  magnetic_field_.header.stamp = ros::Time(sim_time.sec, sim_time.nsec);
181  magnetic_field_.vector.x = field.x;
182  magnetic_field_.vector.y = field.y;
183  magnetic_field_.vector.z = field.z;
184 #endif
185 
187 }
188 
189 // Register this plugin with the simulator
190 GZ_REGISTER_MODEL_PLUGIN(GazeboRosMagnetic)
191 
192 } // namespace gazebo
event::ConnectionPtr updateConnection
#define ROS_FATAL(...)
virtual void Load(physics::WorldPtr _world, sdf::ElementPtr _sdf, const std::string &_prefix="update")
Definition: update_timer.h:52
static const double DEFAULT_DECLINATION
void publish(const boost::shared_ptr< M > &message) const
virtual void Load(sdf::ElementPtr _sdf, const std::string &prefix=std::string())
Definition: sensor_model.h:101
physics::LinkPtr link
The link referred to by this plugin.
ROSCPP_DECL bool isInitialized()
virtual void Reset()
Definition: update_timer.h:175
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
geometry_msgs::Vector3Stamped magnetic_field_
virtual event::ConnectionPtr Connect(const boost::function< void()> &_subscriber, bool connectToWorldUpdateBegin=true)
Definition: update_timer.h:85
static const double DEFAULT_MAGNITUDE
gazebo::math::Vector3 magnetic_field_world_
physics::WorldPtr world
The parent World.
virtual void Disconnect(event::ConnectionPtr const &_c=event::ConnectionPtr())
Definition: update_timer.h:95
virtual void dynamicReconfigureCallback(SensorModelConfig &config, uint32_t level)
common::Time getTimeSinceLastUpdate() const
Definition: update_timer.h:132
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
static const double DEFAULT_REFERENCE_HEADING
virtual void reset()
Definition: sensor_model.h:191
static const double DEFAULT_INCLINATION
virtual void Load(physics::ModelPtr _model, sdf::ElementPtr _sdf)
boost::shared_ptr< dynamic_reconfigure::Server< SensorModelConfig > > dynamic_reconfigure_server_
ros::NodeHandle * node_handle_


hector_gazebo_plugins
Author(s): Stefan Kohlbrecher , Johannes Meyer
autogenerated on Wed Jun 5 2019 22:40:23