ros2_control - jazzy
Loading...
Searching...
No Matches
gravity_compensation.hpp
1// Copyright (c) 2023, Stogl Robotics Consulting UG (haftungsbeschränkt)
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef CONTROL_FILTERS__GRAVITY_COMPENSATION_HPP_
16#define CONTROL_FILTERS__GRAVITY_COMPENSATION_HPP_
17
18#include <exception>
19#include <memory>
20#include <string>
21#include <vector>
22
23#include "filters/filter_base.hpp"
24#include "geometry_msgs/msg/vector3_stamped.hpp"
25#include "tf2_ros/buffer.h"
26#include "tf2_ros/transform_listener.h"
27
28#include "control_toolbox/gravity_compensation_filter_parameters.hpp"
29
30namespace control_filters
31{
32
33template <typename T>
34class GravityCompensation : public filters::FilterBase<T>
35{
36public:
39
42
44 bool configure() override;
45
50 bool update(const T & data_in, T & data_out) override;
51
52protected:
53 void compute_internal_params()
54 {
55 cog_.vector.x = parameters_.tool.CoG[0];
56 cog_.vector.y = parameters_.tool.CoG[1];
57 cog_.vector.z = parameters_.tool.CoG[2];
58 cst_ext_force_.vector.x = parameters_.tool.gravity_field[0] * parameters_.tool.mass;
59 cst_ext_force_.vector.y = parameters_.tool.gravity_field[1] * parameters_.tool.mass;
60 cst_ext_force_.vector.z = parameters_.tool.gravity_field[2] * parameters_.tool.mass;
61 };
62
63private:
64 rclcpp::Clock::SharedPtr clock_;
65 std::shared_ptr<rclcpp::Logger> logger_;
66 std::shared_ptr<gravity_compensation_filter::ParamListener> parameter_handler_;
67 gravity_compensation_filter::Params parameters_;
68
69 // Frames for Transformation of Gravity / CoG Vector
70 std::string world_frame_; // frame in which gravity is given
71 std::string sensor_frame_; // frame in which Cog is given and compution occur
72 // Storage for Calibration Values
73 geometry_msgs::msg::Vector3Stamped cog_; // Center of Gravity Vector (wrt sensor frame)
74 geometry_msgs::msg::Vector3Stamped cst_ext_force_; // Gravity Force Vector (wrt world frame)
75
76 // Filter objects
77 std::unique_ptr<tf2_ros::Buffer> p_tf_Buffer_;
78 std::unique_ptr<tf2_ros::TransformListener> p_tf_Listener_;
79 geometry_msgs::msg::TransformStamped transform_sensor_datain_, transform_world_dataout_,
80 transform_data_out_sensor_, transform_sensor_world_;
81};
82
83template <typename T>
87
88template <typename T>
92
93template <typename T>
95{
96 clock_ = std::make_shared<rclcpp::Clock>(RCL_ROS_TIME);
97 p_tf_Buffer_.reset(new tf2_ros::Buffer(clock_));
98 p_tf_Listener_.reset(new tf2_ros::TransformListener(*p_tf_Buffer_.get(), true));
99
100 logger_.reset(
101 new rclcpp::Logger(this->logging_interface_->get_logger().get_child(this->filter_name_)));
102
103 // Initialize the parameter_listener once
104 if (!parameter_handler_)
105 {
106 try
107 {
108 parameter_handler_ = std::make_shared<gravity_compensation_filter::ParamListener>(
109 this->params_interface_, this->param_prefix_);
110 }
111 catch (std::exception & ex)
112 {
113 RCLCPP_ERROR((*logger_), "GravityCompensation filter cannot be configured: %s", ex.what());
114 parameter_handler_.reset();
115 throw;
116 }
117 }
118 parameters_ = parameter_handler_->get_params();
119 compute_internal_params();
120
121 return true;
122}
123
124} // namespace control_filters
125
126#endif // CONTROL_FILTERS__GRAVITY_COMPENSATION_HPP_
Definition gravity_compensation.hpp:35
bool configure() override
Configure filter parameters
Definition gravity_compensation.hpp:94
bool update(const T &data_in, T &data_out) override
Update the filter and return the data separately.
GravityCompensation()
Constructor.
Definition gravity_compensation.hpp:84
~GravityCompensation()
Destructor.
Definition gravity_compensation.hpp:89