|
| 1 | +#ifndef LBR_ROS2_CONTROL__LBR_TWIST_CONTROLLER_HPP_ |
| 2 | +#define LBR_ROS2_CONTROL__LBR_TWIST_CONTROLLER_HPP_ |
| 3 | + |
| 4 | +#include <algorithm> |
| 5 | +#include <array> |
| 6 | +#include <functional> |
| 7 | +#include <memory> |
| 8 | +#include <stdexcept> |
| 9 | +#include <string> |
| 10 | +#include <vector> |
| 11 | + |
| 12 | +#include "controller_interface/controller_interface.hpp" |
| 13 | +#include "eigen3/Eigen/Core" |
| 14 | +#include "geometry_msgs/msg/twist.hpp" |
| 15 | +#include "hardware_interface/loaned_state_interface.hpp" |
| 16 | +#include "hardware_interface/types/hardware_interface_type_values.hpp" |
| 17 | +#include "rclcpp/rclcpp.hpp" |
| 18 | +#include "realtime_tools/realtime_buffer.h" |
| 19 | + |
| 20 | +#include "friLBRState.h" |
| 21 | + |
| 22 | +#include "lbr_fri_ros2/kinematics.hpp" |
| 23 | +#include "lbr_fri_ros2/pinv.hpp" |
| 24 | +#include "lbr_ros2_control/system_interface_type_values.hpp" |
| 25 | + |
| 26 | +namespace lbr_ros2_control { |
| 27 | +struct TwistParameters { |
| 28 | + std::string chain_root; |
| 29 | + std::string chain_tip; |
| 30 | + double damping; |
| 31 | + double max_linear_velocity; |
| 32 | + double max_angular_velocity; |
| 33 | +}; |
| 34 | + |
| 35 | +class TwistImpl { |
| 36 | +public: |
| 37 | + TwistImpl(const std::string &robot_description, const TwistParameters ¶meters); |
| 38 | + |
| 39 | + void compute(const geometry_msgs::msg::Twist::SharedPtr &twist_target, |
| 40 | + lbr_fri_ros2::Kinematics::const_jnt_pos_array_t_ref q, |
| 41 | + lbr_fri_ros2::Kinematics::jnt_pos_array_t &dq); |
| 42 | + |
| 43 | +protected: |
| 44 | + TwistParameters parameters_; |
| 45 | + |
| 46 | + lbr_fri_ros2::Kinematics::jnt_pos_array_t q_; |
| 47 | + std::unique_ptr<lbr_fri_ros2::Kinematics> kinematics_ptr_; |
| 48 | + Eigen::Matrix<double, KUKA::FRI::LBRState::NUMBER_OF_JOINTS, |
| 49 | + lbr_fri_ros2::Kinematics::CARTESIAN_DOF> |
| 50 | + jacobian_inv_; |
| 51 | + Eigen::Matrix<double, lbr_fri_ros2::Kinematics::CARTESIAN_DOF, 1> twist_target_; |
| 52 | +}; |
| 53 | + |
| 54 | +class TwistController : public controller_interface::ControllerInterface { |
| 55 | +public: |
| 56 | + TwistController(); |
| 57 | + |
| 58 | + controller_interface::InterfaceConfiguration command_interface_configuration() const override; |
| 59 | + |
| 60 | + controller_interface::InterfaceConfiguration state_interface_configuration() const override; |
| 61 | + |
| 62 | + controller_interface::CallbackReturn on_init() override; |
| 63 | + |
| 64 | + controller_interface::return_type update(const rclcpp::Time &time, |
| 65 | + const rclcpp::Duration &period) override; |
| 66 | + |
| 67 | + controller_interface::CallbackReturn |
| 68 | + on_configure(const rclcpp_lifecycle::State &previous_state) override; |
| 69 | + |
| 70 | + controller_interface::CallbackReturn |
| 71 | + on_activate(const rclcpp_lifecycle::State &previous_state) override; |
| 72 | + |
| 73 | + controller_interface::CallbackReturn |
| 74 | + on_deactivate(const rclcpp_lifecycle::State &previous_state) override; |
| 75 | + |
| 76 | +protected: |
| 77 | + bool reference_state_interfaces_(); |
| 78 | + void clear_state_interfaces_(); |
| 79 | + void reset_command_buffer_(); |
| 80 | + void configure_joint_names_(); |
| 81 | + void configure_twist_impl_(); |
| 82 | + |
| 83 | + // joint veloctiy computation |
| 84 | + std::unique_ptr<TwistImpl> twist_impl_ptr_; |
| 85 | + lbr_fri_ros2::Kinematics::jnt_pos_array_t q_, dq_; |
| 86 | + |
| 87 | + // interfaces |
| 88 | + std::array<std::string, KUKA::FRI::LBRState::NUMBER_OF_JOINTS> joint_names_; |
| 89 | + std::vector<std::reference_wrapper<hardware_interface::LoanedStateInterface>> |
| 90 | + joint_position_state_interfaces_; |
| 91 | + std::unique_ptr<std::reference_wrapper<hardware_interface::LoanedStateInterface>> |
| 92 | + sample_time_state_interface_; |
| 93 | + std::unique_ptr<std::reference_wrapper<hardware_interface::LoanedStateInterface>> |
| 94 | + session_state_interface_; |
| 95 | + |
| 96 | + // real-time twist command topic |
| 97 | + realtime_tools::RealtimeBuffer<geometry_msgs::msg::Twist::SharedPtr> rt_twist_ptr_; |
| 98 | + rclcpp::Subscription<geometry_msgs::msg::Twist>::SharedPtr twist_subscription_ptr_; |
| 99 | +}; |
| 100 | +} // namespace lbr_ros2_control |
| 101 | +#endif // LBR_ROS2_CONTROL__LBR_TWIST_CONTROLLER_HPP_ |
0 commit comments