2626#include < rcl_yaml_param_parser/parser.h>
2727
2828using micro_ros_diagnostic_msgs::msg::MicroROSDiagnosticStatus;
29- using diagnostic_msgs::msg::DiagnosticStatus;
29+ using diagnostic_msgs::msg::DiagnosticArray;
30+
31+ static inline std::string VALUE_NOT_FOUND = " NOTFOUND" ;
3032
3133MicroROSDiagnosticBridge::MicroROSDiagnosticBridge (const std::string & path)
3234: Node(" micro_ros_diagnostic_bridge" ),
@@ -40,54 +42,64 @@ MicroROSDiagnosticBridge::MicroROSDiagnosticBridge(const std::string & path)
4042 }
4143 read_lookup_table (lookup_table_path);
4244
45+ rclcpp::QoS qos{rclcpp::KeepLast{10 }};
46+ qos.reliable ();
47+
48+ ros2_diagnostics_pub_ = create_publisher<DiagnosticArray>(
49+ UROS_DIAGNOSTICS_BRIDGE_TOPIC_OUT,
50+ qos);
51+
4352 auto callback =
4453 [this ](const MicroROSDiagnosticStatus::SharedPtr msg_in) -> void
4554 {
4655 RCLCPP_DEBUG (
4756 get_logger (),
4857 " Bridging message from hardware %d, updater %d" , msg_in->hardware_id ,
4958 msg_in->updater_id );
50- msg_out_ = std::make_unique<diagnostic_msgs::msg::DiagnosticStatus>();
59+ auto msg_out = std::make_unique<DiagnosticArray>();
60+ diagnostic_msgs::msg::DiagnosticStatus status_msg{};
5161
5262 auto updater = lookup_updater (msg_in->updater_id );
5363 auto hardware = lookup_hardware (msg_in->hardware_id );
54- msg_out_->hardware_id = hardware;
55- msg_out_->name = updater.name ;
56- msg_out_->message = updater.description ;
57- msg_out_->level = msg_in->level ;
58-
59- diagnostic_msgs::msg::KeyValue keyvalue;
60- RCLCPP_DEBUG (get_logger (), " Bridging updater %d, task %d" , msg_in->updater_id , msg_in->key );
61- keyvalue.key = lookup_key (msg_in->updater_id , msg_in->key );
62- switch (msg_in->value_type ) {
63- case micro_ros_diagnostic_msgs::msg::MicroROSDiagnosticStatus::VALUE_BOOL:
64- keyvalue.value = std::to_string (msg_in->bool_value );
65- break ;
66- case micro_ros_diagnostic_msgs::msg::MicroROSDiagnosticStatus::VALUE_INT:
67- keyvalue.value = std::to_string (msg_in->int_value );
68- break ;
69- case micro_ros_diagnostic_msgs::msg::MicroROSDiagnosticStatus::VALUE_DOUBLE:
70- keyvalue.value = std::to_string (msg_in->double_value );
71- break ;
72- case micro_ros_diagnostic_msgs::msg::MicroROSDiagnosticStatus::VALUE_LOOKUP:
73- keyvalue.value = lookup_value (msg_in->updater_id , msg_in->key , msg_in->value_id );
74- break ;
64+ status_msg.hardware_id = hardware;
65+ status_msg.name = updater.name ;
66+ status_msg.message = updater.description ;
67+ status_msg.level = msg_in->level ;
68+ RCLCPP_DEBUG (get_logger (), " Updater %s HW %s" , updater.name .c_str (), hardware.c_str ());
69+ for (size_t value_index = 0 ; value_index < msg_in->number_of_values ; value_index++) {
70+ diagnostic_msgs::msg::KeyValue keyvalue;
71+ RCLCPP_DEBUG (
72+ get_logger (), " Bridging updater %d, key %d" , msg_in->updater_id ,
73+ msg_in->values [value_index].key );
74+ keyvalue.key = lookup_key (msg_in->updater_id , msg_in->values [value_index].key );
75+ switch (msg_in->values [value_index].value_type ) {
76+ case MicroROSDiagnosticStatus::VALUE_BOOL:
77+ keyvalue.value = std::to_string (msg_in->values [value_index].bool_value );
78+ break ;
79+ case MicroROSDiagnosticStatus::VALUE_INT:
80+ keyvalue.value = std::to_string (msg_in->values [value_index].int_value );
81+ break ;
82+ case MicroROSDiagnosticStatus::VALUE_DOUBLE:
83+ keyvalue.value = std::to_string (msg_in->values [value_index].double_value );
84+ break ;
85+ case MicroROSDiagnosticStatus::VALUE_LOOKUP:
86+ keyvalue.value = lookup_value (
87+ msg_in->updater_id , msg_in->values [value_index].key ,
88+ msg_in->values [value_index].value_id );
89+ break ;
90+ }
91+ status_msg.values .push_back (keyvalue);
92+ RCLCPP_DEBUG (get_logger (), " key %s value %s" , keyvalue.key .c_str (), keyvalue.value .c_str ());
7593 }
76- msg_out_-> values .push_back (keyvalue );
77-
78- ros2_pub_ ->publish (std::move (msg_out_ ));
94+ msg_out-> status .push_back (status_msg );
95+ msg_out-> header . stamp = rclcpp::Clock (). now ();
96+ ros2_diagnostics_pub_ ->publish (std::move (msg_out ));
7997 };
8098
81- rclcpp::QoS qos{rclcpp::KeepLast{10 }};
82- qos.reliable ();
83-
84- uros_sub_ = create_subscription<MicroROSDiagnosticStatus>(
99+ uros_diagnostics_sub_ = create_subscription<MicroROSDiagnosticStatus>(
85100 UROS_DIAGNOSTICS_BRIDGE_TOPIC_IN,
86101 qos,
87102 callback);
88- ros2_pub_ = create_publisher<DiagnosticStatus>(
89- UROS_DIAGNOSTICS_BRIDGE_TOPIC_OUT,
90- qos);
91103}
92104
93105std::string
@@ -102,7 +114,7 @@ MicroROSDiagnosticBridge::lookup_key(
102114 get_logger (),
103115 " Updater %d and key %d, not found in lookup table." ,
104116 updater_id, key);
105- return " NOTFOUND " ;
117+ return VALUE_NOT_FOUND ;
106118 }
107119}
108120
@@ -119,7 +131,7 @@ MicroROSDiagnosticBridge::lookup_value(
119131 get_logger (),
120132 " Updater %d, key %d, and value id %d, not found in lookup table." ,
121133 updater_id, key, value_id);
122- return " NOTFOUND " ;
134+ return VALUE_NOT_FOUND ;
123135 }
124136}
125137
@@ -169,6 +181,9 @@ MicroROSDiagnosticBridge::read_lookup_table(const std::string & path)
169181 for (auto & p : it->second ) {
170182 try {
171183 hardware_map_[std::stoi (p.get_name ())] = p.value_to_string ();
184+ RCLCPP_DEBUG (
185+ get_logger (), " FOUND Parameter: %s HW_ID %s" ,
186+ p.get_name ().c_str (), p.value_to_string ().c_str ());
172187 } catch (const std::invalid_argument &) {
173188 throw std::runtime_error (" Failed to parse hardware_id from lookup_table." );
174189 }
@@ -181,28 +196,37 @@ MicroROSDiagnosticBridge::read_lookup_table(const std::string & path)
181196 auto pos = p.get_name ().find (' .' );
182197 if (pos != std::string::npos) {
183198 updater_key = p.get_name ().substr (0 , pos);
199+ RCLCPP_DEBUG (get_logger (), " Updater key: %s" , updater_key.c_str ());
184200 } else {
185201 throw std::runtime_error (" Failed to load updater key." );
186202 }
187203
188204 // Updater
189205 if (p.get_name ().compare (updater_key + " .name" ) == 0 ) {
190206 updater_name = p.value_to_string ();
207+ RCLCPP_DEBUG (
208+ get_logger (), " Updater Name: %s, Description: %s" ,
209+ updater_name.c_str (), updater_descr.c_str ());
191210 updater_map_[std::stoi (updater_key)] = {updater_name, updater_descr};
192211 }
193212 if (p.get_name ().compare (updater_key + " .description" ) == 0 ) {
194213 updater_descr = p.value_to_string ();
195214 updater_map_[std::stoi (updater_key)] = {updater_name, updater_descr};
215+ RCLCPP_DEBUG (
216+ get_logger (), " Updater Name: %s, Description: %s" ,
217+ updater_name.c_str (), updater_descr.c_str ());
196218 }
197219
198220 // Keys
199221 if (p.get_name ().rfind (updater_key + " .keys" , 0 ) == 0 ) {
200222 auto start = updater_key.length () + 6 ;
201223 pos = p.get_name ().find (' .' , start);
202224 key = p.get_name ().substr (start, pos - start);
225+ RCLCPP_DEBUG (get_logger (), " Updater key position: %ld, key: %s" , pos, key.c_str ());
203226 }
204227 if (p.get_name ().compare (updater_key + " .keys." + key + " .name" ) == 0 ) {
205228 key_name = p.value_to_string ();
229+ RCLCPP_DEBUG (get_logger (), " Key name: %s" , key_name.c_str ());
206230 key_map_[{std::stoi (updater_key), std::stoi (key)}] = key_name;
207231 }
208232
@@ -211,8 +235,8 @@ MicroROSDiagnosticBridge::read_lookup_table(const std::string & path)
211235 auto start = updater_key.length () + key.length () + 14 ;
212236 pos = p.get_name ().find (' .' , start);
213237 auto value_id = std::stoi (p.get_name ().substr (start, pos - start));
214- value_map_[{{std::stoi (updater_key), std::stoi (key)}, value_id}] =
215- p.value_to_string ();
238+ value_map_[{{std::stoi (updater_key), std::stoi (key)}, value_id}] = p. value_to_string ();
239+ RCLCPP_DEBUG ( get_logger (), " Value ID %d Value %s " , value_id, p.value_to_string (). c_str () );
216240 }
217241 }
218242 }
0 commit comments