gazebo_gps_plugin.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2016 Pavel Vechersky, ASL, ETH Zurich, Switzerland
3  * Copyright 2016 Geoffrey Hunter <gbmhunter@gmail.com>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 // MODULE
20 
21 // 3RD PARTY
23 
24 // USER
25 #include "ConnectGazeboToRosTopic.pb.h"
26 
27 namespace gazebo {
28 
30  : SensorPlugin(),
31  // node_handle_(0),
32  random_generator_(random_device_()),
33  pubs_and_subs_created_(false) {}
34 
36 }
37 
38 void GazeboGpsPlugin::Load(sensors::SensorPtr _sensor, sdf::ElementPtr _sdf) {
39  if (kPrintOnPluginLoad) {
40  gzdbg << __FUNCTION__ << "() called." << std::endl;
41  }
42 
43 // Store the pointer to the parent sensor.
44  parent_sensor_ = std::dynamic_pointer_cast<sensors::GpsSensor>(_sensor);
45  world_ = physics::get_world(parent_sensor_->WorldName());
46 
47  //==============================================//
48  //========== READ IN PARAMS FROM SDF ===========//
49  //==============================================//
50 
51  std::string link_name;
52 
53  if (_sdf->HasElement("robotNamespace"))
54  namespace_ = _sdf->GetElement("robotNamespace")->Get<std::string>();
55  else
56  gzerr << "[gazebo_gps_plugin] Please specify a robotNamespace.\n";
57 
58  node_handle_ = gazebo::transport::NodePtr(new transport::Node());
59 
60  // Initialise with default namespace (typically /gazebo/default/)
61  node_handle_->Init();
62 
63  if (_sdf->HasElement("linkName"))
64  link_name = _sdf->GetElement("linkName")->Get<std::string>();
65  else
66  gzerr << "[gazebo_gps_plugin] Please specify a linkName.\n";
67 
68  std::string frame_id = link_name;
69 
70  // Get the pointer to the link that holds the sensor.
71  link_ =
72  boost::dynamic_pointer_cast<physics::Link>(world_->EntityByName(link_name));
73  if (link_ == NULL)
74  gzerr << "[gazebo_gps_plugin] Couldn't find specified link \"" << link_name
75  << "\"\n";
76 
77  // Retrieve the rest of the SDF parameters.
78  double hor_pos_std_dev;
79  double ver_pos_std_dev;
80  double hor_vel_std_dev;
81  double ver_vel_std_dev;
82 
83  getSdfParam<std::string>(_sdf, "gpsTopic", gps_topic_,
85 
86  getSdfParam<std::string>(_sdf, "groundSpeedTopic", ground_speed_topic_,
88 
89  getSdfParam<double>(_sdf, "horPosStdDev", hor_pos_std_dev,
91  getSdfParam<double>(_sdf, "verPosStdDev", ver_pos_std_dev,
93  getSdfParam<double>(_sdf, "horVelStdDev", hor_vel_std_dev,
95  getSdfParam<double>(_sdf, "verVelStdDev", ver_vel_std_dev,
97 
98  // Connect to the sensor update event.
99  this->updateConnection_ = this->parent_sensor_->ConnectUpdated(
100  boost::bind(&GazeboGpsPlugin::OnUpdate, this));
101 
102  // Make sure the parent sensor is active.
103  parent_sensor_->SetActive(true);
104 
105  // Initialize the normal distributions for ground speed.
106  ground_speed_n_[0] = NormalDistribution(0, hor_vel_std_dev);
107  ground_speed_n_[1] = NormalDistribution(0, hor_vel_std_dev);
108  ground_speed_n_[2] = NormalDistribution(0, ver_vel_std_dev);
109 
110  // ============================================ //
111  // ======= POPULATE STATIC PARTS OF MSGS ====== //
112  // ============================================ //
113 
114  // Fill the static parts of the GPS message.
115  gz_gps_message_.mutable_header()->set_frame_id(frame_id);
116  gz_gps_message_.set_service(gz_sensor_msgs::NavSatFix::SERVICE_GPS);
117  gz_gps_message_.set_status(gz_sensor_msgs::NavSatFix::STATUS_FIX);
118  gz_gps_message_.set_position_covariance_type(
119  gz_sensor_msgs::NavSatFix::COVARIANCE_TYPE_KNOWN);
120 
121  for (int i = 0; i < 9; i++) {
122  switch (i) {
123  case 0:
124  gz_gps_message_.add_position_covariance(hor_pos_std_dev *
125  hor_pos_std_dev);
126  break;
127  case 1:
128  case 2:
129  case 3:
130  gz_gps_message_.add_position_covariance(0);
131  break;
132  case 4:
133  gz_gps_message_.add_position_covariance(hor_pos_std_dev *
134  hor_pos_std_dev);
135  break;
136  case 5:
137  case 6:
138  case 7:
139  gz_gps_message_.add_position_covariance(0);
140  break;
141  case 8:
142  gz_gps_message_.add_position_covariance(ver_pos_std_dev *
143  ver_pos_std_dev);
144  break;
145  }
146  }
147 
148  // Fill the static parts of the ground speed message.
149  gz_ground_speed_message_.mutable_header()->set_frame_id(frame_id);
150  gz_ground_speed_message_.mutable_twist()->mutable_angular()->set_x(0.0);
151  gz_ground_speed_message_.mutable_twist()->mutable_angular()->set_y(0.0);
152  gz_ground_speed_message_.mutable_twist()->mutable_angular()->set_z(0.0);
153 }
154 
156  if (kPrintOnUpdates) {
157  gzdbg << __FUNCTION__ << "() called." << std::endl;
158  }
159 
160  if (!pubs_and_subs_created_) {
162  pubs_and_subs_created_ = true;
163  }
164 
165  // Get the time of the last measurement.
166  common::Time current_time;
167 
168  // Get the linear velocity in the world frame.
169  ignition::math::Vector3d W_ground_speed_W_L = link_->WorldLinearVel();
170 
171  // Apply noise to ground speed.
172  W_ground_speed_W_L += ignition::math::Vector3d (ground_speed_n_[0](random_generator_),
175 
176  // Fill the GPS message.
177  current_time = parent_sensor_->LastMeasurementTime();
178 
179  gz_gps_message_.set_latitude(parent_sensor_->Latitude().Degree());
180  gz_gps_message_.set_longitude(parent_sensor_->Longitude().Degree());
181  gz_gps_message_.set_altitude(parent_sensor_->Altitude());
182 
183  gz_gps_message_.mutable_header()->mutable_stamp()->set_sec(current_time.sec);
184  gz_gps_message_.mutable_header()->mutable_stamp()->set_nsec(
185  current_time.nsec);
186 
187  // Fill the ground speed message.
188  gz_ground_speed_message_.mutable_twist()->mutable_linear()->set_x(
189  W_ground_speed_W_L.X());
190  gz_ground_speed_message_.mutable_twist()->mutable_linear()->set_y(
191  W_ground_speed_W_L.Y());
192  gz_ground_speed_message_.mutable_twist()->mutable_linear()->set_z(
193  W_ground_speed_W_L.Z());
194  gz_ground_speed_message_.mutable_header()->mutable_stamp()->set_sec(
195  current_time.sec);
196  gz_ground_speed_message_.mutable_header()->mutable_stamp()->set_nsec(
197  current_time.nsec);
198 
199  // Publish the GPS message.
200  gz_gps_pub_->Publish(gz_gps_message_);
201 
202  // Publish the ground speed message.
204 }
205 
207  // Create temporary "ConnectGazeboToRosTopic" publisher and message
208  gazebo::transport::PublisherPtr connect_gazebo_to_ros_topic_pub =
209  node_handle_->Advertise<gz_std_msgs::ConnectGazeboToRosTopic>(
210  "~/" + kConnectGazeboToRosSubtopic, 1);
211 
212  gz_std_msgs::ConnectGazeboToRosTopic connect_gazebo_to_ros_topic_msg;
213 
214  // ============================================ //
215  // =========== NAV SAT FIX MSG SETUP ========== //
216  // ============================================ //
217  gz_gps_pub_ = node_handle_->Advertise<gz_sensor_msgs::NavSatFix>(
218  "~/" + namespace_ + "/" + gps_topic_, 1);
219 
220  connect_gazebo_to_ros_topic_msg.set_gazebo_topic("~/" + namespace_ + "/" +
221  gps_topic_);
222  connect_gazebo_to_ros_topic_msg.set_ros_topic(namespace_ + "/" + gps_topic_);
223  connect_gazebo_to_ros_topic_msg.set_msgtype(
224  gz_std_msgs::ConnectGazeboToRosTopic::NAV_SAT_FIX);
225  connect_gazebo_to_ros_topic_pub->Publish(connect_gazebo_to_ros_topic_msg,
226  true);
227 
228  // ============================================ //
229  // == GROUND SPEED (TWIST STAMPED) MSG SETUP == //
230  // ============================================ //
232  node_handle_->Advertise<gz_geometry_msgs::TwistStamped>(
233  "~/" + namespace_ + "/" + ground_speed_topic_, 1);
234 
235  connect_gazebo_to_ros_topic_msg.set_gazebo_topic("~/" + namespace_ + "/" +
236  ground_speed_topic_);
237  connect_gazebo_to_ros_topic_msg.set_ros_topic(namespace_ + "/" +
238  ground_speed_topic_);
239  connect_gazebo_to_ros_topic_msg.set_msgtype(
240  gz_std_msgs::ConnectGazeboToRosTopic::TWIST_STAMPED);
241  connect_gazebo_to_ros_topic_pub->Publish(connect_gazebo_to_ros_topic_msg);
242 }
243 
245 }
GZ_REGISTER_SENSOR_PLUGIN(GazeboGpsPlugin)
gz_sensor_msgs::NavSatFix gz_gps_message_
GPS message to be published on sensor update.
gazebo::transport::PublisherPtr gz_ground_speed_pub_
sensors::GpsSensorPtr parent_sensor_
Pointer to the parent sensor.
NormalDistribution ground_speed_n_[3]
Normal distributions for ground speed noise in x, y, and z directions.
gazebo::transport::PublisherPtr gz_gps_pub_
event::ConnectionPtr updateConnection_
Pointer to the update event connection.
std::string frame_id
gz_geometry_msgs::TwistStamped gz_ground_speed_message_
Ground speed message to be published on sensor update.
static constexpr char GPS[]
static const bool kPrintOnPluginLoad
Definition: common.h:41
std::string ground_speed_topic_
Name of topic for ground speed messages, read from SDF file.
std::string gps_topic_
Name of topic for GPS messages, read from SDF file.
static constexpr char GROUND_SPEED[]
std::normal_distribution NormalDistribution
static const bool kPrintOnUpdates
Definition: common.h:42
physics::LinkPtr link_
Pointer to the sensor link.
static constexpr double kDefaultHorVelStdDev
static constexpr double kDefaultVerPosStdDev
static constexpr double kDefaultHorPosStdDev
void CreatePubsAndSubs()
Creates all required publishers and subscribers, incl. routing of messages to/from ROS if required...
static const std::string kConnectGazeboToRosSubtopic
Definition: common.h:56
physics::WorldPtr world_
Pointer to the world.
void Load(sensors::SensorPtr _sensor, sdf::ElementPtr _sdf)
bool pubs_and_subs_created_
Flag that is set to true once CreatePubsAndSubs() is called, used to prevent CreatePubsAndSubs() from...
gazebo::transport::NodePtr node_handle_
void OnUpdate()
Publishes both a NavSatFix and Gazebo message.
static constexpr double kDefaultVerVelStdDev


rotors_gazebo_plugins
Author(s): Fadri Furrer, Michael Burri, Mina Kamel, Janosch Nikolic, Markus Achtelik
autogenerated on Mon Feb 28 2022 23:39:03