-
Notifications
You must be signed in to change notification settings - Fork 773
/
gazebo_ros_p3d.cpp
309 lines (258 loc) · 9.99 KB
/
gazebo_ros_p3d.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// Copyright 2013 Open Source Robotics Foundation, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <ignition/math/Rand.hh>
#include <gazebo_ros/node.hpp>
#include <gazebo_ros/utils.hpp>
#include <gazebo_ros/conversions/geometry_msgs.hpp>
#include <gazebo_ros/conversions/builtin_interfaces.hpp>
#include <gazebo_plugins/gazebo_ros_p3d.hpp>
#include <nav_msgs/msg/odometry.hpp>
#include <rclcpp/rclcpp.hpp>
#ifdef NO_ERROR
// NO_ERROR is a macro defined in Windows that's used as an enum in tf2
#undef NO_ERROR
#endif
#include <tf2_geometry_msgs/tf2_geometry_msgs.hpp>
#include <tf2_ros/transform_broadcaster.h>
#include <tf2_ros/transform_listener.h>
#ifdef IGN_PROFILER_ENABLE
#include <ignition/common/Profiler.hh>
#endif
#include <string>
#include <memory>
namespace gazebo_plugins
{
class GazeboRosP3DPrivate
{
public:
/// Callback to be called at every simulation iteration
/// \param[in] info Updated simulation info
void OnUpdate(const gazebo::common::UpdateInfo & info);
/// The link being traked.
gazebo::physics::LinkPtr link_{nullptr};
/// The body of the frame to display pose, twist
gazebo::physics::LinkPtr reference_link_{nullptr};
/// Pointer to ros node
gazebo_ros::Node::SharedPtr ros_node_{nullptr};
/// Odometry publisher
rclcpp::Publisher<nav_msgs::msg::Odometry>::SharedPtr pub_{nullptr};
/// Odom topic name
std::string topic_name_{"odom"};
/// Frame transform name, should match name of reference link, or be world.
std::string frame_name_{"world"};
/// Constant xyz and rpy offsets
ignition::math::Pose3d offset_;
/// Keep track of the last update time.
gazebo::common::Time last_time_;
/// Publish rate in Hz.
double update_rate_{0.0};
/// Gaussian noise
double gaussian_noise_;
/// Pointer to the update event connection
gazebo::event::ConnectionPtr update_connection_{nullptr};
};
GazeboRosP3D::GazeboRosP3D()
: impl_(std::make_unique<GazeboRosP3DPrivate>())
{
}
GazeboRosP3D::~GazeboRosP3D()
{
}
// Load the controller
void GazeboRosP3D::Load(gazebo::physics::ModelPtr model, sdf::ElementPtr sdf)
{
// Configure the plugin from the SDF file
impl_->ros_node_ = gazebo_ros::Node::Get(sdf);
// Get QoS profiles
const gazebo_ros::QoS & qos = impl_->ros_node_->get_qos();
if (!sdf->HasElement("update_rate")) {
RCLCPP_DEBUG(
impl_->ros_node_->get_logger(), "p3d plugin missing <update_rate>, defaults to 0.0"
" (as fast as possible)");
} else {
impl_->update_rate_ = sdf->GetElement("update_rate")->Get<double>();
}
std::string link_name;
if (!sdf->HasElement("body_name")) {
RCLCPP_ERROR(impl_->ros_node_->get_logger(), "Missing <body_name>, cannot proceed");
return;
} else {
link_name = sdf->GetElement("body_name")->Get<std::string>();
}
impl_->link_ = model->GetLink(link_name);
if (!impl_->link_) {
RCLCPP_ERROR(
impl_->ros_node_->get_logger(), "body_name: %s does not exist\n",
link_name.c_str());
return;
}
impl_->pub_ = impl_->ros_node_->create_publisher<nav_msgs::msg::Odometry>(
impl_->topic_name_, qos.get_publisher_qos(
impl_->topic_name_, rclcpp::SensorDataQoS().reliable()));
impl_->topic_name_ = impl_->pub_->get_topic_name();
RCLCPP_DEBUG(
impl_->ros_node_->get_logger(), "Publishing on topic [%s]", impl_->topic_name_.c_str());
if (sdf->HasElement("xyz_offsets")) {
RCLCPP_WARN(
impl_->ros_node_->get_logger(), "<xyz_offsets> is deprecated, use <xyz_offset> instead.");
impl_->offset_.Pos() = sdf->GetElement("xyz_offsets")->Get<ignition::math::Vector3d>();
}
if (!sdf->HasElement("xyz_offset")) {
if (!sdf->HasElement("xyz_offsets")) {
RCLCPP_DEBUG(impl_->ros_node_->get_logger(), "Missing <xyz_offset>, defaults to 0s");
}
} else {
impl_->offset_.Pos() = sdf->GetElement("xyz_offset")->Get<ignition::math::Vector3d>();
}
if (sdf->HasElement("rpy_offsets")) {
RCLCPP_WARN(
impl_->ros_node_->get_logger(), "<rpy_offsets> is deprecated, use <rpy_offset> instead.");
impl_->offset_.Rot() = ignition::math::Quaterniond(
sdf->GetElement("rpy_offsets")->Get<ignition::math::Vector3d>());
}
if (!sdf->HasElement("rpy_offset")) {
if (!sdf->HasElement("rpy_offsets")) {
RCLCPP_DEBUG(impl_->ros_node_->get_logger(), "Missing <rpy_offset>, defaults to 0s");
}
} else {
impl_->offset_.Rot() = ignition::math::Quaterniond(
sdf->GetElement("rpy_offset")->Get<ignition::math::Vector3d>());
}
if (!sdf->HasElement("gaussian_noise")) {
RCLCPP_DEBUG(impl_->ros_node_->get_logger(), "Missing <gassian_noise>, defaults to 0.0");
impl_->gaussian_noise_ = 0;
} else {
impl_->gaussian_noise_ = sdf->GetElement("gaussian_noise")->Get<double>();
}
impl_->last_time_ = model->GetWorld()->SimTime();
if (!sdf->HasElement("frame_name")) {
RCLCPP_DEBUG(
impl_->ros_node_->get_logger(), "Missing <frame_name>, defaults to world");
} else {
impl_->frame_name_ = sdf->GetElement("frame_name")->Get<std::string>();
}
// If frame_name specified is "/world", "world", "/map" or "map" report
// back inertial values in the gazebo world
if (impl_->frame_name_ != "/world" && impl_->frame_name_ != "world" &&
impl_->frame_name_ != "/map" && impl_->frame_name_ != "map")
{
impl_->reference_link_ = model->GetLink(impl_->frame_name_);
if (!impl_->reference_link_) {
RCLCPP_WARN(
impl_->ros_node_->get_logger(), "<frame_name> [%s] does not exist.",
impl_->frame_name_.c_str());
}
}
// Listen to the update event. This event is broadcast every simulation iteration
impl_->update_connection_ = gazebo::event::Events::ConnectWorldUpdateBegin(
std::bind(&GazeboRosP3DPrivate::OnUpdate, impl_.get(), std::placeholders::_1));
}
// Update the controller
void GazeboRosP3DPrivate::OnUpdate(const gazebo::common::UpdateInfo & info)
{
if (!link_) {
return;
}
#ifdef IGN_PROFILER_ENABLE
IGN_PROFILE("GazeboRosP3DPrivate::OnUpdate");
#endif
gazebo::common::Time current_time = info.simTime;
if (current_time < last_time_) {
RCLCPP_WARN(ros_node_->get_logger(), "Negative update time difference detected.");
last_time_ = current_time;
}
// Rate control
if (update_rate_ > 0 &&
(current_time - last_time_).Double() < (1.0 / update_rate_))
{
return;
}
// If we don't have any subscribers, don't bother composing and sending the message
if (ros_node_->count_subscribers(topic_name_) == 0) {
return;
}
// Differentiate to get accelerations
double tmp_dt = current_time.Double() - last_time_.Double();
if (tmp_dt == 0) {
return;
}
#ifdef IGN_PROFILER_ENABLE
IGN_PROFILE_BEGIN("fill ROS message");
#endif
nav_msgs::msg::Odometry pose_msg;
// Copy data into pose message
pose_msg.header.frame_id = frame_name_;
pose_msg.header.stamp = gazebo_ros::Convert<builtin_interfaces::msg::Time>(current_time);
pose_msg.child_frame_id = link_->GetName();
// Get inertial rates
ignition::math::Vector3d vpos = link_->WorldLinearVel();
ignition::math::Vector3d veul = link_->WorldAngularVel();
// Get pose/orientation
auto pose = link_->WorldPose();
// Apply reference frame
if (reference_link_) {
// Convert to relative pose, rates
auto frame_pose = reference_link_->WorldPose();
auto frame_vpos = reference_link_->WorldLinearVel();
auto frame_veul = reference_link_->WorldAngularVel();
pose.Pos() = pose.Pos() - frame_pose.Pos();
pose.Pos() = frame_pose.Rot().RotateVectorReverse(pose.Pos());
pose.Rot() *= frame_pose.Rot().Inverse();
vpos = frame_pose.Rot().RotateVector(vpos - frame_vpos);
veul = frame_pose.Rot().RotateVector(veul - frame_veul);
}
// Apply constant offsets
// Apply XYZ offsets and get position and rotation components
pose.Pos() = pose.Pos() + offset_.Pos();
// Apply RPY offsets
pose.Rot() = offset_.Rot() * pose.Rot();
pose.Rot().Normalize();
// Fill out messages
pose_msg.pose.pose.position = gazebo_ros::Convert<geometry_msgs::msg::Point>(pose.Pos());
pose_msg.pose.pose.orientation = gazebo_ros::Convert<geometry_msgs::msg::Quaternion>(pose.Rot());
pose_msg.twist.twist.linear.x = vpos.X() + ignition::math::Rand::DblNormal(0, gaussian_noise_);
pose_msg.twist.twist.linear.y = vpos.Y() + ignition::math::Rand::DblNormal(0, gaussian_noise_);
pose_msg.twist.twist.linear.z = vpos.Z() + ignition::math::Rand::DblNormal(0, gaussian_noise_);
pose_msg.twist.twist.angular.x = veul.X() + ignition::math::Rand::DblNormal(0, gaussian_noise_);
pose_msg.twist.twist.angular.y = veul.Y() + ignition::math::Rand::DblNormal(0, gaussian_noise_);
pose_msg.twist.twist.angular.z = veul.Z() + ignition::math::Rand::DblNormal(0, gaussian_noise_);
// Fill in covariance matrix
/// @TODO: let user set separate linear and angular covariance values
double gn2 = gaussian_noise_ * gaussian_noise_;
pose_msg.pose.covariance[0] = gn2;
pose_msg.pose.covariance[7] = gn2;
pose_msg.pose.covariance[14] = gn2;
pose_msg.pose.covariance[21] = gn2;
pose_msg.pose.covariance[28] = gn2;
pose_msg.pose.covariance[35] = gn2;
pose_msg.twist.covariance[0] = gn2;
pose_msg.twist.covariance[7] = gn2;
pose_msg.twist.covariance[14] = gn2;
pose_msg.twist.covariance[21] = gn2;
pose_msg.twist.covariance[28] = gn2;
pose_msg.twist.covariance[35] = gn2;
#ifdef IGN_PROFILER_ENABLE
IGN_PROFILE_END();
IGN_PROFILE_BEGIN("publish");
#endif
// Publish to ROS
pub_->publish(pose_msg);
#ifdef IGN_PROFILER_ENABLE
IGN_PROFILE_END();
#endif
// Save last time stamp
last_time_ = current_time;
}
GZ_REGISTER_MODEL_PLUGIN(GazeboRosP3D)
} // namespace gazebo_plugins