Skip to content

Commit 52e7511

Browse files
committed
Make test codes work. Create a threshold structure so that other packages can use the methods in pose_instability_detector
Signed-off-by: TaikiYamada4 <taiki.yamada@tier4.jp>
1 parent a772b15 commit 52e7511

File tree

3 files changed

+61
-49
lines changed

3 files changed

+61
-49
lines changed

localization/pose_instability_detector/include/autoware/pose_instability_detector/pose_instability_detector.hpp

+15-10
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
#include <deque>
2626
#include <vector>
27+
#include <tuple>
2728

2829
class PoseInstabilityDetector : public rclcpp::Node
2930
{
@@ -38,17 +39,26 @@ class PoseInstabilityDetector : public rclcpp::Node
3839
using DiagnosticArray = diagnostic_msgs::msg::DiagnosticArray;
3940

4041
public:
42+
struct ThresholdValues {
43+
double position_x;
44+
double position_y;
45+
double position_z;
46+
double angle_x;
47+
double angle_y;
48+
double angle_z;
49+
};
50+
4151
explicit PoseInstabilityDetector(const rclcpp::NodeOptions & options = rclcpp::NodeOptions());
52+
ThresholdValues calculate_threshold(double interval_sec);
53+
void dead_reckon(
54+
PoseStamped::SharedPtr & initial_pose, const rclcpp::Time & end_time,
55+
const std::deque<TwistWithCovarianceStamped> & twist_deque, Pose::SharedPtr & estimated_pose);
4256

4357
private:
4458
void callback_odometry(Odometry::ConstSharedPtr odometry_msg_ptr);
4559
void callback_twist(TwistWithCovarianceStamped::ConstSharedPtr twist_msg_ptr);
4660
void callback_timer();
4761

48-
void calculate_threshold(double interval_sec);
49-
void dead_reckon(
50-
PoseStamped::SharedPtr & initial_pose, const rclcpp::Time & end_time,
51-
const std::deque<TwistWithCovarianceStamped> & twist_deque, Pose::SharedPtr & estimated_pose);
5262
std::deque<TwistWithCovarianceStamped> clip_out_necessary_twist(
5363
const std::deque<TwistWithCovarianceStamped> & twist_buffer, const rclcpp::Time & start_time,
5464
const rclcpp::Time & end_time);
@@ -65,12 +75,7 @@ class PoseInstabilityDetector : public rclcpp::Node
6575
// parameters
6676
const double timer_period_; // [sec]
6777

68-
double threshold_diff_position_x_; // longitudinal
69-
double threshold_diff_position_y_; // lateral
70-
double threshold_diff_position_z_; // vertical
71-
double threshold_diff_angle_x_; // roll
72-
double threshold_diff_angle_y_; // pitch
73-
double threshold_diff_angle_z_; // yaw
78+
ThresholdValues threshold_values_;
7479

7580
const double heading_velocity_maximum_; // [m/s]
7681
const double heading_velocity_scale_factor_tolerance_; // [%]

localization/pose_instability_detector/src/pose_instability_detector.cpp

+30-27
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,11 @@ void PoseInstabilityDetector::callback_timer()
136136
diff_pose_pub_->publish(diff_pose);
137137

138138
// publish diagnostics
139-
calculate_threshold((latest_odometry_time - prev_odometry_time).seconds());
139+
ThresholdValues threshold_values = calculate_threshold((latest_odometry_time - prev_odometry_time).seconds());
140140

141-
const std::vector<double> thresholds = {threshold_diff_position_x_, threshold_diff_position_y_,
142-
threshold_diff_position_z_, threshold_diff_angle_x_,
143-
threshold_diff_angle_y_, threshold_diff_angle_z_};
141+
const std::vector<double> thresholds = {threshold_values.position_x, threshold_values.position_y,
142+
threshold_values.position_z, threshold_values.angle_x,
143+
threshold_values.angle_y, threshold_values.angle_z};
144144

145145
const std::vector<std::string> labels = {"diff_position_x", "diff_position_y", "diff_position_z",
146146
"diff_angle_x", "diff_angle_y", "diff_angle_z"};
@@ -176,7 +176,7 @@ void PoseInstabilityDetector::callback_timer()
176176
prev_odometry_ = latest_odometry_;
177177
}
178178

