310 localization_ = localization;
312 trajectory_message_ = planning_published_trajectory;
314 auto debug = cmd->mutable_debug()->mutable_simple_lon_debug();
316 auto lon_debug_info =
injector_->mutable_control_debug_info()->mutable_simple_lon_debug();
317 lon_debug_info->Clear();
320 double vehicle_speed_ori =
injector_->vehicle_state()->linear_velocity();
321 lon_debug_info->set_vehicle_speed(vehicle_speed_ori);
324 lon_debug_info->set_vehicle_speed_filter(current_speed);
327 AERROR <<
"Fail to update the vehicle status identification!";
328 return Status(ErrorCode::CONTROL_COMPUTE_ERROR,
"Fail to update the vehicle status identification!");
331 if (trajectory_analyzer_ ==
nullptr
336 if (!ChooseCurrentParam(trajectory_message_, current_speed, current_lon_based_pidcontroller_conf_)) {
337 AERROR <<
"Fail to load current lon control param!";
338 return Status(ErrorCode::CONTROL_COMPUTE_ERROR,
"Fail to load current lon control param!");
341 if (!ChooseCurrentCalibrationTable(calibration_table_)) {
342 AERROR <<
"Fail to load the current lon control calibration table!";
343 return Status(ErrorCode::CONTROL_COMPUTE_ERROR,
"Fail to load the current lon control calibration table!!");
346 if (calibration_table_.calibration_size() != previous_calibration_table_size_) {
347 AINFO <<
"Update the calibration table, init the new calibration table.";
348 InitControlCalibrationTable(calibration_table_);
349 previous_calibration_table_size_ = calibration_table_.calibration_size();
352 if (!control_interpolation_) {
353 AERROR <<
"Fail to initialize calibration table.";
354 return Status(ErrorCode::CONTROL_COMPUTE_ERROR,
"Fail to initialize calibration table.");
357 double brake_cmd = 0.0;
358 double throttle_cmd = 0.0;
359 double ts = current_lon_based_pidcontroller_conf_.ts();
360 double preview_time = current_lon_based_pidcontroller_conf_.preview_window() * ts;
361 bool enable_leadlag = current_lon_based_pidcontroller_conf_.enable_reverse_leadlag_compensation();
363 standstill_narmal_acceleration_
364 = -std::fabs(current_lon_based_pidcontroller_conf_.standstill_narmal_acceleration());
365 stop_gain_acceleration_ = -std::fabs(current_lon_based_pidcontroller_conf_.stop_gain_acceleration());
367 if (preview_time < 0.0) {
368 const auto error_msg = absl::StrCat(
"Preview time set as: ", preview_time,
" less than 0");
370 return Status(ErrorCode::CONTROL_COMPUTE_ERROR, error_msg);
373 station_leadlag_controller_.
SetLeadlagConf(current_lon_based_pidcontroller_conf_.speed_leadlag_conf());
374 speed_leadlag_controller_.
SetLeadlagConf(current_lon_based_pidcontroller_conf_.speed_leadlag_conf());
376 CheckFinishToElse(planning_published_trajectory);
379 double station_error_limit = current_lon_based_pidcontroller_conf_.station_error_limit();
380 double station_error_limited = 0.0;
381 if (current_lon_based_pidcontroller_conf_.enable_speed_station_preview()) {
382 station_error_limited
383 =
common::math::Clamp(debug->preview_station_error(), -station_error_limit, station_error_limit);
385 station_error_limited =
common::math::Clamp(debug->station_error(), -station_error_limit, station_error_limit);
388 auto station_pid_conf = current_lon_based_pidcontroller_conf_.station_pid_conf();
389 CalcultePidParams(std::fabs(current_speed), station_pid_conf);
390 station_pid_controller_.
SetPID(station_pid_conf);
391 auto speed_pid_conf = current_lon_based_pidcontroller_conf_.speed_pid_conf();
392 CalcultePidParams(std::fabs(current_speed), speed_pid_conf);
393 speed_pid_controller_.
SetPID(speed_pid_conf);
396 if ((current_trajectory_timestamp_ - pre_trajectory_timestamp_ > 1e-4)
397 && planning_published_trajectory->
is_replan()) {
400 ADEBUG <<
"Reset the pid controller integral because of re-planning.";
402 pre_trajectory_timestamp_ = current_trajectory_timestamp_;
404 double speed_offset = station_pid_controller_.
Control(station_error_limited, ts);
405 if (enable_leadlag) {
406 speed_offset = station_leadlag_controller_.
Control(speed_offset, ts);
409 double speed_controller_input = 0.0;
410 double speed_controller_input_limit = current_lon_based_pidcontroller_conf_.speed_controller_input_limit();
411 double speed_controller_input_limited = 0.0;
412 if (current_lon_based_pidcontroller_conf_.enable_speed_station_preview()) {
413 speed_controller_input = speed_offset + debug->preview_speed_error();
415 speed_controller_input = speed_offset + debug->speed_error();
417 speed_controller_input_limited
418 =
common::math::Clamp(speed_controller_input, -speed_controller_input_limit, speed_controller_input_limit);
420 double acceleration_cmd_closeloop = 0.0;
422 acceleration_cmd_closeloop = speed_pid_controller_.
Control(speed_controller_input_limited, ts);
424 if (enable_leadlag) {
425 acceleration_cmd_closeloop = speed_leadlag_controller_.
Control(acceleration_cmd_closeloop, ts);
434 double vehicle_pitch_rad =
injector_->vehicle_state()->pitch();
435 double vehicle_pitch = vehicle_pitch_rad * 180 / M_PI + FLAGS_pitch_offset_deg;
436 ADEBUG <<
"[LON]vehicle_pitch is " << vehicle_pitch;
437 debug->set_vehicle_pitch(vehicle_pitch);
441 double slope_offset_compensation = current_lon_based_pidcontroller_conf_.use_opposite_slope_compensation() *
GRA_ACC
442 * std::sin(vehicle_pitch_rad + FLAGS_pitch_offset_deg * M_PI / 180);
444 if (std::isnan(slope_offset_compensation)) {
445 slope_offset_compensation = 0;
448 debug->set_slope_offset_compensation(slope_offset_compensation);
449 double acceleration_cmd = acceleration_cmd_closeloop + debug->preview_acceleration_reference();
455 if (current_lon_based_pidcontroller_conf_.use_steering_check()
457 && std::abs(current_steer_interval) > current_lon_based_pidcontroller_conf_.steer_cmd_interval()) {
460 ADEBUG <<
"Steer cmd interval is larger than " << current_lon_based_pidcontroller_conf_.steer_cmd_interval();
464 acceleration_cmd = 0;
465 debug->set_is_wait_steer(
true);
467 debug->set_is_wait_steer(
false);
469 debug->set_current_steer_interval(current_steer_interval);
473 debug->set_is_full_stop(
false);
474 debug->set_is_full_stop_soft(
false);
476 auto previous_full_stop =
injector_->Get_previous_lon_debug_info()->is_full_stop()
477 ||
injector_->Get_previous_lon_debug_info()->is_full_stop_soft();
479 IsStopByDestination(debug);
480 IsPedestrianStopLongTerm(debug);
481 if (current_lon_based_pidcontroller_conf_.use_preview_reference_check()
482 && (std::fabs(debug->preview_acceleration_reference()) <= FLAGS_max_acceleration_when_stopped)
485 if (debug->is_stop_reason_by_destination() || debug->is_stop_reason_by_prdestrian()) {
486 debug->set_is_full_stop(
true);
487 ADEBUG <<
"Into full stop within preview acc and reference speed, " <<
"is_full_stop is "
488 << debug->is_full_stop();
490 debug->set_is_full_stop_soft(
true);
491 ADEBUG <<
"Into full stop soft within preview acc and reference speed, " <<
"is_full_stop_soft is "
492 << debug->is_full_stop_soft();
496 double path_remain_add = 0.0;
497 if (vehicle_pitch < current_lon_based_pidcontroller_conf_.pitch_path_remain_param_min()
498 && fabs(current_speed) > current_lon_based_pidcontroller_conf_.downhill_full_stop_min_speed()
500 path_remain_add = fabs(
501 sin(vehicle_pitch * M_PI / 180) * current_lon_based_pidcontroller_conf_.pitch_path_remain_param());
504 if (!previous_full_stop) {
505 max_path_remain_when_stopped_ = current_lon_based_pidcontroller_conf_.path_remain() + path_remain_add;
507 max_path_remain_when_stopped_ = current_lon_based_pidcontroller_conf_.path_remain()
508 + current_lon_based_pidcontroller_conf_.full_stop_path_remain_gain() + path_remain_add;
510 ADEBUG <<
"current max_path_remain_when_stopped_ is " << max_path_remain_when_stopped_;
512 if (((
IsForwardModel() && debug->path_remain() < max_path_remain_when_stopped_)
513 || (!
IsForwardModel() && debug->path_remain() > -max_path_remain_when_stopped_))) {
514 ADEBUG <<
"Into full stop decision by path remain.";
515 if (debug->is_stop_reason_by_destination() || debug->is_stop_reason_by_prdestrian()) {
516 debug->set_is_full_stop(
true);
517 ADEBUG <<
"Current path remain distance: " << debug->path_remain()
518 <<
" is within max_path_remain threshold: " << max_path_remain_when_stopped_
519 <<
", into full stop because vehicle is in destination: " << debug->is_stop_reason_by_destination()
520 <<
" or pedestrian is in long time stop: " << debug->is_stop_reason_by_prdestrian()
521 <<
"is_full_stop flag: " << debug->is_full_stop();
523 debug->set_is_full_stop_soft(
true);
524 ADEBUG <<
"Current path remain distance: " << debug->path_remain()
525 <<
" is within max_path_remain threshold: " << max_path_remain_when_stopped_
526 <<
", but not into full stop because stop not in destination: "
527 << debug->is_stop_reason_by_destination()
528 <<
" or pedestrian is not long time stop: " << debug->is_stop_reason_by_prdestrian()
529 <<
"is_full_stop_soft flag: " << debug->is_full_stop_soft();
532 && debug->is_stop_reason_by_prdestrian()) {
533 ADEBUG <<
"Current stop is for long time pedestrian stop, " << debug->is_stop_reason_by_prdestrian();
534 debug->set_is_full_stop(
true);
537 ADEBUG <<
"Not into full stop decision by path remain.";
540 if (!debug->is_full_stop() && !debug->is_full_stop_soft()) {
541 is_full_stop_wait_time_diff_ = 0;
544 if (!previous_is_full_stop_ && (debug->is_full_stop() || debug->is_full_stop_soft())) {
545 ADEBUG <<
"from go into full stop";
546 previous_is_full_stop_ =
true;
547 form_go_to_full_stop_ =
true;
551 double throttle_lowerbound = std::max(
552 vehicle_param_.
throttle_deadzone(), current_lon_based_pidcontroller_conf_.throttle_minimum_action());
553 double brake_lowerbound
554 = std::max(vehicle_param_.
brake_deadzone(), current_lon_based_pidcontroller_conf_.brake_minimum_action());
555 double calibration_value = 0.0;
556 double acceleration_lookup = (!
IsForwardModel()) ? -acceleration_cmd : acceleration_cmd;
559 + current_lon_based_pidcontroller_conf_.enable_slope_offset() * debug->slope_offset_compensation();
560 double acceleration_lookup_limit = 0.0;
562 if (current_lon_based_pidcontroller_conf_.use_acceleration_lookup_limit()) {
563 acceleration_lookup_limit = (acceleration_lookup > acceleration_lookup_limited) ? acceleration_lookup_limited
564 : acceleration_lookup;
567 if (FLAGS_use_preview_speed_for_table) {
568 if (current_lon_based_pidcontroller_conf_.use_acceleration_lookup_limit()) {
569 calibration_value = control_interpolation_->Interpolate(
570 std::make_pair(debug->preview_speed_reference(), acceleration_lookup_limit));
572 calibration_value = control_interpolation_->Interpolate(
573 std::make_pair(debug->preview_speed_reference(), acceleration_lookup));
576 if (current_lon_based_pidcontroller_conf_.use_acceleration_lookup_limit()) {
577 calibration_value = control_interpolation_->Interpolate(
578 std::make_pair(std::fabs(current_speed), acceleration_lookup_limit));
580 calibration_value = control_interpolation_->Interpolate(
581 std::make_pair(std::fabs(current_speed), acceleration_lookup));
585 if (acceleration_lookup >= 0) {
586 if (calibration_value >= 0) {
587 throttle_cmd = std::max(calibration_value, throttle_lowerbound);
589 throttle_cmd = throttle_lowerbound;
594 if (calibration_value >= 0) {
595 brake_cmd = brake_lowerbound;
597 brake_cmd = std::max(-calibration_value, brake_lowerbound);
601 if (form_go_to_full_stop_ && (debug->is_full_stop() || debug->is_full_stop_soft())) {
602 ADEBUG <<
"from go to full stop in to control";
605 openspace_speed_control_throttle_cmd_ = 0.0;
606 if (debug->acceleration_reference() < current_lon_based_pidcontroller_conf_.emergency_quit_fullstop_mindec()) {
607 ADEBUG <<
"[NewFullStop] emergency stop, use acc before fullstop.";
609 if (((std::fabs(current_speed) < current_lon_based_pidcontroller_conf_.release_end_brake_speed())
610 || (std::fabs(debug->path_remain()) < current_lon_based_pidcontroller_conf_.fullstop_pathremain()))
611 && !begin_end_brake_) {
612 begin_end_brake_ =
true;
613 fullstop_in_brake_ = cmd->
brake();
614 double start_brake = current_lon_based_pidcontroller_conf_.full_stop_pitch_brake_param()
615 * fabs(std::sin(vehicle_pitch_rad + FLAGS_pitch_offset_deg * M_PI / 180));
616 if (start_brake > fullstop_in_brake_) {
617 fullstop_in_brake_ = start_brake;
619 hillup_fullstop_brake_rate_ = std::max(
620 current_lon_based_pidcontroller_conf_.full_stop_max_brake()
621 / (current_lon_based_pidcontroller_conf_.fullstop_window()
622 - current_lon_based_pidcontroller_conf_.full_stop_pitch_window_param()
623 * fabs(std::sin(vehicle_pitch_rad + FLAGS_pitch_offset_deg * M_PI / 180))),
625 ADEBUG <<
"======brake rate = " << hillup_fullstop_brake_rate_;
626 ADEBUG <<
"brake cal window = "
627 << current_lon_based_pidcontroller_conf_.fullstop_window()
628 - fabs(std::sin(vehicle_pitch_rad + FLAGS_pitch_offset_deg * M_PI / 180));
631 double station_err = (debug->station_error() < 0) ? debug->station_error() : 0.0;
632 hillup_fullstop_brake_rate_ = hillup_fullstop_brake_rate_
633 + fabs(station_err) * current_lon_based_pidcontroller_conf_.full_stop_station_err_rate();
634 if (hillup_fullstop_brake_rate_
635 > current_lon_based_pidcontroller_conf_.max_brk_downhill_change_rate()) {
636 hillup_fullstop_brake_rate_ = current_lon_based_pidcontroller_conf_.max_brk_downhill_change_rate();
640 if (begin_end_brake_) {
641 fullstop_in_brake_ = fullstop_in_brake_ + hillup_fullstop_brake_rate_;
642 if ((fullstop_window_count_ > current_lon_based_pidcontroller_conf_.fullstop_window())
643 && (fullstop_in_brake_ > current_lon_based_pidcontroller_conf_.full_stop_max_brake()
644 + current_lon_based_pidcontroller_conf_.full_stop_pitch_brake_param()
645 * fabs(std::sin(vehicle_pitch_rad + FLAGS_pitch_offset_deg * M_PI / 180)))) {
646 hillup_fullstop_brake_rate_ = 0;
648 if (fullstop_in_brake_ >= 100.0) {
649 fullstop_in_brake_ = 100.0;
651 brake_cmd = fullstop_in_brake_;
652 ADEBUG <<
"=======now brkcmd===AL== = " << fullstop_in_brake_;
653 fullstop_window_count_++;
661 if (FLAGS_use_vehicle_epb) {
662 ADEBUG <<
"Into use vehicle epb.";
663 if ((debug->is_full_stop() && IsFullStopLongTerm(debug))
664 || (debug->is_full_stop() && debug->is_stop_reason_by_prdestrian())) {
665 cmd->set_parking_brake(
true);
671 cmd->set_parking_brake(
false);
673 ADEBUG <<
"Into park brake release.";
674 SetParkingBrake(¤t_lon_based_pidcontroller_conf_, cmd);
681 bool is_previous_replan =
injector_->previous_control_debug_mutable()->mutable_replan_debug()->is_need_replan();
682 if (is_previous_replan) {
686 || debug->is_full_stop_soft() || is_previous_replan) {
687 openspace_speed_control_throttle_cmd_ = 0.0;
688 openspace_count_ = 0;
692 && current_lon_based_pidcontroller_conf_.use_parking_speed_control()
693 && std::fabs(debug->path_remain()) > current_lon_based_pidcontroller_conf_.speed_path_remain()) {
695 && (debug->acceleration_reference()
696 <= (-1) * current_lon_based_pidcontroller_conf_.speed_itfc_dcc_emergency()))
698 && (debug->acceleration_reference()
699 >= current_lon_based_pidcontroller_conf_.speed_itfc_dcc_emergency()))) {
701 ADEBUG <<
"=======use speed control.";
703 if (std::fabs(current_speed) <= current_lon_based_pidcontroller_conf_.openspace_r_std_v()) {
704 openspace_speed_control_throttle_cmd_ = openspace_speed_control_throttle_cmd_
705 + current_lon_based_pidcontroller_conf_.openspace_cmd_change();
707 if (std::fabs(current_speed) >= current_lon_based_pidcontroller_conf_.openspace_r_std_v()
708 + current_lon_based_pidcontroller_conf_.openspace_r_err()) {
709 if ((openspace_speed_control_throttle_cmd_
710 >= current_lon_based_pidcontroller_conf_.openspace_start_throttle())
711 && current_lon_based_pidcontroller_conf_.use_maintain_throttle()) {
712 openspace_speed_control_throttle_cmd_
713 = current_lon_based_pidcontroller_conf_.openspace_maintain_throttle();
715 openspace_speed_control_throttle_cmd_ = openspace_speed_control_throttle_cmd_
716 - current_lon_based_pidcontroller_conf_.openspace_cmd_change();
719 if ((std::fabs(current_speed) >= current_lon_based_pidcontroller_conf_.openspace_r_std_v()
720 - current_lon_based_pidcontroller_conf_.openspace_r_err())
721 && (std::fabs(current_speed) <= current_lon_based_pidcontroller_conf_.openspace_r_std_v()
722 + current_lon_based_pidcontroller_conf_.openspace_r_err())
723 && current_lon_based_pidcontroller_conf_.use_maintain_throttle()) {
724 openspace_speed_control_throttle_cmd_
725 = current_lon_based_pidcontroller_conf_.openspace_maintain_throttle();
727 if (vehicle_pitch <= current_lon_based_pidcontroller_conf_.speed_control_pitch_thres()
728 && openspace_speed_control_throttle_cmd_ < current_lon_based_pidcontroller_conf_.pitch_start_throttle()
729 && std::fabs(current_speed) < current_lon_based_pidcontroller_conf_.speed_control_min_speed()
730 && openspace_count_ < current_lon_based_pidcontroller_conf_.openspace_start_num()) {
731 openspace_speed_control_brake_cmd_ = current_lon_based_pidcontroller_conf_.epb_brake();
733 if (std::fabs(current_speed) >= current_lon_based_pidcontroller_conf_.openspace_r_std_v()
734 + current_lon_based_pidcontroller_conf_.openspace_r_err_big()) {
735 openspace_speed_control_brake_cmd_ = openspace_speed_control_brake_cmd_
736 + current_lon_based_pidcontroller_conf_.openspace_brk_change();
737 if (openspace_speed_control_brake_cmd_
738 > current_lon_based_pidcontroller_conf_.openspace_max_brk()) {
739 openspace_speed_control_brake_cmd_ = current_lon_based_pidcontroller_conf_.openspace_max_brk();
742 openspace_speed_control_brake_cmd_ = 0.0;
746 if (openspace_speed_control_throttle_cmd_ <= 0.0) {
747 openspace_speed_control_throttle_cmd_ = 0.0;
750 brake_cmd = openspace_speed_control_brake_cmd_;
751 if (brake_cmd > 0.0) {
754 throttle_cmd = openspace_speed_control_throttle_cmd_;
760 if (brake_cmd > current_lon_based_pidcontroller_conf_.full_stop_max_brake()) {
761 brake_cmd = current_lon_based_pidcontroller_conf_.full_stop_max_brake();
770 brake_cmd = current_lon_based_pidcontroller_conf_.epb_brake();
773 debug->set_throttle_cmd(throttle_cmd);
774 debug->set_brake_cmd(brake_cmd);
776 throttle_cmd =
ThrottleCmdFilter(FLAGS_use_throttle_filter, FLAGS_throttle_smoothing_factor, throttle_cmd);
777 lon_debug_info->set_throttle_cmd_filter(throttle_cmd);
779 cmd->set_throttle(throttle_cmd);
780 cmd->set_brake(brake_cmd);
781 if (current_lon_based_pidcontroller_conf_.use_acceleration_lookup_limit()) {
782 cmd->set_acceleration(acceleration_lookup_limit);
783 previous_acc_output_ = acceleration_lookup_limit;
785 cmd->set_acceleration(acceleration_cmd);
786 previous_acc_output_ = acceleration_cmd;
792 cmd->set_gear_location(trajectory_message_->
gear());
797 if (current_lon_based_pidcontroller_conf_.use_speed_itfc()) {
798 reference_spd_cmd_ = reference_spd_ + debug->speed_offset();
799 if ((reference_spd_ <= current_lon_based_pidcontroller_conf_.speed_itfc_full_stop_speed())
801 if ((debug->path_remain() >= current_lon_based_pidcontroller_conf_.speed_itfc_path_remain_min())
802 && (debug->preview_acceleration_reference()
803 >= current_lon_based_pidcontroller_conf_.speed_itfc_dcc_emergency())
804 && (debug->path_remain() <= current_lon_based_pidcontroller_conf_.speed_itfc_path_remain_max())) {
805 if (debug->preview_acceleration_reference()
806 <= current_lon_based_pidcontroller_conf_.speed_itfc_acc_thres()) {
807 if (reference_spd_ > current_lon_based_pidcontroller_conf_.speed_itfc_stop_speed()) {
808 reference_spd_cmd_ = current_lon_based_pidcontroller_conf_.speed_itfc_speed_cmd();
813 cmd->set_speed(reference_spd_cmd_);
814 if ((debug->is_full_stop() || debug->is_full_stop_soft())
820 debug->set_station_error_limited(station_error_limited);
821 debug->set_speed_offset(speed_offset);
822 debug->set_speed_controller_input_limited(speed_controller_input_limited);
823 debug->set_acceleration_cmd(acceleration_cmd);
824 debug->set_acceleration_lookup(acceleration_lookup);
825 debug->set_acceleration_lookup_limit(acceleration_lookup_limit);
826 debug->set_speed_lookup(current_speed);
827 debug->set_calibration_value(calibration_value);
828 debug->set_acceleration_cmd_closeloop(acceleration_cmd_closeloop);
830 lon_debug_info->set_path_remain_add(path_remain_add);
831 lon_debug_info->set_max_path_remain_when_stopped(max_path_remain_when_stopped_);
832 lon_debug_info->set_form_go_to_full_stop(form_go_to_full_stop_);
833 lon_debug_info->set_begin_end_brake(begin_end_brake_);
834 lon_debug_info->set_hillup_fullstop_brake_rate(hillup_fullstop_brake_rate_);
835 lon_debug_info->set_control_task_name(name_);
837 if (FLAGS_enable_csv_debug && speed_log_file_ !=
nullptr) {
838 fprintf(speed_log_file_,
839 "%.6f, %.6f, %.6f, %.6f, %.6f, %.6f, %.6f, %.6f, %.6f, %.6f,"
840 "%.6f, %.6f, %.6f, %.6f, %.6f, %.6f, %.6f, %d,\r\n",
841 debug->station_reference(),
842 debug->station_error(),
843 station_error_limited,
844 debug->preview_station_error(),
845 debug->speed_reference(),
846 debug->speed_error(),
847 speed_controller_input_limited,
848 debug->preview_speed_reference(),
849 debug->preview_speed_error(),
850 debug->preview_acceleration_reference(),
851 acceleration_cmd_closeloop,
853 debug->acceleration_lookup(),
854 debug->acceleration_lookup_limit(),
855 debug->speed_lookup(),
859 debug->is_full_stop());