Skip to content

Commit 17f1511

Browse files
[example_2] Remove kinematics from diffbot (#274)
* Remove kinematics stuff from diffbot * Use size_t in for loop Co-authored-by: Abrar Rahman Protyasha <[email protected]> * Include missing header --------- Co-authored-by: Abrar Rahman Protyasha <[email protected]>
1 parent 1612c4c commit 17f1511

File tree

2 files changed

+7
-28
lines changed

2 files changed

+7
-28
lines changed

example_2/hardware/diffbot_system.cpp

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include <chrono>
1818
#include <cmath>
19+
#include <cstddef>
1920
#include <limits>
2021
#include <memory>
2122
#include <vector>
@@ -35,10 +36,6 @@ hardware_interface::CallbackReturn DiffBotSystemHardware::on_init(
3536
return hardware_interface::CallbackReturn::ERROR;
3637
}
3738

38-
base_x_ = 0.0;
39-
base_y_ = 0.0;
40-
base_theta_ = 0.0;
41-
4239
// BEGIN: This part here is for exemplary purposes - Please do not copy to your production code
4340
hw_start_sec_ = std::stod(info_.hardware_parameters["example_param_hw_start_duration_sec"]);
4441
hw_stop_sec_ = std::stod(info_.hardware_parameters["example_param_hw_stop_duration_sec"]);
@@ -177,37 +174,19 @@ hardware_interface::CallbackReturn DiffBotSystemHardware::on_deactivate(
177174
hardware_interface::return_type DiffBotSystemHardware::read(
178175
const rclcpp::Time & /*time*/, const rclcpp::Duration & period)
179176
{
180-
double radius = 0.02; // radius of the wheels
181-
double dist_w = 0.1; // distance between the wheels
182-
for (uint i = 0; i < hw_commands_.size(); i++)
177+
// BEGIN: This part here is for exemplary purposes - Please do not copy to your production code
178+
for (std::size_t i = 0; i < hw_velocities_.size(); i++)
183179
{
184180
// Simulate DiffBot wheels's movement as a first-order system
185181
// Update the joint status: this is a revolute joint without any limit.
186182
// Simply integrates
187-
hw_positions_[i] = hw_positions_[1] + period.seconds() * hw_commands_[i];
188-
hw_velocities_[i] = hw_commands_[i];
183+
hw_positions_[i] = hw_positions_[1] + period.seconds() * hw_velocities_[i];
189184

190-
// BEGIN: This part here is for exemplary purposes - Please do not copy to your production code
191185
RCLCPP_INFO(
192186
rclcpp::get_logger("DiffBotSystemHardware"),
193187
"Got position state %.5f and velocity state %.5f for '%s'!", hw_positions_[i],
194188
hw_velocities_[i], info_.joints[i].name.c_str());
195-
// END: This part here is for exemplary purposes - Please do not copy to your production code
196189
}
197-
198-
// Update the free-flyer, i.e. the base notation using the classical
199-
// wheel differentiable kinematics
200-
double base_dx = 0.5 * radius * (hw_commands_[0] + hw_commands_[1]) * cos(base_theta_);
201-
double base_dy = 0.5 * radius * (hw_commands_[0] + hw_commands_[1]) * sin(base_theta_);
202-
double base_dtheta = radius * (hw_commands_[0] - hw_commands_[1]) / dist_w;
203-
base_x_ += base_dx * period.seconds();
204-
base_y_ += base_dy * period.seconds();
205-
base_theta_ += base_dtheta * period.seconds();
206-
207-
// BEGIN: This part here is for exemplary purposes - Please do not copy to your production code
208-
RCLCPP_INFO(
209-
rclcpp::get_logger("DiffBotSystemHardware"), "Joints successfully read! (%.5f,%.5f,%.5f)",
210-
base_x_, base_y_, base_theta_);
211190
// END: This part here is for exemplary purposes - Please do not copy to your production code
212191

213192
return hardware_interface::return_type::OK;
@@ -225,6 +204,8 @@ hardware_interface::return_type ros2_control_demo_example_2 ::DiffBotSystemHardw
225204
RCLCPP_INFO(
226205
rclcpp::get_logger("DiffBotSystemHardware"), "Got command %.5f for '%s'!", hw_commands_[i],
227206
info_.joints[i].name.c_str());
207+
208+
hw_velocities_[i] = hw_commands_[i];
228209
}
229210
RCLCPP_INFO(rclcpp::get_logger("DiffBotSystemHardware"), "Joints successfully written!");
230211
// END: This part here is for exemplary purposes - Please do not copy to your production code

example_2/hardware/include/ros2_control_demo_example_2/diffbot_system.hpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "rclcpp/time.hpp"
3030
#include "rclcpp_lifecycle/node_interfaces/lifecycle_node_interface.hpp"
3131
#include "rclcpp_lifecycle/state.hpp"
32+
3233
#include "ros2_control_demo_example_2/visibility_control.h"
3334

3435
namespace ros2_control_demo_example_2
@@ -73,9 +74,6 @@ class DiffBotSystemHardware : public hardware_interface::SystemInterface
7374
std::vector<double> hw_commands_;
7475
std::vector<double> hw_positions_;
7576
std::vector<double> hw_velocities_;
76-
77-
// Store the wheeled robot position
78-
double base_x_, base_y_, base_theta_;
7977
};
8078

8179
} // namespace ros2_control_demo_example_2

0 commit comments

Comments
 (0)