179-
void PoseInstabilityDetector::calculate_threshold(double interval_sec)
179+
PoseInstabilityDetector::ThresholdValues PoseInstabilityDetector::calculate_threshold(double interval_sec)
180180
{
181181
// Calculate maximum longitudinal difference
182182
const double longitudinal_difference =
@@ -227,12 +227,15 @@ void PoseInstabilityDetector::calculate_threshold(double interval_sec)
227227
const double yaw_difference = roll_difference;
228228

229229
// Set thresholds
230-
threshold_diff_position_x_ = longitudinal_difference + pose_estimator_longitudinal_tolerance_;
231-
threshold_diff_position_y_ = lateral_difference + pose_estimator_lateral_tolerance_;
232-
threshold_diff_position_z_ = vertical_difference + pose_estimator_vertical_tolerance_;
233-
threshold_diff_angle_x_ = roll_difference + pose_estimator_angular_tolerance_;
234-
threshold_diff_angle_y_ = pitch_difference + pose_estimator_angular_tolerance_;
235-
threshold_diff_angle_z_ = yaw_difference + pose_estimator_angular_tolerance_;
230+
ThresholdValues result_values;
231+
result_values.position_x = longitudinal_difference + pose_estimator_longitudinal_tolerance_;
232+
result_values.position_y = lateral_difference + pose_estimator_lateral_tolerance_;
233+
result_values.position_z = vertical_difference + pose_estimator_vertical_tolerance_;
234+
result_values.angle_x = roll_difference + pose_estimator_angular_tolerance_;
235+
result_values.angle_y = pitch_difference + pose_estimator_angular_tolerance_;
236+
result_values.angle_z = yaw_difference + pose_estimator_angular_tolerance_;
237+
238+
return result_values;
236239
}
237240

