41 : monitor_logger_buffer_(
42 apollo::common::monitor::MonitorMessageItem::CANBUS) {}
44bool CanbusComponent::Init() {
49 AINFO <<
"The canbus conf file is loaded: " << FLAGS_canbus_conf_file;
50 ADEBUG <<
"Canbus_conf:" << canbus_conf_.ShortDebugString();
53 AERROR << FLAGS_load_vehicle_library <<
" No such vehicle library";
56 AINFO <<
"Load the vehicle factory library: " << FLAGS_load_vehicle_library;
59 auto vehicle_object = loader.CreateClassObj<AbstractVehicleFactory>(
60 FLAGS_load_vehicle_class_name);
61 if (!vehicle_object) {
62 AERROR <<
"Failed to create the vehicle factory: "
63 << FLAGS_load_vehicle_class_name;
66 AINFO <<
"Successfully create vehicle factory: "
67 << FLAGS_load_vehicle_class_name;
69 vehicle_object_ = vehicle_object;
70 if (!vehicle_object_->Init(&canbus_conf_)) {
71 AERROR <<
"Fail to init vehicle factory.";
74 AINFO <<
"Vehicle factory is successfully initialized.";
76 cyber::ReaderConfig guardian_cmd_reader_config;
77 guardian_cmd_reader_config.channel_name = FLAGS_guardian_topic;
78 guardian_cmd_reader_config.pending_queue_size =
79 FLAGS_guardian_cmd_pending_queue_size;
81 cyber::ReaderConfig control_cmd_reader_config;
82 control_cmd_reader_config.channel_name = FLAGS_control_command_topic;
83 control_cmd_reader_config.pending_queue_size =
84 FLAGS_control_cmd_pending_queue_size;
86 cyber::ReaderConfig chassis_cmd_reader_config;
87 chassis_cmd_reader_config.channel_name = FLAGS_chassis_command_topic;
88 chassis_cmd_reader_config.pending_queue_size =
89 FLAGS_control_cmd_pending_queue_size;
92 if (FLAGS_receive_guardian) {
94 guardian_cmd_reader_config,
95 [
this](
const std::shared_ptr<GuardianCommand> &cmd) {
96 ADEBUG <<
"Received guardian data: run canbus callback.";
98 OnGuardianCommand(*cmd);
100 if ((end_time - start_time) * 1e-6 > FLAGS_guardian_period) {
101 AWARN <<
"Guardian callback time: "
102 << (end_time - start_time) * 1e-3 <<
" ms.";
107 control_cmd_reader_config,
108 [
this](
const std::shared_ptr<ControlCommand> &cmd) {
109 ADEBUG <<
"Received control data: run canbus callback.";
111 OnControlCommand(*cmd);
113 if ((end_time - start_time) * 1e-6 > FLAGS_control_period) {
114 AWARN <<
"Control callback time: " << (end_time - start_time) * 1e-3
122 chassis_cmd_reader_config,
123 [
this](
const std::shared_ptr<ChassisCommand> &cmd) {
124 ADEBUG <<
"Received control data: run canbus callback.";
125 OnChassisCommand(*cmd);
129 chassis_writer_ =
node_->CreateWriter<Chassis>(FLAGS_chassis_topic);
132 if (!vehicle_object_->Start()) {
133 AERROR <<
"Fail to start canclient, cansender, canreceiver, canclient, "
134 "vehicle controller.";
138 AINFO <<
"Start canclient cansender, canreceiver, canclient, vehicle "
139 "controller successfully.";
141 monitor_logger_buffer_.INFO(
"Canbus is started.");
146void CanbusComponent::Clear() {
147 vehicle_object_->Stop();
148 AINFO <<
"Cleanup Canbus component";
151void CanbusComponent::PublishChassis() {
152 Chassis chassis = vehicle_object_->publish_chassis();
153 if (is_control_cmd_time_delay_) {
154 AERROR <<
"cmd time delay";
157 common::util::FillHeader(
node_->Name(), &chassis);
158 chassis_writer_->Write(chassis);
159 ADEBUG << chassis.ShortDebugString();
162bool CanbusComponent::Proc() {
165 if (FLAGS_receive_guardian) {
166 guardian_cmd_reader_->Observe();
167 const auto &guardian_cmd_msg = guardian_cmd_reader_->GetLatestObserved();
168 if (guardian_cmd_msg ==
nullptr) {
169 AERROR <<
"guardian cmd msg is not ready!";
171 OnGuardianCommandCheck(*guardian_cmd_msg);
174 control_command_reader_->Observe();
175 const auto &control_cmd_msg = control_command_reader_->GetLatestObserved();
176 if (control_cmd_msg ==
nullptr) {
177 AERROR <<
"control cmd msg is not ready!";
179 OnControlCommandCheck(*control_cmd_msg);
184 if (vehicle_object_->CheckChassisCommunicationFault()) {
185 AERROR <<
"Can not get the chassis info, please check the chassis "
187 is_chassis_communication_fault_ =
true;
189 is_chassis_communication_fault_ =
false;
196 if (FLAGS_enable_chassis_detail_pub) {
197 vehicle_object_->PublishChassisDetail();
201 if (FLAGS_enable_chassis_detail_sender_pub) {
202 vehicle_object_->PublishChassisDetailSender();
206 vehicle_object_->UpdateHeartbeat();
209 const double time_diff_ms = (end_time - start_time) * 1e-3;
210 if (time_diff_ms > (1 / FLAGS_chassis_freq * 1e3)) {
211 AWARN <<
"CanbusComponent::Proc() takes too much time: " << time_diff_ms
218void CanbusComponent::OnControlCommand(
const ControlCommand &control_command) {
223 if (current_timestamp - last_timestamp_controlcmd_ <
224 FLAGS_min_cmd_interval * 1000) {
225 ADEBUG <<
"Control command comes too soon. Ignore. Required "
226 "FLAGS_min_cmd_interval["
227 << FLAGS_min_cmd_interval <<
"] ms, actual time interval["
228 << (current_timestamp - last_timestamp_controlcmd_) * 1e-3
232 last_timestamp_controlcmd_ = current_timestamp;
234 if (!is_control_cmd_time_delay_) {
235 vehicle_object_->UpdateCommand(&control_command);
239void CanbusComponent::OnControlCommandCheck(
244 double cmd_time_diff =
246 if ((FLAGS_use_control_cmd_check &&
247 (cmd_time_diff > (FLAGS_max_control_miss_num * FLAGS_control_period))) ||
248 is_chassis_communication_fault_) {
250 (FLAGS_max_control_miss_num * FLAGS_control_period))
251 <<
"Control cmd timeout, sequence_number:"
253 <<
", Time_of_delay:" << cmd_time_diff <<
" s"
254 <<
", time delay threshold: "
255 << (FLAGS_max_control_miss_num * FLAGS_control_period) <<
" s"
256 <<
", need to process cmd timeout.";
257 AERROR_IF(is_chassis_communication_fault_)
258 <<
"Chassis communication fault, need to process cmd timeout.";
263 FLAGS_chassis_debug_mode || is_chassis_communication_fault_) {
264 is_control_cmd_time_delay_ =
true;
266 new_guardian_command.mutable_control_command()->CopyFrom(control_command);
267 ProcessGuardianCmdTimeout(&new_guardian_command);
268 ADEBUG <<
"new_guardian_command is "
269 << new_guardian_command.ShortDebugString();
270 vehicle_object_->UpdateCommand(&new_guardian_command.
control_command());
273 is_control_cmd_time_delay_ =
false;
277void CanbusComponent::OnGuardianCommand(
279 if (!is_control_cmd_time_delay_) {
284void CanbusComponent::OnGuardianCommandCheck(
289 double guardian_cmd_time_diff =
291 if ((FLAGS_use_guardian_cmd_check &&
292 (guardian_cmd_time_diff >
293 (FLAGS_max_guardian_miss_num * FLAGS_guardian_period))) ||
294 is_chassis_communication_fault_) {
295 AERROR <<
"Guardain cmd timeout, sequence_number:"
297 <<
", Time_of_delay:" << guardian_cmd_time_diff <<
" s"
298 <<
", time delay threshold: "
299 << (FLAGS_max_guardian_miss_num * FLAGS_guardian_period) <<
" s"
300 <<
", need to process cmd timeout.";
305 is_chassis_communication_fault_) {
306 is_control_cmd_time_delay_ =
true;
308 new_guardian_command.CopyFrom(guardian_command);
309 ProcessGuardianCmdTimeout(&new_guardian_command);
310 ADEBUG <<
"new_guardian_command is "
311 << new_guardian_command.ShortDebugString();
312 vehicle_object_->UpdateCommand(&new_guardian_command.
control_command());
315 is_control_cmd_time_delay_ =
false;
319void CanbusComponent::OnChassisCommand(
const ChassisCommand &chassis_command) {
324 if (current_timestamp - last_timestamp_chassiscmd_ <
325 FLAGS_min_cmd_interval * 1000) {
326 ADEBUG <<
"Control command comes too soon. Ignore.\n Required "
327 "FLAGS_min_cmd_interval["
328 << FLAGS_min_cmd_interval <<
"], actual time interval["
329 << current_timestamp - last_timestamp_chassiscmd_ <<
"].";
332 last_timestamp_chassiscmd_ = current_timestamp;
334 ADEBUG <<
"Control_sequence_number:"
336 << current_timestamp -
341 vehicle_object_->UpdateCommand(&chassis_command);
344common::Status CanbusComponent::OnError(
const std::string &error_msg) {
345 monitor_logger_buffer_.ERROR(error_msg);
346 return ::apollo::common::Status(ErrorCode::CANBUS_ERROR, error_msg);
349void CanbusComponent::ProcessTimeoutByClearCanSender() {
353 !FLAGS_chassis_debug_mode) {
354 ADEBUG <<
"The current driving mode does not need to check cmd timeout.";
355 if (vehicle_object_->IsSendProtocolClear()) {
356 AINFO <<
"send protocol is clear, ignore driving mode, need to recover "
358 vehicle_object_->AddSendProtocol();
363 if (!is_control_cmd_time_delay_previous_ && is_control_cmd_time_delay_) {
364 AINFO <<
"control cmd time latency delay, clear send protocol.";
365 vehicle_object_->ClearSendProtocol();
366 }
else if (is_control_cmd_time_delay_previous_ &&
367 !is_control_cmd_time_delay_) {
368 AINFO <<
"control cmd time latency reover, add send protocol.";
369 if (vehicle_object_->IsSendProtocolClear()) {
370 vehicle_object_->AddSendProtocol();
373 is_control_cmd_time_delay_previous_ = is_control_cmd_time_delay_;
376void CanbusComponent::ProcessGuardianCmdTimeout(
378 AINFO <<
"Into cmd timeout process, set estop.";
379 guardian_command->mutable_control_command()->set_throttle(0.0);
380 guardian_command->mutable_control_command()->set_steering_target(0.0);
381 guardian_command->mutable_control_command()->set_steering_rate(25.0);
382 guardian_command->mutable_control_command()->set_brake(FLAGS_estop_brake);