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

feat(hazard_status_converter): add package #6428

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
62bc9e1
replace with new class
isamu-takagi Oct 24, 2023
ee4e2b1
restore sort
isamu-takagi Oct 25, 2023
74051e4
restore status
isamu-takagi Oct 25, 2023
034512e
change report function
isamu-takagi Oct 25, 2023
544fb0a
create package
isamu-takagi Oct 26, 2023
ba93044
create tests
isamu-takagi Oct 26, 2023
a37adf2
WIP
isamu-takagi Oct 26, 2023
8afff1a
change config structure
isamu-takagi Oct 27, 2023
671e33f
add tests
isamu-takagi Oct 27, 2023
5aa2c7b
update config errors
isamu-takagi Oct 30, 2023
e992312
update tests
isamu-takagi Oct 31, 2023
5847bfa
Merge branch 'feat/system-diagnostic-graph-3' into feat/hazard-status…
isamu-takagi Oct 31, 2023
e5ac8b1
add config
isamu-takagi Oct 31, 2023
cc9bf26
add type estimation
isamu-takagi Oct 31, 2023
5ed8e25
Merge branch 'main' into feat/system-diagnostic-graph-3
isamu-takagi Oct 31, 2023
26032e4
Merge branch 'feat/system-diagnostic-graph-3' into feat/hazard-status…
isamu-takagi Oct 31, 2023
763393b
apply mode
isamu-takagi Nov 1, 2023
5e294a9
Merge branch 'main' into feat/hazard-status-converter
isamu-takagi Nov 1, 2023
a63520c
publish hazard status
isamu-takagi Nov 1, 2023
019d2fc
fix debug code
isamu-takagi Nov 1, 2023
5edf30a
add component state check
isamu-takagi Nov 1, 2023
d0e47c9
modify constant type name
isamu-takagi Nov 1, 2023
9560ce1
Merge branch 'feat/system-diagnostic-graph-3' into feat/hazard-status…
isamu-takagi Nov 2, 2023
3a9854d
Merge branch 'main' into feat/hazard-status-converter
isamu-takagi Nov 2, 2023
075fb93
fix cmake
isamu-takagi Nov 2, 2023
1e84041
update config type name
isamu-takagi Nov 2, 2023
1d37cad
fix conversion table
isamu-takagi Nov 8, 2023
f417f58
Merge branch 'main' into feat/hazard-status-converter
isamu-takagi Nov 9, 2023
200dea6
Merge branch 'main' into feat/hazard-status-converter
isamu-takagi Nov 15, 2023
149111b
Merge branch 'feat/hazard-status-converter' of github.com:isamu-takag…
isamu-takagi Nov 17, 2023
fddcb92
Merge branch 'main' into feat/hazard-status-converter
isamu-takagi Nov 17, 2023
11e3efc
WIP
isamu-takagi Dec 20, 2023
2332320
Merge branch 'main' into feat/hazard-status-converter
isamu-takagi Dec 20, 2023
d3d38dd
remake package
isamu-takagi Dec 20, 2023
5d1d3ef
create converter
isamu-takagi Dec 22, 2023
93ec147
Merge remote-tracking branch 'universe/main' into feat/hazard-status-…
isamu-takagi Dec 22, 2023
0cbe828
fix hazard status converter
isamu-takagi Dec 25, 2023
3618ee3
Merge branch 'main' into feat/hazard-status-converter
isamu-takagi Feb 15, 2024
c0bd70c
Merge branch 'feat/hazard-status-converter' of github.com:isamu-takag…
isamu-takagi Feb 15, 2024
cadabaa
remove ecu prefix
isamu-takagi Feb 15, 2024
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
16 changes: 16 additions & 0 deletions system/hazard_status_converter/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
cmake_minimum_required(VERSION 3.14)
project(hazard_status_converter)

find_package(autoware_cmake REQUIRED)
autoware_package()

ament_auto_add_library(${PROJECT_NAME} SHARED
src/converter.cpp
)

rclcpp_components_register_node(${PROJECT_NAME}
PLUGIN "hazard_status_converter::Converter"
EXECUTABLE converter
)

