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(
177174hardware_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
0 commit comments