Go to the documentation of this file.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 <hector_gazebo_plugins/gazebo_ros_magnetic.h>
00030 #include <gazebo/common/Events.hh>
00031 #include <gazebo/physics/physics.hh>
00032
00033 static const double DEFAULT_MAGNITUDE = 1.0;
00034 static const double DEFAULT_REFERENCE_HEADING = 0.0;
00035 static const double DEFAULT_DECLINATION = 0.0;
00036 static const double DEFAULT_INCLINATION = 60.0;
00037
00038 namespace gazebo {
00039
00040 GazeboRosMagnetic::GazeboRosMagnetic()
00041 {
00042 }
00043
00045
00046 GazeboRosMagnetic::~GazeboRosMagnetic()
00047 {
00048 updateTimer.Disconnect(updateConnection);
00049
00050 dynamic_reconfigure_server_.reset();
00051
00052 node_handle_->shutdown();
00053 delete node_handle_;
00054 }
00055
00057
00058 void GazeboRosMagnetic::Load(physics::ModelPtr _model, sdf::ElementPtr _sdf)
00059 {
00060 world = _model->GetWorld();
00061
00062
00063 if (_sdf->HasElement("robotNamespace"))
00064 namespace_ = _sdf->GetElement("robotNamespace")->GetValue()->GetAsString();
00065 else
00066 namespace_.clear();
00067
00068 if (!_sdf->HasElement("topicName"))
00069 topic_ = "magnetic";
00070 else
00071 topic_ = _sdf->GetElement("topicName")->Get<std::string>();
00072
00073
00074 if (_sdf->HasElement("bodyName"))
00075 {
00076 link_name_ = _sdf->GetElement("bodyName")->GetValue()->GetAsString();
00077 link = _model->GetLink(link_name_);
00078 }
00079 else
00080 {
00081 link = _model->GetLink();
00082 link_name_ = link->GetName();
00083 }
00084
00085 if (!link)
00086 {
00087 ROS_FATAL("GazeboRosMagnetic plugin error: bodyName: %s does not exist\n", link_name_.c_str());
00088 return;
00089 }
00090
00091
00092 frame_id_ = link_name_;
00093 magnitude_ = DEFAULT_MAGNITUDE;
00094 reference_heading_ = DEFAULT_REFERENCE_HEADING * M_PI/180.0;
00095 declination_ = DEFAULT_DECLINATION * M_PI/180.0;
00096 inclination_ = DEFAULT_INCLINATION * M_PI/180.0;
00097
00098 if (_sdf->HasElement("frameId"))
00099 frame_id_ = _sdf->GetElement("frameId")->GetValue()->GetAsString();
00100
00101 if (_sdf->HasElement("magnitude"))
00102 _sdf->GetElement("magnitude")->GetValue()->Get(magnitude_);
00103
00104 if (_sdf->HasElement("referenceHeading"))
00105 if (_sdf->GetElement("referenceHeading")->GetValue()->Get(reference_heading_))
00106 reference_heading_ *= M_PI/180.0;
00107
00108 if (_sdf->HasElement("declination"))
00109 if (_sdf->GetElement("declination")->GetValue()->Get(declination_))
00110 declination_ *= M_PI/180.0;
00111
00112 if (_sdf->HasElement("inclination"))
00113 if (_sdf->GetElement("inclination")->GetValue()->Get(inclination_))
00114 inclination_ *= M_PI/180.0;
00115
00116
00117 magnetic_field_.header.frame_id = frame_id_;
00118 magnetic_field_world_.x = magnitude_ * cos(inclination_) * cos(reference_heading_ - declination_);
00119 magnetic_field_world_.y = magnitude_ * cos(inclination_) * sin(reference_heading_ - declination_);
00120 magnetic_field_world_.z = magnitude_ * -sin(inclination_);
00121
00122 sensor_model_.Load(_sdf);
00123
00124
00125 if (!ros::isInitialized())
00126 {
00127 int argc = 0;
00128 char** argv = NULL;
00129 ros::init(argc,argv,"gazebo",ros::init_options::NoSigintHandler|ros::init_options::AnonymousName);
00130 }
00131
00132 node_handle_ = new ros::NodeHandle(namespace_);
00133 publisher_ = node_handle_->advertise<geometry_msgs::Vector3Stamped>(topic_, 1);
00134
00135
00136 dynamic_reconfigure_server_.reset(new dynamic_reconfigure::Server<SensorModelConfig>(ros::NodeHandle(*node_handle_, topic_)));
00137 dynamic_reconfigure_server_->setCallback(boost::bind(&SensorModel3::dynamicReconfigureCallback, &sensor_model_, _1, _2));
00138
00139 Reset();
00140
00141
00142 updateTimer.Load(world, _sdf);
00143 updateConnection = updateTimer.Connect(boost::bind(&GazeboRosMagnetic::Update, this));
00144 }
00145
00146 void GazeboRosMagnetic::Reset()
00147 {
00148 updateTimer.Reset();
00149 sensor_model_.reset();
00150 }
00151
00153
00154 void GazeboRosMagnetic::Update()
00155 {
00156 common::Time sim_time = world->GetSimTime();
00157 double dt = updateTimer.getTimeSinceLastUpdate().Double();
00158
00159 math::Pose pose = link->GetWorldPose();
00160 math::Vector3 field = sensor_model_(pose.rot.RotateVectorReverse(magnetic_field_world_), dt);
00161
00162 magnetic_field_.header.stamp = ros::Time(sim_time.sec, sim_time.nsec);
00163 magnetic_field_.vector.x = field.x;
00164 magnetic_field_.vector.y = field.y;
00165 magnetic_field_.vector.z = field.z;
00166
00167 publisher_.publish(magnetic_field_);
00168 }
00169
00170
00171 GZ_REGISTER_MODEL_PLUGIN(GazeboRosMagnetic)
00172
00173 }