This repository has been archived by the owner on Feb 6, 2023. It is now read-only.
/
DVLROSPlugin.cc
327 lines (269 loc) · 10.9 KB
/
DVLROSPlugin.cc
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// Copyright (c) 2016 The UUV Simulator Authors.
// All rights reserved.
//
// 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 <uuv_sensor_ros_plugins/DVLROSPlugin.hh>
namespace gazebo
{
/////////////////////////////////////////////////
DVLROSPlugin::DVLROSPlugin() : ROSBaseModelPlugin()
{
this->beamTransformsInitialized = false;
}
/////////////////////////////////////////////////
DVLROSPlugin::~DVLROSPlugin()
{ }
/////////////////////////////////////////////////
void DVLROSPlugin::Load(physics::ModelPtr _model, sdf::ElementPtr _sdf)
{
ROSBaseModelPlugin::Load(_model, _sdf);
// Load the link names for all the beams
std::string beamLinkName;
GetSDFParam<std::string>(_sdf, "beam_link_name_0", beamLinkName, "");
GZ_ASSERT(!beamLinkName.empty(), "Beam 0 link name empty");
this->beamsLinkNames.push_back(beamLinkName);
GetSDFParam<std::string>(_sdf, "beam_link_name_1", beamLinkName, "");
GZ_ASSERT(!beamLinkName.empty(), "Beam 1 link name empty");
this->beamsLinkNames.push_back(beamLinkName);
GetSDFParam<std::string>(_sdf, "beam_link_name_2", beamLinkName, "");
GZ_ASSERT(!beamLinkName.empty(), "Beam 2 link name empty");
this->beamsLinkNames.push_back(beamLinkName);
GetSDFParam<std::string>(_sdf, "beam_link_name_3", beamLinkName, "");
GZ_ASSERT(!beamLinkName.empty(), "Beam 3 link name empty");
this->beamsLinkNames.push_back(beamLinkName);
// Load the beam output topic names
std::string beamTopic;
GetSDFParam<std::string>(_sdf, "beam_topic_0", beamTopic, "");
GZ_ASSERT(!beamTopic.empty(), "Beam 0 topic name empty");
this->beamTopics.push_back(beamTopic);
GetSDFParam<std::string>(_sdf, "beam_topic_1", beamTopic, "");
GZ_ASSERT(!beamTopic.empty(), "Beam 1 topic name empty");
this->beamTopics.push_back(beamTopic);
GetSDFParam<std::string>(_sdf, "beam_topic_2", beamTopic, "");
GZ_ASSERT(!beamTopic.empty(), "Beam 2 topic name empty");
this->beamTopics.push_back(beamTopic);
GetSDFParam<std::string>(_sdf, "beam_topic_3", beamTopic, "");
GZ_ASSERT(!beamTopic.empty(), "Beam 3 topic name empty");
this->beamTopics.push_back(beamTopic);
// Create beam subscribers
this->beamSub0.reset(new message_filters::Subscriber<sensor_msgs::Range>(
*this->rosNode.get(), this->beamTopics[0], 1));
this->beamSub1.reset(new message_filters::Subscriber<sensor_msgs::Range>(
*this->rosNode.get(), this->beamTopics[1], 1));
this->beamSub2.reset(new message_filters::Subscriber<sensor_msgs::Range>(
*this->rosNode.get(), this->beamTopics[2], 1));
this->beamSub3.reset(new message_filters::Subscriber<sensor_msgs::Range>(
*this->rosNode.get(), this->beamTopics[3], 1));
for (int i = 0; i < 4; i++)
this->dvlBeamMsgs.push_back(uuv_sensor_ros_plugins_msgs::DVLBeam());
// Synchronize the beam topics
this->syncBeamMessages.reset(new message_filters::TimeSynchronizer<
sensor_msgs::Range, sensor_msgs::Range,
sensor_msgs::Range, sensor_msgs::Range>(
*this->beamSub0.get(), *this->beamSub1.get(), *this->beamSub2.get(),
*this->beamSub3.get(), 10));
// Set synchronized callback function for the DVL beams
this->syncBeamMessages->registerCallback(
boost::bind(&DVLROSPlugin::OnBeamCallback, this, _1, _2, _3, _4));
// Initialize the default DVL output
this->rosSensorOutputPub =
this->rosNode->advertise<uuv_sensor_ros_plugins_msgs::DVL>(
this->sensorOutputTopic, 1);
this->twistPub =
this->rosNode->advertise<geometry_msgs::TwistWithCovarianceStamped>(
this->sensorOutputTopic + "_twist", 1);
// Initialize ROS messages headers
if (this->enableLocalNEDFrame)
{
// Use the local NED frame format
this->dvlROSMsg.header.frame_id = this->tfLocalNEDFrame.child_frame_id_;
this->twistROSMsg.header.frame_id = this->tfLocalNEDFrame.child_frame_id_;
}
else
{
// Use the link's frame ID
this->dvlROSMsg.header.frame_id = this->link->GetName();
this->twistROSMsg.header.frame_id = this->link->GetName();
}
double variance = this->noiseSigma * this->noiseSigma;
// Set covariance
for (int i = 0; i < 9; i++)
this->dvlROSMsg.velocity_covariance[i] = 0.0;
this->dvlROSMsg.velocity_covariance[0] = variance;
this->dvlROSMsg.velocity_covariance[4] = variance;
this->dvlROSMsg.velocity_covariance[8] = variance;
for (int i = 0; i < 36; i++)
this->twistROSMsg.twist.covariance[i] = 0.0;
this->twistROSMsg.twist.covariance[0] = variance;
this->twistROSMsg.twist.covariance[7] = variance;
this->twistROSMsg.twist.covariance[14] = variance;
this->twistROSMsg.twist.covariance[21] = -1; // not available
this->twistROSMsg.twist.covariance[28] = -1; // not available
this->twistROSMsg.twist.covariance[35] = -1; // not available
if (this->gazeboMsgEnabled)
{
this->gazeboSensorOutputPub =
this->gazeboNode->Advertise<sensor_msgs::msgs::Dvl>(
this->robotNamespace + "/" + this->sensorOutputTopic, 1);
}
}
/////////////////////////////////////////////////
bool DVLROSPlugin::OnUpdate(const common::UpdateInfo& _info)
{
// Publish sensor state
this->PublishState();
if (!this->EnableMeasurement(_info))
return false;
if (this->enableLocalNEDFrame)
this->SendLocalNEDTransform();
ignition::math::Vector3d bodyVel;
if (!this->UpdateBeamTransforms())
return false;
// Read true body velocity
// TODO Temporary solution to generate DVL message, use beams in the future
// instead
#if GAZEBO_MAJOR_VERSION >= 8
bodyVel = this->link->RelativeLinearVel();
#else
bodyVel = this->link->GetRelativeLinearVel().Ign();
#endif
bodyVel.X() += this->GetGaussianNoise(this->noiseAmp);
bodyVel.Y() += this->GetGaussianNoise(this->noiseAmp);
bodyVel.Z() += this->GetGaussianNoise(this->noiseAmp);
if (this->enableLocalNEDFrame)
bodyVel = this->localNEDFrame.Rot().RotateVector(bodyVel);
if (this->gazeboMsgEnabled)
{
sensor_msgs::msgs::Dvl dvlGazeboMsg;
double variance = this->noiseSigma * this->noiseSigma;
for (int i = 0; i < 9; i++)
{
if (i == 0 || i == 4 || i == 8)
dvlGazeboMsg.add_linear_velocity_covariance(variance);
else
dvlGazeboMsg.add_linear_velocity_covariance(0.0);
}
// Publish simulated measurement
gazebo::msgs::Vector3d* v = new gazebo::msgs::Vector3d();
v->set_x(bodyVel.X());
v->set_y(bodyVel.Y());
v->set_z(bodyVel.Z());
dvlGazeboMsg.set_allocated_linear_velocity(v);
this->gazeboSensorOutputPub->Publish(dvlGazeboMsg);
}
// Publish ROS DVL message
this->dvlROSMsg.header.stamp.sec = _info.simTime.sec;
this->dvlROSMsg.header.stamp.nsec = _info.simTime.nsec;
this->dvlROSMsg.altitude = this->altitude;
this->dvlROSMsg.beams = this->dvlBeamMsgs;
this->dvlROSMsg.velocity.x = bodyVel.X();
this->dvlROSMsg.velocity.y = bodyVel.Y();
this->dvlROSMsg.velocity.z = bodyVel.Z();
this->rosSensorOutputPub.publish(this->dvlROSMsg);
this->twistROSMsg.header.stamp = this->dvlROSMsg.header.stamp;
this->twistROSMsg.twist.twist.linear.x = bodyVel.X();
this->twistROSMsg.twist.twist.linear.y = bodyVel.Y();
this->twistROSMsg.twist.twist.linear.z = bodyVel.Z();
this->twistPub.publish(this->twistROSMsg);
// Read the current simulation time
#if GAZEBO_MAJOR_VERSION >= 8
this->lastMeasurementTime = this->world->SimTime();
#else
this->lastMeasurementTime = this->world->GetSimTime();
#endif
return true;
}
/////////////////////////////////////////////////
void DVLROSPlugin::OnBeamCallback(const sensor_msgs::RangeConstPtr& _range0,
const sensor_msgs::RangeConstPtr& _range1,
const sensor_msgs::RangeConstPtr& _range2,
const sensor_msgs::RangeConstPtr& _range3)
{
if (_range0->range == _range0->min_range &&
_range1->range == _range1->min_range &&
_range2->range == _range2->min_range &&
_range3->range == _range3->min_range)
{
this->altitude = ALTITUDE_OUT_OF_RANGE;
return;
}
if (_range0->range == _range0->max_range &&
_range1->range == _range1->max_range &&
_range2->range == _range2->max_range &&
_range3->range == _range3->max_range)
{
this->altitude = ALTITUDE_OUT_OF_RANGE;
return;
}
// TODO Compute the altitude taking into account the vehicle's orientation
this->altitude =
0.25 * (_range0->range + _range1->range + _range2->range + _range3->range);
this->dvlBeamMsgs[0].range = _range0->range;
this->dvlBeamMsgs[1].range = _range1->range;
this->dvlBeamMsgs[2].range = _range2->range;
this->dvlBeamMsgs[3].range = _range3->range;
}
/////////////////////////////////////////////////
bool DVLROSPlugin::UpdateBeamTransforms()
{
if (this->beamPoses.size() == 4)
return true;
tf::StampedTransform beamTransform;
std::string targetFrame, sourceFrame;
bool success = true;
for (int i = 0; i < this->beamsLinkNames.size(); i++)
{
sourceFrame = this->beamsLinkNames[i];
if (!this->enableLocalNEDFrame)
targetFrame = this->link->GetName();
else
targetFrame = tfLocalNEDFrame.child_frame_id_;
try
{
ros::Time now = ros::Time::now();
this->transformListener.lookupTransform(
targetFrame, sourceFrame, ros::Time(0),
beamTransform);
}
catch(tf::TransformException &ex)
{
success = false;
break;
}
ignition::math::Pose3d pose;
pose.Pos() = ignition::math::Vector3d(
beamTransform.getOrigin().x(),
beamTransform.getOrigin().y(),
beamTransform.getOrigin().z());
pose.Rot() = ignition::math::Quaterniond(
beamTransform.getRotation().getW(),
beamTransform.getRotation().getAxis().x(),
beamTransform.getRotation().getAxis().y(),
beamTransform.getRotation().getAxis().z());
this->dvlBeamMsgs[i].pose = geometry_msgs::PoseStamped();
this->dvlBeamMsgs[i].pose.header.stamp = ros::Time::now();
this->dvlBeamMsgs[i].pose.header.frame_id = sourceFrame;
this->dvlBeamMsgs[i].pose.pose.position.x = beamTransform.getOrigin().x();
this->dvlBeamMsgs[i].pose.pose.position.y = beamTransform.getOrigin().y();
this->dvlBeamMsgs[i].pose.pose.position.z = beamTransform.getOrigin().z();
this->dvlBeamMsgs[i].pose.pose.orientation.x = beamTransform.getRotation().getAxis().x();
this->dvlBeamMsgs[i].pose.pose.orientation.y = beamTransform.getRotation().getAxis().y();
this->dvlBeamMsgs[i].pose.pose.orientation.z = beamTransform.getRotation().getAxis().z();
this->dvlBeamMsgs[i].pose.pose.orientation.w = beamTransform.getRotation().getW();
this->beamPoses.push_back(pose);
}
return success;
}
/////////////////////////////////////////////////
GZ_REGISTER_MODEL_PLUGIN(DVLROSPlugin)
}