ament_auto_package(INSTALL_TO_SHARE launch)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<launch>
<node pkg="hazard_status_converter" exec="converter" name="hazard_status_converter"/>
</launch>
25 changes: 25 additions & 0 deletions system/hazard_status_converter/package.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
<name>hazard_status_converter</name>
<version>0.1.0</version>
<description>The hazard_status_converter package</description>
<maintainer email="isamu.takagi@tier4.jp">Takagi, Isamu</maintainer>
<license>Apache License 2.0</license>

<buildtool_depend>ament_cmake_auto</buildtool_depend>
<buildtool_depend>autoware_cmake</buildtool_depend>

<depend>autoware_auto_system_msgs</depend>
<depend>diagnostic_msgs</depend>
<depend>rclcpp</depend>
<depend>rclcpp_components</depend>
<depend>tier4_system_msgs</depend>

<test_depend>ament_lint_auto</test_depend>
<test_depend>autoware_lint_common</test_depend>

<export>
<build_type>ament_cmake</build_type>
</export>
</package>
162 changes: 162 additions & 0 deletions system/hazard_status_converter/src/converter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
// Copyright 2023 The Autoware Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "converter.hpp"

#include <utility>
#include <vector>

namespace
{

using autoware_auto_system_msgs::msg::HazardStatus;
using autoware_auto_system_msgs::msg::HazardStatusStamped;
using diagnostic_msgs::msg::DiagnosticStatus;
using tier4_system_msgs::msg::DiagnosticGraph;
using tier4_system_msgs::msg::DiagnosticNode;
using DiagnosticLevel = DiagnosticStatus::_level_type;

enum class HazardLevel { NF, SF, LF, SPF };

struct TempNode
{
const DiagnosticNode & node;
bool is_auto_tree;
};

HazardLevel get_hazard_level(const TempNode & node, DiagnosticLevel auto_mode_level)
{
// Convert the level according to the table below.
// The Level other than auto mode is considered OK.
// |-------|-------------------------------|
// | Level | Root level |
// |-------|-------------------------------|
// | | OK | WARN | ERROR | STALE |
// | OK | NF | NF | NF | NF |
// | WARN | SF | LF | LF | LF |
// | ERROR | SF | LF | SPF | SPF |
// | STALE | SF | LF | SPF | SPF |
// |-------|-------------------------------|

const auto root_level = node.is_auto_tree ? auto_mode_level : DiagnosticStatus::OK;
const auto node_level = node.node.status.level;

Check warning on line 53 in system/hazard_status_converter/src/converter.cpp

View check run for this annotation

Codecov / codecov/patch

system/hazard_status_converter/src/converter.cpp#L52-L53

Added lines #L52 - L53 were not covered by tests

if (node_level == DiagnosticStatus::OK) {

Check warning on line 55 in system/hazard_status_converter/src/converter.cpp

View check run for this annotation

Codecov / codecov/patch

system/hazard_status_converter/src/converter.cpp#L55

Added line #L55 was not covered by tests
return HazardLevel::NF;
}
if (root_level == DiagnosticStatus::OK) {

Check warning on line 58 in system/hazard_status_converter/src/converter.cpp

View check run for this annotation

Codecov / codecov/patch

system/hazard_status_converter/src/converter.cpp#L58

Added line #L58 was not covered by tests
return HazardLevel::SF;
}
if (node_level == DiagnosticStatus::WARN) {

Check warning on line 61 in system/hazard_status_converter/src/converter.cpp

View check run for this annotation

Codecov / codecov/patch

system/hazard_status_converter/src/converter.cpp#L61

Added line #L61 was not covered by tests
return HazardLevel::LF;
}
if (root_level == DiagnosticStatus::WARN) {

Check warning on line 64 in system/hazard_status_converter/src/converter.cpp

View check run for this annotation

Codecov / codecov/patch

system/hazard_status_converter/src/converter.cpp#L64

Added line #L64 was not covered by tests
return HazardLevel::LF;
}
return HazardLevel::SPF;
}

void set_auto_tree(std::vector<TempNode> & nodes, int index)

Check warning on line 70 in system/hazard_status_converter/src/converter.cpp

View check run for this annotation

Codecov / codecov/patch

system/hazard_status_converter/src/converter.cpp#L70

Added line #L70 was not covered by tests
{
TempNode & node = nodes[index];
if (node.is_auto_tree) {

Check warning on line 73 in system/hazard_status_converter/src/converter.cpp

View check run for this annotation

Codecov / codecov/patch

system/hazard_status_converter/src/converter.cpp#L72-L73

Added lines #L72 - L73 were not covered by tests
return;
}

node.is_auto_tree = true;
for (const auto & link : node.node.links) {
set_auto_tree(nodes, link.index);

Check warning on line 79 in system/hazard_status_converter/src/converter.cpp

View check run for this annotation

Codecov / codecov/patch

system/hazard_status_converter/src/converter.cpp#L77-L79

Added lines #L77 - L79 were not covered by tests
}
}

HazardStatusStamped convert_hazard_diagnostics(const DiagnosticGraph & graph)

Check warning on line 83 in system/hazard_status_converter/src/converter.cpp

View check run for this annotation

Codecov / codecov/patch

system/hazard_status_converter/src/converter.cpp#L83

Added line #L83 was not covered by tests
{
// Create temporary tree for conversion.
std::vector<TempNode> nodes;
nodes.reserve(graph.nodes.size());
for (const auto & node : graph.nodes) {
nodes.push_back({node, false});

Check warning on line 89 in system/hazard_status_converter/src/converter.cpp

View check run for this annotation

Codecov / codecov/patch

system/hazard_status_converter/src/converter.cpp#L87-L89

Added lines #L87 - L89 were not covered by tests
}

// Mark nodes included in the auto mode tree.
DiagnosticLevel auto_mode_level = DiagnosticStatus::STALE;
for (size_t index = 0; index < nodes.size(); ++index) {
const auto & status = nodes[index].node.status;
if (status.name == "/autoware/modes/autonomous") {
set_auto_tree(nodes, index);
auto_mode_level = status.level;

Check warning on line 98 in system/hazard_status_converter/src/converter.cpp

View check run for this annotation

Codecov / codecov/patch

system/hazard_status_converter/src/converter.cpp#L94-L98

Added lines #L94 - L98 were not covered by tests
}
}

// Calculate hazard level from node level and root level.
HazardStatusStamped hazard;
for (const auto & node : nodes) {

Check warning on line 104 in system/hazard_status_converter/src/converter.cpp

View check run for this annotation

Codecov / codecov/patch

system/hazard_status_converter/src/converter.cpp#L104

Added line #L104 was not covered by tests
switch (get_hazard_level(node, auto_mode_level)) {
case HazardLevel::NF:
hazard.status.diag_no_fault.push_back(node.node.status);

Check warning on line 107 in system/hazard_status_converter/src/converter.cpp

View check run for this annotation

Codecov / codecov/patch

system/hazard_status_converter/src/converter.cpp#L107

Added line #L107 was not covered by tests
break;
case HazardLevel::SF:
hazard.status.diag_safe_fault.push_back(node.node.status);

Check warning on line 110 in system/hazard_status_converter/src/converter.cpp

View check run for this annotation

Codecov / codecov/patch

system/hazard_status_converter/src/converter.cpp#L110

Added line #L110 was not covered by tests
break;
case HazardLevel::LF:
hazard.status.diag_latent_fault.push_back(node.node.status);

Check warning on line 113 in system/hazard_status_converter/src/converter.cpp

View check run for this annotation

Codecov / codecov/patch

system/hazard_status_converter/src/converter.cpp#L112-L113

Added lines #L112 - L113 were not covered by tests
break;
case HazardLevel::SPF:
hazard.status.diag_single_point_fault.push_back(node.node.status);

Check warning on line 116 in system/hazard_status_converter/src/converter.cpp

View check run for this annotation

Codecov / codecov/patch

system/hazard_status_converter/src/converter.cpp#L116

Added line #L116 was not covered by tests
break;
}
}
return hazard;

Check warning on line 120 in system/hazard_status_converter/src/converter.cpp

View check run for this annotation

Codecov / codecov/patch

system/hazard_status_converter/src/converter.cpp#L120

Added line #L120 was not covered by tests
}

Check warning on line 121 in system/hazard_status_converter/src/converter.cpp

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

❌ New issue: Complex Method

convert_hazard_diagnostics has a cyclomatic complexity of 9, 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 121 in system/hazard_status_converter/src/converter.cpp

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

❌ New issue: Bumpy Road Ahead

convert_hazard_diagnostics has 2 blocks with nested conditional logic. Any nesting of 2 or deeper is considered. Threshold is one single, nested 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.

} // namespace

