Skip to content

Commit 58ab52a

Browse files
feat(dummy_diag_publisher): update param setting (#1716)
* feat: update param setting Signed-off-by: TetsuKawa <kawaguchitnon@icloud.com> * style(pre-commit): autofix --------- Signed-off-by: TetsuKawa <kawaguchitnon@icloud.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 9225c6f commit 58ab52a

File tree

4 files changed

+76
-9
lines changed

4 files changed

+76
-9
lines changed

launch/tier4_control_launch/launch/control.launch.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@
3030
from launch_ros.substitutions import FindPackageShare
3131
import yaml
3232

33+
3334
def get_control_cmd_topic(context):
34-
is_redundant = LaunchConfiguration('launch_redundancy_system_components').perform(context)
35-
system_run_mode = LaunchConfiguration('system_run_mode').perform(context)
35+
is_redundant = LaunchConfiguration("launch_redundancy_system_components").perform(context)
36+
system_run_mode = LaunchConfiguration("system_run_mode").perform(context)
37+
38+
if is_redundant.lower() == "true" and system_run_mode.lower() == "planning_simulation":
39+
return "/main/control/command/control_cmd"
40+
return "/control/command/control_cmd"
3641

37-
if is_redundant.lower() == 'true' and system_run_mode.lower() == 'planning_simulation':
38-
return '/main/control/command/control_cmd'
39-
return '/control/command/control_cmd'
4042

4143
def launch_setup(context, *args, **kwargs):
4244
with open(LaunchConfiguration("vehicle_param_file").perform(context), "r") as f:

system/dummy_diag_publisher/include/dummy_diag_publisher/dummy_diag_publisher_core.hpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,13 @@ class DummyDiagPublisher : public rclcpp::Node
6060
DummyDiagConfig config_;
6161

6262
RequiredDiags required_diags_;
63+
rclcpp::node_interfaces::OnSetParametersCallbackHandle::SharedPtr param_callback_handle_;
64+
6365
void loadRequiredDiags();
66+
rcl_interfaces::msg::SetParametersResult onSetParams(
67+
const std::vector<rclcpp::Parameter> & parameters);
6468

65-
std::optional<Status> convertStrToStatus(std::string & status_str);
69+
std::optional<Status> convertStrToStatus(const std::string & status_str);
6670
std::string convertStatusToStr(const Status & status);
6771
diagnostic_msgs::msg::DiagnosticStatus::_level_type convertStatusToLevel(const Status & status);
6872

system/dummy_diag_publisher/package.xml

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
<buildtool_depend>ament_cmake_auto</buildtool_depend>
1212
<buildtool_depend>autoware_cmake</buildtool_depend>
1313

14-
<depend>autoware_universe_utils</depend>
15-
<depend>diagnostic_updater</depend>
14+
<depend>diagnostic_msgs</depend>
1615
<depend>fmt</depend>
1716
<depend>rclcpp</depend>
1817
<depend>rclcpp_components</depend>

system/dummy_diag_publisher/src/dummy_diag_publisher_core.cpp

+63-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
#define FMT_HEADER_ONLY
1818
#include <fmt/format.h>
1919

20+
#include <sstream>
21+
#include <unordered_map>
22+
2023
namespace
2124
{
2225
std::vector<std::string> split(const std::string & str, const char delim)
@@ -32,7 +35,7 @@ std::vector<std::string> split(const std::string & str, const char delim)
3235
} // namespace
3336

3437
std::optional<DummyDiagPublisher::Status> DummyDiagPublisher::convertStrToStatus(
35-
std::string & status_str)
38+
const std::string & status_str)
3639
{
3740
static std::unordered_map<std::string, Status> const table = {
3841
{"OK", Status::OK}, {"Warn", Status::WARN}, {"Error", Status::ERROR}, {"Stale", Status::STALE}};
@@ -139,6 +142,61 @@ rclcpp::NodeOptions override_options(rclcpp::NodeOptions options)
139142
true);
140143
}
141144

145+
rcl_interfaces::msg::SetParametersResult DummyDiagPublisher::onSetParams(
146+
const std::vector<rclcpp::Parameter> & parameters)
147+
{
148+
rcl_interfaces::msg::SetParametersResult result;
149+
result.successful = true;
150+
151+
for (const auto & parameter : parameters) {
152+
bool param_found = false;
153+
const auto & param_name = parameter.get_name();
154+
155+
for (auto & diag : required_diags_) {
156+
if (param_name == diag.name + std::string(".status")) {
157+
param_found = true;
158+
auto new_status = convertStrToStatus(parameter.as_string());
159+
if (new_status) {
160+
diag.status = *new_status;
161+
RCLCPP_INFO(
162+
this->get_logger(), "Updated %s status to: %s", diag.name.c_str(),
163+
parameter.as_string().c_str());
164+
} else {
165+
result.successful = false;
166+
result.reason = "Invalid status value for: " + parameter.as_string();
167+
RCLCPP_WARN(
168+
this->get_logger(), "Invalid status value for %s: %s", diag.name.c_str(),
169+
parameter.as_string().c_str());
170+
}
171+
} else if (param_name == diag.name + std::string(".is_active")) {
172+
param_found = true;
173+
try {
174+
diag.is_active = parameter.as_bool();
175+
RCLCPP_INFO(
176+
this->get_logger(), "Updated %s is_active to: %s", diag.name.c_str(),
177+
diag.is_active ? "true" : "false");
178+
} catch (const rclcpp::ParameterTypeException & e) {
179+
result.successful = false;
180+
result.reason = "Invalid is_active value for: " + parameter.as_string();
181+
RCLCPP_WARN(
182+
this->get_logger(), "Invalid is_active value for %s: %s", diag.name.c_str(),
183+
parameter.as_string().c_str());
184+
}
185+
}
186+
}
187+
188+
if (!param_found) {
189+
result.successful = false;
190+
result.reason = "Parameter not registered: " + parameter.get_name();
191+
RCLCPP_WARN(
192+
this->get_logger(), "Attempted to set unregistered parameter: %s",
193+
parameter.get_name().c_str());
194+
}
195+
}
196+
197+
return result;
198+
}
199+
142200
DummyDiagPublisher::DummyDiagPublisher(const rclcpp::NodeOptions & options)
143201
: Node("dummy_diag_publisher", override_options(options))
144202

@@ -159,6 +217,10 @@ DummyDiagPublisher::DummyDiagPublisher(const rclcpp::NodeOptions & options)
159217

160218
// Publisher
161219
pub_ = create_publisher<diagnostic_msgs::msg::DiagnosticArray>("/diagnostics", rclcpp::QoS(1));
220+
221+
// Parameter Callback Handle
222+
param_callback_handle_ = this->add_on_set_parameters_callback(
223+
std::bind(&DummyDiagPublisher::onSetParams, this, std::placeholders::_1));
162224
}
163225

164226
#include <rclcpp_components/register_node_macro.hpp>

0 commit comments

Comments
 (0)