gravity_compensation.h
Go to the documentation of this file.
1 /*****************************************************************************
2  *
3  * Copyright 2016 Intelligent Industrial Robotics (IIROB) Group,
4  * Institute for Anthropomatics and Robotics (IAR) -
5  * Intelligent Process Control and Robotics (IPR),
6  * Karlsruhe Institute of Technology (KIT)
7  *
8  * Author: Andreea Tulbure, email: andreea.tulbure@student.kit.edu
9  * Denis Štogl, email: denis.stogl@kit.edu
10  * Alexandar Pollmann
11  *
12  * Date of creation: 2015-2017
13  *
14  * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
15  *
16  * Redistribution and use in source and binary forms,
17  * with or without modification, are permitted provided
18  * that the following conditions are met:
19  *
20  * * Redistributions of source code must retain the above copyright
21  * notice, this list of conditions and the following disclaimer.
22  * * Redistributions in binary form must reproduce the above copyright
23  * notice, this list of conditions and the following disclaimer in the
24  * documentation and/or other materials provided with the distribution.
25  * * Neither the name of the copyright holder nor the names of its
26  * contributors may be used to endorse or promote products derived from
27  * this software without specific prior written permission.
28  *
29  * This package is free software: you can redistribute it and/or modify
30  * it under the terms of the GNU Lesser General Public License as published by
31  * the Free Software Foundation, either version 3 of the License, or
32  * (at your option) any later version.
33  *
34  * This package is distributed in the hope that it will be useful,
35  * but WITHOUT ANY WARRANTY; without even the implied warranty of
36  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37  * GNU Lesser General Public License for more details.
38  *
39  * You should have received a copy of the GNU Lesser General Public License
40  * along with this package. If not, see <http://www.gnu.org/licenses/>.
41 *****************************************************************************/
42 
43 #ifndef IIROB_FILTERS_GRAVITY_COMPENSATION_H
44 #define IIROB_FILTERS_GRAVITY_COMPENSATION_H
45 #include <stdint.h>
46 #include <cstring>
47 #include <stdio.h>
48 
49 #include <boost/scoped_ptr.hpp>
50 #include <ros/ros.h>
51 #include <geometry_msgs/WrenchStamped.h>
52 #include <geometry_msgs/Vector3Stamped.h>
56 #include <iirob_filters/GravityCompensationParameters.h>
57 #include <iirob_filters/GravityCompensationConfig.h>
58 #include <dynamic_reconfigure/server.h>
59 #include <filters/filter_base.h>
60 
61 namespace iirob_filters{
62 
63 template <typename T>
65 {
66 public:
68 
72 
73  virtual bool configure();
74 
79  virtual bool update( const T & data_in, T& data_out);
80 
81 private:
82  iirob_filters::GravityCompensationParameters params_;
83 
84  // Storage for Calibration Values
85  geometry_msgs::Vector3Stamped cog_; // Center of Gravity Vector (wrt Sensor Frame)
86  double force_z_; // Gravitational Force
87 
88  // Frames for Transformation of Gravity / CoG Vector
89  std::string world_frame_;
90  std::string sensor_frame_;
91 
92  // tf2 objects
95  geometry_msgs::TransformStamped transform_, transform_back_;
96 
98 
99  dynamic_reconfigure::Server<iirob_filters::GravityCompensationConfig> reconfigCalibrationSrv_; // Dynamic reconfiguration service
100 
101  void reconfigureConfigurationRequest(iirob_filters::GravityCompensationConfig& config, uint32_t level);
102 };
103 
104 template <typename T>
105 GravityCompensator<T>::GravityCompensator(): params_{ros::NodeHandle("~/GravityCompensation/params").getNamespace()}
106 {
108 }
109 
110 template <typename T>
112 {
113 }
114 
115 template <typename T>
117 {
118  params_.fromParamServer();
119  if(params_.world_frame == " ")
120  ROS_ERROR("GravityCompensator did not find param world_frame");
121  if(params_.sensor_frame == " ")
122  ROS_DEBUG("GravityCompensator did not find param sensor_frame");
123  if(params_.CoG_x == 0)
124  ROS_DEBUG("GravityCompensator did not find param CoG_x");
125  if(params_.CoG_y == 0)
126  ROS_DEBUG("GravityCompensator did not find param CoG_y");
127  if(params_.CoG_z == 0)
128  ROS_DEBUG("GravityCompensator did not find param CoG_z");
129  if(params_.force == 0)
130  ROS_DEBUG("GravityCompensator did not find param force");
131 
132  world_frame_ = params_.world_frame;
133  sensor_frame_ = params_.sensor_frame;
134  cog_.vector.x = params_.CoG_x;
135  cog_.vector.y = params_.CoG_y;
136  cog_.vector.z = params_.CoG_z;
137  force_z_ = params_.force;
138 
139  ROS_INFO("Gravity Compensation Params: world_frame: %s; sensor_frame: %s; CoG_x:%f; CoG_y:%f; CoG_z:%f; force: %f." , world_frame_.c_str(), sensor_frame_.c_str(),
140  cog_.vector.x,cog_. vector.y,cog_. vector.z,force_z_);
141 
143  p_tf_Listener = new tf2_ros::TransformListener(*p_tf_Buffer_,true);
145 
146  return true;
147 }
148 template <typename T>
149 bool GravityCompensator<T>::update(const T & data_in, T& data_out)
150 {
151  try
152  {
153  transform_ = p_tf_Buffer_->lookupTransform(world_frame_, data_in.header.frame_id, ros::Time(0));
154  transform_back_ = p_tf_Buffer_->lookupTransform(data_in.header.frame_id, world_frame_, ros::Time(0));
156  }
157  catch (tf2::TransformException ex)
158  {
159  if (_num_transform_errors%100 == 0){
160  ROS_ERROR("%s", ex.what());
161  }
163  }
164 
165  geometry_msgs::Vector3Stamped temp_force_transformed, temp_torque_transformed, temp_vector_in, temp_vector_out;
166 
167  temp_vector_in.vector = data_in.wrench.force;
168  tf2::doTransform(temp_vector_in, temp_force_transformed, transform_);
169 
170  temp_vector_in.vector = data_in.wrench.torque;
171  tf2::doTransform(temp_vector_in, temp_torque_transformed, transform_);
172 
173  // Transform CoG Vector
174  geometry_msgs::Vector3Stamped cog_transformed;
175  tf2::doTransform(cog_, cog_transformed, transform_);
176 
177  // Compensate for gravity force
178  temp_force_transformed.vector.z += force_z_;
179  // Compensation Values for torque result from Crossprod of cog Vector and (0 0 G)
180  temp_torque_transformed.vector.x += (force_z_ * cog_transformed.vector.y);
181  temp_torque_transformed.vector.y -= (force_z_ * cog_transformed.vector.x);
182 
183  // Copy Message and Compensate values for Gravity Force and Resulting Torque
184  //geometry_msgs::WrenchStamped compensated_wrench;
185  data_out = data_in;
186 
187  tf2::doTransform(temp_force_transformed, temp_vector_out, transform_back_);
188  data_out.wrench.force = temp_vector_out.vector;
189 
190  tf2::doTransform(temp_torque_transformed, temp_vector_out, transform_back_);
191  data_out.wrench.torque = temp_vector_out.vector;
192  return true;
193 }
194 
195 template <typename T>
196 void GravityCompensator<T>::reconfigureConfigurationRequest(iirob_filters::GravityCompensationConfig& config, uint32_t level)
197 {
198  //params_.fromConfig(config);
199  world_frame_ = params_.world_frame;
200  sensor_frame_ = params_.sensor_frame;
201  cog_.vector.x = params_.CoG_x;
202  cog_.vector.y = params_.CoG_y;
203  cog_.vector.z = params_.CoG_z;
204  force_z_ = params_.force;
205 
206 
207 };
208 
209 }
210 #endif
211 
void reconfigureConfigurationRequest(iirob_filters::GravityCompensationConfig &config, uint32_t level)
virtual bool update(const T &data_in, T &data_out)
Update the filter and return the data seperately.
virtual geometry_msgs::TransformStamped lookupTransform(const std::string &target_frame, const std::string &source_frame, const ros::Time &time, const ros::Duration timeout) const
geometry_msgs::Vector3Stamped cog_
void doTransform(const T &data_in, T &data_out, const geometry_msgs::TransformStamped &transform)
geometry_msgs::TransformStamped transform_
tf2_ros::TransformListener * p_tf_Listener
#define ROS_INFO(...)
~GravityCompensator()
Destructor to clean up.
dynamic_reconfigure::Server< iirob_filters::GravityCompensationConfig > reconfigCalibrationSrv_
iirob_filters::GravityCompensationParameters params_
#define ROS_ERROR(...)
geometry_msgs::TransformStamped transform_back_
#define ROS_DEBUG(...)


iirob_filters
Author(s): Denis Štogl
autogenerated on Fri Sep 18 2020 03:32:19