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 common::util::FillHeader(
node_->Name(), &chassis);
154 chassis_writer_->Write(chassis);
155 ADEBUG << chassis.ShortDebugString();
158bool CanbusComponent::Proc() {
161 if (FLAGS_receive_guardian) {
162 guardian_cmd_reader_->Observe();
163 const auto &guardian_cmd_msg = guardian_cmd_reader_->GetLatestObserved();
164 if (guardian_cmd_msg ==
nullptr) {
165 AERROR <<
"guardian cmd msg is not ready!";
167 OnGuardianCommandCheck(*guardian_cmd_msg);
170 control_command_reader_->Observe();
171 const auto &control_cmd_msg = control_command_reader_->GetLatestObserved();
172 if (control_cmd_msg ==
nullptr) {
173 AERROR <<
"control cmd msg is not ready!";
175 OnControlCommandCheck(*control_cmd_msg);
180 if (vehicle_object_->CheckChassisCommunicationFault()) {
181 AERROR <<
"Can not get the chassis info, please check the chassis "
189 if (FLAGS_enable_chassis_detail_pub) {
190 vehicle_object_->PublishChassisDetail();
194 if (FLAGS_enable_chassis_detail_sender_pub) {
195 vehicle_object_->PublishChassisDetailSender();
199 vehicle_object_->UpdateHeartbeat();
202 const double time_diff_ms = (end_time - start_time) * 1e-3;
203 if (time_diff_ms > (1 / FLAGS_chassis_freq * 1e3)) {
204 AWARN <<
"CanbusComponent::Proc() takes too much time: " << time_diff_ms
211void CanbusComponent::OnControlCommand(
const ControlCommand &control_command) {
216 if (current_timestamp - last_timestamp_controlcmd_ <
217 FLAGS_min_cmd_interval * 1000) {
218 ADEBUG <<
"Control command comes too soon. Ignore. Required "
219 "FLAGS_min_cmd_interval["
220 << FLAGS_min_cmd_interval <<
"] ms, actual time interval["
221 << (current_timestamp - last_timestamp_controlcmd_) * 1e-3
225 last_timestamp_controlcmd_ = current_timestamp;
227 if (!is_control_cmd_time_delay_) {
228 vehicle_object_->UpdateCommand(&control_command);
232void CanbusComponent::OnControlCommandCheck(
237 double cmd_time_diff =
239 if (FLAGS_use_control_cmd_check &&
240 (cmd_time_diff > (FLAGS_max_control_miss_num * FLAGS_control_period))) {
241 AERROR <<
"Control cmd timeout, sequence_number:"
243 <<
", Time_of_delay:" << cmd_time_diff <<
" s"
244 <<
", time delay threshold: "
245 << (FLAGS_max_control_miss_num * FLAGS_control_period) <<
" s";
250 is_control_cmd_time_delay_ =
true;
252 new_guardian_command.mutable_control_command()->CopyFrom(control_command);
253 ProcessGuardianCmdTimeout(&new_guardian_command);
254 ADEBUG <<
"new_guardian_command is "
255 << new_guardian_command.ShortDebugString();
256 vehicle_object_->UpdateCommand(&new_guardian_command.
control_command());
259 is_control_cmd_time_delay_ =
false;
263void CanbusComponent::OnGuardianCommand(
265 if (!is_control_cmd_time_delay_) {
270void CanbusComponent::OnGuardianCommandCheck(
275 double guardian_cmd_time_diff =
277 if (FLAGS_use_guardian_cmd_check &&
278 (guardian_cmd_time_diff >
279 (FLAGS_max_guardian_miss_num * FLAGS_guardian_period))) {
280 AERROR <<
"Guardain cmd timeout, sequence_number:"
282 <<
", Time_of_delay:" << guardian_cmd_time_diff <<
" s"
283 <<
", time delay threshold: "
284 << (FLAGS_max_guardian_miss_num * FLAGS_guardian_period) <<
" s";
289 is_control_cmd_time_delay_ =
true;
291 new_guardian_command.CopyFrom(guardian_command);
292 ProcessGuardianCmdTimeout(&new_guardian_command);
293 ADEBUG <<
"new_guardian_command is "
294 << new_guardian_command.ShortDebugString();
295 vehicle_object_->UpdateCommand(&new_guardian_command.
control_command());
298 is_control_cmd_time_delay_ =
false;
302void CanbusComponent::OnChassisCommand(
const ChassisCommand &chassis_command) {
307 if (current_timestamp - last_timestamp_chassiscmd_ <
308 FLAGS_min_cmd_interval * 1000) {
309 ADEBUG <<
"Control command comes too soon. Ignore.\n Required "
310 "FLAGS_min_cmd_interval["
311 << FLAGS_min_cmd_interval <<
"], actual time interval["
312 << current_timestamp - last_timestamp_chassiscmd_ <<
"].";
315 last_timestamp_chassiscmd_ = current_timestamp;
317 ADEBUG <<
"Control_sequence_number:"
319 << current_timestamp -
324 vehicle_object_->UpdateCommand(&chassis_command);
327common::Status CanbusComponent::OnError(
const std::string &error_msg) {
328 monitor_logger_buffer_.ERROR(error_msg);
329 return ::apollo::common::Status(ErrorCode::CANBUS_ERROR, error_msg);
332void CanbusComponent::ProcessTimeoutByClearCanSender() {
336 !FLAGS_chassis_debug_mode) {
337 ADEBUG <<
"The current driving mode does not need to check cmd timeout.";
338 if (vehicle_object_->IsSendProtocolClear()) {
339 AINFO <<
"send protocol is clear, ignore driving mode, need to recover "
341 vehicle_object_->AddSendProtocol();
346 if (!is_control_cmd_time_delay_previous_ && is_control_cmd_time_delay_) {
347 AINFO <<
"control cmd time latency delay, clear send protocol.";
348 vehicle_object_->ClearSendProtocol();
349 }
else if (is_control_cmd_time_delay_previous_ &&
350 !is_control_cmd_time_delay_) {
351 AINFO <<
"control cmd time latency reover, add send protocol.";
352 if (vehicle_object_->IsSendProtocolClear()) {
353 vehicle_object_->AddSendProtocol();
356 is_control_cmd_time_delay_previous_ = is_control_cmd_time_delay_;
359void CanbusComponent::ProcessGuardianCmdTimeout(
361 AINFO <<
"Into cmd timeout process, set estop.";
362 guardian_command->mutable_control_command()->set_throttle(0.0);
363 guardian_command->mutable_control_command()->set_steering_target(0.0);
364 guardian_command->mutable_control_command()->set_steering_rate(25.0);
365 guardian_command->mutable_control_command()->set_brake(FLAGS_estop_brake);