35using ::apollo::common::ErrorCode;
36using ::apollo::common::VehicleSignal;
37using ::apollo::control::ControlCommand;
38using ::apollo::drivers::canbus::ProtocolData;
42const int32_t kMaxFailAttempt = 10;
43const int32_t CHECK_RESPONSE_STEER_UNIT_FLAG = 1;
44const int32_t CHECK_RESPONSE_SPEED_UNIT_FLAG = 2;
50 CanSender<::apollo::canbus::Gem>*
const can_sender,
51 MessageManager<::apollo::canbus::Gem>*
const message_manager) {
53 AINFO <<
"GemController has already been initialized.";
54 return ErrorCode::CANBUS_ERROR;
57 common::VehicleConfigHelper::Instance()->GetConfig().vehicle_param());
59 if (!
params_.has_driving_mode()) {
60 AERROR <<
"Vehicle conf pb not set driving_mode.";
61 return ErrorCode::CANBUS_ERROR;
64 if (can_sender ==
nullptr) {
65 AERROR <<
"Canbus sender is null.";
66 return ErrorCode::CANBUS_ERROR;
70 if (message_manager ==
nullptr) {
71 AERROR <<
"Protocol manager is null.";
72 return ErrorCode::CANBUS_ERROR;
79 if (brake_cmd_6b_ ==
nullptr) {
80 AERROR <<
"Brakecmd6b does not exist in the GemMessageManager!";
81 return ErrorCode::CANBUS_ERROR;
86 if (accel_cmd_67_ ==
nullptr) {
87 AERROR <<
"Accelcmd67 does not exist in the GemMessageManager!";
88 return ErrorCode::CANBUS_ERROR;
93 if (steering_cmd_6d_ ==
nullptr) {
94 AERROR <<
"Steeringcmd6d does not exist in the GemMessageManager!";
95 return ErrorCode::CANBUS_ERROR;
100 if (shift_cmd_65_ ==
nullptr) {
101 AERROR <<
"Shiftcmd65 does not exist in the GemMessageManager!";
102 return ErrorCode::CANBUS_ERROR;
106 if (turn_cmd_63_ ==
nullptr) {
107 AERROR <<
"Turncmd63 does not exist in the GemMessageManager!";
108 return ErrorCode::CANBUS_ERROR;
112 if (global_cmd_69_ ==
nullptr) {
113 AERROR <<
"Turncmd63 does not exist in the GemMessageManager!";
114 return ErrorCode::CANBUS_ERROR;
125 AINFO <<
"GemController is initialized.";
128 return ErrorCode::OK;
135 AERROR <<
"GemController has NOT been initiated.";
138 const auto& update_func = [
this] { SecurityDogThreadFunc(); };
139 thread_.reset(
new std::thread(update_func));
146 AERROR <<
"GemController stops or starts improperly!";
150 if (thread_ !=
nullptr && thread_->joinable()) {
153 AINFO <<
"GemController stopped.";
169 chassis_.set_error_code(chassis_error_code());
172 chassis_.set_engine_started(
true);
175 if (chassis_detail.has_vehicle_speed_rpt_6f() &&
177 chassis_.set_speed_mps(
static_cast<float>(
180 chassis_.set_speed_mps(0);
184 chassis_.set_fuel_range_m(0);
186 if (chassis_detail.has_accel_rpt_68() &&
188 chassis_.set_throttle_percentage(
191 chassis_.set_throttle_percentage(0);
194 if (chassis_detail.has_brake_rpt_6c() &&
196 chassis_.set_brake_percentage(
199 chassis_.set_brake_percentage(0);
203 if (chassis_detail.has_shift_rpt_66() &&
220 chassis_.set_gear_location(gear_pos);
227 if (chassis_detail.has_steering_rpt_1_6e() &&
229 chassis_.set_steering_percentage(
233 chassis_.set_steering_percentage(0);
236 if (chassis_detail.has_global_rpt_6a() &&
253 if (chassis_detail.has_light() &&
254 chassis_detail.
light().has_turn_light_type() &&
257 chassis_.mutable_signal()->set_turn_signal(
261 chassis_.mutable_signal()->set_turn_signal(
264 chassis_.mutable_signal()->set_turn_signal(
268 chassis_.mutable_signal()->set_turn_signal(
274 if (chassis_error_mask_) {
275 chassis_.set_chassis_error_mask(chassis_error_mask_);
282 chassis_.mutable_engage_advice()->set_advice(
285 chassis_.mutable_engage_advice()->set_advice(
287 chassis_.mutable_engage_advice()->set_reason(
288 "CANBUS not ready, firmware error or emergency button pressed!");
294bool GemController::VerifyID() {
return true; }
296void GemController::Emergency() {
302ErrorCode GemController::EnableAutoMode() {
304 AINFO <<
"Already in COMPLETE_AUTO_DRIVE mode";
305 return ErrorCode::OK;
315 CHECK_RESPONSE_STEER_UNIT_FLAG | CHECK_RESPONSE_SPEED_UNIT_FLAG;
316 if (!CheckResponse(flag,
true)) {
317 AERROR <<
"Failed to switch to COMPLETE_AUTO_DRIVE mode.";
320 return ErrorCode::CANBUS_ERROR;
323 AINFO <<
"Switch to COMPLETE_AUTO_DRIVE mode ok.";
324 return ErrorCode::OK;
327ErrorCode GemController::DisableAutoMode() {
332 AINFO <<
"Switch to COMPLETE_MANUAL ok.";
333 return ErrorCode::OK;
336ErrorCode GemController::EnableSteeringOnlyMode() {
337 AFATAL <<
"Not supported!";
338 return ErrorCode::OK;
341ErrorCode GemController::EnableSpeedOnlyMode() {
342 AFATAL <<
"Not supported!";
343 return ErrorCode::OK;
350 AINFO <<
"This drive mode no need to set gear.";
354 switch (gear_position) {
368 AERROR <<
"Gear command is invalid!";
384void GemController::Brake(
double pedal) {
389 AINFO <<
"The current drive mode does not need to set acceleration.";
398void GemController::Throttle(
double pedal) {
401 AINFO <<
"The current drive mode does not need to set acceleration.";
410void GemController::Acceleration(
double acc) {
413 AINFO <<
"The current drive mode does not need to set acceleration.";
423void GemController::Steer(
double angle) {
426 AINFO <<
"The current driving mode does not need to set steer.";
438void GemController::Steer(
double angle,
double angle_spd) {
441 AINFO <<
"The current driving mode does not need to set steer.";
446 const double real_angle_spd =
447 ProtocolData<::apollo::canbus::Gem>::BoundedValue(
455void GemController::SetEpbBreak(
const ControlCommand& command) {
456 if (command.parking_brake()) {
462ErrorCode GemController::HandleCustomOperation(
463 const external_command::ChassisCommand& command) {
464 return ErrorCode::OK;
467void GemController::SetBeam(
const VehicleSignal& vehicle_signal) {
468 if (vehicle_signal.high_beam()) {
470 }
else if (vehicle_signal.low_beam()) {
477void GemController::SetHorn(
const VehicleSignal& vehicle_signal) {
478 if (vehicle_signal.horn()) {
485void GemController::SetTurningSignal(
const VehicleSignal& vehicle_signal) {
487 auto signal = vehicle_signal.turn_signal();
497void GemController::ResetProtocol() {
message_manager_->ResetSendMessages(); }
499bool GemController::CheckChassisError() {
504void GemController::SecurityDogThreadFunc() {
505 int32_t vertical_ctrl_fail = 0;
506 int32_t horizontal_ctrl_fail = 0;
509 AERROR <<
"Failed to run SecurityDogThreadFunc() because can_sender_ is "
514 std::this_thread::yield();
517 std::chrono::duration<double, std::micro> default_period{50000};
523 bool emergency_mode =
false;
528 !CheckResponse(CHECK_RESPONSE_STEER_UNIT_FLAG,
false)) {
529 ++horizontal_ctrl_fail;
530 if (horizontal_ctrl_fail >= kMaxFailAttempt) {
531 emergency_mode =
true;
535 horizontal_ctrl_fail = 0;
541 !CheckResponse(CHECK_RESPONSE_SPEED_UNIT_FLAG,
false)) {
542 ++vertical_ctrl_fail;
543 if (vertical_ctrl_fail >= kMaxFailAttempt) {
544 emergency_mode =
true;
548 vertical_ctrl_fail = 0;
550 if (CheckChassisError()) {
552 emergency_mode =
true;
560 std::chrono::duration<double, std::micro> elapsed{end - start};
561 if (elapsed < default_period) {
562 std::this_thread::sleep_for(default_period - elapsed);
564 AERROR <<
"Too much time consumption in GemController looping process:"
570bool GemController::CheckResponse(
const int32_t flags,
bool need_wait) {
576void GemController::set_chassis_error_mask(
const int32_t mask) {
577 std::lock_guard<std::mutex> lock(chassis_mask_mutex_);
578 chassis_error_mask_ = mask;
581int32_t GemController::chassis_error_mask() {
582 std::lock_guard<std::mutex> lock(chassis_mask_mutex_);
583 return chassis_error_mask_;
587 std::lock_guard<std::mutex> lock(chassis_error_code_mutex_);
588 return chassis_error_code_;
591void GemController::set_chassis_error_code(
593 std::lock_guard<std::mutex> lock(chassis_error_code_mutex_);
594 chassis_error_code_ = error_code;