Skip to content

Commit f8ef146

Browse files
authored
feat(pid_longitudinal_controller): add smooth_stop mode in debug_values (#9681)
Signed-off-by: Takayuki Murooka <takayuki5168@gmail.com>
1 parent 33190c6 commit f8ef146

File tree

5 files changed

+54
-22
lines changed

5 files changed

+54
-22
lines changed

control/autoware_pid_longitudinal_controller/include/autoware/pid_longitudinal_controller/debug_values.hpp

+4
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class DebugValues
6363
ACC_CMD_ACC_FB_APPLIED = 33,
6464
PITCH_LPF_RAD = 34,
6565
PITCH_LPF_DEG = 35,
66+
SMOOTH_STOP_MODE = 36,
6667

6768
SIZE // this is the number of enum elements
6869
};
@@ -78,6 +79,9 @@ class DebugValues
7879
* @return array of all debug values
7980
*/
8081
std::array<double, static_cast<size_t>(TYPE::SIZE)> getValues() const { return m_values; }
82+
double getValue(const size_t index) const { return m_values.at(index); }
83+
double getValue(const TYPE type) const { return m_values.at(static_cast<size_t>(type)); }
84+
8185
/**
8286
* @brief set the given type to the given value
8387
* @param [in] type TYPE of the value

control/autoware_pid_longitudinal_controller/include/autoware/pid_longitudinal_controller/smooth_stop.hpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#ifndef AUTOWARE__PID_LONGITUDINAL_CONTROLLER__SMOOTH_STOP_HPP_
1616
#define AUTOWARE__PID_LONGITUDINAL_CONTROLLER__SMOOTH_STOP_HPP_
1717

18+
#include "autoware/pid_longitudinal_controller/debug_values.hpp"
1819
#include "rclcpp/rclcpp.hpp"
1920

2021
#include <experimental/optional> // NOLINT
@@ -85,7 +86,8 @@ class SmoothStop
8586
*/
8687
double calculate(
8788
const double stop_dist, const double current_vel, const double current_acc,
88-
const std::vector<std::pair<rclcpp::Time, double>> & vel_hist, const double delay_time);
89+
const std::vector<std::pair<rclcpp::Time, double>> & vel_hist, const double delay_time,
90+
DebugValues & debug_values);
8991

9092
private:
9193
struct Params
@@ -106,6 +108,8 @@ class SmoothStop
106108
};
107109
Params m_params;
108110

111+
enum class Mode { STRONG = 0, WEAK, WEAK_STOP, STRONG_STOP };
112+
109113
double m_strong_acc;
110114
rclcpp::Time m_weak_acc_time;
111115
bool m_is_set_params = false;

control/autoware_pid_longitudinal_controller/src/pid_longitudinal_controller.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -871,7 +871,7 @@ PidLongitudinalController::Motion PidLongitudinalController::calcCtrlCmd(
871871
} else if (m_control_state == ControlState::STOPPING) {
872872
raw_ctrl_cmd.acc = m_smooth_stop.calculate(
873873
control_data.stop_dist, control_data.current_motion.vel, control_data.current_motion.acc,
874-
m_vel_hist, m_delay_compensation_time);
874+
m_vel_hist, m_delay_compensation_time, m_debug_values);
875875
raw_ctrl_cmd.vel = m_stopped_state_params.vel;
876876

