Skip to content

Commit 4f6849e

Browse files
committed
mpc fail checker diagnostic added
Signed-off-by: Zhe Shen <lucidshenzhe@gmail.com>
1 parent f3cd528 commit 4f6849e

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

control/mpc_lateral_controller/include/mpc_lateral_controller/mpc_lateral_controller.hpp

+6
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ class MpcLateralController : public trajectory_follower::LateralControllerBase
9191
// trajectory buffer for detecting new trajectory
9292
std::deque<Trajectory> m_trajectory_buffer;
9393

94+
Updater diag_updater_{this}; // Diagnostic updater for publishing diagnostic data.
95+
96+
void setStatus(DiagnosticStatusWrapper & stat, const bool & m_MPC_failed);
97+
98+
void setupDiag();
99+
94100
std::unique_ptr<MPC> m_mpc; // MPC object for trajectory following.
95101

96102
// Check is mpc output converged

control/mpc_lateral_controller/src/mpc_lateral_controller.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ MpcLateralController::MpcLateralController(rclcpp::Node & node)
152152

153153
m_mpc->setLogger(logger_);
154154
m_mpc->setClock(clock_);
155+
156+
setupDiag();
155157
}
156158

157159
MpcLateralController::~MpcLateralController()
@@ -227,6 +229,28 @@ std::shared_ptr<SteeringOffsetEstimator> MpcLateralController::createSteerOffset
227229
return steering_offset_;
228230
}
229231

232+
void MpcLateralController::setStatus(
233+
DiagnosticStatusWrapper & stat, const bool & is_mpc_solved)
234+
{
235+
if (is_mpc_solved) {
236+
stat.summary(DiagnosticStatus::OK, "MPC succeeded.");
237+
} else {
238+
const std::string & error_msg = "The MPC solver failed. Call MRM to stop the car.";
239+
stat.summary(DiagnosticStatus::ERROR, error_msg);
240+
}
241+
}
242+
243+
void MpcLateralController::setupDiag()
244+
{
245+
auto & d = diag_updater_;
246+
d.setHardwareID("mpc_lateral_controller");
247+
248+
d.add("MPC_solve_checker", [&](auto & stat) {
249+
setStatus(
250+
stat, is_mpc_solved);
251+
});
252+
}
253+
230254
trajectory_follower::LateralOutput MpcLateralController::run(
231255
trajectory_follower::InputData const & input_data)
232256
{
@@ -255,6 +279,8 @@ trajectory_follower::LateralOutput MpcLateralController::run(
255279
const bool is_mpc_solved = m_mpc->calculateMPC(
256280
m_current_steering, m_current_kinematic_state, ctrl_cmd, predicted_traj, debug_values);
257281

282+
diag_updater_.force_update();
283+
258284
// reset previous MPC result
259285
// Note: When a large deviation from the trajectory occurs, the optimization stops and
260286
// the vehicle will return to the path by re-planning the trajectory or external operation.

0 commit comments

Comments
 (0)