forked from autowarefoundation/autoware.universe
-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathgnss_poser_core.cpp
398 lines (347 loc) · 15.7 KB
/
gnss_poser_core.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
// Copyright 2020 Tier IV, 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 "gnss_poser/gnss_poser_core.hpp"
#include <geography_utils/height.hpp>
#include <geography_utils/projection.hpp>
#include <autoware_sensing_msgs/msg/gnss_ins_orientation_stamped.hpp>
#include <algorithm>
#include <memory>
#include <string>
#include <vector>
namespace gnss_poser
{
GNSSPoser::GNSSPoser(const rclcpp::NodeOptions & node_options)
: rclcpp::Node("gnss_poser", node_options),
tf2_listener_(tf2_buffer_),
tf2_broadcaster_(*this),
base_frame_(declare_parameter<std::string>("base_frame")),
gnss_base_frame_(declare_parameter<std::string>("gnss_base_frame")),
map_frame_(declare_parameter<std::string>("map_frame")),
use_gnss_ins_orientation_(declare_parameter<bool>("use_gnss_ins_orientation")),
msg_gnss_ins_orientation_stamped_(
std::make_shared<autoware_sensing_msgs::msg::GnssInsOrientationStamped>()),
gnss_pose_pub_method(declare_parameter<int>("gnss_pose_pub_method"))
{
// Subscribe to map_projector_info topic
const auto adaptor = component_interface_utils::NodeAdaptor(this);
adaptor.init_sub(
sub_map_projector_info_,
[this](const MapProjectorInfo::Message::ConstSharedPtr msg) { callbackMapProjectorInfo(msg); });
int buff_epoch = declare_parameter<int>("buff_epoch");
position_buffer_.set_capacity(buff_epoch);
nav_sat_fix_sub_ = create_subscription<sensor_msgs::msg::NavSatFix>(
"fix", rclcpp::QoS{1}, std::bind(&GNSSPoser::callbackNavSatFix, this, std::placeholders::_1));
autoware_orientation_sub_ =
create_subscription<autoware_sensing_msgs::msg::GnssInsOrientationStamped>(
"autoware_orientation", rclcpp::QoS{1},
std::bind(&GNSSPoser::callbackGnssInsOrientationStamped, this, std::placeholders::_1));
pose_pub_ = create_publisher<geometry_msgs::msg::PoseStamped>("gnss_pose", rclcpp::QoS{1});
pose_cov_pub_ = create_publisher<geometry_msgs::msg::PoseWithCovarianceStamped>(
"gnss_pose_cov", rclcpp::QoS{1});
fixed_pub_ = create_publisher<tier4_debug_msgs::msg::BoolStamped>("gnss_fixed", rclcpp::QoS{1});
}
void GNSSPoser::callbackMapProjectorInfo(const MapProjectorInfo::Message::ConstSharedPtr msg)
{
projector_info_ = *msg;
received_map_projector_info_ = true;
}
void GNSSPoser::callbackNavSatFix(
const sensor_msgs::msg::NavSatFix::ConstSharedPtr nav_sat_fix_msg_ptr)
{
// Return immediately if map_projector_info has not been received yet.
if (!received_map_projector_info_) {
RCLCPP_WARN_THROTTLE(
this->get_logger(), *this->get_clock(), std::chrono::milliseconds(1000).count(),
"map_projector_info has not been received yet. Check if the map_projection_loader is "
"successfully launched.");
return;
}
// check fixed topic
const bool is_fixed = isFixed(nav_sat_fix_msg_ptr->status);
// publish is_fixed topic
tier4_debug_msgs::msg::BoolStamped is_fixed_msg;
is_fixed_msg.stamp = this->now();
is_fixed_msg.data = is_fixed;
fixed_pub_->publish(is_fixed_msg);
if (!is_fixed) {
RCLCPP_WARN_STREAM_THROTTLE(
this->get_logger(), *this->get_clock(), std::chrono::milliseconds(1000).count(),
"Not Fixed Topic. Skipping Calculate.");
return;
}
// get position
geographic_msgs::msg::GeoPoint gps_point;
gps_point.latitude = nav_sat_fix_msg_ptr->latitude;
gps_point.longitude = nav_sat_fix_msg_ptr->longitude;
gps_point.altitude = nav_sat_fix_msg_ptr->altitude;
geometry_msgs::msg::Point position = geography_utils::project_forward(gps_point, projector_info_);
position.z = geography_utils::convert_height(
position.z, gps_point.latitude, gps_point.longitude, MapProjectorInfo::Message::WGS84,
projector_info_.vertical_datum);
geometry_msgs::msg::Pose gnss_antenna_pose{};
// publish pose immediately
if (!gnss_pose_pub_method) {
gnss_antenna_pose.position = position;
} else {
// fill position buffer
position_buffer_.push_front(position);
if (!position_buffer_.full()) {
RCLCPP_WARN_STREAM_THROTTLE(
this->get_logger(), *this->get_clock(), std::chrono::milliseconds(1000).count(),
"Buffering Position. Output Skipped.");
return;
}
// publish average pose or median pose of position buffer
gnss_antenna_pose.position = (gnss_pose_pub_method == 1) ? getAveragePosition(position_buffer_)
: getMedianPosition(position_buffer_);
}
// calc gnss antenna orientation
geometry_msgs::msg::Quaternion orientation;
if (use_gnss_ins_orientation_) {
orientation = msg_gnss_ins_orientation_stamped_->orientation.orientation;
} else {
static auto prev_position = gnss_antenna_pose.position;
orientation = getQuaternionByPositionDifference(gnss_antenna_pose.position, prev_position);
prev_position = gnss_antenna_pose.position;
}
gnss_antenna_pose.orientation = orientation;
tf2::Transform tf_map2gnss_antenna{};
tf2::fromMsg(gnss_antenna_pose, tf_map2gnss_antenna);
// get TF from gnss_antenna to base_link
auto tf_gnss_antenna2base_link_msg_ptr = std::make_shared<geometry_msgs::msg::TransformStamped>();
const std::string gnss_frame = nav_sat_fix_msg_ptr->header.frame_id;
getStaticTransform(
base_frame_, gnss_frame, tf_gnss_antenna2base_link_msg_ptr, nav_sat_fix_msg_ptr->header.stamp);
tf2::Transform tf_gnss_antenna2base_link{};
tf2::fromMsg(tf_gnss_antenna2base_link_msg_ptr->transform, tf_gnss_antenna2base_link);
// transform pose from gnss_antenna(in map frame) to base_link(in map frame)
tf2::Transform tf_map2base_link{};
tf_map2base_link = tf_map2gnss_antenna * tf_gnss_antenna2base_link;
geometry_msgs::msg::PoseStamped gnss_base_pose_msg{};
gnss_base_pose_msg.header.stamp = nav_sat_fix_msg_ptr->header.stamp;
gnss_base_pose_msg.header.frame_id = map_frame_;
tf2::toMsg(tf_map2base_link, gnss_base_pose_msg.pose);
// publish gnss_base_link pose in map frame
pose_pub_->publish(gnss_base_pose_msg);
// publish gnss_base_link pose_cov in map frame
geometry_msgs::msg::PoseWithCovarianceStamped gnss_base_pose_cov_msg;
gnss_base_pose_cov_msg.header = gnss_base_pose_msg.header;
gnss_base_pose_cov_msg.pose.pose = gnss_base_pose_msg.pose;
gnss_base_pose_cov_msg.pose.covariance[7 * 0] =
canGetCovariance(*nav_sat_fix_msg_ptr) ? nav_sat_fix_msg_ptr->position_covariance[0] : 10.0;
gnss_base_pose_cov_msg.pose.covariance[7 * 1] =
canGetCovariance(*nav_sat_fix_msg_ptr) ? nav_sat_fix_msg_ptr->position_covariance[4] : 10.0;
gnss_base_pose_cov_msg.pose.covariance[7 * 2] =
canGetCovariance(*nav_sat_fix_msg_ptr) ? nav_sat_fix_msg_ptr->position_covariance[8] : 10.0;
if (use_gnss_ins_orientation_) {
gnss_base_pose_cov_msg.pose.covariance[7 * 3] =
std::pow(msg_gnss_ins_orientation_stamped_->orientation.rmse_rotation_x, 2);
gnss_base_pose_cov_msg.pose.covariance[7 * 4] =
std::pow(msg_gnss_ins_orientation_stamped_->orientation.rmse_rotation_y, 2);
gnss_base_pose_cov_msg.pose.covariance[7 * 5] =
std::pow(msg_gnss_ins_orientation_stamped_->orientation.rmse_rotation_z, 2);
} else {
gnss_base_pose_cov_msg.pose.covariance[7 * 3] = 0.1;
gnss_base_pose_cov_msg.pose.covariance[7 * 4] = 0.1;
gnss_base_pose_cov_msg.pose.covariance[7 * 5] = 1.0;
}
pose_cov_pub_->publish(gnss_base_pose_cov_msg);
// broadcast map to gnss_base_link
publishTF(map_frame_, gnss_base_frame_, gnss_base_pose_msg);
}
void GNSSPoser::callbackGnssInsOrientationStamped(
const autoware_sensing_msgs::msg::GnssInsOrientationStamped::ConstSharedPtr msg)
{
*msg_gnss_ins_orientation_stamped_ = *msg;
}
bool GNSSPoser::isFixed(const sensor_msgs::msg::NavSatStatus & nav_sat_status_msg)
{
return nav_sat_status_msg.status >= sensor_msgs::msg::NavSatStatus::STATUS_FIX;
}
bool GNSSPoser::canGetCovariance(const sensor_msgs::msg::NavSatFix & nav_sat_fix_msg)
{
return nav_sat_fix_msg.position_covariance_type >
sensor_msgs::msg::NavSatFix::COVARIANCE_TYPE_UNKNOWN;
}
geometry_msgs::msg::Point GNSSPoser::getMedianPosition(
const boost::circular_buffer<geometry_msgs::msg::Point> & position_buffer)
{
auto getMedian = [](std::vector<double> array) {
std::sort(std::begin(array), std::end(array));
const size_t median_index = array.size() / 2;
double median = (array.size() % 2)
? (array.at(median_index))
: ((array.at(median_index) + array.at(median_index - 1)) / 2);
return median;
};
std::vector<double> array_x;
std::vector<double> array_y;
std::vector<double> array_z;
for (const auto & position : position_buffer) {
array_x.push_back(position.x);
array_y.push_back(position.y);
array_z.push_back(position.z);
}
geometry_msgs::msg::Point median_point;
median_point.x = getMedian(array_x);
median_point.y = getMedian(array_y);
median_point.z = getMedian(array_z);
return median_point;
}
geometry_msgs::msg::Point GNSSPoser::getAveragePosition(
const boost::circular_buffer<geometry_msgs::msg::Point> & position_buffer)
{
std::vector<double> array_x;
std::vector<double> array_y;
std::vector<double> array_z;
for (const auto & position : position_buffer) {
array_x.push_back(position.x);
array_y.push_back(position.y);
array_z.push_back(position.z);
}
geometry_msgs::msg::Point average_point;
average_point.x =
std::reduce(array_x.begin(), array_x.end()) / static_cast<double>(array_x.size());
average_point.y =
std::reduce(array_y.begin(), array_y.end()) / static_cast<double>(array_y.size());
average_point.z =
std::reduce(array_z.begin(), array_z.end()) / static_cast<double>(array_z.size());
return average_point;
}
geometry_msgs::msg::Quaternion GNSSPoser::getQuaternionByHeading(const int heading)
{
int heading_conv = 0;
// convert heading[0(North)~360] to yaw[-M_PI(West)~M_PI]
if (heading >= 0 && heading <= 27000000) {
heading_conv = 9000000 - heading;
} else {
heading_conv = 45000000 - heading;
}
const double yaw = (heading_conv * 1e-5) * M_PI / 180.0;
tf2::Quaternion quaternion;
quaternion.setRPY(0, 0, yaw);
return tf2::toMsg(quaternion);
}
geometry_msgs::msg::Quaternion GNSSPoser::getQuaternionByPositionDifference(
const geometry_msgs::msg::Point & point, const geometry_msgs::msg::Point & prev_point)
{
const double yaw = std::atan2(point.y - prev_point.y, point.x - prev_point.x);
tf2::Quaternion quaternion;
quaternion.setRPY(0, 0, yaw);
return tf2::toMsg(quaternion);
}
bool GNSSPoser::getTransform(
const std::string & target_frame, const std::string & source_frame,
const geometry_msgs::msg::TransformStamped::SharedPtr transform_stamped_ptr)
{
if (target_frame == source_frame) {
transform_stamped_ptr->header.stamp = this->now();
transform_stamped_ptr->header.frame_id = target_frame;
transform_stamped_ptr->child_frame_id = source_frame;
transform_stamped_ptr->transform.translation.x = 0.0;
transform_stamped_ptr->transform.translation.y = 0.0;
transform_stamped_ptr->transform.translation.z = 0.0;
transform_stamped_ptr->transform.rotation.x = 0.0;
transform_stamped_ptr->transform.rotation.y = 0.0;
transform_stamped_ptr->transform.rotation.z = 0.0;
transform_stamped_ptr->transform.rotation.w = 1.0;
return true;
}
try {
*transform_stamped_ptr =
tf2_buffer_.lookupTransform(target_frame, source_frame, tf2::TimePointZero);
} catch (tf2::TransformException & ex) {
RCLCPP_WARN_STREAM_THROTTLE(
this->get_logger(), *this->get_clock(), std::chrono::milliseconds(1000).count(), ex.what());
RCLCPP_WARN_STREAM_THROTTLE(
this->get_logger(), *this->get_clock(), std::chrono::milliseconds(1000).count(),
"Please publish TF " << target_frame.c_str() << " to " << source_frame.c_str());
transform_stamped_ptr->header.stamp = this->now();
transform_stamped_ptr->header.frame_id = target_frame;
transform_stamped_ptr->child_frame_id = source_frame;
transform_stamped_ptr->transform.translation.x = 0.0;
transform_stamped_ptr->transform.translation.y = 0.0;
transform_stamped_ptr->transform.translation.z = 0.0;
transform_stamped_ptr->transform.rotation.x = 0.0;
transform_stamped_ptr->transform.rotation.y = 0.0;
transform_stamped_ptr->transform.rotation.z = 0.0;
transform_stamped_ptr->transform.rotation.w = 1.0;
return false;
}
return true;
}
bool GNSSPoser::getStaticTransform(
const std::string & target_frame, const std::string & source_frame,
const geometry_msgs::msg::TransformStamped::SharedPtr transform_stamped_ptr,
const builtin_interfaces::msg::Time & stamp)
{
if (target_frame == source_frame) {
transform_stamped_ptr->header.stamp = stamp;
transform_stamped_ptr->header.frame_id = target_frame;
transform_stamped_ptr->child_frame_id = source_frame;
transform_stamped_ptr->transform.translation.x = 0.0;
transform_stamped_ptr->transform.translation.y = 0.0;
transform_stamped_ptr->transform.translation.z = 0.0;
transform_stamped_ptr->transform.rotation.x = 0.0;
transform_stamped_ptr->transform.rotation.y = 0.0;
transform_stamped_ptr->transform.rotation.z = 0.0;
transform_stamped_ptr->transform.rotation.w = 1.0;
return true;
}
try {
*transform_stamped_ptr = tf2_buffer_.lookupTransform(
target_frame, source_frame,
tf2::TimePoint(std::chrono::seconds(stamp.sec) + std::chrono::nanoseconds(stamp.nanosec)));
} catch (tf2::TransformException & ex) {
RCLCPP_WARN_STREAM_THROTTLE(
this->get_logger(), *this->get_clock(), std::chrono::milliseconds(1000).count(), ex.what());
RCLCPP_WARN_STREAM_THROTTLE(
this->get_logger(), *this->get_clock(), std::chrono::milliseconds(1000).count(),
"Please publish TF " << target_frame.c_str() << " to " << source_frame.c_str());
transform_stamped_ptr->header.stamp = stamp;
transform_stamped_ptr->header.frame_id = target_frame;
transform_stamped_ptr->child_frame_id = source_frame;
transform_stamped_ptr->transform.translation.x = 0.0;
transform_stamped_ptr->transform.translation.y = 0.0;
transform_stamped_ptr->transform.translation.z = 0.0;
transform_stamped_ptr->transform.rotation.x = 0.0;
transform_stamped_ptr->transform.rotation.y = 0.0;
transform_stamped_ptr->transform.rotation.z = 0.0;
transform_stamped_ptr->transform.rotation.w = 1.0;
return false;
}
return true;
}
void GNSSPoser::publishTF(
const std::string & frame_id, const std::string & child_frame_id,
const geometry_msgs::msg::PoseStamped & pose_msg)
{
geometry_msgs::msg::TransformStamped transform_stamped;
transform_stamped.header.frame_id = frame_id;
transform_stamped.child_frame_id = child_frame_id;
transform_stamped.header.stamp = pose_msg.header.stamp;
transform_stamped.transform.translation.x = pose_msg.pose.position.x;
transform_stamped.transform.translation.y = pose_msg.pose.position.y;
transform_stamped.transform.translation.z = pose_msg.pose.position.z;
tf2::Quaternion tf_quaternion;
tf2::fromMsg(pose_msg.pose.orientation, tf_quaternion);
transform_stamped.transform.rotation.x = tf_quaternion.x();
transform_stamped.transform.rotation.y = tf_quaternion.y();
transform_stamped.transform.rotation.z = tf_quaternion.z();
transform_stamped.transform.rotation.w = tf_quaternion.w();
tf2_broadcaster_.sendTransform(transform_stamped);
}
} // namespace gnss_poser
#include <rclcpp_components/register_node_macro.hpp>
RCLCPP_COMPONENTS_REGISTER_NODE(gnss_poser::GNSSPoser)