Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(mrm_handler): fix bug in operation mode availability timeout #6513

Merged
merged 8 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions system/mrm_handler/include/mrm_handler/mrm_handler_core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ class MrmHandler : public rclcpp::Node
// Heartbeat
rclcpp::Time stamp_operation_mode_availability_;
std::optional<rclcpp::Time> stamp_autonomous_become_unavailable_ = std::nullopt;
bool is_operation_mode_availability_timeout;
void checkOperationModeAvailabilityTimeout();

// Algorithm
bool is_emergency_holding_ = false;
Expand Down
43 changes: 31 additions & 12 deletions system/mrm_handler/src/mrm_handler/mrm_handler_core.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 TIER IV, Inc.

Check notice on line 1 in system/mrm_handler/src/mrm_handler/mrm_handler_core.cpp

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

ℹ Getting worse: Overall Code Complexity

The mean cyclomatic complexity increases from 4.77 to 4.83, threshold = 4. This file has many conditional statements (e.g. if, for, while) across its implementation, leading to lower code health. Avoid adding more conditionals.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -91,6 +91,7 @@
mrm_state_.stamp = this->now();
mrm_state_.state = autoware_adapi_v1_msgs::msg::MrmState::NORMAL;
mrm_state_.behavior = autoware_adapi_v1_msgs::msg::MrmState::NONE;
is_operation_mode_availability_timeout = false;

Check warning on line 94 in system/mrm_handler/src/mrm_handler/mrm_handler_core.cpp

View check run for this annotation

Codecov / codecov/patch

system/mrm_handler/src/mrm_handler/mrm_handler_core.cpp#L94

Added line #L94 was not covered by tests

// Timer
const auto update_period_ns = rclcpp::Rate(param_.update_rate).period();
Expand Down Expand Up @@ -358,24 +359,29 @@
return true;
}

void MrmHandler::onTimer()
void MrmHandler::checkOperationModeAvailabilityTimeout()

Check warning on line 362 in system/mrm_handler/src/mrm_handler/mrm_handler_core.cpp

View check run for this annotation

Codecov / codecov/patch

system/mrm_handler/src/mrm_handler/mrm_handler_core.cpp#L362

Added line #L362 was not covered by tests
{
if (!isDataReady()) {
return;
}
const bool is_operation_mode_availability_timeout =
if (

Check warning on line 364 in system/mrm_handler/src/mrm_handler/mrm_handler_core.cpp

View check run for this annotation

Codecov / codecov/patch

system/mrm_handler/src/mrm_handler/mrm_handler_core.cpp#L364

Added line #L364 was not covered by tests
(this->now() - stamp_operation_mode_availability_).seconds() >
param_.timeout_operation_mode_availability;
if (is_operation_mode_availability_timeout) {
param_.timeout_operation_mode_availability) {
is_operation_mode_availability_timeout = true;

Check warning on line 367 in system/mrm_handler/src/mrm_handler/mrm_handler_core.cpp

View check run for this annotation

Codecov / codecov/patch

system/mrm_handler/src/mrm_handler/mrm_handler_core.cpp#L366-L367

Added lines #L366 - L367 were not covered by tests
RCLCPP_WARN_THROTTLE(
this->get_logger(), *this->get_clock(), std::chrono::milliseconds(1000).count(),
"heartbeat operation_mode_availability is timeout");
mrm_state_.state = autoware_adapi_v1_msgs::msg::MrmState::MRM_OPERATING;
publishHazardCmd();
publishGearCmd();
"operation_mode_availability is timeout");
} else {
is_operation_mode_availability_timeout = false;

Check warning on line 372 in system/mrm_handler/src/mrm_handler/mrm_handler_core.cpp

View check run for this annotation

Codecov / codecov/patch

system/mrm_handler/src/mrm_handler/mrm_handler_core.cpp#L372

Added line #L372 was not covered by tests
}
}

Check warning on line 374 in system/mrm_handler/src/mrm_handler/mrm_handler_core.cpp

View check run for this annotation

Codecov / codecov/patch

system/mrm_handler/src/mrm_handler/mrm_handler_core.cpp#L374

Added line #L374 was not covered by tests

void MrmHandler::onTimer()

Check warning on line 376 in system/mrm_handler/src/mrm_handler/mrm_handler_core.cpp

View check run for this annotation

Codecov / codecov/patch

system/mrm_handler/src/mrm_handler/mrm_handler_core.cpp#L376