238241
void PoseInstabilityDetector::dead_reckon(
@@ -302,9 +305,25 @@ PoseInstabilityDetector::clip_out_necessary_twist(
302305
const std::deque<TwistWithCovarianceStamped> & twist_buffer, const rclcpp::Time & start_time,
303306
const rclcpp::Time & end_time)
304307
{
308+
// If there is only one element in the twist_buffer, return a deque that has the same twist
309+
// from the start till the end
310+
if (twist_buffer.size() == 1) {
311+
TwistWithCovarianceStamped twist = twist_buffer.front();
312+
std::deque<TwistWithCovarianceStamped> simple_twist_deque;
313+
314+
twist.header.stamp = start_time;
315+
simple_twist_deque.push_back(twist);
316+
317+
twist.header.stamp = end_time;
318+
simple_twist_deque.push_back(twist);
319+
320+
return simple_twist_deque;
321+
}
322+
305323
// get iterator to the element that is right before start_time (if it does not exist, start_it =
306324
// twist_buffer.begin())
307325
auto start_it = twist_buffer.begin();
326+
308327
for (auto it = twist_buffer.begin(); it != twist_buffer.end(); ++it) {
309328
if (rclcpp::Time(it->header.stamp) > start_time) {
310329
break;
@@ -326,21 +345,6 @@ PoseInstabilityDetector::clip_out_necessary_twist(
326345
// Create result deque
327346
std::deque<TwistWithCovarianceStamped> result_deque(start_it, end_it);
328347

329-
// If the result deque has only one element, return a deque that starts and ends with the same
330-
// element
331-
if (result_deque.size() == 1) {
332-
TwistWithCovarianceStamped twist = *start_it;
333-
result_deque.clear();
334-
335-
twist.header.stamp = start_time;
336-
result_deque.push_back(twist);
337-
338-
twist.header.stamp = end_time;
339-
result_deque.push_back(twist);
340-
341-
return result_deque;
342-
}
343-
344348
// If the first element is later than start_time, add the first element to the front of the
345349
// result_deque
346350
if (rclcpp::Time(result_deque.front().header.stamp) > start_time) {
@@ -395,7 +399,6 @@ PoseInstabilityDetector::clip_out_necessary_twist(
395399

396400
result_deque[result_deque.size() - 1].header.stamp = end_time;
397401
}
398-
399402
return result_deque;
400403
}
401404

localization/pose_instability_detector/test/test.cpp

+16-12
Original file line numberDiff line numberDiff line change
@@ -81,18 +81,18 @@ TEST_F(TestPoseInstabilityDetector, output_ok_when_twist_matches_odometry) // N
8181
timestamp.nanosec = 0;
8282
helper_->send_odometry_message(timestamp, 10.0, 0.0, 0.0);
8383

84+
// send the twist message1 (move 1m in x direction)
85+
timestamp.sec = 0;
86+
timestamp.nanosec = 5e8;
87+
helper_->send_twist_message(timestamp, 2.0, 0.0, 0.0);
88+
8489
// process the above message (by timer_callback)
8590
helper_->received_diagnostic_array_flag = false;
8691
while (!helper_->received_diagnostic_array_flag) {
8792
executor_.spin_some();
8893
std::this_thread::sleep_for(std::chrono::milliseconds(10));
8994
}
9095

91-
// send the twist message1 (move 1m in x direction)
92-
timestamp.sec = 0;
93-
timestamp.nanosec = 5e8;
94-
helper_->send_twist_message(timestamp, 2.0, 0.0, 0.0);
95-
9696
// send the twist message2 (move 1m in x direction)
9797
timestamp.sec = 1;
9898
timestamp.nanosec = 0;
@@ -101,7 +101,9 @@ TEST_F(TestPoseInstabilityDetector, output_ok_when_twist_matches_odometry) // N
101101
// send the second odometry message (finish x = 12)
102102
timestamp.sec = 2;
103103
timestamp.nanosec = 0;
104-
helper_->send_odometry_message(timestamp, 12.0, 0.0, 0.0);
104+
helper_->send_odometry_message(timestamp, 14.0, 0.0, 0.0);
105+
106+
executor_.spin_some();
105107

106108
// process the above messages (by timer_callback)
107109
helper_->received_diagnostic_array_flag = false;
@@ -124,18 +126,18 @@ TEST_F(TestPoseInstabilityDetector, output_warn_when_twist_is_too_small) // NOL
124126
timestamp.nanosec = 0;
125127
helper_->send_odometry_message(timestamp, 10.0, 0.0, 0.0);
126128

129+
// send the twist message1 (move 0.1m in x direction)
130+
timestamp.sec = 0;
131+
timestamp.nanosec = 5e8;
132+
helper_->send_twist_message(timestamp, 0.2, 0.0, 0.0);
133+
127134
// process the above message (by timer_callback)
128135
helper_->received_diagnostic_array_flag = false;
129136
while (!helper_->received_diagnostic_array_flag) {
130137
executor_.spin_some();
131138
std::this_thread::sleep_for(std::chrono::milliseconds(10));
132139
}
133140

134-
// send the twist message1 (move 0.1m in x direction)
135-
timestamp.sec = 0;
136-
timestamp.nanosec = 5e8;
137-
helper_->send_twist_message(timestamp, 0.2, 0.0, 0.0);
138-
139141
// send the twist message2 (move 0.1m in x direction)
140142
timestamp.sec = 1;
141143
timestamp.nanosec = 0;
@@ -144,7 +146,9 @@ TEST_F(TestPoseInstabilityDetector, output_warn_when_twist_is_too_small) // NOL
144146
// send the second odometry message (finish x = 12)
145147
timestamp.sec = 2;
146148
timestamp.nanosec = 0;
147-
helper_->send_odometry_message(timestamp, 12.0, 0.0, 0.0);
149+
helper_->send_odometry_message(timestamp, 14.0, 0.0, 0.0);
150+
151+
executor_.spin_some();
148152

149153
// process the above messages (by timer_callback)
150154
helper_->received_diagnostic_array_flag = false;

0 commit comments

Comments
 (0)