-
Notifications
You must be signed in to change notification settings - Fork 0
added status msgs #23
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
base: jazzy
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| #ifndef DYNAMIXEL_ROS_CONTROL_DIAGNOSTIC_STATE_H | ||
| #define DYNAMIXEL_ROS_CONTROL_DIAGNOSTIC_STATE_H | ||
|
|
||
| #include <map> | ||
| #include <string> | ||
|
|
||
| #include <diagnostic_msgs/msg/diagnostic_status.hpp> | ||
|
|
||
| namespace dynamixel_ros_control { | ||
|
|
||
| /// @brief Lightweight snapshot of diagnostic values for cheap change detection (no heap allocations). | ||
| struct DiagnosticState | ||
| { | ||
| bool hw_ok{true}; | ||
| bool e_stop_active{false}; | ||
| bool torque_enabled{false}; | ||
| unsigned int read_errors{0}; | ||
| unsigned int write_errors{0}; | ||
| std::map<std::string, int32_t> joint_hw_errors; ///< Per-joint hardware error status (only non-OK entries). | ||
|
|
||
| bool operator==(const DiagnosticState& o) const | ||
| { | ||
| return hw_ok == o.hw_ok && e_stop_active == o.e_stop_active && torque_enabled == o.torque_enabled && | ||
| read_errors == o.read_errors && write_errors == o.write_errors && joint_hw_errors == o.joint_hw_errors; | ||
| } | ||
| bool operator!=(const DiagnosticState& o) const | ||
| { | ||
| return !(*this == o); | ||
| } | ||
|
|
||
| /// @brief Convert to a DiagnosticStatus message. Performs string allocations — only call when publishing. | ||
| diagnostic_msgs::msg::DiagnosticStatus toMsg(const std::string& name) const; | ||
|
|
||
| /// @brief Convert a hardware error status bitfield to a human-readable string. | ||
| static std::string hardwareErrorToString(int32_t error_status); | ||
| }; | ||
|
|
||
| } // namespace dynamixel_ros_control | ||
| #endif | ||
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,69 @@ | ||||||||||||
| #include "dynamixel_ros_control/diagnostic_state.hpp" | ||||||||||||
|
|
||||||||||||
| #include "dynamixel_ros_control/dynamixel.hpp" | ||||||||||||
|
|
||||||||||||
| namespace dynamixel_ros_control { | ||||||||||||
|
|
||||||||||||
| diagnostic_msgs::msg::DiagnosticStatus DiagnosticState::toMsg(const std::string& name) const | ||||||||||||
| { | ||||||||||||
| diagnostic_msgs::msg::DiagnosticStatus status; | ||||||||||||
| status.name = name; | ||||||||||||
| status.hardware_id = name; | ||||||||||||
|
|
||||||||||||
| if (!hw_ok) { | ||||||||||||
| status.level = diagnostic_msgs::msg::DiagnosticStatus::ERROR; | ||||||||||||
| status.message = "Hardware error detected"; | ||||||||||||
| } else if (e_stop_active) { | ||||||||||||
| status.level = diagnostic_msgs::msg::DiagnosticStatus::WARN; | ||||||||||||
| status.message = "E-Stop active"; | ||||||||||||
| } else { | ||||||||||||
| status.level = diagnostic_msgs::msg::DiagnosticStatus::OK; | ||||||||||||
| status.message = "OK"; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| status.values.resize(4 + joint_hw_errors.size()); | ||||||||||||
| status.values[0].key = "e_stop_active"; | ||||||||||||
| status.values[0].value = e_stop_active ? "true" : "false"; | ||||||||||||
| status.values[1].key = "torque_enabled"; | ||||||||||||
| status.values[1].value = torque_enabled ? "true" : "false"; | ||||||||||||
| status.values[2].key = "consecutive_read_errors"; | ||||||||||||
| status.values[2].value = std::to_string(read_errors); | ||||||||||||
| status.values[3].key = "consecutive_write_errors"; | ||||||||||||
| status.values[3].value = std::to_string(write_errors); | ||||||||||||
|
|
||||||||||||
| size_t i = 4; | ||||||||||||
| for (const auto& [joint_name, error_status] : joint_hw_errors) { | ||||||||||||
| status.values[i].key = joint_name + "/hardware_error"; | ||||||||||||
| status.values[i].value = hardwareErrorToString(error_status); | ||||||||||||
| ++i; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| return status; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| std::string DiagnosticState::hardwareErrorToString(int32_t error_status) | ||||||||||||
| { | ||||||||||||
| if (error_status == OK) { | ||||||||||||
| return "ok"; | ||||||||||||
| } | ||||||||||||
| std::string result; | ||||||||||||
| if (error_status & VOLTAGE_ERROR) | ||||||||||||
| result += "Voltage, "; | ||||||||||||
| if (error_status & HALL_SENSOR_ERROR) | ||||||||||||
| result += "Hall Sensor, "; | ||||||||||||
| if (error_status & OVERHEATING_ERROR) | ||||||||||||
| result += "Overheating, "; | ||||||||||||
| if (error_status & MOTOR_ENCODER_ERROR) | ||||||||||||
| result += "Motor Encoder, "; | ||||||||||||
| if (error_status & ELECTRICAL_SHOCK_ERROR) | ||||||||||||
| result += "Electrical Shock, "; | ||||||||||||
| if (error_status & OVERLOAD_ERROR) | ||||||||||||
| result += "Overload, "; | ||||||||||||
| // Remove trailing ", " | ||||||||||||
| if (result.size() >= 2) { | ||||||||||||
| result.erase(result.size() - 2); | ||||||||||||
| } | ||||||||||||
|
||||||||||||
| } | |
| } | |
| if (result.empty()) { | |
| return "Unknown error (status=" + std::to_string(error_status) + ")"; | |
| } |
Copilot
AI
Feb 25, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment "Remove trailing ', '" states that 2 characters should be removed, but the erased length should account for the actual trailing string which is ", " (comma and space). While the code is correct (2 characters), the string concatenation logic always adds ", " after each error type. However, if only one error is present, the trailing ", " is correctly removed. Consider using a more robust string building approach, such as using a vector to collect error strings and then joining them with ", " to avoid the need for trailing removal logic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment states "no heap allocations" but the struct contains
std::map<std::string, int32_t> joint_hw_errorswhich performs heap allocations when entries are added or when strings are stored. This comment is misleading. Consider updating the comment to be more accurate, such as "Lightweight snapshot for cheap change detection using value comparison" without claiming no heap allocations.