18
18
#include " autoware/universe_utils/geometry/geometry.hpp"
19
19
#include " autoware/universe_utils/math/normalization.hpp"
20
20
21
+ #include < fmt/format.h>
22
+
21
23
#include < algorithm>
22
24
#include < limits>
23
25
#include < memory>
@@ -424,7 +426,8 @@ trajectory_follower::LongitudinalOutput PidLongitudinalController::run(
424
426
// self pose is far from trajectory
425
427
if (control_data.is_far_from_trajectory ) {
426
428
if (m_enable_large_tracking_error_emergency) {
427
- m_control_state = ControlState::EMERGENCY; // update control state
429
+ // update control state
430
+ changeControlState (ControlState::EMERGENCY, " the tracking error is too large" );
428
431
}
429
432
const Motion raw_ctrl_cmd = calcEmergencyCtrlCmd (control_data.dt ); // calculate control command
430
433
m_prev_raw_ctrl_cmd = raw_ctrl_cmd;
@@ -606,13 +609,23 @@ PidLongitudinalController::Motion PidLongitudinalController::calcEmergencyCtrlCm
606
609
longitudinal_utils::applyDiffLimitFilter (raw_ctrl_cmd.acc , m_prev_raw_ctrl_cmd.acc , dt, p.jerk );
607
610
m_debug_values.setValues (DebugValues::TYPE::ACC_CMD_JERK_LIMITED, raw_ctrl_cmd.acc );
608
611
609
- RCLCPP_ERROR_THROTTLE (
610
- logger_, *clock_, 3000 , " [Emergency stop] vel: %3.3f, acc: %3.3f" , raw_ctrl_cmd.vel ,
611
- raw_ctrl_cmd.acc );
612
-
613
612
return raw_ctrl_cmd;
614
613
}
615
614
615
+ void PidLongitudinalController::changeControlState (
616
+ const ControlState & control_state, const std::string & reason)
617
+ {
618
+ if (control_state != m_control_state) {
619
+ RCLCPP_DEBUG_STREAM (
620
+ logger_,
621
+ " controller state changed: " << toStr (m_control_state) << " -> " << toStr (control_state));
622
+ if (control_state == ControlState::EMERGENCY) {
623
+ RCLCPP_ERROR (logger_, " Emergency Stop since %s" , reason.c_str ());
624
+ }
625
+ }
626
+ m_control_state = control_state;
627
+ }
628
+
616
629
void PidLongitudinalController::updateControlState (const ControlData & control_data)
617
630
{
618
631
const double current_vel = control_data.current_motion .vel ;
@@ -662,19 +675,16 @@ void PidLongitudinalController::updateControlState(const ControlData & control_d
662
675
// ==========================================================================================
663
676
const double current_vel_cmd = std::fabs (
664
677
control_data.interpolated_traj .points .at (control_data.nearest_idx ).longitudinal_velocity_mps );
665
- const bool emergency_condition = m_enable_overshoot_emergency &&
666
- stop_dist < -p.emergency_state_overshoot_stop_dist &&
667
- current_vel_cmd < vel_epsilon;
668
- const bool has_nonzero_target_vel = std::abs (current_vel_cmd) > 1.0e-5 ;
669
-
670
- const auto changeState = [this ](const auto s) {
671
- if (s != m_control_state) {
672
- RCLCPP_DEBUG_STREAM (
673
- logger_, " controller state changed: " << toStr (m_control_state) << " -> " << toStr (s));
678
+ const auto emergency_condition = [&]() {
679
+ if (
680
+ m_enable_overshoot_emergency && stop_dist < -p.emergency_state_overshoot_stop_dist &&
681
+ current_vel_cmd < vel_epsilon) {
682
+ return ResultWithReason{
683
+ true , fmt::format (" the target velocity {} is less than {}" , current_vel_cmd, vel_epsilon)};
674
684
}
675
- m_control_state = s ;
676
- return ;
677
- } ;
685
+ return ResultWithReason{ false } ;
686
+ }() ;
687
+ const bool has_nonzero_target_vel = std::abs (current_vel_cmd) > 1.0e-5 ;
678
688
679
689
const auto debug_msg_once = [this ](const auto & s) { RCLCPP_DEBUG_ONCE (logger_, " %s" , s); };
680
690
@@ -694,11 +704,11 @@ void PidLongitudinalController::updateControlState(const ControlData & control_d
694
704
// transit state
695
705
// in DRIVE state
696
706
if (m_control_state == ControlState::DRIVE) {
697
- if (emergency_condition) {
698
- return changeState (ControlState::EMERGENCY);
707
+ if (emergency_condition. result ) {
708
+ return changeControlState (ControlState::EMERGENCY, emergency_condition. reason );
699
709
}
700
710
if (!is_under_control && stopped_condition) {
701
- return changeState (ControlState::STOPPED);
711
+ return changeControlState (ControlState::STOPPED);
702
712
}
703
713
704
714
if (m_enable_smooth_stop) {
@@ -709,31 +719,31 @@ void PidLongitudinalController::updateControlState(const ControlData & control_d
709
719
control_data.stop_dist -
710
720
0.5 * (pred_vel_in_target + current_vel) * m_delay_compensation_time;
711
721
m_smooth_stop.init (pred_vel_in_target, pred_stop_dist);
712
- return changeState (ControlState::STOPPING);
722
+ return changeControlState (ControlState::STOPPING);
713
723
}
714
724
} else {
715
725
if (stopped_condition && !departure_condition_from_stopped) {
716
- return changeState (ControlState::STOPPED);
726
+ return changeControlState (ControlState::STOPPED);
717
727
}
718
728
}
719
729
return ;
720
730
}
721
731
722
732
// in STOPPING state
723
733
if (m_control_state == ControlState::STOPPING) {
724
- if (emergency_condition) {
725
- return changeState (ControlState::EMERGENCY);
734
+ if (emergency_condition. result ) {
735
+ return changeControlState (ControlState::EMERGENCY, emergency_condition. reason );
726
736
}
727
737
if (stopped_condition) {
728
- return changeState (ControlState::STOPPED);
738
+ return changeControlState (ControlState::STOPPED);
729
739
}
730
740
731
741
if (departure_condition_from_stopping) {
732
742
m_pid_vel.reset ();
733
743
m_lpf_vel_error->reset (0.0 );
734
744
// prevent the car from taking a long time to start to move
735
745
m_prev_ctrl_cmd.acc = std::max (0.0 , m_prev_raw_ctrl_cmd.acc );
736
- return changeState (ControlState::DRIVE);
746
+ return changeControlState (ControlState::DRIVE);
737
747
}
738
748
return ;
739
749
}
@@ -779,7 +789,7 @@ void PidLongitudinalController::updateControlState(const ControlData & control_d
779
789
m_pid_vel.reset ();
780
790
m_lpf_vel_error->reset (0.0 );
781
791
m_lpf_acc_error->reset (0.0 );
782
- return changeState (ControlState::DRIVE);
792
+ return changeControlState (ControlState::DRIVE);
783
793
}
784
794
785
795
return ;
@@ -788,13 +798,13 @@ void PidLongitudinalController::updateControlState(const ControlData & control_d
788
798
// in EMERGENCY state
789
799
if (m_control_state == ControlState::EMERGENCY) {
790
800
if (stopped_condition) {
791
- return changeState (ControlState::STOPPED);
801
+ return changeControlState (ControlState::STOPPED);
792
802
}
793
803
794
- if (!emergency_condition) {
804
+ if (!emergency_condition. result ) {
795
805
if (!is_under_control) {
796
806
// NOTE: On manual driving, no need stopping to exit the emergency.
797
- return changeState (ControlState::DRIVE);
807
+ return changeControlState (ControlState::DRIVE);
798
808
}
799
809
}
800
810
return ;
@@ -1213,7 +1223,6 @@ void PidLongitudinalController::updateDebugVelAcc(const ControlData & control_da
1213
1223
1214
1224
void PidLongitudinalController::setupDiagnosticUpdater ()
1215
1225
{
1216
- diag_updater_->setHardwareID (" autoware_pid_longitudinal_controller" );
1217
1226
diag_updater_->add (" control_state" , this , &PidLongitudinalController::checkControlState);
1218
1227
}
1219
1228
0 commit comments