33using ::apollo::common::ErrorCode;
34using ::apollo::common::VehicleSignal;
35using ::apollo::control::ControlCommand;
36using ::apollo::drivers::canbus::ProtocolData;
39const int32_t kMaxFailAttempt = 10;
40const int32_t CHECK_RESPONSE_STEER_UNIT_FLAG = 1;
41const int32_t CHECK_RESPONSE_SPEED_UNIT_FLAG = 2;
57 CanSender<::apollo::canbus::Demo>*
const can_sender,
58 MessageManager<::apollo::canbus::Demo>*
const message_manager) {
60 AINFO <<
"DemoController has already been initiated.";
61 return ErrorCode::CANBUS_ERROR;
65 common::VehicleConfigHelper::Instance()->GetConfig().vehicle_param());
67 if (!
params_.has_driving_mode()) {
68 AERROR <<
"Vehicle conf pb not set driving_mode.";
69 return ErrorCode::CANBUS_ERROR;
72 if (can_sender ==
nullptr) {
73 AERROR <<
"Canbus sender is null.";
74 return ErrorCode::CANBUS_ERROR;
78 if (message_manager ==
nullptr) {
79 AERROR <<
"protocol manager is null.";
80 return ErrorCode::CANBUS_ERROR;
87 if (brake_command_101_ ==
nullptr) {
88 AERROR <<
"Brakecommand101 does not exist in the DemoMessageManager!";
89 return ErrorCode::CANBUS_ERROR;
94 if (gear_command_103_ ==
nullptr) {
95 AERROR <<
"Gearcommand103 does not exist in the DemoMessageManager!";
96 return ErrorCode::CANBUS_ERROR;
101 if (park_command_104_ ==
nullptr) {
102 AERROR <<
"Parkcommand104 does not exist in the DemoMessageManager!";
103 return ErrorCode::CANBUS_ERROR;
108 if (steering_command_102_ ==
nullptr) {
109 AERROR <<
"Steeringcommand102 does not exist in the DemoMessageManager!";
110 return ErrorCode::CANBUS_ERROR;
115 if (throttle_command_100_ ==
nullptr) {
116 AERROR <<
"Throttlecommand100 does not exist in the DemoMessageManager!";
117 return ErrorCode::CANBUS_ERROR;
122 if (vehicle_mode_command_105_ ==
nullptr) {
123 AERROR <<
"Vehiclemodecommand105 does not exist in the DemoMessageManager!";
124 return ErrorCode::CANBUS_ERROR;
129 AINFO <<
"DemoController is initialized.";
132 return ErrorCode::OK;
139 AERROR <<
"DemoController has NOT been initiated.";
142 const auto& update_func = [
this] { SecurityDogThreadFunc(); };
143 thread_.reset(
new std::thread(update_func));
150 AERROR <<
"DemoController stops or starts improperly!";
154 if (thread_ !=
nullptr && thread_->joinable()) {
157 AINFO <<
"DemoController stopped.";
170 chassis_.set_error_code(chassis_error_code());
173 chassis_.set_engine_started(
true);
176 if (chassis_detail.has_vcu_report_505() &&
178 chassis_.set_speed_mps(
181 chassis_.set_speed_mps(0);
185 if (chassis_detail.has_throttle_report_500() &&
187 chassis_.set_throttle_percentage(
static_cast<float>(
190 chassis_.set_throttle_percentage(0);
194 if (chassis_detail.has_brake_report_501() &&
196 chassis_.set_brake_percentage(
static_cast<float>(
199 chassis_.set_brake_percentage(0);
203 if (chassis_detail.has_gear_report_503() &&
224 chassis_.set_gear_location(gear_pos);
230 if (chassis_detail.has_steering_report_502() &&
232 chassis_.set_steering_percentage(
static_cast<float>(
236 chassis_.set_steering_percentage(0);
242 if (chassis_detail.has_brake_report_501() &&
244 chassis_.mutable_check_response()->set_is_esp_online(
248 if (chassis_detail.has_steering_report_502() &&
250 chassis_.mutable_check_response()->set_is_eps_online(
254 if (chassis_detail.has_throttle_report_500() &&
256 chassis_.mutable_check_response()->set_is_vcu_online(
261 if (CheckChassisError()) {
262 chassis_.mutable_engage_advice()->set_advice(
264 chassis_.mutable_engage_advice()->set_reason(
265 "Chassis has some fault, please check the chassis_detail.");
270 chassis_.mutable_engage_advice()->set_advice(
272 chassis_.mutable_engage_advice()->set_reason(
273 "demo chassis detail is lost! Please check the communication error.");
289void DemoController::Emergency() {
294ErrorCode DemoController::EnableAutoMode() {
296 AINFO <<
"already in COMPLETE_AUTO_DRIVE mode";
297 return ErrorCode::OK;
313 CHECK_RESPONSE_STEER_UNIT_FLAG | CHECK_RESPONSE_SPEED_UNIT_FLAG;
314 if (!CheckResponse(flag,
true)) {
315 AERROR <<
"Failed to switch to COMPLETE_AUTO_DRIVE mode. Please check the "
316 "emergency button or chassis.";
319 return ErrorCode::CANBUS_ERROR;
322 AINFO <<
"Switch to COMPLETE_AUTO_DRIVE mode ok.";
323 return ErrorCode::OK;
326ErrorCode DemoController::DisableAutoMode() {
331 AINFO <<
"Switch to COMPLETE_MANUAL ok.";
332 return ErrorCode::OK;
335ErrorCode DemoController::EnableSteeringOnlyMode() {
339 AINFO <<
"Already in AUTO_STEER_ONLY mode.";
340 return ErrorCode::OK;
369 return ErrorCode::OK;
372ErrorCode DemoController::EnableSpeedOnlyMode() {
376 AINFO <<
"Already in AUTO_SPEED_ONLY mode";
377 return ErrorCode::OK;
406 return ErrorCode::OK;
413 AINFO <<
"This drive mode no need to set gear.";
416 switch (gear_position) {
446void DemoController::Brake(
double pedal) {
452 AINFO <<
"The current drive mode does not need to set brake pedal.";
460void DemoController::Throttle(
double pedal) {
463 AINFO <<
"The current drive mode does not need to set throttle pedal.";
472void DemoController::Acceleration(
double acc) {
475 AINFO <<
"The current drive mode does not need to set acceleration.";
485void DemoController::Speed(
double speed) {
488 AINFO <<
"The current drive mode does not need to set speed.";
500void DemoController::Steer(
double angle) {
503 AINFO <<
"The current driving mode does not need to set steer.";
506 const double real_angle =
514void DemoController::Steer(
double angle,
double angle_spd) {
517 AINFO <<
"The current driving mode does not need to set steer.";
520 const double real_angle =
525void DemoController::SetEpbBreak(
const ControlCommand& command) {
526 if (command.parking_brake()) {
533void DemoController::SetBeam(
const VehicleSignal& vehicle_signal) {
534 if (vehicle_signal.high_beam()) {
536 }
else if (vehicle_signal.low_beam()) {
543void DemoController::SetHorn(
const VehicleSignal& vehicle_signal) {
544 if (vehicle_signal.horn()) {
551void DemoController::SetTurningSignal(
const VehicleSignal& vehicle_signal) {
565ErrorCode DemoController::HandleCustomOperation(
566 const external_command::ChassisCommand& command) {
567 return ErrorCode::OK;
570bool DemoController::VerifyID() {
572 AERROR <<
"Failed to get the vin. Get vin again.";
581bool DemoController::CheckVin() {
596void DemoController::GetVin() {
606void DemoController::ResetVin() {
616void DemoController::ResetProtocol() {
message_manager_->ResetSendMessages(); }
618bool DemoController::CheckChassisError() {
620 AERROR_EVERY(100) <<
"ChassisDetail has no demo vehicle info.";
632void DemoController::SecurityDogThreadFunc() {
633 int32_t vertical_ctrl_fail = 0;
634 int32_t horizontal_ctrl_fail = 0;
637 AERROR <<
"Failed to run SecurityDogThreadFunc() because can_sender_ is "
642 std::this_thread::yield();
645 std::chrono::duration<double, std::micro> default_period{50000};
651 bool emergency_mode =
false;
656 !CheckResponse(CHECK_RESPONSE_STEER_UNIT_FLAG,
false)) {
657 ++horizontal_ctrl_fail;
658 if (horizontal_ctrl_fail >= kMaxFailAttempt) {
659 emergency_mode =
true;
660 AERROR <<
"Driving_mode is into emergency by steer manual intervention";
664 horizontal_ctrl_fail = 0;
670 !CheckResponse(CHECK_RESPONSE_SPEED_UNIT_FLAG,
false)) {
671 ++vertical_ctrl_fail;
672 if (vertical_ctrl_fail >= kMaxFailAttempt) {
673 emergency_mode =
true;
674 AERROR <<
"Driving_mode is into emergency by speed manual intervention";
678 vertical_ctrl_fail = 0;
682 if (CheckChassisError()) {
684 emergency_mode =
true;
701 std::chrono::duration<double, std::micro> elapsed{end - start};
702 if (elapsed < default_period) {
703 std::this_thread::sleep_for(default_period - elapsed);
705 AERROR <<
"Too much time consumption in DemoController looping process:"
711bool DemoController::CheckResponse(
const int32_t flags,
bool need_wait) {
712 int32_t retry_num = 20;
713 bool is_eps_online =
false;
714 bool is_vcu_online =
false;
715 bool is_esp_online =
false;
718 bool check_ok =
true;
719 if (flags & CHECK_RESPONSE_STEER_UNIT_FLAG) {
720 is_eps_online = chassis_.has_check_response() &&
723 check_ok = check_ok && is_eps_online;
726 if (flags & CHECK_RESPONSE_SPEED_UNIT_FLAG) {
727 is_vcu_online = chassis_.has_check_response() &&
730 is_esp_online = chassis_.has_check_response() &&
733 check_ok = check_ok && is_vcu_online && is_esp_online;
738 AINFO <<
"Need to check response again.";
742 std::this_thread::sleep_for(
743 std::chrono::duration<double, std::milli>(20));
745 }
while (need_wait && retry_num);
747 if (flags & CHECK_RESPONSE_STEER_UNIT_FLAG) {
748 AERROR <<
"steer check_response fail: is_eps_online:" << is_eps_online;
751 if (flags & CHECK_RESPONSE_SPEED_UNIT_FLAG) {
752 AERROR <<
"speed check_response fail: " <<
"is_vcu_online:" << is_vcu_online
753 <<
", is_esp_online:" << is_esp_online;
759void DemoController::set_chassis_error_mask(
const int32_t mask) {
760 std::lock_guard<std::mutex> lock(chassis_mask_mutex_);
761 chassis_error_mask_ = mask;
764int32_t DemoController::chassis_error_mask() {
765 std::lock_guard<std::mutex> lock(chassis_mask_mutex_);
766 return chassis_error_mask_;
770 std::lock_guard<std::mutex> lock(chassis_error_code_mutex_);
771 return chassis_error_code_;
774void DemoController::set_chassis_error_code(
776 std::lock_guard<std::mutex> lock(chassis_error_code_mutex_);
777 chassis_error_code_ = error_code;