14
14
15
15
#include " dummy_diag_publisher/dummy_diag_publisher_core.hpp"
16
16
17
- #include < rcl_interfaces/msg/detail/set_parameters_result__struct.hpp>
18
-
19
17
#define FMT_HEADER_ONLY
20
- #include < diagnostic_updater/diagnostic_updater.hpp>
21
- #include < rcl_interfaces/msg/set_parameters_result.hpp>
22
- #include < rclcpp/create_timer.hpp>
23
- #include < tier4_autoware_utils/ros/update_param.hpp>
24
-
25
18
#include < fmt/format.h>
26
19
27
- #include < memory>
28
- #include < stdexcept>
29
- #include < string>
30
- #include < utility>
31
- #include < vector>
32
-
33
20
namespace
34
21
{
35
22
std::vector<std::string> split (const std::string & str, const char delim)
@@ -44,82 +31,6 @@ std::vector<std::string> split(const std::string & str, const char delim)
44
31
}
45
32
} // namespace
46
33
47
- rcl_interfaces::msg::SetParametersResult DummyDiagPublisher::paramCallback (
48
- const std::vector<rclcpp::Parameter> & parameters)
49
- {
50
- rcl_interfaces::msg::SetParametersResult result;
51
-
52
- for (const auto & param : parameters) {
53
- const auto param_name = param.get_name ();
54
- const auto split_names = split (param_name, ' .' );
55
- const auto & diag_name = split_names.at (0 );
56
- auto it = std::find_if (
57
- std::begin (required_diags_), std::end (required_diags_),
58
- [&diag_name](DummyDiagConfig config) { return config.name == diag_name; });
59
- if (it == std::end (required_diags_)) { // diag name not found
60
- result.successful = false ;
61
- result.reason = " no matching diag name" ;
62
- } else { // diag name found
63
- const auto status_prefix_str = diag_name + std::string (" .status" );
64
- const auto is_active_prefix_str = diag_name + std::string (" .is_active" );
65
- auto status_str = convertStatusToStr (it->status );
66
- auto prev_status_str = status_str;
67
- auto is_active = true ;
68
- try {
69
- tier4_autoware_utils::updateParam (parameters, status_prefix_str, status_str);
70
- tier4_autoware_utils::updateParam (parameters, is_active_prefix_str, is_active);
71
- } catch (const rclcpp::exceptions::InvalidParameterTypeException & e) {
72
- result.successful = false ;
73
- result.reason = e.what ();
74
- return result;
75
- }
76
- const auto status = convertStrToStatus (status_str);
77
- if (!status) {
78
- result.successful = false ;
79
- result.reason = " invalid status" ;
80
- return result;
81
- }
82
- result = updateDiag (diag_name, *it, is_active, *status);
83
- } // end diag name found
84
- }
85
- return result;
86
- }
87
-
88
- // update diag with new param
89
- rcl_interfaces::msg::SetParametersResult DummyDiagPublisher::updateDiag (
90
- const std::string diag_name, DummyDiagConfig & config, bool is_active, const Status status)
91
- {
92
- rcl_interfaces::msg::SetParametersResult result;
93
- result.successful = true ;
94
- result.reason = " success" ;
95
-
96
- if (is_active == config.is_active && config.status == status) { // diag config not changed
97
- result.successful = true ;
98
- result.reason = " config not changed" ;
99
- } else if (is_active == true && config.is_active == false ) { // newly activated
100
- config.is_active = true ;
101
- if (config.status == status) { // status not changed
102
- addDiagByStatus (diag_name, config.status );
103
- } else { // status changed
104
- config.status = status;
105
- addDiagByStatus (diag_name, status);
106
- }
107
- } else { // deactivated or status changed
108
- if (!updater_.removeByName (diag_name)) {
109
- result.successful = false ;
110
- result.reason = " updater removal failed" ;
111
- return result;
112
- }
113
- if (is_active == false ) { // deactivated
114
- config.is_active = false ;
115
- } else { // status changed
116
- config.status = status;
117
- addDiagByStatus (diag_name, status);
118
- }
119
- }
120
- return result;
121
- }
122
-
123
34
std::optional<DummyDiagPublisher::Status> DummyDiagPublisher::convertStrToStatus (
124
35
std::string & status_str)
125
36
{
@@ -147,6 +58,21 @@ std::string DummyDiagPublisher::convertStatusToStr(const Status & status)
147
58
}
148
59
}
149
60
61
+ diagnostic_msgs::msg::DiagnosticStatus::_level_type DummyDiagPublisher::convertStatusToLevel (
62
+ const Status & status)
63
+ {
64
+ switch (status) {
65
+ case Status::OK:
66
+ return diagnostic_msgs::msg::DiagnosticStatus::OK;
67
+ case Status::WARN:
68
+ return diagnostic_msgs::msg::DiagnosticStatus::WARN;
69
+ case Status::ERROR:
70
+ return diagnostic_msgs::msg::DiagnosticStatus::ERROR;
71
+ default :
72
+ return diagnostic_msgs::msg::DiagnosticStatus::STALE;
73
+ }
74
+ }
75
+
150
76
void DummyDiagPublisher::loadRequiredDiags ()
151
77
{
152
78
const auto param_key = std::string (" required_diags" );
@@ -189,91 +115,44 @@ void DummyDiagPublisher::loadRequiredDiags()
189
115
}
190
116
}
191
117
192
- void DummyDiagPublisher::produceOKDiagnostics (diagnostic_updater::DiagnosticStatusWrapper & stat)
193
- {
194
- diagnostic_msgs::msg::DiagnosticStatus status;
195
-
196
- status.level = diagnostic_msgs::msg::DiagnosticStatus::OK;
197
- status.message = diag_config_.msg_ok ;
198
-
199
- stat.summary (status.level , status.message );
200
- }
201
- void DummyDiagPublisher::produceWarnDiagnostics (diagnostic_updater::DiagnosticStatusWrapper & stat)
202
- {
203
- diagnostic_msgs::msg::DiagnosticStatus status;
204
-
205
- status.level = diagnostic_msgs::msg::DiagnosticStatus::WARN;
206
- status.message = diag_config_.msg_warn ;
207
-
208
- stat.summary (status.level , status.message );
209
- }
210
- void DummyDiagPublisher::produceErrorDiagnostics (diagnostic_updater::DiagnosticStatusWrapper & stat)
211
- {
212
- diagnostic_msgs::msg::DiagnosticStatus status;
213
-
214
- status.level = diagnostic_msgs::msg::DiagnosticStatus::ERROR;
215
- status.message = diag_config_.msg_error ;
216
-
217
- stat.summary (status.level , status.message );
218
- }
219
- void DummyDiagPublisher::produceStaleDiagnostics (diagnostic_updater::DiagnosticStatusWrapper & stat)
220
- {
221
- diagnostic_msgs::msg::DiagnosticStatus status;
222
-
223
- status.level = diagnostic_msgs::msg::DiagnosticStatus::STALE;
224
- status.message = diag_config_.msg_stale ;
225
-
226
- stat.summary (status.level , status.message );
227
- }
228
- void DummyDiagPublisher::addDiagByStatus (const std::string & diag_name, const Status status)
229
- {
230
- if (status == Status::OK) {
231
- updater_.add (diag_name, this , &DummyDiagPublisher::produceOKDiagnostics);
232
- } else if (status == Status::WARN) {
233
- updater_.add (diag_name, this , &DummyDiagPublisher::produceWarnDiagnostics);
234
- } else if (status == Status::ERROR) {
235
- updater_.add (diag_name, this , &DummyDiagPublisher::produceErrorDiagnostics);
236
- } else if (status == Status::STALE) {
237
- updater_.add (diag_name, this , &DummyDiagPublisher::produceStaleDiagnostics);
238
- } else {
239
- throw std::runtime_error (" invalid status" );
240
- }
241
- }
242
-
243
118
void DummyDiagPublisher::onTimer ()
244
119
{
245
- updater_.force_update ();
120
+ diagnostic_msgs::msg::DiagnosticArray array;
121
+
122
+ for (const auto & e : required_diags_) {
123
+ if (e.is_active ) {
124
+ diagnostic_msgs::msg::DiagnosticStatus status;
125
+ status.hardware_id = diag_config_.hardware_id ;
126
+ status.name = e.name ;
127
+ status.message = convertStatusToStr (e.status );
128
+ status.level = convertStatusToLevel (e.status );
129
+ array.status .push_back (status);
130
+ }
131
+ }
132
+ array.header .stamp = this ->now ();
133
+ pub_->publish (array);
246
134
}
247
135
248
136
DummyDiagPublisher::DummyDiagPublisher ()
249
137
: Node(
250
138
" dummy_diag_publisher" , rclcpp::NodeOptions()
251
139
.allow_undeclared_parameters(true )
252
- .automatically_declare_parameters_from_overrides(true )),
253
- updater_(this )
254
-
140
+ .automatically_declare_parameters_from_overrides(true ))
255
141
{
256
142
// Parameter
257
143
update_rate_ = this ->get_parameter_or (" update_rate" , 10.0 );
258
144
259
- // Set parameter callback
260
- set_param_res_ = this ->add_on_set_parameters_callback (
261
- std::bind (&DummyDiagPublisher::paramCallback, this , std::placeholders::_1));
262
-
263
145
// Diagnostic Updater
264
146
loadRequiredDiags ();
265
147
266
148
const std::string hardware_id = " dummy_diag" ;
267
- updater_.setHardwareID (hardware_id);
268
149
diag_config_ = DiagConfig{hardware_id, " OK" , " Warn" , " Error" , " Stale" };
269
- for (const auto & config : required_diags_) {
270
- if (config.is_active ) {
271
- addDiagByStatus (config.name , config.status );
272
- }
273
- }
274
150
275
151
// Timer
276
152
const auto period_ns = rclcpp::Rate (update_rate_).period ();
277
153
timer_ = rclcpp::create_timer (
278
154
this , get_clock (), period_ns, std::bind (&DummyDiagPublisher::onTimer, this ));
155
+
156
+ // Publisher
157
+ pub_ = create_publisher<diagnostic_msgs::msg::DiagnosticArray>(" /diagnostics" , rclcpp::QoS (1 ));
279
158
}
0 commit comments