Skip to content

Commit 460c1a6

Browse files
Now we can switch controllers.
1 parent e5f890e commit 460c1a6

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

webots_ros2_control/include/webots_ros2_control/Ros2ControlSystem.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ namespace webots_ros2_control {
6161

6262
std::vector<hardware_interface::StateInterface> export_state_interfaces() override;
6363
std::vector<hardware_interface::CommandInterface> export_command_interfaces() override;
64+
65+
hardware_interface::return_type perform_command_mode_switch(const std::vector<std::string>& start_interfaces, const std::vector<std::string>& stop_interfaces) override;
66+
6467
hardware_interface::return_type read(const rclcpp::Time & /*time*/, const rclcpp::Duration & /*period*/) override;
6568
hardware_interface::return_type write(const rclcpp::Time & /*time*/, const rclcpp::Duration & /*period*/) override;
6669

webots_ros2_control/src/Ros2ControlSystem.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,43 @@ namespace webots_ros2_control {
133133
return rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn::SUCCESS;
134134
}
135135

136+
hardware_interface::return_type Ros2ControlSystem::perform_command_mode_switch(
137+
const std::vector<std::string> &start_interfaces, const std::vector<std::string> &stop_interfaces) {
138+
for (Joint &joint : mJoints) {
139+
for (const std::string &interface_name : stop_interfaces) {
140+
// Clear joint control method bits corresponding to stop interfaces
141+
if (interface_name == (joint.name + "/" + hardware_interface::HW_IF_POSITION)) {
142+
joint.controlPosition = false;
143+
}
144+
if (interface_name == (joint.name + "/" + hardware_interface::HW_IF_VELOCITY)) {
145+
joint.controlVelocity = false;
146+
}
147+
if (interface_name == (joint.name + "/" + hardware_interface::HW_IF_EFFORT)) {
148+
joint.controlEffort = false;
149+
}
150+
}
151+
152+
// Set joint control method bits corresponding to start interfaces
153+
for (const std::string &interface_name : start_interfaces) {
154+
if (interface_name == (joint.name + "/" + hardware_interface::HW_IF_POSITION)) {
155+
joint.controlPosition = true;
156+
}
157+
if (interface_name == (joint.name + "/" + hardware_interface::HW_IF_VELOCITY)) {
158+
joint.controlVelocity = true;
159+
}
160+
if (interface_name == (joint.name + "/" + hardware_interface::HW_IF_EFFORT)) {
161+
joint.controlEffort = true;
162+
}
163+
}
164+
if (joint.motor && joint.controlVelocity && !joint.controlPosition) {
165+
wb_motor_set_position(joint.motor, INFINITY);
166+
wb_motor_set_velocity(joint.motor, 0.0);
167+
}
168+
}
169+
170+
return hardware_interface::return_type::OK;
171+
}
172+
136173
hardware_interface::return_type Ros2ControlSystem::read(const rclcpp::Time & /*time*/, const rclcpp::Duration & /*period*/) {
137174
static double lastReadTime = 0;
138175

0 commit comments

Comments
 (0)