Added line #L376 was not covered by tests
{
if (!isDataReady()) {

Check warning on line 378 in system/mrm_handler/src/mrm_handler/mrm_handler_core.cpp

View check run for this annotation

Codecov / codecov/patch

system/mrm_handler/src/mrm_handler/mrm_handler_core.cpp#L378

Added line #L378 was not covered by tests
return;
}

// Check whether operation_mode_availability is timeout
checkOperationModeAvailabilityTimeout();

Check warning on line 383 in system/mrm_handler/src/mrm_handler/mrm_handler_core.cpp

View check run for this annotation

Codecov / codecov/patch

system/mrm_handler/src/mrm_handler/mrm_handler_core.cpp#L383

Added line #L383 was not covered by tests

// Update Emergency State
updateMrmState();

Expand Down Expand Up @@ -477,22 +483,28 @@

// State machine
if (mrm_state_.behavior == MrmState::NONE) {
if (is_operation_mode_availability_timeout) {

Check warning on line 486 in system/mrm_handler/src/mrm_handler/mrm_handler_core.cpp

View check run for this annotation

Codecov / codecov/patch

system/mrm_handler/src/mrm_handler/mrm_handler_core.cpp#L486

Added line #L486 was not covered by tests
return MrmState::EMERGENCY_STOP;
}
if (operation_mode_availability_->pull_over) {
if (param_.use_pull_over) {
return MrmState::PULL_OVER;
}
}
if (operation_mode_availability_->comfortable_stop) {
if (param_.use_comfortable_stop) {
return MrmState::COMFORTABLE_STOP;
}
}
if (!operation_mode_availability_->emergency_stop) {
RCLCPP_WARN(this->get_logger(), "no mrm operation available: operate emergency_stop");
}
return MrmState::EMERGENCY_STOP;
}
if (mrm_state_.behavior == MrmState::PULL_OVER) {
if (is_operation_mode_availability_timeout) {

Check warning on line 505 in system/mrm_handler/src/mrm_handler/mrm_handler_core.cpp

View check run for this annotation

Codecov / codecov/patch

system/mrm_handler/src/mrm_handler/mrm_handler_core.cpp#L505

Added line #L505 was not covered by tests
return MrmState::EMERGENCY_STOP;
}
if (operation_mode_availability_->pull_over) {
if (param_.use_pull_over) {
return MrmState::PULL_OVER;
Expand All @@ -509,6 +521,9 @@
return MrmState::EMERGENCY_STOP;
}
if (mrm_state_.behavior == MrmState::COMFORTABLE_STOP) {
if (is_operation_mode_availability_timeout) {

Check warning on line 524 in system/mrm_handler/src/mrm_handler/mrm_handler_core.cpp

View check run for this annotation

Codecov / codecov/patch

system/mrm_handler/src/mrm_handler/mrm_handler_core.cpp#L524

Added line #L524 was not covered by tests
return MrmState::EMERGENCY_STOP;
}
if (isStopped() && operation_mode_availability_->pull_over) {
if (param_.use_pull_over) {
return MrmState::PULL_OVER;
Expand All @@ -525,6 +540,9 @@
return MrmState::EMERGENCY_STOP;
}
if (mrm_state_.behavior == MrmState::EMERGENCY_STOP) {
if (is_operation_mode_availability_timeout) {

Check warning on line 543 in system/mrm_handler/src/mrm_handler/mrm_handler_core.cpp

View check run for this annotation

Codecov / codecov/patch

system/mrm_handler/src/mrm_handler/mrm_handler_core.cpp#L543

Added line #L543 was not covered by tests
return MrmState::EMERGENCY_STOP;
}

Check warning on line 545 in system/mrm_handler/src/mrm_handler/mrm_handler_core.cpp

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

❌ Getting worse: Complex Method

MrmHandler::getCurrentMrmBehavior increases in cyclomatic complexity from 25 to 29, threshold = 9. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.

Check warning on line 545 in system/mrm_handler/src/mrm_handler/mrm_handler_core.cpp

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

❌ Getting worse: Bumpy Road Ahead

MrmHandler::getCurrentMrmBehavior increases from 7 to 11 logical blocks with deeply nested code, threshold is one single block per function. The Bumpy Road code smell is a function that contains multiple chunks of nested conditional logic. The deeper the nesting and the more bumps, the lower the code health.
if (isStopped() && operation_mode_availability_->pull_over) {
if (param_.use_pull_over) {
return MrmState::PULL_OVER;
Expand All @@ -551,7 +569,8 @@

bool MrmHandler::isEmergency() const
{
return !operation_mode_availability_->autonomous || is_emergency_holding_;
return !operation_mode_availability_->autonomous || is_emergency_holding_ ||
is_operation_mode_availability_timeout;

Check warning on line 573 in system/mrm_handler/src/mrm_handler/mrm_handler_core.cpp

View check run for this annotation

Codecov / codecov/patch

system/mrm_handler/src/mrm_handler/mrm_handler_core.cpp#L572-L573

Added lines #L572 - L573 were not covered by tests
}

bool MrmHandler::isArrivedAtGoal()
Expand Down
Loading