-
Notifications
You must be signed in to change notification settings - Fork 37
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: can reconnect #29
Open
uhobeike
wants to merge
2
commits into
odriverobotics:main
Choose a base branch
from
Shinsotsu-Tsukuba-Challenger:feat/can-reconnect
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
|
||
#include "can_helpers.hpp" | ||
#include "can_simple_messages.hpp" | ||
#include "hardware_interface/lexical_casts.hpp" | ||
#include "hardware_interface/system_interface.hpp" | ||
#include "hardware_interface/types/hardware_interface_type_values.hpp" | ||
#include "odrive_enums.h" | ||
|
@@ -37,6 +38,8 @@ class ODriveHardwareInterface final : public hardware_interface::SystemInterface | |
private: | ||
void on_can_msg(const can_frame& frame); | ||
|
||
void on_can_reconnect(); | ||
|
||
EpollEventLoop event_loop_; | ||
std::vector<Axis> axes_; | ||
std::string can_intf_name_; | ||
|
@@ -61,10 +64,10 @@ struct Axis { | |
|
||
// State (ODrives => ros2_control) | ||
// rclcpp::Time encoder_estimates_timestamp_; | ||
// uint32_t axis_error_ = 0; | ||
// uint8_t axis_state_ = 0; | ||
// uint8_t procedure_result_ = 0; | ||
// uint8_t trajectory_done_flag_ = 0; | ||
uint32_t axis_error_ = 0; | ||
uint8_t axis_state_ = 0; | ||
uint8_t procedure_result_ = 0; | ||
uint8_t trajectory_done_flag_ = 0; | ||
double pos_estimate_ = NAN; // [rad] | ||
double vel_estimate_ = NAN; // [rad/s] | ||
// double iq_setpoint_ = NAN; | ||
|
@@ -281,6 +284,10 @@ return_type ODriveHardwareInterface::read(const rclcpp::Time& timestamp, const r | |
} | ||
|
||
return_type ODriveHardwareInterface::write(const rclcpp::Time&, const rclcpp::Duration&) { | ||
if (hardware_interface::parse_bool(info_.hardware_parameters["can_reconnect"]) && can_intf_.has_can_reconnected()) { | ||
on_can_reconnect(); | ||
} | ||
|
||
for (auto& axis : axes_) { | ||
// Send the CAN message that fits the set of enabled setpoints | ||
if (axis.pos_input_enabled_) { | ||
|
@@ -339,7 +346,55 @@ void Axis::on_can_msg(const rclcpp::Time&, const can_frame& frame) { | |
torque_estimate_ = msg.Torque_Estimate; | ||
} | ||
} break; | ||
// silently ignore unimplemented command IDs | ||
case Heartbeat_msg_t::cmd_id: { | ||
if (Heartbeat_msg_t msg; try_decode(msg)) { | ||
axis_error_ = msg.Axis_Error; | ||
axis_state_ = msg.Axis_State; | ||
procedure_result_ = msg.Procedure_Result; | ||
trajectory_done_flag_ = msg.Trajectory_Done_Flag; | ||
} | ||
} break; | ||
// silently ignore unimplemented command IDs | ||
} | ||
} | ||
|
||
void ODriveHardwareInterface::on_can_reconnect() { | ||
for (size_t i = 0; i < axes_.size(); ++i) { | ||
Axis& axis = axes_[i]; | ||
if (!(axis.axis_error_ == 0 && axis.axis_state_ == 8 && axis.procedure_result_ == 0 && axis.trajectory_done_flag_ == 1)) { | ||
Set_Controller_Mode_msg_t msg; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The content of this All things considered, I think two separate functions make sense: void ODriveHardwareInterface::request_closed_loop_control() {
// send Set_Controller_Mode_msg_t, Clear_Errors_msg_t and Set_Axis_State_msg_t
}
void ODriveHardwareInterface::request_idle() {
// send single Set_Axis_State_msg_t message
} |
||
if (axis.pos_input_enabled_) { | ||
RCLCPP_INFO(rclcpp::get_logger("ODriveHardwareInterface"), "Setting %s to position control", info_.joints[i].name.c_str()); | ||
msg.Control_Mode = CONTROL_MODE_POSITION_CONTROL; | ||
msg.Input_Mode = INPUT_MODE_PASSTHROUGH; | ||
} else if (axis.vel_input_enabled_) { | ||
RCLCPP_INFO(rclcpp::get_logger("ODriveHardwareInterface"), "Setting %s to velocity control", info_.joints[i].name.c_str()); | ||
msg.Control_Mode = CONTROL_MODE_VELOCITY_CONTROL; | ||
msg.Input_Mode = INPUT_MODE_PASSTHROUGH; | ||
} else { | ||
RCLCPP_INFO(rclcpp::get_logger("ODriveHardwareInterface"), "Setting %s to torque control", info_.joints[i].name.c_str()); | ||
msg.Control_Mode = CONTROL_MODE_TORQUE_CONTROL; | ||
msg.Input_Mode = INPUT_MODE_PASSTHROUGH; | ||
} | ||
|
||
bool any_enabled = axis.pos_input_enabled_ || axis.vel_input_enabled_ || axis.torque_input_enabled_; | ||
|
||
if (any_enabled) { | ||
axis.send(msg); // Set control mode | ||
} | ||
|
||
// Set axis state | ||
Clear_Errors_msg_t msg1; | ||
msg1.Identify = 0; | ||
axis.send(msg1); | ||
|
||
// Set axis state | ||
Set_Axis_State_msg_t msg2; | ||
msg2.Axis_Requested_State = any_enabled ? AXIS_STATE_CLOSED_LOOP_CONTROL : AXIS_STATE_IDLE; | ||
axis.send(msg2); | ||
} else { | ||
can_intf_.reset_can_connection_check(); | ||
} | ||
} | ||
} | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 if-condition needs to be revised:
!(axis.axis_state_ == 8)
means "re-enable whenever the state is something other than CLOSED_LOOP_CONTROL". I prefer the more conservative checkaxis.axis_state_ == 0
which means "re-enable only when the state is IDLE". So during other states (calibration), the ROS plugin doesn't interfere.!(axis.axis_error_ == 0)
is somewhat redundant with the state check. However I think it would be nice to introduce a configurable filter such that the ROS interface only re-enables if the error is signed off by the developer.axis.procedure_result_
needs to be ignored, it is only reported asProcedureResult.SUCCESS (0)
due to an oversight in the firmware. It will beProcedureResult.BUSY (1)
in future firmware versions (but we can't rely on that here either because it would break backwards compatibility)axis.trajectory_done_flag_
needs to be ignored, it is undefined for input modes other thanInputMode.TRAP_TRAJ
. (It happens to default totrue
ifTRAP_TRAJ
has not been used since last reboot)So in the end it becomes something like: