forked from autowarefoundation/autoware_universe
-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathcontroller_node.cpp
372 lines (319 loc) · 13.5 KB
/
controller_node.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
// Copyright 2021 Tier IV, Inc. All rights reserved.
//
// 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 "autoware/trajectory_follower_node/controller_node.hpp"
#include "autoware/mpc_lateral_controller/mpc_lateral_controller.hpp"
#include "autoware/pid_longitudinal_controller/pid_longitudinal_controller.hpp"
#include "autoware/pure_pursuit/autoware_pure_pursuit_lateral_controller.hpp"
#include "autoware_utils/ros/marker_helper.hpp"
#include <autoware/trajectory_follower_base/lateral_controller_base.hpp>
#include <algorithm>
#include <limits>
#include <memory>
#include <string>
#include <utility>
#include <vector>
namespace
{
template <typename T>
std::vector<T> resampleHorizonByZeroOrderHold(
const std::vector<T> & original_horizon, const double original_time_step_ms,
const double new_time_step_ms)
{
std::vector<T> resampled_horizon{};
const size_t step_factor = static_cast<size_t>(original_time_step_ms / new_time_step_ms);
const size_t resampled_size = original_horizon.size() * step_factor;
resampled_horizon.reserve(resampled_size);
for (const auto & command : original_horizon) {
for (size_t i = 0; i < step_factor; ++i) {
resampled_horizon.push_back(command);
}
}
return resampled_horizon;
}
} // namespace
namespace autoware::motion::control::trajectory_follower_node
{
Controller::Controller(const rclcpp::NodeOptions & node_options) : Node("controller", node_options)
{
using std::placeholders::_1;
const double ctrl_period = declare_parameter<double>("ctrl_period");
timeout_thr_sec_ = declare_parameter<double>("timeout_thr_sec");
// NOTE: It is possible that using control_horizon could be expected to enhance performance,
// but it is not a formal interface topic, only an experimental one.
// So it is disabled by default.
enable_control_cmd_horizon_pub_ =
declare_parameter<bool>("enable_control_cmd_horizon_pub", false);
diag_updater_->setHardwareID("trajectory_follower_node");
const auto lateral_controller_mode =
getLateralControllerMode(declare_parameter<std::string>("lateral_controller_mode"));
switch (lateral_controller_mode) {
case LateralControllerMode::MPC: {
lateral_controller_ =
std::make_shared<mpc_lateral_controller::MpcLateralController>(*this, diag_updater_);
break;
}
case LateralControllerMode::PURE_PURSUIT: {
lateral_controller_ =
std::make_shared<autoware::pure_pursuit::PurePursuitLateralController>(*this);
break;
}
default:
throw std::domain_error("[LateralController] invalid algorithm");
}
const auto longitudinal_controller_mode =
getLongitudinalControllerMode(declare_parameter<std::string>("longitudinal_controller_mode"));
switch (longitudinal_controller_mode) {
case LongitudinalControllerMode::PID: {
longitudinal_controller_ =
std::make_shared<pid_longitudinal_controller::PidLongitudinalController>(
*this, diag_updater_);
break;
}
default:
throw std::domain_error("[LongitudinalController] invalid algorithm");
}
control_cmd_pub_ = create_publisher<autoware_control_msgs::msg::Control>(
"~/output/control_cmd", rclcpp::QoS{1}.transient_local());
pub_processing_time_lat_ms_ =
create_publisher<Float64Stamped>("~/lateral/debug/processing_time_ms", 1);
pub_processing_time_lon_ms_ =
create_publisher<Float64Stamped>("~/longitudinal/debug/processing_time_ms", 1);
debug_marker_pub_ =
create_publisher<visualization_msgs::msg::MarkerArray>("~/output/debug_marker", rclcpp::QoS{1});
if (enable_control_cmd_horizon_pub_) {
control_cmd_horizon_pub_ = create_publisher<autoware_control_msgs::msg::ControlHorizon>(
"~/debug/control_cmd_horizon", 1);
}
// Timer
{
const auto period_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::duration<double>(ctrl_period));
timer_control_ = rclcpp::create_timer(
this, get_clock(), period_ns, std::bind(&Controller::callbackTimerControl, this));
}
logger_configure_ = std::make_unique<autoware_utils::LoggerLevelConfigure>(this);
published_time_publisher_ = std::make_unique<autoware_utils::PublishedTimePublisher>(this);
}
Controller::LateralControllerMode Controller::getLateralControllerMode(
const std::string & controller_mode) const
{
if (controller_mode == "mpc") return LateralControllerMode::MPC;
if (controller_mode == "pure_pursuit") return LateralControllerMode::PURE_PURSUIT;
return LateralControllerMode::INVALID;
}
Controller::LongitudinalControllerMode Controller::getLongitudinalControllerMode(
const std::string & controller_mode) const
{
if (controller_mode == "pid") return LongitudinalControllerMode::PID;
return LongitudinalControllerMode::INVALID;
}
bool Controller::processData(rclcpp::Clock & clock)
{
bool is_ready = true;
const auto & logData = [&clock, this](const std::string & data_type) {
RCLCPP_INFO_THROTTLE(
get_logger(), clock, logger_throttle_interval, "Waiting for %s data", data_type.c_str());
};
const auto & getData = [&logData](auto & dest, auto & sub, const std::string & data_type = "") {
const auto temp = sub.take_data();
if (temp) {
dest = temp;
return true;
}
if (!data_type.empty()) logData(data_type);
return false;
};
is_ready &= getData(current_accel_ptr_, sub_accel_, "acceleration");
is_ready &= getData(current_steering_ptr_, sub_steering_, "steering");
is_ready &= getData(current_trajectory_ptr_, sub_ref_path_, "trajectory");
is_ready &= getData(current_odometry_ptr_, sub_odometry_, "odometry");
is_ready &= getData(current_operation_mode_ptr_, sub_operation_mode_, "operation mode");
return is_ready;
}
bool Controller::isTimeOut(
const trajectory_follower::LongitudinalOutput & lon_out,
const trajectory_follower::LateralOutput & lat_out)
{
const auto now = this->now();
if ((now - lat_out.control_cmd.stamp).seconds() > timeout_thr_sec_) {
RCLCPP_ERROR_THROTTLE(
get_logger(), *get_clock(), 5000 /*ms*/,
"Lateral control command too old, control_cmd will not be published.");
return true;
}
if ((now - lon_out.control_cmd.stamp).seconds() > timeout_thr_sec_) {
RCLCPP_ERROR_THROTTLE(
get_logger(), *get_clock(), 5000 /*ms*/,
"Longitudinal control command too old, control_cmd will not be published.");
return true;
}
return false;
}
boost::optional<trajectory_follower::InputData> Controller::createInputData(rclcpp::Clock & clock)
{
if (!processData(clock)) {
return {};
}
trajectory_follower::InputData input_data;
input_data.current_trajectory = *current_trajectory_ptr_;
input_data.current_odometry = *current_odometry_ptr_;
input_data.current_steering = *current_steering_ptr_;
input_data.current_accel = *current_accel_ptr_;
input_data.current_operation_mode = *current_operation_mode_ptr_;
return input_data;
}
void Controller::callbackTimerControl()
{
autoware_control_msgs::msg::Control out;
out.stamp = this->now();
// 1. create input data
const auto input_data = createInputData(*get_clock());
if (!input_data) {
RCLCPP_INFO_THROTTLE(
get_logger(), *get_clock(), 5000, "Control is skipped since input data is not ready.");
return;
}
// 2. check if controllers are ready
const bool is_lat_ready = lateral_controller_->isReady(*input_data);
const bool is_lon_ready = longitudinal_controller_->isReady(*input_data);
if (!is_lat_ready || !is_lon_ready) {
RCLCPP_INFO_THROTTLE(
get_logger(), *get_clock(), 5000,
"Control is skipped since lateral and/or longitudinal controllers are not ready to run.");
return;
}
// 3. run controllers
stop_watch_.tic("lateral");
const auto lat_out = lateral_controller_->run(*input_data);
publishProcessingTime(stop_watch_.toc("lateral"), pub_processing_time_lat_ms_);
stop_watch_.tic("longitudinal");
const auto lon_out = longitudinal_controller_->run(*input_data);
publishProcessingTime(stop_watch_.toc("longitudinal"), pub_processing_time_lon_ms_);
// 4. sync with each other controllers
longitudinal_controller_->sync(lat_out.sync_data);
lateral_controller_->sync(lon_out.sync_data);
// TODO(Horibe): Think specification. This comes from the old implementation.
if (isTimeOut(lon_out, lat_out)) return;
// 5. publish control command
out.lateral = lat_out.control_cmd;
out.longitudinal = lon_out.control_cmd;
control_cmd_pub_->publish(out);
// 6. publish debug
published_time_publisher_->publish_if_subscribed(control_cmd_pub_, out.stamp);
publishDebugMarker(*input_data, lat_out);
// 7. publish experimental topic
if (enable_control_cmd_horizon_pub_) {
const auto control_horizon =
mergeLatLonHorizon(lat_out.control_cmd_horizon, lon_out.control_cmd_horizon, this->now());
if (control_horizon.has_value()) {
control_cmd_horizon_pub_->publish(control_horizon.value());
}
}
}
void Controller::publishDebugMarker(
const trajectory_follower::InputData & input_data,
const trajectory_follower::LateralOutput & lat_out) const
{
visualization_msgs::msg::MarkerArray debug_marker_array{};
// steer converged marker
{
auto marker = autoware_utils::create_default_marker(
"map", this->now(), "steer_converged", 0, visualization_msgs::msg::Marker::TEXT_VIEW_FACING,
autoware_utils::create_marker_scale(0.0, 0.0, 1.0),
autoware_utils::create_marker_color(1.0, 1.0, 1.0, 0.99));
marker.pose = input_data.current_odometry.pose.pose;
std::stringstream ss;
const double current = input_data.current_steering.steering_tire_angle;
const double cmd = lat_out.control_cmd.steering_tire_angle;
const double diff = current - cmd;
ss << "current:" << current << " cmd:" << cmd << " diff:" << diff
<< (lat_out.sync_data.is_steer_converged ? " (converged)" : " (not converged)");
marker.text = ss.str();
debug_marker_array.markers.push_back(marker);
}
debug_marker_pub_->publish(debug_marker_array);
}
void Controller::publishProcessingTime(
const double t_ms, const rclcpp::Publisher<Float64Stamped>::SharedPtr pub)
{
Float64Stamped msg{};
msg.stamp = this->now();
msg.data = t_ms;
pub->publish(msg);
}
std::optional<ControlHorizon> Controller::mergeLatLonHorizon(
const LateralHorizon & lateral_horizon, const LongitudinalHorizon & longitudinal_horizon,
const rclcpp::Time & stamp)
{
if (lateral_horizon.controls.empty() || longitudinal_horizon.controls.empty()) {
return std::nullopt;
}
autoware_control_msgs::msg::ControlHorizon control_horizon{};
control_horizon.stamp = stamp;
// If either of the horizons has only one control, repeat the control to match the other horizon.
if (lateral_horizon.controls.size() == 1) {
control_horizon.time_step_ms = longitudinal_horizon.time_step_ms;
const auto lateral = lateral_horizon.controls.front();
for (const auto & longitudinal : longitudinal_horizon.controls) {
autoware_control_msgs::msg::Control control;
control.longitudinal = longitudinal;
control.lateral = lateral;
control.stamp = stamp;
control_horizon.controls.push_back(control);
}
return control_horizon;
}
if (longitudinal_horizon.controls.size() == 1) {
control_horizon.time_step_ms = lateral_horizon.time_step_ms;
const auto longitudinal = longitudinal_horizon.controls.front();
for (const auto & lateral : lateral_horizon.controls) {
autoware_control_msgs::msg::Control control;
control.longitudinal = longitudinal;
control.lateral = lateral;
control.stamp = stamp;
control_horizon.controls.push_back(control);
}
return control_horizon;
}
// If both horizons have multiple controls, align the time steps and zero-order hold the controls.
// calculate greatest common divisor of time steps
const auto gcd_double = [](const double a, const double b) {
const double precision = 1e9;
const int int_a = static_cast<int>(round(a * precision));
const int int_b = static_cast<int>(round(b * precision));
return static_cast<double>(std::gcd(int_a, int_b)) / precision;
};
const double time_step_ms =
gcd_double(lateral_horizon.time_step_ms, longitudinal_horizon.time_step_ms);
control_horizon.time_step_ms = time_step_ms;
const auto lateral_controls = resampleHorizonByZeroOrderHold(
lateral_horizon.controls, lateral_horizon.time_step_ms, time_step_ms);
const auto longitudinal_controls = resampleHorizonByZeroOrderHold(
longitudinal_horizon.controls, longitudinal_horizon.time_step_ms, time_step_ms);
if (lateral_controls.size() != longitudinal_controls.size()) {
return std::nullopt;
}
const size_t num_steps = lateral_controls.size();
for (size_t i = 0; i < num_steps; ++i) {
autoware_control_msgs::msg::Control control{};
control.stamp = stamp;
control.lateral = lateral_controls.at(i);
control.longitudinal = longitudinal_controls.at(i);
control_horizon.controls.push_back(control);
}
return control_horizon;
}
} // namespace autoware::motion::control::trajectory_follower_node
#include "rclcpp_components/register_node_macro.hpp"
RCLCPP_COMPONENTS_REGISTER_NODE(autoware::motion::control::trajectory_follower_node::Controller)