forked from autowarefoundation/autoware_universe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgyro_odometer_core.cpp
282 lines (240 loc) · 11 KB
/
gyro_odometer_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
// Copyright 2015-2019 Autoware Foundation
//
// 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 "gyro_odometer/gyro_odometer_core.hpp"
#include <rclcpp/rclcpp.hpp>
#ifdef ROS_DISTRO_GALACTIC
#include <tf2_geometry_msgs/tf2_geometry_msgs.h>
#else
#include <tf2_geometry_msgs/tf2_geometry_msgs.hpp>
#endif
#include <fmt/core.h>
#include <cmath>
#include <memory>
#include <string>
namespace autoware::gyro_odometer
{
GyroOdometerNode::GyroOdometerNode(const rclcpp::NodeOptions & node_options)
: Node("gyro_odometer", node_options),
output_frame_(declare_parameter<std::string>("output_frame")),
message_timeout_sec_(declare_parameter<double>("message_timeout_sec")),
vehicle_twist_arrived_(false),
imu_arrived_(false)
{
transform_listener_ = std::make_shared<tier4_autoware_utils::TransformListener>(this);
logger_configure_ = std::make_unique<tier4_autoware_utils::LoggerLevelConfigure>(this);
vehicle_twist_sub_ = create_subscription<geometry_msgs::msg::TwistWithCovarianceStamped>(
"vehicle/twist_with_covariance", rclcpp::QoS{100},
std::bind(&GyroOdometerNode::callbackVehicleTwist, this, std::placeholders::_1));
imu_sub_ = create_subscription<sensor_msgs::msg::Imu>(
"imu", rclcpp::QoS{100},
std::bind(&GyroOdometerNode::callbackImu, this, std::placeholders::_1));
twist_raw_pub_ = create_publisher<geometry_msgs::msg::TwistStamped>("twist_raw", rclcpp::QoS{10});
twist_with_covariance_raw_pub_ = create_publisher<geometry_msgs::msg::TwistWithCovarianceStamped>(
"twist_with_covariance_raw", rclcpp::QoS{10});
twist_pub_ = create_publisher<geometry_msgs::msg::TwistStamped>("twist", rclcpp::QoS{10});
twist_with_covariance_pub_ = create_publisher<geometry_msgs::msg::TwistWithCovarianceStamped>(
"twist_with_covariance", rclcpp::QoS{10});
// TODO(YamatoAndo) createTimer
}
GyroOdometerNode::~GyroOdometerNode()
{
}
std::array<double, 9> transformCovariance(const std::array<double, 9> & cov)
{
using COV_IDX = tier4_autoware_utils::xyz_covariance_index::XYZ_COV_IDX;
double max_cov = std::max({cov[COV_IDX::X_X], cov[COV_IDX::Y_Y], cov[COV_IDX::Z_Z]});
std::array<double, 9> cov_transformed;
cov_transformed.fill(0.);
cov_transformed[COV_IDX::X_X] = max_cov;
cov_transformed[COV_IDX::Y_Y] = max_cov;
cov_transformed[COV_IDX::Z_Z] = max_cov;
return cov_transformed;
}
geometry_msgs::msg::TwistWithCovarianceStamped concatGyroAndOdometer(
const std::deque<geometry_msgs::msg::TwistWithCovarianceStamped> & vehicle_twist_queue,
const std::deque<sensor_msgs::msg::Imu> & gyro_queue)
{
using COV_IDX_XYZ = tier4_autoware_utils::xyz_covariance_index::XYZ_COV_IDX;
using COV_IDX_XYZRPY = tier4_autoware_utils::xyzrpy_covariance_index::XYZRPY_COV_IDX;
double vx_mean = 0;
geometry_msgs::msg::Vector3 gyro_mean{};
double vx_covariance_original = 0;
geometry_msgs::msg::Vector3 gyro_covariance_original{};
for (const auto & vehicle_twist : vehicle_twist_queue) {
vx_mean += vehicle_twist.twist.twist.linear.x;
vx_covariance_original += vehicle_twist.twist.covariance[0 * 6 + 0];
}
vx_mean /= vehicle_twist_queue.size();
vx_covariance_original /= vehicle_twist_queue.size();
for (const auto & gyro : gyro_queue) {
gyro_mean.x += gyro.angular_velocity.x;
gyro_mean.y += gyro.angular_velocity.y;
gyro_mean.z += gyro.angular_velocity.z;
gyro_covariance_original.x += gyro.angular_velocity_covariance[COV_IDX_XYZ::X_X];
gyro_covariance_original.y += gyro.angular_velocity_covariance[COV_IDX_XYZ::Y_Y];
gyro_covariance_original.z += gyro.angular_velocity_covariance[COV_IDX_XYZ::Z_Z];
}
gyro_mean.x /= gyro_queue.size();
gyro_mean.y /= gyro_queue.size();
gyro_mean.z /= gyro_queue.size();
gyro_covariance_original.x /= gyro_queue.size();
gyro_covariance_original.y /= gyro_queue.size();
gyro_covariance_original.z /= gyro_queue.size();
geometry_msgs::msg::TwistWithCovarianceStamped twist_with_cov;
const auto latest_vehicle_twist_stamp = rclcpp::Time(vehicle_twist_queue.back().header.stamp);
const auto latest_imu_stamp = rclcpp::Time(gyro_queue.back().header.stamp);
if (latest_vehicle_twist_stamp < latest_imu_stamp) {
twist_with_cov.header.stamp = latest_imu_stamp;
} else {
twist_with_cov.header.stamp = latest_vehicle_twist_stamp;
}
twist_with_cov.header.frame_id = gyro_queue.front().header.frame_id;
twist_with_cov.twist.twist.linear.x = vx_mean;
twist_with_cov.twist.twist.angular = gyro_mean;
// From a statistical point of view, here we reduce the covariances according to the number of
// observed data
twist_with_cov.twist.covariance[COV_IDX_XYZRPY::X_X] =
vx_covariance_original / vehicle_twist_queue.size();
twist_with_cov.twist.covariance[COV_IDX_XYZRPY::Y_Y] = 100000.0;
twist_with_cov.twist.covariance[COV_IDX_XYZRPY::Z_Z] = 100000.0;
twist_with_cov.twist.covariance[COV_IDX_XYZRPY::ROLL_ROLL] =
gyro_covariance_original.x / gyro_queue.size();
twist_with_cov.twist.covariance[COV_IDX_XYZRPY::PITCH_PITCH] =
gyro_covariance_original.y / gyro_queue.size();
twist_with_cov.twist.covariance[COV_IDX_XYZRPY::YAW_YAW] =
gyro_covariance_original.z / gyro_queue.size();
return twist_with_cov;
}
void GyroOdometerNode::callbackVehicleTwist(
const geometry_msgs::msg::TwistWithCovarianceStamped::ConstSharedPtr vehicle_twist_ptr)
{
vehicle_twist_arrived_ = true;
if (!imu_arrived_) {
RCLCPP_WARN_THROTTLE(this->get_logger(), *this->get_clock(), 1000, "Imu msg is not subscribed");
vehicle_twist_queue_.clear();
gyro_queue_.clear();
return;
}
const double twist_dt = std::abs((this->now() - vehicle_twist_ptr->header.stamp).seconds());
if (twist_dt > message_timeout_sec_) {
const std::string error_msg = fmt::format(
"Twist msg is timeout. twist_dt: {}[sec], tolerance {}[sec]", twist_dt, message_timeout_sec_);
RCLCPP_ERROR_THROTTLE(this->get_logger(), *this->get_clock(), 1000, error_msg.c_str());
vehicle_twist_queue_.clear();
gyro_queue_.clear();
return;
}
vehicle_twist_queue_.push_back(*vehicle_twist_ptr);
if (gyro_queue_.empty()) return;
const double imu_dt = std::abs((this->now() - gyro_queue_.back().header.stamp).seconds());
if (imu_dt > message_timeout_sec_) {
const std::string error_msg = fmt::format(
"Imu msg is timeout. twist_dt: {}[sec], tolerance {}[sec]", imu_dt, message_timeout_sec_);
RCLCPP_ERROR_THROTTLE(this->get_logger(), *this->get_clock(), 1000, error_msg.c_str());
vehicle_twist_queue_.clear();
gyro_queue_.clear();
return;
}
const geometry_msgs::msg::TwistWithCovarianceStamped twist_with_cov_raw =
concatGyroAndOdometer(vehicle_twist_queue_, gyro_queue_);
publishData(twist_with_cov_raw);
vehicle_twist_queue_.clear();
gyro_queue_.clear();
}
void GyroOdometerNode::callbackImu(const sensor_msgs::msg::Imu::ConstSharedPtr imu_msg_ptr)
{
imu_arrived_ = true;
if (!vehicle_twist_arrived_) {
RCLCPP_WARN_THROTTLE(
this->get_logger(), *this->get_clock(), 1000, "Twist msg is not subscribed");
vehicle_twist_queue_.clear();
gyro_queue_.clear();
return;
}
const double imu_dt = std::abs((this->now() - imu_msg_ptr->header.stamp).seconds());
if (imu_dt > message_timeout_sec_) {
const std::string error_msg = fmt::format(
"Imu msg is timeout. imu_dt: {}[sec], tolerance {}[sec]", imu_dt, message_timeout_sec_);
RCLCPP_ERROR_THROTTLE(this->get_logger(), *this->get_clock(), 1000, error_msg.c_str());
vehicle_twist_queue_.clear();
gyro_queue_.clear();
return;
}
geometry_msgs::msg::TransformStamped::ConstSharedPtr tf_imu2base_ptr =
transform_listener_->getLatestTransform(imu_msg_ptr->header.frame_id, output_frame_);
if (!tf_imu2base_ptr) {
RCLCPP_ERROR(
this->get_logger(), "Please publish TF %s to %s", output_frame_.c_str(),
(imu_msg_ptr->header.frame_id).c_str());
vehicle_twist_queue_.clear();
gyro_queue_.clear();
return;
}
geometry_msgs::msg::Vector3Stamped angular_velocity;
angular_velocity.header = imu_msg_ptr->header;
angular_velocity.vector = imu_msg_ptr->angular_velocity;
geometry_msgs::msg::Vector3Stamped transformed_angular_velocity;
transformed_angular_velocity.header = tf_imu2base_ptr->header;
tf2::doTransform(angular_velocity, transformed_angular_velocity, *tf_imu2base_ptr);
sensor_msgs::msg::Imu gyro_base_link;
gyro_base_link.header = imu_msg_ptr->header;
gyro_base_link.header.frame_id = output_frame_;
gyro_base_link.angular_velocity = transformed_angular_velocity.vector;
gyro_base_link.angular_velocity_covariance =
transformCovariance(imu_msg_ptr->angular_velocity_covariance);
gyro_queue_.push_back(gyro_base_link);
if (vehicle_twist_queue_.empty()) return;
const double twist_dt =
std::abs((this->now() - vehicle_twist_queue_.back().header.stamp).seconds());
if (twist_dt > message_timeout_sec_) {
const std::string error_msg = fmt::format(
"Twist msg is timeout. twist_dt: {}[sec], tolerance {}[sec]", twist_dt, message_timeout_sec_);
RCLCPP_ERROR_THROTTLE(this->get_logger(), *this->get_clock(), 1000, error_msg.c_str());
vehicle_twist_queue_.clear();
gyro_queue_.clear();
return;
}
const geometry_msgs::msg::TwistWithCovarianceStamped twist_with_cov_raw =
concatGyroAndOdometer(vehicle_twist_queue_, gyro_queue_);
publishData(twist_with_cov_raw);
vehicle_twist_queue_.clear();
gyro_queue_.clear();
}
void GyroOdometerNode::publishData(
const geometry_msgs::msg::TwistWithCovarianceStamped & twist_with_cov_raw)
{
geometry_msgs::msg::TwistStamped twist_raw;
twist_raw.header = twist_with_cov_raw.header;
twist_raw.twist = twist_with_cov_raw.twist.twist;
twist_raw_pub_->publish(twist_raw);
twist_with_covariance_raw_pub_->publish(twist_with_cov_raw);
geometry_msgs::msg::TwistWithCovarianceStamped twist_with_covariance = twist_with_cov_raw;
geometry_msgs::msg::TwistStamped twist = twist_raw;
// clear imu yaw bias if vehicle is stopped
if (
std::fabs(twist_with_cov_raw.twist.twist.angular.z) < 0.01 &&
std::fabs(twist_with_cov_raw.twist.twist.linear.x) < 0.01) {
twist.twist.angular.x = 0.0;
twist.twist.angular.y = 0.0;
twist.twist.angular.z = 0.0;
twist_with_covariance.twist.twist.angular.x = 0.0;
twist_with_covariance.twist.twist.angular.y = 0.0;
twist_with_covariance.twist.twist.angular.z = 0.0;
}
twist_pub_->publish(twist);
twist_with_covariance_pub_->publish(twist_with_covariance);
}
} // namespace autoware::gyro_odometer
#include <rclcpp_components/register_node_macro.hpp>
RCLCPP_COMPONENTS_REGISTER_NODE(autoware::gyro_odometer::GyroOdometerNode)