877877
RCLCPP_DEBUG(

control/autoware_pid_longitudinal_controller/src/smooth_stop.cpp

+9-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ std::experimental::optional<double> SmoothStop::calcTimeToStop(
116116

117117
double SmoothStop::calculate(
118118
const double stop_dist, const double current_vel, const double current_acc,
119-
const std::vector<std::pair<rclcpp::Time, double>> & vel_hist, const double delay_time)
119+
const std::vector<std::pair<rclcpp::Time, double>> & vel_hist, const double delay_time,
120+
DebugValues & debug_values)
120121
{
121122
if (!m_is_set_params) {
122123
throw std::runtime_error("Trying to calculate uninitialized SmoothStop");
@@ -132,8 +133,11 @@ double SmoothStop::calculate(
132133

133134
// when exceeding the stopline (stop_dist is negative in these cases.)
134135
if (stop_dist < m_params.strong_stop_dist) { // when exceeding the stopline much
136+
debug_values.setValues(
137+
DebugValues::TYPE::SMOOTH_STOP_MODE, static_cast<int>(Mode::STRONG_STOP));
135138
return m_params.strong_stop_acc;
136139
} else if (stop_dist < m_params.weak_stop_dist) { // when exceeding the stopline a bit
140+
debug_values.setValues(DebugValues::TYPE::SMOOTH_STOP_MODE, static_cast<int>(Mode::WEAK_STOP));
137141
return m_params.weak_stop_acc;
138142
}
139143

@@ -143,19 +147,23 @@ double SmoothStop::calculate(
143147
if (
144148
(time_to_stop && *time_to_stop > m_params.weak_stop_time + delay_time) ||
145149
(!time_to_stop && is_fast_vel)) {
150+
debug_values.setValues(DebugValues::TYPE::SMOOTH_STOP_MODE, static_cast<int>(Mode::STRONG));
146151
return m_strong_acc;
147152
}
148153

149154
m_weak_acc_time = rclcpp::Clock{RCL_ROS_TIME}.now();
155+
debug_values.setValues(DebugValues::TYPE::SMOOTH_STOP_MODE, static_cast<int>(Mode::WEAK));
150156
return m_params.weak_acc;
151157
}
152158

153159
// for 0.5 seconds after the car stopped
154160
if ((rclcpp::Clock{RCL_ROS_TIME}.now() - m_weak_acc_time).seconds() < 0.5) {
161+
debug_values.setValues(DebugValues::TYPE::SMOOTH_STOP_MODE, static_cast<int>(Mode::WEAK));
155162
return m_params.weak_acc;
156163
}
157164

158165
// when the car is not running
166+
debug_values.setValues(DebugValues::TYPE::SMOOTH_STOP_MODE, static_cast<int>(Mode::STRONG_STOP));
159167
return m_params.strong_stop_acc;
160168
}
161169
} // namespace autoware::motion::control::pid_longitudinal_controller

control/autoware_pid_longitudinal_controller/test/test_smooth_stop.cpp

+35-19
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
TEST(TestSmoothStop, calculate_stopping_acceleration)
2323
{
24+
using ::autoware::motion::control::pid_longitudinal_controller::DebugValues;
2425
using ::autoware::motion::control::pid_longitudinal_controller::SmoothStop;
2526
using rclcpp::Duration;
2627
using rclcpp::Time;
@@ -40,9 +41,10 @@ TEST(TestSmoothStop, calculate_stopping_acceleration)
4041
const double delay_time = 0.17;
4142

4243
SmoothStop ss;
44+
DebugValues debug_values;
4345

4446
// Cannot calculate before setting parameters
45-
EXPECT_THROW(ss.calculate(0.0, 0.0, 0.0, {}, delay_time), std::runtime_error);
47+
EXPECT_THROW(ss.calculate(0.0, 0.0, 0.0, {}, delay_time, debug_values), std::runtime_error);
4648

4749
ss.setParams(
4850
max_strong_acc, min_strong_acc, weak_acc, weak_stop_acc, strong_stop_acc, max_fast_vel,
@@ -62,54 +64,68 @@ TEST(TestSmoothStop, calculate_stopping_acceleration)
6264
stop_dist = strong_stop_dist - 0.1;
6365
current_vel = 2.0;
6466
ss.init(vel_in_target, stop_dist);
65-
accel = ss.calculate(stop_dist, current_vel, current_acc, velocity_history, delay_time);
67+
accel =
68+
ss.calculate(stop_dist, current_vel, current_acc, velocity_history, delay_time, debug_values);
6669
EXPECT_EQ(accel, strong_stop_acc);
70+
EXPECT_EQ(debug_values.getValue(DebugValues::TYPE::SMOOTH_STOP_MODE), 3);
6771

6872
// weak stop when the stop distance is below the threshold (but not bellow the strong_stop_dist)
6973
stop_dist = weak_stop_dist - 0.1;
7074
current_vel = 2.0;
7175
ss.init(vel_in_target, stop_dist);
72-
accel = ss.calculate(stop_dist, current_vel, current_acc, velocity_history, delay_time);
76+
accel =
77+
ss.calculate(stop_dist, current_vel, current_acc, velocity_history, delay_time, debug_values);
7378
EXPECT_EQ(accel, weak_stop_acc);
79+
EXPECT_EQ(debug_values.getValue(DebugValues::TYPE::SMOOTH_STOP_MODE), 2);
7480

7581
// if not running, weak accel for 0.5 seconds after the previous init or previous weak_acc
7682
rclcpp::Rate rate_quart(1.0 / 0.25);
7783
rclcpp::Rate rate_half(1.0 / 0.5);
7884
stop_dist = 0.0;
7985
current_vel = 0.0;
80-
EXPECT_EQ(
81-
ss.calculate(stop_dist, current_vel, current_acc, velocity_history, delay_time), weak_acc);
86+
accel =
87+
ss.calculate(stop_dist, current_vel, current_acc, velocity_history, delay_time, debug_values);
88+
EXPECT_EQ(accel, weak_acc);
89+
EXPECT_EQ(debug_values.getValue(DebugValues::TYPE::SMOOTH_STOP_MODE), 1);
8290
rate_quart.sleep();
83-
EXPECT_EQ(
84-
ss.calculate(stop_dist, current_vel, current_acc, velocity_history, delay_time), weak_acc);
91+
accel =
92+
ss.calculate(stop_dist, current_vel, current_acc, velocity_history, delay_time, debug_values);
93+
EXPECT_EQ(accel, weak_acc);
94+
EXPECT_EQ(debug_values.getValue(DebugValues::TYPE::SMOOTH_STOP_MODE), 1);
8595
rate_half.sleep();
86-
EXPECT_NE(
87-
ss.calculate(stop_dist, current_vel, current_acc, velocity_history, delay_time), weak_acc);
96+
accel =
97+
ss.calculate(stop_dist, current_vel, current_acc, velocity_history, delay_time, debug_values);
98+
EXPECT_NE(accel, weak_acc);
99+
EXPECT_NE(debug_values.getValue(DebugValues::TYPE::SMOOTH_STOP_MODE), 1);
88100

89101
// strong stop when the car is not running (and is at least 0.5seconds after initialization)
90-
EXPECT_EQ(
91-
ss.calculate(stop_dist, current_vel, current_acc, velocity_history, delay_time),
92-
strong_stop_acc);
102+
accel =
103+
ss.calculate(stop_dist, current_vel, current_acc, velocity_history, delay_time, debug_values);
104+
EXPECT_EQ(accel, strong_stop_acc);
105+
EXPECT_EQ(debug_values.getValue(DebugValues::TYPE::SMOOTH_STOP_MODE), 3);
93106

94107
// accel between min/max_strong_acc when the car is running:
95108
// not predicted to exceed the stop line and is predicted to stop after weak_stop_time + delay
96109
stop_dist = 1.0;
97110
current_vel = 1.0;
98111
vel_in_target = 1.0;
99112
ss.init(vel_in_target, stop_dist);
100-
EXPECT_EQ(
101-
ss.calculate(stop_dist, current_vel, current_acc, velocity_history, delay_time),
102-
max_strong_acc);
113+
accel =
114+
ss.calculate(stop_dist, current_vel, current_acc, velocity_history, delay_time, debug_values);
115+
EXPECT_EQ(accel, max_strong_acc);
116+
EXPECT_EQ(debug_values.getValue(DebugValues::TYPE::SMOOTH_STOP_MODE), 0);
103117

104118
vel_in_target = std::sqrt(2.0);
105119
ss.init(vel_in_target, stop_dist);
106-
EXPECT_EQ(
107-
ss.calculate(stop_dist, current_vel, current_acc, velocity_history, delay_time),
108-
min_strong_acc);
120+
accel =
121+
ss.calculate(stop_dist, current_vel, current_acc, velocity_history, delay_time, debug_values);
122+
EXPECT_EQ(accel, min_strong_acc);
123+
EXPECT_EQ(debug_values.getValue(DebugValues::TYPE::SMOOTH_STOP_MODE), 0);
109124

110125
for (double vel_in_target = 1.1; vel_in_target < std::sqrt(2.0); vel_in_target += 0.1) {
111126
ss.init(vel_in_target, stop_dist);
112-
accel = ss.calculate(stop_dist, current_vel, current_acc, velocity_history, delay_time);
127+
accel =
128+
ss.calculate(stop_dist, current_vel, current_acc, velocity_history, delay_time, debug_values);
113129
EXPECT_GT(accel, min_strong_acc);
114130
EXPECT_LT(accel, max_strong_acc);
115131
}

0 commit comments

Comments
 (0)