namespace hazard_status_converter
{

Converter::Converter(const rclcpp::NodeOptions & options) : Node("converter", options)

Check warning on line 128 in system/hazard_status_converter/src/converter.cpp

View check run for this annotation

Codecov / codecov/patch

system/hazard_status_converter/src/converter.cpp#L128

Added line #L128 was not covered by tests
{
pub_hazard_ = create_publisher<HazardStatusStamped>("/hazard_status", rclcpp::QoS(1));
sub_graph_ = create_subscription<DiagnosticGraph>(
"/diagnostics_graph", rclcpp::QoS(3),
std::bind(&Converter::on_graph, this, std::placeholders::_1));
}

Check warning on line 134 in system/hazard_status_converter/src/converter.cpp

View check run for this annotation

Codecov / codecov/patch

system/hazard_status_converter/src/converter.cpp#L130-L134

Added lines #L130 - L134 were not covered by tests

void Converter::on_graph(const DiagnosticGraph::ConstSharedPtr msg)

Check warning on line 136 in system/hazard_status_converter/src/converter.cpp

View check run for this annotation

Codecov / codecov/patch

system/hazard_status_converter/src/converter.cpp#L136

Added line #L136 was not covered by tests
{
const auto get_system_level = [](const HazardStatus & status) {
if (!status.diag_single_point_fault.empty()) {

Check warning on line 139 in system/hazard_status_converter/src/converter.cpp

View check run for this annotation

Codecov / codecov/patch

system/hazard_status_converter/src/converter.cpp#L139

Added line #L139 was not covered by tests
return HazardStatus::SINGLE_POINT_FAULT;
}
if (!status.diag_latent_fault.empty()) {

Check warning on line 142 in system/hazard_status_converter/src/converter.cpp

View check run for this annotation

Codecov / codecov/patch

system/hazard_status_converter/src/converter.cpp#L142

Added line #L142 was not covered by tests
return HazardStatus::LATENT_FAULT;
}
if (!status.diag_safe_fault.empty()) {
return HazardStatus::SAFE_FAULT;

Check warning on line 146 in system/hazard_status_converter/src/converter.cpp

View check run for this annotation

Codecov / codecov/patch

system/hazard_status_converter/src/converter.cpp#L145-L146

Added lines #L145 - L146 were not covered by tests
}
return HazardStatus::NO_FAULT;
};

HazardStatusStamped hazard = convert_hazard_diagnostics(*msg);
hazard.stamp = msg->stamp;
hazard.status.level = get_system_level(hazard.status);
hazard.status.emergency = hazard.status.level == HazardStatus::SINGLE_POINT_FAULT;
hazard.status.emergency_holding = false;
pub_hazard_->publish(hazard);
}

Check warning on line 157 in system/hazard_status_converter/src/converter.cpp

View check run for this annotation

Codecov / codecov/patch

system/hazard_status_converter/src/converter.cpp#L151-L157

Added lines #L151 - L157 were not covered by tests

} // namespace hazard_status_converter

