Skip to content

Commit

Permalink
refactor(mrm_handler): use switch for state machine (#7277)
Browse files Browse the repository at this point in the history
* refactor nested if elses

Signed-off-by: Daniel Sanchez <[email protected]>

* delete other commits

Signed-off-by: Daniel Sanchez <[email protected]>

* return for consistency

Signed-off-by: Daniel Sanchez <[email protected]>

---------

Signed-off-by: Daniel Sanchez <[email protected]>
  • Loading branch information
danielsanchezaran authored Jun 6, 2024
1 parent ff2fbd6 commit 7d69e00
Showing 1 changed file with 29 additions and 29 deletions.
58 changes: 29 additions & 29 deletions system/mrm_handler/src/mrm_handler/mrm_handler_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -453,48 +453,48 @@ void MrmHandler::updateMrmState()
// Check emergency
const bool is_emergency = isEmergency();

// Send recovery events if is not an emergency
if (!is_emergency) {
if (mrm_state_.state != MrmState::NORMAL) transitionTo(MrmState::NORMAL);
return;
}

// Get mode
const bool is_auto_mode = control_mode_->mode == ControlModeReport::AUTONOMOUS;

// State Machine
if (mrm_state_.state == MrmState::NORMAL) {
// NORMAL
if (is_auto_mode && is_emergency) {
transitionTo(MrmState::MRM_OPERATING);
return;
}
} else {
// Emergency
// Send recovery events if "not emergency"
if (!is_emergency) {
transitionTo(MrmState::NORMAL);
switch (mrm_state_.state) {
case MrmState::NORMAL:
if (is_auto_mode) {
transitionTo(MrmState::MRM_OPERATING);
}
return;
}

if (mrm_state_.state == MrmState::MRM_OPERATING) {
// TODO(TetsuKawa): Check MRC is accomplished
if (mrm_state_.behavior == MrmState::PULL_OVER) {
if (isStopped() && isArrivedAtGoal()) {
transitionTo(MrmState::MRM_SUCCEEDED);
return;
}
} else {
if (isStopped()) {
transitionTo(MrmState::MRM_SUCCEEDED);
return;
}
case MrmState::MRM_OPERATING:
if (!isStopped()) return;
if (mrm_state_.behavior != MrmState::PULL_OVER) {
transitionTo(MrmState::MRM_SUCCEEDED);
return;
}
if (isArrivedAtGoal()) {
transitionTo(MrmState::MRM_SUCCEEDED);
}
} else if (mrm_state_.state == MrmState::MRM_SUCCEEDED) {
const auto current_mrm_behavior = getCurrentMrmBehavior();
if (current_mrm_behavior != mrm_state_.behavior) {
return;

case MrmState::MRM_SUCCEEDED:
if (mrm_state_.behavior != getCurrentMrmBehavior()) {
transitionTo(MrmState::MRM_OPERATING);
}
} else if (mrm_state_.state == MrmState::MRM_FAILED) {
return;
case MrmState::MRM_FAILED:
// Do nothing(only checking common recovery events)
} else {
return;

default: {
const auto msg = "invalid state: " + std::to_string(mrm_state_.state);
throw std::runtime_error(msg);
}
return;
}
}

Expand Down

0 comments on commit 7d69e00

Please sign in to comment.