19#include "modules/common_msgs/basic_msgs/vehicle_signal.pb.h"
32using ::apollo::common::ErrorCode;
33using ::apollo::common::VehicleSignal;
34using ::apollo::control::ControlCommand;
35using ::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;
47 CanSender<::apollo::canbus::Transit>*
const can_sender,
48 MessageManager<::apollo::canbus::Transit>*
const message_manager) {
50 AINFO <<
"TransitController has already been initialized.";
51 return ErrorCode::CANBUS_ERROR;
55 common::VehicleConfigHelper::Instance()->GetConfig().vehicle_param());
58 if (!
params_.has_driving_mode()) {
59 AERROR <<
"Vehicle conf pb not set driving_mode.";
60 return ErrorCode::CANBUS_ERROR;
63 if (can_sender ==
nullptr) {
64 AERROR <<
"Canbus sender is null.";
65 return ErrorCode::CANBUS_ERROR;
69 if (message_manager ==
nullptr) {
70 AERROR <<
"protocol manager is null.";
71 return ErrorCode::CANBUS_ERROR;
78 if (adc_auxiliarycontrol_110_ ==
nullptr) {
79 AERROR <<
"Adcauxiliarycontrol110 does not exist in the "
80 "TransitMessageManager!";
81 return ErrorCode::CANBUS_ERROR;
86 if (adc_motioncontrol1_10_ ==
nullptr) {
88 <<
"Adcmotioncontrol110 does not exist in the TransitMessageManager!";
89 return ErrorCode::CANBUS_ERROR;
95 if (adc_motioncontrollimits1_12_ ==
nullptr) {
96 AERROR <<
"Adcmotioncontrollimits112 does not exist in the "
97 "TransitMessageManager!";
98 return ErrorCode::CANBUS_ERROR;
103 if (llc_diag_brakecontrol_721_ ==
nullptr) {
104 AERROR <<
"Llcdiagbrakecontrol721 does not exist in the "
105 "TransitMessageManager!";
106 return ErrorCode::CANBUS_ERROR;
112 if (llc_diag_steeringcontrol_722_ ==
nullptr) {
113 AERROR <<
"Llcdiagsteeringcontrol722 does not exist in the "
114 "TransitMessageManager!";
115 return ErrorCode::CANBUS_ERROR;
123 adc_motioncontrollimits1_12_,
false);
125 llc_diag_brakecontrol_721_,
false);
127 llc_diag_steeringcontrol_722_,
false);
130 AINFO <<
"TransitController is initialized.";
133 return ErrorCode::OK;
140 AERROR <<
"TransitController has NOT been initiated.";
143 const auto& update_func = [
this] { SecurityDogThreadFunc(); };
144 thread_.reset(
new std::thread(update_func));
151 AERROR <<
"TransitController stops or starts improperly!";
155 if (thread_ !=
nullptr && thread_->joinable()) {
158 AINFO <<
"TransitController stopped.";
174 chassis_.set_error_code(chassis_error_code());
177 chassis_.set_engine_started(
true);
180 if (motion20.has_llc_fbk_throttleposition()) {
181 chassis_.set_throttle_percentage(
182 static_cast<float>(motion20.llc_fbk_throttleposition()));
188 if (motion20.has_llc_fbk_brakepercentrear()) {
190 chassis_.set_brake_percentage(
191 static_cast<float>(motion20.llc_fbk_brakepercentrear()));
194 if (motion20.has_llc_fbk_gear()) {
195 switch (motion20.llc_fbk_gear()) {
214 if (motion21.has_llc_fbk_vehiclespeed()) {
215 chassis_.set_speed_mps(
static_cast<float>(motion21.llc_fbk_vehiclespeed()));
218 if (motion21.has_llc_fbk_steeringangle()) {
219 chassis_.set_steering_percentage(
220 static_cast<float>(-1.0 * motion21.llc_fbk_steeringangle() * M_PI /
225 if (aux.has_llc_fbk_turnsignal()) {
226 switch (aux.llc_fbk_turnsignal()) {
228 chassis_.mutable_signal()->set_turn_signal(
232 chassis_.mutable_signal()->set_turn_signal(
236 chassis_.mutable_signal()->set_turn_signal(
247bool TransitController::VerifyID() {
return true; }
249void TransitController::Emergency() {
254ErrorCode TransitController::EnableAutoMode() {
256 AINFO <<
"Already in COMPLETE_AUTO_DRIVE mode";
257 return ErrorCode::OK;
267 Adc_motioncontrol1_10::
268 ADC_CMD_LONGITUDINALCONTROLMODE_DIRECT_THROTTLE_BRAKE);
271 if (!CheckResponse()) {
272 AERROR <<
"Failed to switch to COMPLETE_AUTO_DRIVE mode.";
274 return ErrorCode::CANBUS_ERROR;
276 if (button_pressed_) {
278 AINFO <<
"Switch to COMPLETE_AUTO_DRIVE mode ok.";
281 AINFO <<
"Physical button not pressed yet.";
283 return ErrorCode::OK;
286ErrorCode TransitController::DisableAutoMode() {
291 AINFO <<
"Switch to COMPLETE_MANUAL ok.";
292 return ErrorCode::OK;
295ErrorCode TransitController::EnableSteeringOnlyMode() {
299 AINFO <<
"Already in AUTO_STEER_ONLY mode";
300 return ErrorCode::OK;
313 return ErrorCode::OK;
316ErrorCode TransitController::EnableSpeedOnlyMode() {
320 AINFO <<
"Already in AUTO_SPEED_ONLY mode";
321 return ErrorCode::OK;
331 Adc_motioncontrol1_10::
332 ADC_CMD_LONGITUDINALCONTROLMODE_DIRECT_THROTTLE_BRAKE);
334 if (!CheckResponse()) {
335 AERROR <<
"Failed to switch to AUTO_STEER_ONLY mode.";
338 return ErrorCode::CANBUS_ERROR;
340 if (button_pressed_) {
342 AINFO <<
"Switch to COMPLETE_AUTO_DRIVE mode ok.";
345 AINFO <<
"Physical button not pressed yet.";
347 return ErrorCode::OK;
354 AINFO <<
"This drive mode no need to set gear.";
357 switch (gear_position) {
389 AERROR <<
"Gear command is invalid!";
407void TransitController::Brake(
double pedal) {
412 AINFO <<
"The current drive mode does not need to set acceleration.";
415 if (button_pressed_) {
424void TransitController::Throttle(
double pedal) {
427 AINFO <<
"The current drive mode does not need to set acceleration.";
430 if (button_pressed_) {
439void TransitController::Acceleration(
double acc) {
442 AINFO <<
"The current drive mode does not need to set acceleration.";
452void TransitController::Steer(
double angle) {
455 AINFO <<
"The current driving mode does not need to set steer.";
459 const double real_angle =
469void TransitController::Steer(
double angle,
double angle_spd) {
472 AINFO <<
"The current driving mode does not need to set steer.";
477 const double real_angle =
486void TransitController::SetEpbBreak(
const ControlCommand& command) {
487 if (command.parking_brake()) {
494ErrorCode TransitController::HandleCustomOperation(
495 const external_command::ChassisCommand& command) {
496 return ErrorCode::OK;
499void TransitController::SetBeam(
const VehicleSignal& vehicle_signal) {
500 if (vehicle_signal.has_high_beam() && vehicle_signal.high_beam()) {
502 }
else if (vehicle_signal.has_low_beam() && vehicle_signal.low_beam()) {
510void TransitController::SetHorn(
const VehicleSignal& vehicle_signal) {
511 if (vehicle_signal.horn()) {
518void TransitController::SetTurningSignal(
const VehicleSignal& vehicle_signal) {
520 auto signal = vehicle_signal.turn_signal();
533void TransitController::ResetProtocol() {
537bool TransitController::CheckChassisError() {
542void TransitController::SecurityDogThreadFunc() {
543 int32_t vertical_ctrl_fail = 0;
544 int32_t horizontal_ctrl_fail = 0;
547 AERROR <<
"Failed to run SecurityDogThreadFunc() because can_sender_ is "
552 std::this_thread::yield();
555 std::chrono::duration<double, std::micro> default_period{50000};
561 bool emergency_mode =
false;
567 ++horizontal_ctrl_fail;
568 if (horizontal_ctrl_fail >= kMaxFailAttempt) {
569 emergency_mode =
true;
573 horizontal_ctrl_fail = 0;
580 ++vertical_ctrl_fail;
581 if (vertical_ctrl_fail >= kMaxFailAttempt) {
582 emergency_mode =
true;
586 vertical_ctrl_fail = 0;
594 std::chrono::duration<double, std::micro> elapsed{end - start};
595 if (elapsed < default_period) {
596 std::this_thread::sleep_for(default_period - elapsed);
599 <<
"Too much time consumption in TransitController looping process:"
605bool TransitController::CheckResponse() {
607 Transit chassis_detail;
613 auto& motion1_20 = chassis_detail.llc_motionfeedback1_20();
615 return (motion1_20.llc_fbk_state() ==
617 motion1_20.llc_fbk_state() ==
619 motion1_20.llc_fbk_state() ==
621 motion1_20.llc_fbk_state() ==
625void TransitController::set_chassis_error_mask(
const int32_t mask) {
626 std::lock_guard<std::mutex> lock(chassis_mask_mutex_);
627 chassis_error_mask_ = mask;
630int32_t TransitController::chassis_error_mask() {
631 std::lock_guard<std::mutex> lock(chassis_mask_mutex_);
632 return chassis_error_mask_;
636 std::lock_guard<std::mutex> lock(chassis_error_code_mutex_);
637 return chassis_error_code_;
640void TransitController::set_chassis_error_code(
642 std::lock_guard<std::mutex> lock(chassis_error_code_mutex_);
643 chassis_error_code_ = error_code;
646bool TransitController::CheckSafetyError(
647 const ::apollo::canbus::Transit& chassis_detail) {
651void TransitController::SetLimits() {
Defines SenderMessage class and CanSender class.
common::VehicleParam vehicle_params_
canbus::VehicleParameter params_
CanSender< ::apollo::canbus::Transit > * can_sender_
virtual void set_driving_mode(const Chassis::DrivingMode &driving_mode)
MessageManager< ::apollo::canbus::Transit > * message_manager_
virtual Chassis::DrivingMode driving_mode()
Adcauxiliarycontrol110 * set_adc_cmd_horn(bool adc_cmd_horn)
Adcauxiliarycontrol110 * set_adc_cmd_turnsignal(Adc_auxiliarycontrol_110::Adc_cmd_turnsignalType adc_cmd_turnsignal)
Adcauxiliarycontrol110 * set_adc_cmd_lowbeam(bool adc_cmd_lowbeam)
Adcauxiliarycontrol110 * set_adc_cmd_highbeam(bool adc_cmd_highbeam)
Adcmotioncontrol110 * set_adc_cmd_gear(Adc_motioncontrol1_10::Adc_cmd_gearType adc_cmd_gear)
Adcmotioncontrol110 * set_adc_cmd_brakepercentage(double adc_cmd_brakepercentage)
Adcmotioncontrol110 * set_adc_cmd_throttleposition(double adc_cmd_throttleposition)
Adcmotioncontrol110 * set_adc_cmd_parkingbrake(bool adc_cmd_parkingbrake)
Adcmotioncontrol110 * set_adc_cmd_longitudinalcontrolmode(Adc_motioncontrol1_10::Adc_cmd_longitudinalcontrolmodeType adc_cmd_longitudinalcontrolmode)
Adcmotioncontrol110 * set_adc_cmd_autonomyrequest(Adc_motioncontrol1_10::Adc_cmd_autonomyrequestType adc_cmd_autonomyrequest)
Adcmotioncontrol110 * set_adc_cmd_steerwheelangle(double adc_cmd_steerwheelangle)
Adcmotioncontrol110 * set_adc_cmd_steeringcontrolmode(Adc_motioncontrol1_10::Adc_cmd_steeringcontrolmodeType adc_cmd_steeringcontrolmode)
Adcmotioncontrollimits112 * set_adc_cmd_steeringrate(double adc_cmd_steeringrate)
Adcmotioncontrollimits112 * set_adc_cmd_throttlecommandlimit(double adc_cmd_throttlecommandlimit)
Adcmotioncontrollimits112 * set_adc_cmd_steerwheelanglelimit(double adc_cmd_steerwheelanglelimit)
Chassis chassis() override
calculate and return the chassis.
bool Start() override
start the vehicle controller.
virtual ~TransitController()
::apollo::common::ErrorCode Init(const VehicleParameter ¶ms, CanSender<::apollo::canbus::Transit > *const can_sender, MessageManager<::apollo::canbus::Transit > *const message_manager) override
void Stop() override
stop the vehicle controller.
uint64_t ToMicrosecond() const
convert time to microsecond (us).
static Time Now()
get the current time.
#define AERROR_EVERY(freq)
The class of ProtocolData
@ ADC_CMD_TURNSIGNAL_RIGHT
@ ADC_CMD_TURNSIGNAL_LEFT
@ ADC_CMD_TURNSIGNAL_NONE
@ ADC_CMD_AUTONOMYREQUEST_AUTONOMY_REQUESTED
@ ADC_CMD_LONGITUDINALCONTROLMODE_NONE
@ ADC_CMD_STEERINGCONTROLMODE_ANGLE
@ ADC_CMD_STEERINGCONTROLMODE_NONE
@ LLC_FBK_STATE_AUTONOMY_REQUESTED
@ LLC_FBK_STATE_AUTONOMY_NOT_ALLOWED
@ LLC_FBK_STATE_AUTONOMY_ALLOWED
optional Llc_fbk_stateType llc_fbk_state
optional Llc_motionfeedback1_20 llc_motionfeedback1_20
optional Llc_motionfeedback2_21 llc_motionfeedback2_21
optional Llc_auxiliaryfeedback_120 llc_auxiliaryfeedback_120
optional double max_steer_angle
The class of VehicleController