#include <rclcpp_components/register_node_macro.hpp>
RCLCPP_COMPONENTS_REGISTER_NODE(hazard_status_converter::Converter)

Check warning on line 162 in system/hazard_status_converter/src/converter.cpp

View check run for this annotation

Codecov / codecov/patch

system/hazard_status_converter/src/converter.cpp#L162

Added line #L162 was not covered by tests
45 changes: 45 additions & 0 deletions system/hazard_status_converter/src/converter.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2023 The Autoware Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef CONVERTER_HPP_
#define CONVERTER_HPP_

#include <rclcpp/rclcpp.hpp>

#include <autoware_auto_system_msgs/msg/hazard_status_stamped.hpp>
#include <tier4_system_msgs/msg/diagnostic_graph.hpp>

#include <string>
#include <unordered_map>
#include <vector>

namespace hazard_status_converter
{

class Converter : public rclcpp::Node
{
public:
explicit Converter(const rclcpp::NodeOptions & options);

private:
using DiagnosticGraph = tier4_system_msgs::msg::DiagnosticGraph;
using HazardStatusStamped = autoware_auto_system_msgs::msg::HazardStatusStamped;
rclcpp::Subscription<DiagnosticGraph>::SharedPtr sub_graph_;
rclcpp::Publisher<HazardStatusStamped>::SharedPtr pub_hazard_;
void on_graph(const DiagnosticGraph::ConstSharedPtr msg);
};

} // namespace hazard_status_converter

#endif // CONVERTER_HPP_
Loading