DerivationFilter.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package ArmarX
19  * @author Mirko Waechter( mirko.waechter at kit dot edu)
20  * @date 2016
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
25 #include "DerivationFilter.h"
26 
27 #include <iterator>
28 
30 
31 namespace armarx::filters
32 {
33 
35  {
36  windowFilterSize = 2;
37  }
38 
40  DerivationFilter::calculate(const Ice::Current& c) const
41  {
42  std::unique_lock lock(historyMutex);
43  if (dataHistory.size() == 0)
44  {
45  return nullptr;
46  }
47 
48  VariantTypeId type = dataHistory.begin()->second->getType();
49  double diff = 0;
50 
51  double deriv = 0;
52  for (auto it = std::next(dataHistory.begin()); it != dataHistory.end(); it++)
53  {
54  auto itPrev = std::prev(it);
55  if (type == VariantType::Float)
56  {
57  diff = (it->second->getFloat() - itPrev->second->getFloat());
58  }
59  else if (type == VariantType::Double)
60  {
61  diff = (it->second->getDouble() - itPrev->second->getDouble());
62  }
63  else if (type == VariantType::Int)
64  {
65  diff = (it->second->getInt() - itPrev->second->getInt());
66  }
67  else if (type == VariantType::Long)
68  {
69  diff = (it->second->getLong() - itPrev->second->getLong());
70  }
71  double deltaT = it->first - itPrev->first;
72  if (deltaT > 0)
73  {
74  deriv += diff / (deltaT * 0.000001);
75  }
76  }
77 
78  deriv /= dataHistory.size() - 1;
79 
80 
81  if (type == VariantType::Float)
82  {
83  return new Variant((float)deriv);
84  }
85  else if (type == VariantType::Double)
86  {
87  return new Variant(deriv);
88  }
89  else if (type == VariantType::Int)
90  {
91  return new Variant((int)deriv);
92  }
93  else if (type == VariantType::Long)
94  {
95  return new Variant((long)deriv);
96  }
97 
99  }
100 
101  ParameterTypeList
102  DerivationFilter::getSupportedTypes(const Ice::Current& c) const
103  {
104  ParameterTypeList result;
105  result.push_back(VariantType::Int);
106  result.push_back(VariantType::Long);
107  result.push_back(VariantType::Float);
108  result.push_back(VariantType::Double);
109  return result;
110  }
111 
112  StringFloatDictionary
113  DerivationFilter::getProperties(const Ice::Current& c) const
114  {
115  return StringFloatDictionary{{"minSampleTimeDelta", minSampleTimeDelta},
116  {"windowFilterSize", windowFilterSize}};
117  }
118 
119  void
120  DerivationFilter::setProperties(const StringFloatDictionary& newValues, const Ice::Current& c)
121  {
122  auto it = newValues.find("minSampleTimeDelta");
123  if (it != newValues.end())
124  {
125  minSampleTimeDelta = it->second;
126  }
127  it = newValues.find("windowFilterSize");
128  if (it != newValues.end())
129  {
130  windowFilterSize = it->second;
131  }
132  }
133 
134 } // namespace armarx::filters
DerivationFilter.h
armarx::Variant
The Variant class is described here: Variants.
Definition: Variant.h:223
armarx::VariantType::Float
const VariantTypeId Float
Definition: Variant.h:919
armarx::filters::DerivationFilter::getProperties
armarx::StringFloatDictionary getProperties(const Ice::Current &c=Ice::emptyCurrent) const override
Definition: DerivationFilter.cpp:113
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:46
IceInternal::Handle<::armarx::VariantBase >
armarx::filters
Definition: AverageFilter.h:32
InvalidTypeException.h
armarx::VariantType::Double
const VariantTypeId Double
Definition: Variant.h:920
armarx::filters::DerivationFilter::setProperties
void setProperties(const armarx::StringFloatDictionary &newValues, const Ice::Current &c=Ice::emptyCurrent) override
Definition: DerivationFilter.cpp:120
armarx::filters::DerivationFilter::calculate
VariantBasePtr calculate(const Ice::Current &c=Ice::emptyCurrent) const override
Definition: DerivationFilter.cpp:40
armarx::VariantType::Long
const VariantTypeId Long
Definition: Variant.h:918
armarx::exceptions::user::UnsupportedTypeException
Definition: InvalidTypeException.h:76
armarx::VariantTypeId
Ice::Int VariantTypeId
Definition: Variant.h:43
armarx::filters::DerivationFilter::getSupportedTypes
ParameterTypeList getSupportedTypes(const Ice::Current &c=Ice::emptyCurrent) const override
This filter supports: Int, Long, Float, Double.
Definition: DerivationFilter.cpp:102
armarx::DatafieldFilter::dataHistory
TimeVariantBaseMap dataHistory
Definition: DatafieldFilter.h:83
armarx::DatafieldFilter::historyMutex
std::mutex historyMutex
Definition: DatafieldFilter.h:84
armarx::VariantType::Int
const VariantTypeId Int
Definition: Variant.h:917
armarx::filters::DerivationFilter::DerivationFilter
DerivationFilter()
Definition: DerivationFilter.cpp:34