Apollo 10.0
自动驾驶开放平台
devkit_controller.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2020 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
18
19#include <string>
20
21#include "modules/common_msgs/basic_msgs/vehicle_signal.pb.h"
22
23#include "cyber/common/log.h"
24#include "cyber/time/time.h"
30
31namespace apollo {
32namespace canbus {
33namespace devkit {
34
35using ::apollo::common::ErrorCode;
36using ::apollo::common::VehicleSignal;
37using ::apollo::control::ControlCommand;
38using ::apollo::drivers::canbus::ProtocolData;
39
40namespace {
41
42const int32_t kMaxFailAttempt = 10;
43const int32_t CHECK_RESPONSE_STEER_UNIT_FLAG = 1;
44const int32_t CHECK_RESPONSE_SPEED_UNIT_FLAG = 2;
45bool emergency_brake = false;
46} // namespace
47
49 can_sender_->AddMessage(Throttlecommand100::ID, throttle_command_100_, false);
50 can_sender_->AddMessage(Brakecommand101::ID, brake_command_101_, false);
51 can_sender_->AddMessage(Gearcommand103::ID, gear_command_103_, false);
52 can_sender_->AddMessage(Parkcommand104::ID, park_command_104_, false);
53 can_sender_->AddMessage(Steeringcommand102::ID, steering_command_102_, false);
54 can_sender_->AddMessage(Vehiclemodecommand105::ID, vehicle_mode_command_105_,
55 false);
56}
57
59 const VehicleParameter& params,
60 CanSender<::apollo::canbus::Devkit>* const can_sender,
61 MessageManager<::apollo::canbus::Devkit>* const message_manager) {
62 if (is_initialized_) {
63 AINFO << "DevkitController has already been initiated.";
64 return ErrorCode::CANBUS_ERROR;
65 }
66 vehicle_params_.CopyFrom(
67 common::VehicleConfigHelper::Instance()->GetConfig().vehicle_param());
68
69 params_.CopyFrom(params);
70 if (!params_.has_driving_mode()) {
71 AERROR << "Vehicle conf pb not set driving_mode.";
72 return ErrorCode::CANBUS_ERROR;
73 }
74
75 if (can_sender == nullptr) {
76 return ErrorCode::CANBUS_ERROR;
77 }
78 can_sender_ = can_sender;
79
80 if (message_manager == nullptr) {
81 AERROR << "protocol manager is null.";
82 return ErrorCode::CANBUS_ERROR;
83 }
84 message_manager_ = message_manager;
85
86 // sender part
87 brake_command_101_ = dynamic_cast<Brakecommand101*>(
88 message_manager_->GetMutableProtocolDataById(Brakecommand101::ID));
89 if (brake_command_101_ == nullptr) {
90 AERROR << "Brakecommand101 does not exist in the DevkitMessageManager!";
91 return ErrorCode::CANBUS_ERROR;
92 }
93
94 gear_command_103_ = dynamic_cast<Gearcommand103*>(
95 message_manager_->GetMutableProtocolDataById(Gearcommand103::ID));
96 if (gear_command_103_ == nullptr) {
97 AERROR << "Gearcommand103 does not exist in the DevkitMessageManager!";
98 return ErrorCode::CANBUS_ERROR;
99 }
100
101 park_command_104_ = dynamic_cast<Parkcommand104*>(
102 message_manager_->GetMutableProtocolDataById(Parkcommand104::ID));
103 if (park_command_104_ == nullptr) {
104 AERROR << "Parkcommand104 does not exist in the DevkitMessageManager!";
105 return ErrorCode::CANBUS_ERROR;
106 }
107
108 steering_command_102_ = dynamic_cast<Steeringcommand102*>(
109 message_manager_->GetMutableProtocolDataById(Steeringcommand102::ID));
110 if (steering_command_102_ == nullptr) {
111 AERROR << "Steeringcommand102 does not exist in the DevkitMessageManager!";
112 return ErrorCode::CANBUS_ERROR;
113 }
114
115 throttle_command_100_ = dynamic_cast<Throttlecommand100*>(
116 message_manager_->GetMutableProtocolDataById(Throttlecommand100::ID));
117 if (throttle_command_100_ == nullptr) {
118 AERROR << "Throttlecommand100 does not exist in the DevkitMessageManager!";
119 return ErrorCode::CANBUS_ERROR;
120 }
121
122 vehicle_mode_command_105_ = dynamic_cast<Vehiclemodecommand105*>(
123 message_manager_->GetMutableProtocolDataById(Vehiclemodecommand105::ID));
124 if (vehicle_mode_command_105_ == nullptr) {
125 AERROR
126 << "Vehiclemodecommand105 does not exist in the DevkitMessageManager!";
127 return ErrorCode::CANBUS_ERROR;
128 }
129
131
132 AINFO << "DevkitController is initialized.";
133
134 is_initialized_ = true;
135 return ErrorCode::OK;
136}
137
139
141 if (!is_initialized_) {
142 AERROR << "DevkitController has NOT been initiated.";
143 return false;
144 }
145 const auto& update_func = [this] { SecurityDogThreadFunc(); };
146 thread_.reset(new std::thread(update_func));
147
148 return true;
149}
150
152 if (!is_initialized_) {
153 AERROR << "DevkitController stops or starts improperly!";
154 return;
155 }
156
157 if (thread_ != nullptr && thread_->joinable()) {
158 thread_->join();
159 thread_.reset();
160 AINFO << "DevkitController stopped.";
161 }
162}
163
165 chassis_.Clear();
166
167 Devkit chassis_detail = GetNewRecvChassisDetail();
168
169 // 1, 2
170 // if (driving_mode() == Chassis::EMERGENCY_MODE) {
171 // set_chassis_error_code(Chassis::NO_ERROR);
172 // }
173
174 chassis_.set_driving_mode(driving_mode());
175 chassis_.set_error_code(chassis_error_code());
176
177 // 3
178 chassis_.set_engine_started(true);
179 // 4 engine rpm ch has no engine rpm
180 // chassis_.set_engine_rpm(0);
181 // 5 wheel spd
182 if (chassis_detail.has_wheelspeed_report_506()) {
183 if (chassis_detail.wheelspeed_report_506().has_rr()) {
184 chassis_.mutable_wheel_speed()->set_wheel_spd_rr(
185 chassis_detail.wheelspeed_report_506().rr());
186 }
187 if (chassis_detail.wheelspeed_report_506().has_rl()) {
188 chassis_.mutable_wheel_speed()->set_wheel_spd_rl(
189 chassis_detail.wheelspeed_report_506().rl());
190 }
191 if (chassis_detail.wheelspeed_report_506().has_fr()) {
192 chassis_.mutable_wheel_speed()->set_wheel_spd_fr(
193 chassis_detail.wheelspeed_report_506().fr());
194 }
195 if (chassis_detail.wheelspeed_report_506().has_fl()) {
196 chassis_.mutable_wheel_speed()->set_wheel_spd_fl(
197 chassis_detail.wheelspeed_report_506().fl());
198 }
199 }
200 // 6 speed_mps
201 if (chassis_detail.has_vcu_report_505() &&
202 chassis_detail.vcu_report_505().has_speed()) {
203 chassis_.set_speed_mps(
204 std::abs(static_cast<float>(chassis_detail.vcu_report_505().speed())));
205 } else {
206 chassis_.set_speed_mps(0);
207 }
208 // 7 no odometer
209 // chassis_.set_odometer_m(0);
210 // 8 no fuel. do not set;
211 // chassis_.set_fuel_range_m(0);
212 // 9 throttle
213 if (chassis_detail.has_throttle_report_500() &&
214 chassis_detail.throttle_report_500().has_throttle_pedal_actual()) {
215 chassis_.set_throttle_percentage(static_cast<float>(
216 chassis_detail.throttle_report_500().throttle_pedal_actual()));
217 } else {
218 chassis_.set_throttle_percentage(0);
219 }
220 // throttle sender cmd
221 if (chassis_detail.has_throttle_command_100() &&
222 chassis_detail.throttle_command_100().has_throttle_pedal_target()) {
223 chassis_.set_throttle_percentage_cmd(static_cast<float>(
224 chassis_detail.throttle_command_100().throttle_pedal_target()));
225 } else {
226 chassis_.set_throttle_percentage_cmd(0);
227 }
228 // 10 brake
229 if (chassis_detail.has_brake_report_501() &&
230 chassis_detail.brake_report_501().has_brake_pedal_actual()) {
231 chassis_.set_brake_percentage(static_cast<float>(
232 chassis_detail.brake_report_501().brake_pedal_actual()));
233 } else {
234 chassis_.set_brake_percentage(0);
235 }
236 // brake sender cmd
237 if (chassis_detail.has_brake_command_101() &&
238 chassis_detail.brake_command_101().has_brake_pedal_target()) {
239 chassis_.set_brake_percentage_cmd(static_cast<float>(
240 chassis_detail.brake_command_101().brake_pedal_target()));
241 } else {
242 chassis_.set_brake_percentage_cmd(0);
243 }
244 // 23, previously 11 gear
245 if (chassis_detail.has_gear_report_503() &&
246 chassis_detail.gear_report_503().has_gear_actual()) {
248
249 if (chassis_detail.gear_report_503().gear_actual() ==
251 gear_pos = Chassis::GEAR_INVALID;
252 }
253 if (chassis_detail.gear_report_503().gear_actual() ==
255 gear_pos = Chassis::GEAR_NEUTRAL;
256 }
257 if (chassis_detail.gear_report_503().gear_actual() ==
259 gear_pos = Chassis::GEAR_REVERSE;
260 }
261 if (chassis_detail.gear_report_503().gear_actual() ==
263 gear_pos = Chassis::GEAR_DRIVE;
264 }
265 if (chassis_detail.gear_report_503().gear_actual() ==
267 gear_pos = Chassis::GEAR_PARKING;
268 }
269 chassis_.set_gear_location(gear_pos);
270 } else {
271 chassis_.set_gear_location(Chassis::GEAR_NONE);
272 }
273 // 12 steering
274 if (chassis_detail.has_steering_report_502() &&
275 chassis_detail.steering_report_502().has_steer_angle_actual()) {
276 chassis_.set_steering_percentage(static_cast<float>(
277 chassis_detail.steering_report_502().steer_angle_actual() * 100.0 /
278 vehicle_params_.max_steer_angle() * M_PI / 180));
279 } else {
280 chassis_.set_steering_percentage(0);
281 }
282 // steering sender cmd
283 if (chassis_detail.has_steering_command_102() &&
284 chassis_detail.steering_command_102().has_steer_angle_target()) {
285 chassis_.set_steering_percentage_cmd(static_cast<float>(
286 chassis_detail.steering_command_102().steer_angle_target()));
287 } else {
288 chassis_.set_steering_percentage_cmd(0);
289 }
290 // 13 parking brake
291 if (chassis_detail.has_park_report_504() &&
292 chassis_detail.park_report_504().has_parking_actual()) {
293 if (chassis_detail.park_report_504().parking_actual() ==
295 chassis_.set_parking_brake(true);
296 } else {
297 chassis_.set_parking_brake(false);
298 }
299 } else {
300 chassis_.set_parking_brake(false);
301 }
302 // 14 battery soc
303 if (chassis_detail.has_bms_report_512() &&
304 chassis_detail.bms_report_512().has_battery_soc_percentage()) {
305 chassis_.set_battery_soc_percentage(
306 chassis_detail.bms_report_512().battery_soc_percentage());
307 // 15 give engage_advice based on battery low soc warn
308 if (chassis_detail.bms_report_512().is_battery_soc_low()) {
309 chassis_.mutable_engage_advice()->set_advice(
311 chassis_.mutable_engage_advice()->set_reason(
312 "Battery soc percentage is lower than 15%, please charge it "
313 "quickly!");
314 }
315 } else {
316 chassis_.set_battery_soc_percentage(0);
317 }
318 // 16 sonor list
319 // to do(ALL):check your vehicle type, confirm your sonar position because of
320 // every vhechle has different sonars assembly.
321 // 08 09 10 11
322 if (chassis_detail.has_ultr_sensor_1_507()) {
323 chassis_.mutable_surround()->set_sonar08(
324 chassis_detail.ultr_sensor_1_507().uiuss8_tof_direct());
325 chassis_.mutable_surround()->set_sonar09(
326 chassis_detail.ultr_sensor_1_507().uiuss9_tof_direct());
327 chassis_.mutable_surround()->set_sonar10(
328 chassis_detail.ultr_sensor_1_507().uiuss10_tof_direct());
329 chassis_.mutable_surround()->set_sonar11(
330 chassis_detail.ultr_sensor_1_507().uiuss11_tof_direct());
331 } else {
332 chassis_.mutable_surround()->set_sonar08(0);
333 chassis_.mutable_surround()->set_sonar09(0);
334 chassis_.mutable_surround()->set_sonar10(0);
335 chassis_.mutable_surround()->set_sonar11(0);
336 }
337 // 2 3 4 5
338 if (chassis_detail.has_ultr_sensor_3_509()) {
339 chassis_.mutable_surround()->set_sonar02(
340 chassis_detail.ultr_sensor_3_509().uiuss2_tof_direct());
341 chassis_.mutable_surround()->set_sonar03(
342 chassis_detail.ultr_sensor_3_509().uiuss3_tof_direct());
343 chassis_.mutable_surround()->set_sonar04(
344 chassis_detail.ultr_sensor_3_509().uiuss4_tof_direct());
345 chassis_.mutable_surround()->set_sonar05(
346 chassis_detail.ultr_sensor_3_509().uiuss5_tof_direct());
347 } else {
348 chassis_.mutable_surround()->set_sonar02(0);
349 chassis_.mutable_surround()->set_sonar03(0);
350 chassis_.mutable_surround()->set_sonar04(0);
351 chassis_.mutable_surround()->set_sonar05(0);
352 }
353 // 0 1 6 7
354 if (chassis_detail.has_ultr_sensor_5_511()) {
355 chassis_.mutable_surround()->set_sonar00(
356 chassis_detail.ultr_sensor_5_511().uiuss0_tof_direct());
357 chassis_.mutable_surround()->set_sonar01(
358 chassis_detail.ultr_sensor_5_511().uiuss1_tof_direct());
359 chassis_.mutable_surround()->set_sonar06(
360 chassis_detail.ultr_sensor_5_511().uiuss6_tof_direct());
361 chassis_.mutable_surround()->set_sonar07(
362 chassis_detail.ultr_sensor_5_511().uiuss7_tof_direct());
363 } else {
364 chassis_.mutable_surround()->set_sonar00(0);
365 chassis_.mutable_surround()->set_sonar01(0);
366 chassis_.mutable_surround()->set_sonar06(0);
367 chassis_.mutable_surround()->set_sonar07(0);
368 }
369 // 17 set vin
370 // vin set 17 bits, like LSBN1234567890123 is prased as
371 // vin17(L),vin16(S),vin15(B),......,vin03(1),vin02(2),vin01(3)
372 std::string vin = "";
373 if (chassis_detail.has_vin_resp1_514()) {
374 Vin_resp1_514 vin_resp1_514 = chassis_detail.vin_resp1_514();
375 vin += vin_resp1_514.vin00();
376 vin += vin_resp1_514.vin01();
377 vin += vin_resp1_514.vin02();
378 vin += vin_resp1_514.vin03();
379 vin += vin_resp1_514.vin04();
380 vin += vin_resp1_514.vin05();
381 vin += vin_resp1_514.vin06();
382 vin += vin_resp1_514.vin07();
383 }
384 if (chassis_detail.has_vin_resp2_515()) {
385 Vin_resp2_515 vin_resp2_515 = chassis_detail.vin_resp2_515();
386 vin += vin_resp2_515.vin08();
387 vin += vin_resp2_515.vin09();
388 vin += vin_resp2_515.vin10();
389 vin += vin_resp2_515.vin11();
390 vin += vin_resp2_515.vin12();
391 vin += vin_resp2_515.vin13();
392 vin += vin_resp2_515.vin14();
393 vin += vin_resp2_515.vin15();
394 }
395 if (chassis_detail.has_vin_resp3_516()) {
396 Vin_resp3_516 vin_resp3_516 = chassis_detail.vin_resp3_516();
397 vin += vin_resp3_516.vin16();
398 }
399 std::reverse(vin.begin(), vin.end());
400 chassis_.mutable_vehicle_id()->set_vin(vin);
401 // 18 front bumper event
402 if (chassis_detail.has_vcu_report_505() &&
403 chassis_detail.vcu_report_505().has_frontcrash_state()) {
404 if (chassis_detail.vcu_report_505().frontcrash_state() ==
406 chassis_.set_front_bumper_event(Chassis::BUMPER_PRESSED);
407 } else {
408 chassis_.set_front_bumper_event(Chassis::BUMPER_NORMAL);
409 }
410 } else {
411 chassis_.set_front_bumper_event(Chassis::BUMPER_INVALID);
412 }
413 // 19 back bumper event
414 if (chassis_detail.has_vcu_report_505() &&
415 chassis_detail.vcu_report_505().has_backcrash_state()) {
416 if (chassis_detail.vcu_report_505().backcrash_state() ==
418 chassis_.set_back_bumper_event(Chassis::BUMPER_PRESSED);
419 } else {
420 chassis_.set_back_bumper_event(Chassis::BUMPER_NORMAL);
421 }
422 } else {
423 chassis_.set_back_bumper_event(Chassis::BUMPER_INVALID);
424 }
425
426 // 20 add checkresponse signal
427 if (chassis_detail.has_brake_report_501() &&
428 chassis_detail.brake_report_501().has_brake_en_state()) {
429 chassis_.mutable_check_response()->set_is_esp_online(
430 chassis_detail.brake_report_501().brake_en_state() == 1);
431 }
432 if (chassis_detail.has_steering_report_502() &&
433 chassis_detail.steering_report_502().has_steer_en_state()) {
434 chassis_.mutable_check_response()->set_is_eps_online(
435 chassis_detail.steering_report_502().steer_en_state() == 1);
436 }
437 if (chassis_detail.has_throttle_report_500() &&
438 chassis_detail.throttle_report_500().has_throttle_en_state()) {
439 chassis_.mutable_check_response()->set_is_vcu_online(
440 chassis_detail.throttle_report_500().throttle_en_state() == 1);
441 }
442
443 if (CheckChassisError()) {
444 chassis_.mutable_engage_advice()->set_advice(
446 chassis_.mutable_engage_advice()->set_reason(
447 "Chassis has some fault, please check the chassis_detail.");
448 }
449
450 // check the chassis detail lost
452 chassis_.mutable_engage_advice()->set_advice(
454 chassis_.mutable_engage_advice()->set_reason(
455 "devkit chassis detail is lost! Please check the communication error.");
456 set_chassis_error_code(Chassis::CHASSIS_CAN_LOST);
458 }
459
460 return chassis_;
461}
462
463bool DevkitController::VerifyID() {
464 if (!CheckVin()) {
465 AERROR << "Failed to get the vin.";
466 GetVin();
467 return false;
468 } else {
469 ResetVin();
470 return true;
471 }
472}
473
474void DevkitController::Emergency() {
476 ResetProtocol();
477}
478
479ErrorCode DevkitController::EnableAutoMode() {
481 AINFO << "already in COMPLETE_AUTO_DRIVE mode";
482 return ErrorCode::OK;
483 }
484 // set enable
485 brake_command_101_->set_brake_en_ctrl(
487 throttle_command_100_->set_throttle_en_ctrl(
489 steering_command_102_->set_steer_en_ctrl(
493 // set AEB enable
494 if (FLAGS_enable_aeb) {
495 brake_command_101_->set_aeb_en_ctrl(
497 AINFO << "Set AEB enable";
498 }
499
500 can_sender_->Update();
501 const int32_t flag =
502 CHECK_RESPONSE_STEER_UNIT_FLAG | CHECK_RESPONSE_SPEED_UNIT_FLAG;
503 if (!CheckResponse(flag, true)) {
504 AERROR << "Failed to switch to COMPLETE_AUTO_DRIVE mode. Please check the "
505 "emergency button or chassis.";
506 Emergency();
507 set_chassis_error_code(Chassis::CHASSIS_ERROR);
508 return ErrorCode::CANBUS_ERROR;
509 }
511 AINFO << "Switch to COMPLETE_AUTO_DRIVE mode ok.";
512 return ErrorCode::OK;
513}
514
515ErrorCode DevkitController::DisableAutoMode() {
516 ResetProtocol();
517 can_sender_->Update();
519 set_chassis_error_code(Chassis::NO_ERROR);
520 AINFO << "Switch to COMPLETE_MANUAL ok.";
521 return ErrorCode::OK;
522}
523
524ErrorCode DevkitController::EnableSteeringOnlyMode() {
528 AINFO << "Already in AUTO_STEER_ONLY mode.";
529 return ErrorCode::OK;
530 }
531 AFATAL << "SpeedOnlyMode is not supported in devkit!";
532 return ErrorCode::CANBUS_ERROR;
533}
534
535ErrorCode DevkitController::EnableSpeedOnlyMode() {
539 AINFO << "Already in AUTO_SPEED_ONLY mode";
540 return ErrorCode::OK;
541 }
542 AFATAL << "SpeedOnlyMode is not supported in devkit!";
543 return ErrorCode::CANBUS_ERROR;
544}
545
546// NEUTRAL, REVERSE, DRIVE
547void DevkitController::Gear(Chassis::GearPosition gear_position) {
550 AINFO << "This drive mode no need to set gear.";
551 return;
552 }
553 switch (gear_position) {
556 break;
557 }
560 break;
561 }
562 case Chassis::GEAR_DRIVE: {
564 break;
565 }
568 break;
569 }
572 break;
573 }
574 default: {
576 break;
577 }
578 }
579}
580
581// brake with pedal
582// pedal:0.00~99.99, unit:%
583void DevkitController::Brake(double pedal) {
584 // double real_value = params_.max_acc() * acceleration / 100;
585 // TODO(All) : Update brake value based on mode
588 AINFO << "The current drive mode does not need to set brake pedal.";
589 return;
590 }
591 if (!emergency_brake) {
592 brake_command_101_->set_brake_pedal_target(pedal);
593 }
594 // brake_command_101_->set_brake_pedal_target(pedal);
595}
596
597// drive with pedal
598// pedal:0.0~99.9 unit:%s
599void DevkitController::Throttle(double pedal) {
602 AINFO << "The current drive mode does not need to set throttle pedal.";
603 return;
604 }
605 if (!emergency_brake) {
606 throttle_command_100_->set_throttle_pedal_target(pedal);
607 }
608 // throttle_command_100_->set_throttle_pedal_target(pedal);
609}
610
611// confirm the car is driven by acceleration command instead of
612// throttle/brake pedal drive with acceleration/deceleration acc:-7.0 ~ 5.0,
613// unit:m/s^2
614void DevkitController::Acceleration(double acc) {
617 AINFO << "The current drive mode does not need to set acceleration.";
618 return;
619 }
620 // None
621}
622
623// confirm the car is driven by speed command
624// speed:-xx.0~xx.0, unit:m/s
625void DevkitController::Speed(double speed) {
628 AINFO << "The current drive mode does not need to set speed.";
629 return;
630 }
631 /* ADD YOUR OWN CAR CHASSIS OPERATION
632 // TODO(ALL): CHECK YOUR VEHICLE WHETHER SUPPORT THIS DRIVE MODE
633 */
634}
635
636// devkit default, left:+, right:-
637// need to be compatible with control module, so reverse
638// steering with default angle speed, 25-250 (default:250)
639// angle:-99.99~0.00~99.99, unit:, left:+, right:-
640void DevkitController::Steer(double angle) {
643 AINFO << "The current driving mode does not need to set steer.";
644 return;
645 }
646 const double real_angle =
647 vehicle_params_.max_steer_angle() / M_PI * 180 * angle / 100.0;
648
649 if (!emergency_brake) {
650 steering_command_102_->set_steer_angle_target(real_angle)
652 }
653}
654
655// steering with new angle speed
656// angle:-99.99~0.00~99.99, unit:, left:+, right:-
657// angle_spd:25~250(default:250), unit:deg/s
658void DevkitController::Steer(double angle, double angle_spd) {
661 AINFO << "The current driving mode does not need to set steer.";
662 return;
663 }
664 const double real_angle =
665 vehicle_params_.max_steer_angle() / M_PI * 180 * angle / 100.0;
666
667 if (!emergency_brake) {
668 steering_command_102_->set_steer_angle_target(real_angle)
670 }
671}
672
673void DevkitController::SetEpbBreak(const ControlCommand& command) {
674 if (command.parking_brake()) {
675 park_command_104_->set_park_target(
677 } else {
679 }
680}
681
682ErrorCode DevkitController::HandleCustomOperation(
683 const external_command::ChassisCommand& command) {
684 return ErrorCode::OK;
685}
686
687void DevkitController::SetBeam(const VehicleSignal& vehicle_signal) {
688 if (vehicle_signal.high_beam()) {
689 // None
690 } else if (vehicle_signal.low_beam()) {
691 // None
692 }
693}
694
695void DevkitController::SetHorn(const VehicleSignal& vehicle_signal) {
696 if (vehicle_signal.horn()) {
697 // None
698 } else {
699 // None
700 }
701}
702
703void DevkitController::SetTurningSignal(const VehicleSignal& vehicle_signal) {
704 // Set Turn Signal
705 auto signal = vehicle_signal.turn_signal();
706 if (signal == common::VehicleSignal::TURN_LEFT) {
707 vehicle_mode_command_105_->set_turn_light_ctrl(
709 } else if (signal == common::VehicleSignal::TURN_RIGHT) {
710 vehicle_mode_command_105_->set_turn_light_ctrl(
712 } else if (signal == common::VehicleSignal::TURN_HAZARD_WARNING) {
713 vehicle_mode_command_105_->set_turn_light_ctrl(
715 } else {
716 vehicle_mode_command_105_->set_turn_light_ctrl(
718 }
719}
720
721bool DevkitController::CheckVin() {
722 if (chassis_.vehicle_id().vin().size() == 17) {
723 AINFO << "Vin check success! Vehicel vin is "
724 << chassis_.vehicle_id().vin();
725 return true;
726 } else {
727 AINFO << "Vin check failed! Current vin size is "
728 << chassis_.vehicle_id().vin().size();
729 return false;
730 }
731}
732
733void DevkitController::GetVin() {
734 vehicle_mode_command_105_->set_vin_req(
736 AINFO << "Get vin";
737 can_sender_->Update();
738}
739
740void DevkitController::ResetVin() {
741 vehicle_mode_command_105_->set_vin_req(
743 AINFO << "Reset vin";
744 can_sender_->Update();
745}
746
747void DevkitController::ResetProtocol() {
748 message_manager_->ResetSendMessages();
749}
750
751bool DevkitController::CheckChassisError() {
753 AERROR_EVERY(100) << "ChassisDetail has no devkit vehicle info.";
754 return false;
755 }
756
757 Devkit chassis_detail = GetNewRecvChassisDetail();
758 // steer fault
759 if (chassis_detail.has_steering_report_502()) {
761 chassis_detail.steering_report_502().steer_flt1()) {
762 AERROR_EVERY(100) << "Chassis has steer system fault.";
763 return true;
764 }
765 }
766 // drive fault
767 if (chassis_detail.has_throttle_report_500()) {
769 chassis_detail.throttle_report_500().throttle_flt1()) {
770 AERROR_EVERY(100) << "Chassis has drive system fault.";
771 return true;
772 }
773 }
774 // brake fault
775 if (chassis_detail.has_brake_report_501()) {
777 chassis_detail.brake_report_501().brake_flt1()) {
778 AERROR_EVERY(100) << "Chassis has brake system fault.";
779 return true;
780 }
781 }
782 // battery soc low
783 if (chassis_detail.has_bms_report_512()) {
784 if (chassis_detail.bms_report_512().is_battery_soc_low()) {
785 AERROR_EVERY(100) << "Chassis battery has low soc, please charge.";
786 return true;
787 }
788 }
789 // battery over emperature fault
790 if (chassis_detail.has_bms_report_512()) {
792 chassis_detail.bms_report_512().battery_flt_over_temp()) {
793 AERROR_EVERY(100) << "Chassis battery has over temperature fault.";
794 return true;
795 }
796 }
797 // battery low temperature fault
798 if (chassis_detail.has_bms_report_512()) {
800 chassis_detail.bms_report_512().battery_flt_low_temp()) {
801 AERROR_EVERY(100) << "Chassis battery has below low temperature fault.";
802 return true;
803 }
804 }
805
806 return false;
807}
808
809void DevkitController::SecurityDogThreadFunc() {
810 int32_t vertical_ctrl_fail = 0;
811 int32_t horizontal_ctrl_fail = 0;
812
813 if (can_sender_ == nullptr) {
814 AERROR << "Failed to run SecurityDogThreadFunc() because can_sender_ is "
815 "nullptr.";
816 return;
817 }
818 while (!can_sender_->IsRunning()) {
819 std::this_thread::yield();
820 }
821
822 std::chrono::duration<double, std::micro> default_period{50000};
823 int64_t start = 0;
824 int64_t end = 0;
825 while (can_sender_->IsRunning()) {
827 const Chassis::DrivingMode mode = driving_mode();
828 bool emergency_mode = false;
829 emergency_brake = false;
830
831 // 1. horizontal control check
832 if ((mode == Chassis::COMPLETE_AUTO_DRIVE ||
833 mode == Chassis::AUTO_STEER_ONLY) &&
834 CheckResponse(CHECK_RESPONSE_STEER_UNIT_FLAG, false) == false) {
835 ++horizontal_ctrl_fail;
836 if (horizontal_ctrl_fail >= kMaxFailAttempt) {
837 emergency_mode = true;
838 AERROR << "Driving_mode is into emergency by steer manual intervention";
839 set_chassis_error_code(Chassis::MANUAL_INTERVENTION);
840 }
841 } else {
842 horizontal_ctrl_fail = 0;
843 }
844
845 // 2. vertical control check
846 if ((mode == Chassis::COMPLETE_AUTO_DRIVE ||
847 mode == Chassis::AUTO_SPEED_ONLY) &&
848 !CheckResponse(CHECK_RESPONSE_SPEED_UNIT_FLAG, false)) {
849 ++vertical_ctrl_fail;
850 if (vertical_ctrl_fail >= kMaxFailAttempt) {
851 emergency_mode = true;
852 AERROR << "Driving_mode is into emergency by speed manual intervention";
853 set_chassis_error_code(Chassis::MANUAL_INTERVENTION);
854 }
855 } else {
856 vertical_ctrl_fail = 0;
857 }
858
859 // 3. chassis fault check
860 if (CheckChassisError()) {
861 set_chassis_error_code(Chassis::CHASSIS_ERROR);
862 emergency_mode = true;
863 if (chassis_.speed_mps() > 0.3) {
864 emergency_brake = true;
865 }
866 }
867
868 if (emergency_mode && mode != Chassis::EMERGENCY_MODE) {
870 if (emergency_brake) {
871 throttle_command_100_->set_throttle_pedal_target(0);
872 brake_command_101_->set_brake_pedal_target(40);
873 steering_command_102_->set_steer_angle_target(0);
874 std::this_thread::sleep_for(
875 std::chrono::duration<double, std::milli>(3000));
876 }
877 message_manager_->ResetSendMessages();
878 can_sender_->Update();
879 emergency_brake = false;
880 }
881
882 // recove error code
883 if (!emergency_mode && !is_chassis_communication_error_ &&
884 mode == Chassis::EMERGENCY_MODE) {
885 set_chassis_error_code(Chassis::NO_ERROR);
886 }
887
889 std::chrono::duration<double, std::micro> elapsed{end - start};
890 if (elapsed < default_period) {
891 std::this_thread::sleep_for(default_period - elapsed);
892 } else {
893 AERROR << "Too much time consumption in DevkitController looping "
894 "process:"
895 << elapsed.count();
896 }
897 }
898}
899
900bool DevkitController::CheckResponse(const int32_t flags, bool need_wait) {
901 int32_t retry_num = 20;
902 Devkit chassis_detail;
903 bool is_eps_online = false;
904 bool is_vcu_online = false;
905 bool is_esp_online = false;
906
907 do {
908 bool check_ok = true;
909 if (flags & CHECK_RESPONSE_STEER_UNIT_FLAG) {
910 is_eps_online = chassis_.has_check_response() &&
911 chassis_.check_response().has_is_eps_online() &&
912 chassis_.check_response().is_eps_online();
913 check_ok = check_ok && is_eps_online;
914 }
915
916 if (flags & CHECK_RESPONSE_SPEED_UNIT_FLAG) {
917 is_vcu_online = chassis_.has_check_response() &&
918 chassis_.check_response().has_is_vcu_online() &&
919 chassis_.check_response().is_vcu_online();
920 is_esp_online = chassis_.has_check_response() &&
921 chassis_.check_response().has_is_esp_online() &&
922 chassis_.check_response().is_esp_online();
923 check_ok = check_ok && is_vcu_online && is_esp_online;
924 }
925 if (check_ok) {
926 return true;
927 } else {
928 AINFO << "Need to check response again.";
929 }
930 if (need_wait) {
931 --retry_num;
932 std::this_thread::sleep_for(
933 std::chrono::duration<double, std::milli>(20));
934 }
935 } while (need_wait && retry_num);
936
937 if (flags & CHECK_RESPONSE_STEER_UNIT_FLAG) {
938 AERROR << "steer check_response fail: is_eps_online:" << is_eps_online;
939 }
940
941 if (flags & CHECK_RESPONSE_SPEED_UNIT_FLAG) {
942 AERROR << "speed check_response fail: " << "is_vcu_online:" << is_vcu_online
943 << ", is_esp_online:" << is_esp_online;
944 }
945
946 return false;
947}
948
949void DevkitController::set_chassis_error_mask(const int32_t mask) {
950 std::lock_guard<std::mutex> lock(chassis_mask_mutex_);
951 chassis_error_mask_ = mask;
952}
953
954int32_t DevkitController::chassis_error_mask() {
955 std::lock_guard<std::mutex> lock(chassis_mask_mutex_);
956 return chassis_error_mask_;
957}
958
959Chassis::ErrorCode DevkitController::chassis_error_code() {
960 std::lock_guard<std::mutex> lock(chassis_error_code_mutex_);
961 return chassis_error_code_;
962}
963
964void DevkitController::set_chassis_error_code(
965 const Chassis::ErrorCode& error_code) {
966 std::lock_guard<std::mutex> lock(chassis_error_code_mutex_);
967 chassis_error_code_ = error_code;
968}
969
970} // namespace devkit
971} // namespace canbus
972} // namespace apollo
Defines SenderMessage class and CanSender class.
virtual void set_driving_mode(const Chassis::DrivingMode &driving_mode)
MessageManager< ::apollo::canbus::Devkit > * message_manager_
Brakecommand101 * set_brake_pedal_target(double brake_pedal_target)
Brakecommand101 * set_aeb_en_ctrl(Brake_command_101::Aeb_en_ctrlType aeb_en_ctrl)
Brakecommand101 * set_brake_en_ctrl(Brake_command_101::Brake_en_ctrlType brake_en_ctrl)
bool Start() override
start the vehicle controller.
::apollo::common::ErrorCode Init(const VehicleParameter &params, CanSender<::apollo::canbus::Devkit > *const can_sender, MessageManager<::apollo::canbus::Devkit > *const message_manager) override
void AddSendMessage() override
add the sender message.
void Stop() override
stop the vehicle controller.
Chassis chassis() override
calculate and return the chassis.
Gearcommand103 * set_gear_en_ctrl(Gear_command_103::Gear_en_ctrlType gear_en_ctrl)
Gearcommand103 * set_gear_target(Gear_command_103::Gear_targetType gear_target)
Parkcommand104 * set_park_en_ctrl(Park_command_104::Park_en_ctrlType park_en_ctrl)
Parkcommand104 * set_park_target(Park_command_104::Park_targetType park_target)
Steeringcommand102 * set_steer_angle_spd_target(int steer_angle_spd_target)
Steeringcommand102 * set_steer_en_ctrl(Steering_command_102::Steer_en_ctrlType steer_en_ctrl)
Steeringcommand102 * set_steer_angle_target(int steer_angle_target)
Throttlecommand100 * set_throttle_en_ctrl(Throttle_command_100::Throttle_en_ctrlType throttle_en_ctrl)
Throttlecommand100 * set_throttle_pedal_target(double throttle_pedal_target)
Vehiclemodecommand105 * set_vin_req(Vehicle_mode_command_105::Vin_reqType vin_req)
Vehiclemodecommand105 * set_turn_light_ctrl(Vehicle_mode_command_105::Turn_light_ctrlType turn_light_ctrl)
uint64_t ToMicrosecond() const
convert time to microsecond (us).
Definition time.cc:85
static Time Now()
get the current time.
Definition time.cc:57
#define AERROR
Definition log.h:44
#define AERROR_EVERY(freq)
Definition log.h:86
#define AFATAL
Definition log.h:45
#define AINFO
Definition log.h:42
class register implement
Definition arena_queue.h:37
The class of ProtocolData
optional int32 battery_soc_percentage
Definition devkit.proto:406
optional double brake_pedal_target
Definition demo.proto:44
optional Brake_en_stateType brake_en_state
Definition demo.proto:194
optional double brake_pedal_actual
Definition demo.proto:188
optional apollo::common::VehicleID vehicle_id
optional CheckResponse check_response
optional float speed_mps
Definition chassis.proto:70
optional Bms_report_512 bms_report_512
Definition devkit.proto:481
optional Brake_command_101 brake_command_101
Definition devkit.proto:465
optional Ultr_sensor_3_509 ultr_sensor_3_509
Definition devkit.proto:478
optional Gear_report_503 gear_report_503
Definition devkit.proto:472
optional Park_report_504 park_report_504
Definition devkit.proto:473
optional Vin_resp2_515 vin_resp2_515
Definition devkit.proto:484
optional Wheelspeed_report_506 wheelspeed_report_506
Definition devkit.proto:475
optional Vcu_report_505 vcu_report_505
Definition devkit.proto:474
optional Steering_report_502 steering_report_502
Definition devkit.proto:471
optional Vin_resp3_516 vin_resp3_516
Definition devkit.proto:485
optional Throttle_command_100 throttle_command_100
Definition devkit.proto:464
optional Ultr_sensor_1_507 ultr_sensor_1_507
Definition devkit.proto:476
optional Steering_command_102 steering_command_102
Definition devkit.proto:466
optional Brake_report_501 brake_report_501
Definition devkit.proto:470
optional Ultr_sensor_5_511 ultr_sensor_5_511
Definition devkit.proto:480
optional Vin_resp1_514 vin_resp1_514
Definition devkit.proto:483
optional Throttle_report_500 throttle_report_500
Definition devkit.proto:469
optional Gear_actualType gear_actual
Definition demo.proto:243
optional Parking_actualType parking_actual
Definition demo.proto:257
optional Steer_en_stateType steer_en_state
Definition demo.proto:222
optional double throttle_pedal_target
Definition demo.proto:20
optional double throttle_pedal_actual
Definition demo.proto:162
optional Throttle_en_stateType throttle_en_state
Definition demo.proto:168
optional double uiuss9_tof_direct
Definition demo.proto:346
optional double uiuss8_tof_direct
Definition demo.proto:348
optional double uiuss11_tof_direct
Definition demo.proto:350
optional double uiuss10_tof_direct
Definition demo.proto:352
optional double uiuss4_tof_direct
Definition demo.proto:372
optional double uiuss2_tof_direct
Definition demo.proto:376
optional double uiuss3_tof_direct
Definition demo.proto:374
optional double uiuss5_tof_direct
Definition demo.proto:370
optional double uiuss7_tof_direct
Definition demo.proto:394
optional double uiuss6_tof_direct
Definition demo.proto:396
optional double uiuss0_tof_direct
Definition demo.proto:400
optional double uiuss1_tof_direct
Definition demo.proto:398
optional Backcrash_stateType backcrash_state
Definition demo.proto:322
optional Frontcrash_stateType frontcrash_state
Definition demo.proto:320
The class of VehicleController