forked from ApolloAuto/apollo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrol_component.cc
491 lines (419 loc) · 17.8 KB
/
control_component.cc
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/******************************************************************************
* Copyright 2017 The Apollo Authors. 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 "modules/control/control_component.h"
#include "absl/strings/str_cat.h"
#include "cyber/common/file.h"
#include "cyber/common/log.h"
#include "cyber/time/clock.h"
#include "modules/common/adapters/adapter_gflags.h"
#include "modules/common/latency_recorder/latency_recorder.h"
#include "modules/common/vehicle_state/vehicle_state_provider.h"
#include "modules/control/common/control_gflags.h"
namespace apollo {
namespace control {
using apollo::canbus::Chassis;
using apollo::common::ErrorCode;
using apollo::common::Status;
using apollo::common::VehicleStateProvider;
using apollo::cyber::Clock;
using apollo::localization::LocalizationEstimate;
using apollo::planning::ADCTrajectory;
ControlComponent::ControlComponent()
: monitor_logger_buffer_(common::monitor::MonitorMessageItem::CONTROL) {}
bool ControlComponent::Init() {
injector_ = std::make_shared<DependencyInjector>();
init_time_ = Clock::Now();
AINFO << "Control init, starting ...";
ACHECK(
cyber::common::GetProtoFromFile(FLAGS_control_conf_file, &control_conf_))
<< "Unable to load control conf file: " + FLAGS_control_conf_file;
AINFO << "Conf file: " << FLAGS_control_conf_file << " is loaded.";
AINFO << "Conf file: " << ConfigFilePath() << " is loaded.";
// initial controller agent when not using control submodules
ADEBUG << "FLAGS_use_control_submodules: " << FLAGS_use_control_submodules;
if (!FLAGS_use_control_submodules &&
!controller_agent_.Init(injector_, &control_conf_).ok()) {
// set controller
ADEBUG << "original control";
monitor_logger_buffer_.ERROR("Control init controller failed! Stopping...");
return false;
}
cyber::ReaderConfig chassis_reader_config;
chassis_reader_config.channel_name = FLAGS_chassis_topic;
chassis_reader_config.pending_queue_size = FLAGS_chassis_pending_queue_size;
chassis_reader_ =
node_->CreateReader<Chassis>(chassis_reader_config, nullptr);
ACHECK(chassis_reader_ != nullptr);
cyber::ReaderConfig planning_reader_config;
planning_reader_config.channel_name = FLAGS_planning_trajectory_topic;
planning_reader_config.pending_queue_size = FLAGS_planning_pending_queue_size;
trajectory_reader_ =
node_->CreateReader<ADCTrajectory>(planning_reader_config, nullptr);
ACHECK(trajectory_reader_ != nullptr);
cyber::ReaderConfig localization_reader_config;
localization_reader_config.channel_name = FLAGS_localization_topic;
localization_reader_config.pending_queue_size =
FLAGS_localization_pending_queue_size;
localization_reader_ = node_->CreateReader<LocalizationEstimate>(
localization_reader_config, nullptr);
ACHECK(localization_reader_ != nullptr);
cyber::ReaderConfig pad_msg_reader_config;
pad_msg_reader_config.channel_name = FLAGS_pad_topic;
pad_msg_reader_config.pending_queue_size = FLAGS_pad_msg_pending_queue_size;
pad_msg_reader_ =
node_->CreateReader<PadMessage>(pad_msg_reader_config, nullptr);
ACHECK(pad_msg_reader_ != nullptr);
if (!FLAGS_use_control_submodules) {
control_cmd_writer_ =
node_->CreateWriter<ControlCommand>(FLAGS_control_command_topic);
ACHECK(control_cmd_writer_ != nullptr);
} else {
local_view_writer_ =
node_->CreateWriter<LocalView>(FLAGS_control_local_view_topic);
ACHECK(local_view_writer_ != nullptr);
}
// set initial vehicle state by cmd
// need to sleep, because advertised channel is not ready immediately
// simple test shows a short delay of 80 ms or so
AINFO << "Control resetting vehicle state, sleeping for 1000 ms ...";
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
// should init_vehicle first, let car enter work status, then use status msg
// trigger control
AINFO << "Control default driving action is "
<< DrivingAction_Name(control_conf_.action());
pad_msg_.set_action(control_conf_.action());
return true;
}
void ControlComponent::OnPad(const std::shared_ptr<PadMessage> &pad) {
std::lock_guard<std::mutex> lock(mutex_);
pad_msg_.CopyFrom(*pad);
ADEBUG << "Received Pad Msg:" << pad_msg_.DebugString();
AERROR_IF(!pad_msg_.has_action()) << "pad message check failed!";
}
void ControlComponent::OnChassis(const std::shared_ptr<Chassis> &chassis) {
ADEBUG << "Received chassis data: run chassis callback.";
std::lock_guard<std::mutex> lock(mutex_);
latest_chassis_.CopyFrom(*chassis);
}
void ControlComponent::OnPlanning(
const std::shared_ptr<ADCTrajectory> &trajectory) {
ADEBUG << "Received chassis data: run trajectory callback.";
std::lock_guard<std::mutex> lock(mutex_);
latest_trajectory_.CopyFrom(*trajectory);
}
void ControlComponent::OnLocalization(
const std::shared_ptr<LocalizationEstimate> &localization) {
ADEBUG << "Received control data: run localization message callback.";
std::lock_guard<std::mutex> lock(mutex_);
latest_localization_.CopyFrom(*localization);
}
void ControlComponent::OnMonitor(
const common::monitor::MonitorMessage &monitor_message) {
for (const auto &item : monitor_message.item()) {
if (item.log_level() == common::monitor::MonitorMessageItem::FATAL) {
estop_ = true;
return;
}
}
}
Status ControlComponent::ProduceControlCommand(
ControlCommand *control_command) {
Status status = CheckInput(&local_view_);
// check data
if (!status.ok()) {
AERROR_EVERY(100) << "Control input data failed: "
<< status.error_message();
control_command->mutable_engage_advice()->set_advice(
apollo::common::EngageAdvice::DISALLOW_ENGAGE);
control_command->mutable_engage_advice()->set_reason(
status.error_message());
estop_ = true;
estop_reason_ = status.error_message();
} else {
Status status_ts = CheckTimestamp(local_view_);
if (!status_ts.ok()) {
AERROR << "Input messages timeout";
// estop_ = true;
status = status_ts;
if (local_view_.chassis().driving_mode() !=
apollo::canbus::Chassis::COMPLETE_AUTO_DRIVE) {
control_command->mutable_engage_advice()->set_advice(
apollo::common::EngageAdvice::DISALLOW_ENGAGE);
control_command->mutable_engage_advice()->set_reason(
status.error_message());
}
} else {
control_command->mutable_engage_advice()->set_advice(
apollo::common::EngageAdvice::READY_TO_ENGAGE);
}
}
// check estop
estop_ = control_conf_.enable_persistent_estop()
? estop_ || local_view_.trajectory().estop().is_estop()
: local_view_.trajectory().estop().is_estop();
if (local_view_.trajectory().estop().is_estop()) {
estop_ = true;
estop_reason_ = "estop from planning : ";
estop_reason_ += local_view_.trajectory().estop().reason();
}
if (local_view_.trajectory().trajectory_point().empty()) {
AWARN_EVERY(100) << "planning has no trajectory point. ";
estop_ = true;
estop_reason_ = "estop for empty planning trajectory, planning headers: " +
local_view_.trajectory().header().ShortDebugString();
}
if (FLAGS_enable_gear_drive_negative_speed_protection) {
const double kEpsilon = 0.001;
auto first_trajectory_point = local_view_.trajectory().trajectory_point(0);
if (local_view_.chassis().gear_location() == Chassis::GEAR_DRIVE &&
first_trajectory_point.v() < -1 * kEpsilon) {
estop_ = true;
estop_reason_ = "estop for negative speed when gear_drive";
}
}
if (!estop_) {
if (local_view_.chassis().driving_mode() == Chassis::COMPLETE_MANUAL) {
controller_agent_.Reset();
AINFO_EVERY(100) << "Reset Controllers in Manual Mode";
}
auto debug = control_command->mutable_debug()->mutable_input_debug();
debug->mutable_localization_header()->CopyFrom(
local_view_.localization().header());
debug->mutable_canbus_header()->CopyFrom(local_view_.chassis().header());
debug->mutable_trajectory_header()->CopyFrom(
local_view_.trajectory().header());
if (local_view_.trajectory().is_replan()) {
latest_replan_trajectory_header_ = local_view_.trajectory().header();
}
if (latest_replan_trajectory_header_.has_sequence_num()) {
debug->mutable_latest_replan_trajectory_header()->CopyFrom(
latest_replan_trajectory_header_);
}
// controller agent
Status status_compute = controller_agent_.ComputeControlCommand(
&local_view_.localization(), &local_view_.chassis(),
&local_view_.trajectory(), control_command);
if (!status_compute.ok()) {
AERROR << "Control main function failed"
<< " with localization: "
<< local_view_.localization().ShortDebugString()
<< " with chassis: " << local_view_.chassis().ShortDebugString()
<< " with trajectory: "
<< local_view_.trajectory().ShortDebugString()
<< " with cmd: " << control_command->ShortDebugString()
<< " status:" << status_compute.error_message();
estop_ = true;
estop_reason_ = status_compute.error_message();
status = status_compute;
}
}
// if planning set estop, then no control process triggered
if (estop_) {
AWARN_EVERY(100) << "Estop triggered! No control core method executed!";
// set Estop command
control_command->set_speed(0);
control_command->set_throttle(0);
control_command->set_brake(control_conf_.soft_estop_brake());
control_command->set_gear_location(Chassis::GEAR_DRIVE);
}
// check signal
if (local_view_.trajectory().decision().has_vehicle_signal()) {
control_command->mutable_signal()->CopyFrom(
local_view_.trajectory().decision().vehicle_signal());
}
return status;
}
bool ControlComponent::Proc() {
const auto start_time = Clock::Now();
chassis_reader_->Observe();
const auto &chassis_msg = chassis_reader_->GetLatestObserved();
if (chassis_msg == nullptr) {
AERROR << "Chassis msg is not ready!";
return false;
}
OnChassis(chassis_msg);
trajectory_reader_->Observe();
const auto &trajectory_msg = trajectory_reader_->GetLatestObserved();
if (trajectory_msg == nullptr) {
AERROR << "planning msg is not ready!";
return false;
}
OnPlanning(trajectory_msg);
localization_reader_->Observe();
const auto &localization_msg = localization_reader_->GetLatestObserved();
if (localization_msg == nullptr) {
AERROR << "localization msg is not ready!";
return false;
}
OnLocalization(localization_msg);
pad_msg_reader_->Observe();
const auto &pad_msg = pad_msg_reader_->GetLatestObserved();
if (pad_msg != nullptr) {
OnPad(pad_msg);
}
{
// TODO(SHU): to avoid redundent copy
std::lock_guard<std::mutex> lock(mutex_);
local_view_.mutable_chassis()->CopyFrom(latest_chassis_);
local_view_.mutable_trajectory()->CopyFrom(latest_trajectory_);
local_view_.mutable_localization()->CopyFrom(latest_localization_);
if (pad_msg != nullptr) {
local_view_.mutable_pad_msg()->CopyFrom(pad_msg_);
}
}
// use control submodules
if (FLAGS_use_control_submodules) {
local_view_.mutable_header()->set_lidar_timestamp(
local_view_.trajectory().header().lidar_timestamp());
local_view_.mutable_header()->set_camera_timestamp(
local_view_.trajectory().header().camera_timestamp());
local_view_.mutable_header()->set_radar_timestamp(
local_view_.trajectory().header().radar_timestamp());
common::util::FillHeader(FLAGS_control_local_view_topic, &local_view_);
const auto end_time = Clock::Now();
// measure latency
static apollo::common::LatencyRecorder latency_recorder(
FLAGS_control_local_view_topic);
latency_recorder.AppendLatencyRecord(
local_view_.trajectory().header().lidar_timestamp(), start_time,
end_time);
local_view_writer_->Write(local_view_);
return true;
}
if (pad_msg != nullptr) {
ADEBUG << "pad_msg: " << pad_msg_.ShortDebugString();
if (pad_msg_.action() == DrivingAction::RESET) {
AINFO << "Control received RESET action!";
estop_ = false;
estop_reason_.clear();
}
pad_received_ = true;
}
if (control_conf_.is_control_test_mode() &&
control_conf_.control_test_duration() > 0 &&
(start_time - init_time_).ToSecond() >
control_conf_.control_test_duration()) {
AERROR << "Control finished testing. exit";
return false;
}
ControlCommand control_command;
Status status = ProduceControlCommand(&control_command);
AERROR_IF(!status.ok()) << "Failed to produce control command:"
<< status.error_message();
if (pad_received_) {
control_command.mutable_pad_msg()->CopyFrom(pad_msg_);
pad_received_ = false;
}
// forward estop reason among following control frames.
if (estop_) {
control_command.mutable_header()->mutable_status()->set_msg(estop_reason_);
}
// set header
control_command.mutable_header()->set_lidar_timestamp(
local_view_.trajectory().header().lidar_timestamp());
control_command.mutable_header()->set_camera_timestamp(
local_view_.trajectory().header().camera_timestamp());
control_command.mutable_header()->set_radar_timestamp(
local_view_.trajectory().header().radar_timestamp());
common::util::FillHeader(node_->Name(), &control_command);
ADEBUG << control_command.ShortDebugString();
if (control_conf_.is_control_test_mode()) {
ADEBUG << "Skip publish control command in test mode";
return true;
}
const auto end_time = Clock::Now();
const double time_diff_ms = (end_time - start_time).ToSecond() * 1e3;
ADEBUG << "total control time spend: " << time_diff_ms << " ms.";
control_command.mutable_latency_stats()->set_total_time_ms(time_diff_ms);
control_command.mutable_latency_stats()->set_total_time_exceeded(
time_diff_ms > control_conf_.control_period() * 1e3);
ADEBUG << "control cycle time is: " << time_diff_ms << " ms.";
status.Save(control_command.mutable_header()->mutable_status());
// measure latency
if (local_view_.trajectory().header().has_lidar_timestamp()) {
static apollo::common::LatencyRecorder latency_recorder(
FLAGS_control_command_topic);
latency_recorder.AppendLatencyRecord(
local_view_.trajectory().header().lidar_timestamp(), start_time,
end_time);
}
control_cmd_writer_->Write(control_command);
return true;
}
Status ControlComponent::CheckInput(LocalView *local_view) {
ADEBUG << "Received localization:"
<< local_view->localization().ShortDebugString();
ADEBUG << "Received chassis:" << local_view->chassis().ShortDebugString();
if (!local_view->trajectory().estop().is_estop() &&
local_view->trajectory().trajectory_point().empty()) {
AWARN_EVERY(100) << "planning has no trajectory point. ";
const std::string msg =
absl::StrCat("planning has no trajectory point. planning_seq_num:",
local_view->trajectory().header().sequence_num());
return Status(ErrorCode::CONTROL_COMPUTE_ERROR, msg);
}
for (auto &trajectory_point :
*local_view->mutable_trajectory()->mutable_trajectory_point()) {
if (std::abs(trajectory_point.v()) <
control_conf_.minimum_speed_resolution() &&
std::abs(trajectory_point.a()) <
control_conf_.max_acceleration_when_stopped()) {
trajectory_point.set_v(0.0);
trajectory_point.set_a(0.0);
}
}
injector_->vehicle_state()->Update(local_view->localization(),
local_view->chassis());
return Status::OK();
}
Status ControlComponent::CheckTimestamp(const LocalView &local_view) {
if (!control_conf_.enable_input_timestamp_check() ||
control_conf_.is_control_test_mode()) {
ADEBUG << "Skip input timestamp check by gflags.";
return Status::OK();
}
double current_timestamp = Clock::NowInSeconds();
double localization_diff =
current_timestamp - local_view.localization().header().timestamp_sec();
if (localization_diff > (control_conf_.max_localization_miss_num() *
control_conf_.localization_period())) {
AERROR << "Localization msg lost for " << std::setprecision(6)
<< localization_diff << "s";
monitor_logger_buffer_.ERROR("Localization msg lost");
return Status(ErrorCode::CONTROL_COMPUTE_ERROR, "Localization msg timeout");
}
double chassis_diff =
current_timestamp - local_view.chassis().header().timestamp_sec();
if (chassis_diff >
(control_conf_.max_chassis_miss_num() * control_conf_.chassis_period())) {
AERROR << "Chassis msg lost for " << std::setprecision(6) << chassis_diff
<< "s";
monitor_logger_buffer_.ERROR("Chassis msg lost");
return Status(ErrorCode::CONTROL_COMPUTE_ERROR, "Chassis msg timeout");
}
double trajectory_diff =
current_timestamp - local_view.trajectory().header().timestamp_sec();
if (trajectory_diff > (control_conf_.max_planning_miss_num() *
control_conf_.trajectory_period())) {
AERROR << "Trajectory msg lost for " << std::setprecision(6)
<< trajectory_diff << "s";
monitor_logger_buffer_.ERROR("Trajectory msg lost");
return Status(ErrorCode::CONTROL_COMPUTE_ERROR, "Trajectory msg timeout");
}
return Status::OK();
}
} // namespace control
} // namespace apollo