moving_mean_filter.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: Denis Štogl, email: denis.stogl@kit.edu
9  *
10  * Date of creation: 2016
11  *
12  * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
13  *
14  * Redistribution and use in source and binary forms,
15  * with or without modification, are permitted provided
16  * that the following conditions are met:
17  *
18  * * Redistributions of source code must retain the above copyright
19  * notice, this list of conditions and the following disclaimer.
20  * * Redistributions in binary form must reproduce the above copyright
21  * notice, this list of conditions and the following disclaimer in the
22  * documentation and/or other materials provided with the distribution.
23  * * Neither the name of the copyright holder nor the names of its
24  * contributors may be used to endorse or promote products derived from
25  * this software without specific prior written permission.
26  *
27  * This package is free software: you can redistribute it and/or modify
28  * it under the terms of the GNU Lesser General Public License as published by
29  * the Free Software Foundation, either version 3 of the License, or
30  * (at your option) any later version.
31  *
32  * This package is distributed in the hope that it will be useful,
33  * but WITHOUT ANY WARRANTY; without even the implied warranty of
34  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35  * GNU Lesser General Public License for more details.
36  *
37  * You should have received a copy of the GNU Lesser General Public License
38  * along with this package. If not, see <http://www.gnu.org/licenses/>.
39 *****************************************************************************/
40 
41 #ifndef IIROB_FILTERS_MOVING_MEAN_FILTER_H
42 #define IIROB_FILTERS_MOVING_MEAN_FILTER_H
43 
44 #include <ros/ros.h>
45 #include <geometry_msgs/WrenchStamped.h>
46 #include <iirob_filters/MovingMeanParameters.h>
47 #include <filters/filter_base.h>
48 #include <math.h>
49 namespace iirob_filters{
50 
51 template <typename T>
53 {
54 public:
55 
58 
59  virtual bool configure();
60  virtual bool update(const T & data_in, T& data_out);
61 
62 private:
63  // Parameters
64  iirob_filters::MovingMeanParameters params_;
65  int divider_;
66 
67  // Filter parametrs
69  std::vector<T> values;
70 };
71 
72 template <typename T>
73 MovingMeanFilter<T>::MovingMeanFilter():params_{ros::NodeHandle("~/MovingMeanFilter/params").getNamespace()}
74 {
75 }
76 
77 template <typename T>
79 {
80 }
81 
82 template <typename T>
84 {
85  params_.fromParamServer();
86  divider_ = params_.divider;
87 
88  ROS_INFO("Moving Mean Filter Params: Divider: %d " , divider_);
89 
90  if(divider_ == 0)
91  ROS_ERROR("MovingMeanFilter did not find param divider");
92  return true;
93 }
94 
95 template <typename T>
96 bool MovingMeanFilter<T>::update(const T & data_in, T& data_out)
97 {
98  if (values.size() < divider_) {
99  values.push_back(data_in);
100  data_out = data_in;
101  return true;
102  }
103 
104  values.erase(values.begin());
105  values.push_back(data_in);
106 
107  T sum;
108  sum.wrench.force.x = 0.0;
109  sum.wrench.force.y = 0.0;
110  sum.wrench.force.z = 0.0;
111  sum.wrench.torque.x = 0.0;
112  sum.wrench.torque.y = 0.0;
113  sum.wrench.torque.x = 0.0;
114  for(int i =0 ; i<values.size(); ++i)
115  {
116  sum.wrench.force.x += values[i].wrench.force.x;
117  sum.wrench.force.y += values[i].wrench.force.y;
118  sum.wrench.force.z += values[i].wrench.force.z;
119  sum.wrench.torque.x += values[i].wrench.torque.x;
120  sum.wrench.torque.y += values[i].wrench.torque.y;
121  sum.wrench.torque.z += values[i].wrench.torque.z;
122 
123  }
124  data_out.wrench.force.x = sum.wrench.force.x / values.size();
125  data_out.wrench.force.y = sum.wrench.force.y / values.size();
126  data_out.wrench.force.z = sum.wrench.force.z / values.size();
127  data_out.wrench.torque.x = sum.wrench.torque.x / values.size();
128  data_out.wrench.torque.y = sum.wrench.torque.y / values.size();
129  data_out.wrench.torque.z = sum.wrench.torque.z / values.size();
130 
131  return true;
132 };
133 }
134 
135 #endif
136 
137 
virtual bool update(const T &data_in, T &data_out)
#define ROS_INFO(...)
iirob_filters::MovingMeanParameters params_
#define ROS_ERROR(...)


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