Skip to content

Commit 2f761e1

Browse files
feat(imu_corrector): changed publication algorithm and content of /diagnostics in gyro_bias_estimator (#6139)
* Let diagnostics be updated even if expections occur in timer_callback Signed-off-by: TaikiYamada4 <taiki.yamada@tier4.jp> * Fixed the summary message to be "Not updated" when the gyro bias is not updated. Fixed typo. Signed-off-by: TaikiYamada4 <taiki.yamada@tier4.jp> * Removed force_update() since updater_ originaly updates diagnostics automatically. Set the update peirod of diagnostics to be same to the timer_callback. Changed words of the diagnostics message a bit. Signed-off-by: TaikiYamada4 <taiki.yamada@tier4.jp> * style(pre-commit): autofix * Let the period of diagnostics publication independent to timer. Let update_diagnostics to be simple. Signed-off-by: TaikiYamada4 <taiki.yamada@tier4.jp> * style(pre-commit): autofix * Fixed typo Signed-off-by: TaikiYamada4 <taiki.yamada@tier4.jp> * style(pre-commit): autofix * Added setPeriod after force_update to reset the timer of updater_. Then, there will be no duplicates of diagnostics. Signed-off-by: TaikiYamada4 <taiki.yamada@tier4.jp> * style(pre-commit): autofix * Set diagnostics values to have the same precision Added gyro_bias_threshold_ to the diagnostics Signed-off-by: TaikiYamada4 <taiki.yamada@tier4.jp> * Updated README.md to match the paramters in gyro_bias_estimator Signed-off-by: TaikiYamada4 <taiki.yamada@tier4.jp> * style(pre-commit): autofix --------- Signed-off-by: TaikiYamada4 <taiki.yamada@tier4.jp> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 685b5b6 commit 2f761e1

File tree

4 files changed

+89
-33
lines changed

4 files changed

+89
-33
lines changed

sensing/imu_corrector/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ In the future, with careful implementation for pose errors, the IMU bias estimat
7070

7171
### Parameters
7272

73+
Note that this node also uses `angular_velocity_offset_x`, `angular_velocity_offset_y`, `angular_velocity_offset_z` parameters from `imu_corrector.param.yaml`.
74+
7375
| Name | Type | Description |
7476
| ------------------------------------- | ------ | ------------------------------------------------------------------------------------------- |
75-
| `angular_velocity_offset_x` | double | roll rate offset in imu_link [rad/s] |
76-
| `angular_velocity_offset_y` | double | pitch rate offset imu_link [rad/s] |
77-
| `angular_velocity_offset_z` | double | yaw rate offset imu_link [rad/s] |
7877
| `gyro_bias_threshold` | double | threshold of the bias of the gyroscope [rad/s] |
7978
| `timer_callback_interval_sec` | double | seconds about the timer callback function [sec] |
79+
| `diagnostics_updater_interval_sec` | double | period of the diagnostics updater [sec] |
8080
| `straight_motion_ang_vel_upper_limit` | double | upper limit of yaw angular velocity, beyond which motion is not considered straight [rad/s] |

sensing/imu_corrector/config/gyro_bias_estimator.param.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
ros__parameters:
33
gyro_bias_threshold: 0.0015 # [rad/s]
44
timer_callback_interval_sec: 0.5 # [sec]
5+
diagnostics_updater_interval_sec: 0.5 # [sec]
56
straight_motion_ang_vel_upper_limit: 0.015 # [rad/s]

sensing/imu_corrector/src/gyro_bias_estimator.cpp

+69-30
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,16 @@ GyroBiasEstimator::GyroBiasEstimator()
3030
angular_velocity_offset_y_(declare_parameter<double>("angular_velocity_offset_y")),
3131
angular_velocity_offset_z_(declare_parameter<double>("angular_velocity_offset_z")),
3232
timer_callback_interval_sec_(declare_parameter<double>("timer_callback_interval_sec")),
33+
diagnostics_updater_interval_sec_(declare_parameter<double>("diagnostics_updater_interval_sec")),
3334
straight_motion_ang_vel_upper_limit_(
3435
declare_parameter<double>("straight_motion_ang_vel_upper_limit")),
3536
updater_(this),
3637
gyro_bias_(std::nullopt)
3738
{
3839
updater_.setHardwareID(get_name());
3940
updater_.add("gyro_bias_validator", this, &GyroBiasEstimator::update_diagnostics);
41+
// diagnostic_updater is designed to be updated at the same rate as the timer
42+
updater_.setPeriod(diagnostics_updater_interval_sec_);
4043

4144
gyro_bias_estimation_module_ = std::make_unique<GyroBiasEstimationModule>();
4245

@@ -57,6 +60,18 @@ GyroBiasEstimator::GyroBiasEstimator()
5760
this->get_node_timers_interface()->add_timer(timer_, nullptr);
5861

5962
transform_listener_ = std::make_shared<tier4_autoware_utils::TransformListener>(this);
63+
64+
// initialize diagnostics_info_
65+
{
66+
diagnostics_info_.level = diagnostic_msgs::msg::DiagnosticStatus::OK;
67+
diagnostics_info_.summary_message = "Not initialized";
68+
diagnostics_info_.gyro_bias_x_for_imu_corrector = std::nan("");
69+
diagnostics_info_.gyro_bias_y_for_imu_corrector = std::nan("");
70+
diagnostics_info_.gyro_bias_z_for_imu_corrector = std::nan("");
71+
diagnostics_info_.estimated_gyro_bias_x = std::nan("");
72+
diagnostics_info_.estimated_gyro_bias_y = std::nan("");
73+
diagnostics_info_.estimated_gyro_bias_z = std::nan("");
74+
}
6075
}
6176

6277
void GyroBiasEstimator::callback_imu(const Imu::ConstSharedPtr imu_msg_ptr)
@@ -99,6 +114,7 @@ void GyroBiasEstimator::callback_odom(const Odometry::ConstSharedPtr odom_msg_pt
99114
void GyroBiasEstimator::timer_callback()
100115
{
101116
if (pose_buf_.empty()) {
117+
diagnostics_info_.summary_message = "Skipped update (pose_buf is empty)";
102118
return;
103119
}
104120

@@ -112,6 +128,7 @@ void GyroBiasEstimator::timer_callback()
112128
const rclcpp::Time t0_rclcpp_time = rclcpp::Time(pose_buf.front().header.stamp);
113129
const rclcpp::Time t1_rclcpp_time = rclcpp::Time(pose_buf.back().header.stamp);
114130
if (t1_rclcpp_time <= t0_rclcpp_time) {
131+
diagnostics_info_.summary_message = "Skipped update (pose_buf is not in chronological order)";
115132
return;
116133
}
117134

@@ -127,6 +144,7 @@ void GyroBiasEstimator::timer_callback()
127144
// Check gyro data size
128145
// Data size must be greater than or equal to 2 since the time difference will be taken later
129146
if (gyro_filtered.size() <= 1) {
147+
diagnostics_info_.summary_message = "Skipped update (gyro_filtered size is less than 2)";
130148
return;
131149
}
132150

@@ -140,6 +158,8 @@ void GyroBiasEstimator::timer_callback()
140158
const double yaw_vel = yaw_diff / time_diff;
141159
const bool is_straight = (yaw_vel < straight_motion_ang_vel_upper_limit_);
142160
if (!is_straight) {
161+
diagnostics_info_.summary_message =
162+
"Skipped update (yaw angular velocity is greater than straight_motion_ang_vel_upper_limit)";
143163
return;
144164
}
145165

@@ -151,12 +171,45 @@ void GyroBiasEstimator::timer_callback()
151171
if (!tf_base2imu_ptr) {
152172
RCLCPP_ERROR(
153173
this->get_logger(), "Please publish TF %s to %s", imu_frame_.c_str(), output_frame_.c_str());
174+
175+
diagnostics_info_.summary_message = "Skipped update (tf between base and imu is not available)";
154176
return;
155177
}
178+
156179
gyro_bias_ =
157180
transform_vector3(gyro_bias_estimation_module_->get_bias_base_link(), *tf_base2imu_ptr);
158181

182+
validate_gyro_bias();
159183
updater_.force_update();
184+
updater_.setPeriod(diagnostics_updater_interval_sec_); // to reset timer inside the updater
185+
}
186+
187+
void GyroBiasEstimator::validate_gyro_bias()
188+
{
189+
// Calculate diagnostics key-values
190+
diagnostics_info_.gyro_bias_x_for_imu_corrector = gyro_bias_.value().x;
191+
diagnostics_info_.gyro_bias_y_for_imu_corrector = gyro_bias_.value().y;
192+
diagnostics_info_.gyro_bias_z_for_imu_corrector = gyro_bias_.value().z;
193+
diagnostics_info_.estimated_gyro_bias_x = gyro_bias_.value().x - angular_velocity_offset_x_;
194+
diagnostics_info_.estimated_gyro_bias_y = gyro_bias_.value().y - angular_velocity_offset_y_;
195+
diagnostics_info_.estimated_gyro_bias_z = gyro_bias_.value().z - angular_velocity_offset_z_;
196+
197+
// Validation
198+
const bool is_bias_small_enough =
199+
std::abs(diagnostics_info_.estimated_gyro_bias_x) < gyro_bias_threshold_ &&
200+
std::abs(diagnostics_info_.estimated_gyro_bias_y) < gyro_bias_threshold_ &&
201+
std::abs(diagnostics_info_.estimated_gyro_bias_z) < gyro_bias_threshold_;
202+
203+
// Update diagnostics
204+
if (is_bias_small_enough) {
205+
diagnostics_info_.level = diagnostic_msgs::msg::DiagnosticStatus::OK;
206+
diagnostics_info_.summary_message = "Successfully updated";
207+
} else {
208+
diagnostics_info_.level = diagnostic_msgs::msg::DiagnosticStatus::WARN;
209+
diagnostics_info_.summary_message =
210+
"Gyro bias may be incorrect. Please calibrate IMU and reflect the result in imu_corrector. "
211+
"You may also use the output of gyro_bias_estimator.";
212+
}
160213
}
161214

162215
geometry_msgs::msg::Vector3 GyroBiasEstimator::transform_vector3(
@@ -172,36 +225,22 @@ geometry_msgs::msg::Vector3 GyroBiasEstimator::transform_vector3(
172225

173226
void GyroBiasEstimator::update_diagnostics(diagnostic_updater::DiagnosticStatusWrapper & stat)
174227
{
175-
if (gyro_bias_ == std::nullopt) {
176-
stat.add("gyro_bias", "Bias estimation is not yet ready because of insufficient data.");
177-
stat.summary(diagnostic_msgs::msg::DiagnosticStatus::OK, "Not initialized");
178-
} else {
179-
stat.add("gyro_bias_x_for_imu_corrector", gyro_bias_.value().x);
180-
stat.add("gyro_bias_y_for_imu_corrector", gyro_bias_.value().y);
181-
stat.add("gyro_bias_z_for_imu_corrector", gyro_bias_.value().z);
182-
183-
stat.add("estimated_gyro_bias_x", gyro_bias_.value().x - angular_velocity_offset_x_);
184-
stat.add("estimated_gyro_bias_y", gyro_bias_.value().y - angular_velocity_offset_y_);
185-
stat.add("estimated_gyro_bias_z", gyro_bias_.value().z - angular_velocity_offset_z_);
186-
187-
// Validation
188-
const bool is_bias_small_enough =
189-
std::abs(gyro_bias_.value().x - angular_velocity_offset_x_) < gyro_bias_threshold_ &&
190-
std::abs(gyro_bias_.value().y - angular_velocity_offset_y_) < gyro_bias_threshold_ &&
191-
std::abs(gyro_bias_.value().z - angular_velocity_offset_z_) < gyro_bias_threshold_;
192-
193-
// Update diagnostics
194-
if (is_bias_small_enough) {
195-
stat.add("gyro_bias", "OK");
196-
stat.summary(diagnostic_msgs::msg::DiagnosticStatus::OK, "OK");
197-
} else {
198-
stat.add(
199-
"gyro_bias",
200-
"Gyro bias may be incorrect. Please calibrate IMU and reflect the result in "
201-
"imu_corrector. You may also use the output of gyro_bias_estimator.");
202-
stat.summary(diagnostic_msgs::msg::DiagnosticStatus::WARN, "WARN");
203-
}
204-
}
228+
auto f = [](const double & value) {
229+
std::stringstream ss;
230+
ss << std::fixed << std::setprecision(8) << value;
231+
return ss.str();
232+
};
233+
234+
stat.summary(diagnostics_info_.level, diagnostics_info_.summary_message);
235+
stat.add("gyro_bias_x_for_imu_corrector", f(diagnostics_info_.gyro_bias_x_for_imu_corrector));
236+
stat.add("gyro_bias_y_for_imu_corrector", f(diagnostics_info_.gyro_bias_y_for_imu_corrector));
237+
stat.add("gyro_bias_z_for_imu_corrector", f(diagnostics_info_.gyro_bias_z_for_imu_corrector));
238+
239+
stat.add("estimated_gyro_bias_x", f(diagnostics_info_.estimated_gyro_bias_x));
240+
stat.add("estimated_gyro_bias_y", f(diagnostics_info_.estimated_gyro_bias_y));
241+
stat.add("estimated_gyro_bias_z", f(diagnostics_info_.estimated_gyro_bias_z));
242+
243+
stat.add("gyro_bias_threshold", f(gyro_bias_threshold_));
205244
}
206245

207246
} // namespace imu_corrector

sensing/imu_corrector/src/gyro_bias_estimator.hpp

+16
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class GyroBiasEstimator : public rclcpp::Node
4949
void callback_imu(const Imu::ConstSharedPtr imu_msg_ptr);
5050
void callback_odom(const Odometry::ConstSharedPtr odom_msg_ptr);
5151
void timer_callback();
52+
void validate_gyro_bias();
5253

5354
static geometry_msgs::msg::Vector3 transform_vector3(
5455
const geometry_msgs::msg::Vector3 & vec,
@@ -68,6 +69,7 @@ class GyroBiasEstimator : public rclcpp::Node
6869
const double angular_velocity_offset_y_;
6970
const double angular_velocity_offset_z_;
7071
const double timer_callback_interval_sec_;
72+
const double diagnostics_updater_interval_sec_;
7173
const double straight_motion_ang_vel_upper_limit_;
7274

7375
diagnostic_updater::Updater updater_;
@@ -80,6 +82,20 @@ class GyroBiasEstimator : public rclcpp::Node
8082

8183
std::vector<geometry_msgs::msg::Vector3Stamped> gyro_all_;
8284
std::vector<geometry_msgs::msg::PoseStamped> pose_buf_;
85+
86+
struct DiagnosticsInfo
87+
{
88+
unsigned char level;
89+
std::string summary_message;
90+
double gyro_bias_x_for_imu_corrector;
91+
double gyro_bias_y_for_imu_corrector;
92+
double gyro_bias_z_for_imu_corrector;
93+
double estimated_gyro_bias_x;
94+
double estimated_gyro_bias_y;
95+
double estimated_gyro_bias_z;
96+
};
97+
98+
DiagnosticsInfo diagnostics_info_;
8399
};
84100
} // namespace imu_corrector
85101

0 commit comments

Comments
 (0)