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"
27 #include <iterator>
28 namespace armarx::filters
29 {
30 
32  {
33  windowFilterSize = 2;
34  }
35 
36  VariantBasePtr DerivationFilter::calculate(const Ice::Current& c) const
37  {
38  std::unique_lock lock(historyMutex);
39  if (dataHistory.size() == 0)
40  {
41  return nullptr;
42  }
43 
44  VariantTypeId type = dataHistory.begin()->second->getType();
45  double diff = 0;
46 
47  double deriv = 0;
48  for (auto it = std::next(dataHistory.begin()); it != dataHistory.end(); it++)
49  {
50  auto itPrev = std::prev(it);
51  if (type == VariantType::Float)
52  {
53  diff = (it->second->getFloat() - itPrev->second->getFloat());
54  }
55  else if (type == VariantType::Double)
56  {
57  diff = (it->second->getDouble() - itPrev->second->getDouble());
58  }
59  else if (type == VariantType::Int)
60  {
61  diff = (it->second->getInt() - itPrev->second->getInt());
62  }
63  else if (type == VariantType::Long)
64  {
65  diff = (it->second->getLong() - itPrev->second->getLong());
66  }
67  double deltaT = it->first - itPrev->first;
68  if (deltaT > 0)
69  {
70  deriv += diff / (deltaT * 0.000001);
71  }
72  }
73 
74  deriv /= dataHistory.size() - 1;
75 
76 
77 
78 
79  if (type == VariantType::Float)
80  {
81  return new Variant((float)deriv);
82  }
83  else if (type == VariantType::Double)
84  {
85  return new Variant(deriv);
86  }
87  else if (type == VariantType::Int)
88  {
89  return new Variant((int)deriv);
90  }
91  else if (type == VariantType::Long)
92  {
93  return new Variant((long)deriv);
94  }
95 
97  }
98 
99  ParameterTypeList DerivationFilter::getSupportedTypes(const Ice::Current& c) const
100  {
101  ParameterTypeList result;
102  result.push_back(VariantType::Int);
103  result.push_back(VariantType::Long);
104  result.push_back(VariantType::Float);
105  result.push_back(VariantType::Double);
106  return result;
107  }
108 
109  StringFloatDictionary DerivationFilter::getProperties(const Ice::Current& c) const
110  {
111  return StringFloatDictionary {{"minSampleTimeDelta", minSampleTimeDelta}, {"windowFilterSize", windowFilterSize}};
112  }
113 
114  void DerivationFilter::setProperties(const StringFloatDictionary& newValues, const Ice::Current& c)
115  {
116  auto it = newValues.find("minSampleTimeDelta");
117  if (it != newValues.end())
118  {
119  minSampleTimeDelta = it->second;
120  }
121  it = newValues.find("windowFilterSize");
122  if (it != newValues.end())
123  {
124  windowFilterSize = it->second;
125  }
126  }
127 
128 }
129 
DerivationFilter.h
armarx::Variant
The Variant class is described here: Variants.
Definition: Variant.h:224
armarx::VariantType::Float
const VariantTypeId Float
Definition: Variant.h:918
armarx::filters::DerivationFilter::getProperties
armarx::StringFloatDictionary getProperties(const Ice::Current &c=Ice::emptyCurrent) const override
Definition: DerivationFilter.cpp:109
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
IceInternal::Handle<::armarx::VariantBase >
armarx::filters
Definition: AverageFilter.h:31
InvalidTypeException.h
armarx::VariantType::Double
const VariantTypeId Double
Definition: Variant.h:919
armarx::filters::DerivationFilter::setProperties
void setProperties(const armarx::StringFloatDictionary &newValues, const Ice::Current &c=Ice::emptyCurrent) override
Definition: DerivationFilter.cpp:114
armarx::filters::DerivationFilter::calculate
VariantBasePtr calculate(const Ice::Current &c=Ice::emptyCurrent) const override
Definition: DerivationFilter.cpp:36
armarx::VariantType::Long
const VariantTypeId Long
Definition: Variant.h:917
armarx::exceptions::user::UnsupportedTypeException
Definition: InvalidTypeException.h:67
armarx::VariantTypeId
Ice::Int VariantTypeId
Definition: Variant.h:44
armarx::filters::DerivationFilter::getSupportedTypes
ParameterTypeList getSupportedTypes(const Ice::Current &c=Ice::emptyCurrent) const override
This filter supports: Int, Long, Float, Double.
Definition: DerivationFilter.cpp:99
armarx::DatafieldFilter::dataHistory
TimeVariantBaseMap dataHistory
Definition: DatafieldFilter.h:79
armarx::DatafieldFilter::historyMutex
std::mutex historyMutex
Definition: DatafieldFilter.h:80
armarx::VariantType::Int
const VariantTypeId Int
Definition: Variant.h:916
armarx::filters::DerivationFilter::DerivationFilter
DerivationFilter()
Definition: DerivationFilter.cpp:31