MedianDerivativeFilterV3.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
19  * @author
20  * @date
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
26 
28 
29 
30 using namespace armarx;
31 using namespace filters;
32 
33 armarx::filters::MedianDerivativeFilterV3::MedianDerivativeFilterV3(std::size_t offsetWindowSize, std::size_t windowSize)
34  : MedianFilter(windowSize)
35 {
36  this->valueIndex = 0;
37  this->offsetIndex = 0;
38  this->currentValue = Eigen::Vector3f::Zero();
39  this->currentOffset = Eigen::Vector3f::Zero();
40 
41  this->offsetWindowSize = offsetWindowSize;
42 }
43 
45 {
46  std::unique_lock lock(historyMutex);
47 
48  if (dataHistory.size() == 0)
49  {
50  return NULL;
51  }
52 
53  VariantPtr var = VariantPtr::dynamicCast(dataHistory.begin()->second);
54  VariantTypeId type = var->getType();
55 
56  if (type == VariantType::Vector3)
57  {
58  Vector3Ptr vecVar = new Vector3(currentValue);
59  return new Variant(vecVar);
60  }
61  else if (type == VariantType::FramedDirection)
62  {
63  FramedDirectionPtr p = var->get<FramedDirection>();
64  FramedDirectionPtr vecVar = new FramedDirection(currentValue, p->frame, p->agent);
65  return new Variant(vecVar);
66  }
67  else if (type == VariantType::FramedPosition)
68  {
69  FramedPositionPtr p = var->get<FramedPosition>();
70  FramedPositionPtr vecVar = new FramedPosition(currentValue, p->frame, p->agent);
71  return new Variant(vecVar);
72  }
73  else
74  {
75  ARMARX_WARNING_S << "Unsupported Variant Type: " << var->getTypeName();
76  return NULL;
77  }
78 
79 }
80 
81 armarx::ParameterTypeList armarx::filters::MedianDerivativeFilterV3::getSupportedTypes(const Ice::Current& c) const
82 {
83  ParameterTypeList result = MedianFilter::getSupportedTypes(c);
84  result.push_back(VariantType::Vector3);
85  result.push_back(VariantType::FramedDirection);
86  result.push_back(VariantType::FramedPosition);
87  return result;
88 }
89 
90 float armarx::filters::MedianDerivativeFilterV3::median(std::vector<float>& values)
91 {
92  std::sort(values.begin(), values.end());
93  return values.size() % 2 == 0 ? (values.at(values.size() / 2 - 1) + values.at(values.size() / 2)) / 2 : values.at(values.size() / 2);
94 }
95 
96 Eigen::Vector3f armarx::filters::MedianDerivativeFilterV3::calculateMedian(const std::vector<Eigen::Vector3f>& data)
97 {
98  // increase cache-efficiency by iterating over data one time, storing all three vectors
99  std::vector<float> values[3];
100  for (int i = 0; i < 3; ++i)
101  {
102  values[i].reserve(data.size());
103  }
104  for (const Eigen::Vector3f& v : data)
105  {
106  for (int i = 0; i < 3; ++i)
107  {
108  values[i].push_back(v(i));
109  }
110  }
111 
112  Eigen::Vector3f result;
113  for (int i = 0; i < 3; ++i)
114  {
115  result(i) = median(values[i]);
116  }
117  return result;
118 }
119 
121 {
122  VariantTypeId type = value->getType();
124  {
125  Eigen::Vector3f inputValue = VariantPtr::dynamicCast(value)->get<Vector3>()->toEigen();
126 
127  bool offsetValid = false;
128  if (offsetData.size() < unsigned(offsetWindowSize))
129  {
130  offsetData.push_back(inputValue);
131  }
132  else
133  {
134  offsetData[offsetIndex] = inputValue;
135  offsetIndex = (offsetIndex + 1) % offsetWindowSize;
136  this->currentOffset = calculateMedian(offsetData);
137  offsetValid = true;
138  }
139 
140  if (valueData.size() < unsigned(windowFilterSize))
141  {
142  valueData.push_back(inputValue);
143  }
144  else
145  {
146  valueData[valueIndex] = inputValue;
147  valueIndex = (valueIndex + 1) % windowFilterSize;
148  }
149 
150  // Keep currentValue at zero while currentOffset is invalid.
151  if (offsetValid)
152  {
153  // otherwise, compute it
154  this->currentValue = calculateMedian(valueData) - currentOffset;
155  }
156  }
157  else
158  {
159  ARMARX_WARNING_S << "Unsupported Variant Type: " << value->getTypeName();
160  }
161  DatafieldFilter::update(timestamp, value, c);
162 }
armarx::Variant
The Variant class is described here: Variants.
Definition: Variant.h:224
armarx::filters::MedianDerivativeFilterV3::getSupportedTypes
ParameterTypeList getSupportedTypes(const Ice::Current &c) const override
This filter supports: Vector3, FramedDirection, FramedPosition.
Definition: MedianDerivativeFilterV3.cpp:81
armarx::viz::toEigen
Eigen::Matrix4f toEigen(data::GlobalPose const &pose)
Definition: Interaction.h:46
ProsthesisInterface.values
values
Definition: ProsthesisInterface.py:190
armarx::VariantType::Vector3
const VariantTypeId Vector3
Definition: Pose.h:38
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
IceInternal::Handle<::armarx::VariantBase >
armarx::DatafieldFilter::update
void update(Ice::Long timestamp, const VariantBasePtr &value, const Ice::Current &c=Ice::emptyCurrent) override
Adds the given value to the data map, erases old values if maximum size was reached,...
Definition: DatafieldFilter.cpp:44
armarx::filters::MedianFilter
The MedianFilter class provides an implementation for a median for datafields of type float,...
Definition: MedianFilter.h:46
armarx::filters::MedianFilter::getSupportedTypes
ParameterTypeList getSupportedTypes(const Ice::Current &c=Ice::emptyCurrent) const override
This filter supports: Int, Long, Float, Double.
Definition: MedianFilter.cpp:67
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
MedianDerivativeFilterV3.h
armarx::filters::MedianDerivativeFilterV3::update
void update(Ice::Long timestamp, const VariantBasePtr &value, const Ice::Current &c) override
Definition: MedianDerivativeFilterV3.cpp:120
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
armarx::FramedPosition
The FramedPosition class.
Definition: FramedPose.h:142
armarx::filters::MedianDerivativeFilterV3::calculate
VariantBasePtr calculate(const Ice::Current &c) const override
Definition: MedianDerivativeFilterV3.cpp:44
armarx::Vector3
The Vector3 class.
Definition: Pose.h:112
armarx::VariantType::Long
const VariantTypeId Long
Definition: Variant.h:917
armarx::VariantTypeId
Ice::Int VariantTypeId
Definition: Variant.h:44
ARMARX_WARNING_S
#define ARMARX_WARNING_S
Definition: Logging.h:206
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
armarx::FramedDirection
FramedDirection is a 3 dimensional direction vector with a reference frame. The reference frame can b...
Definition: FramedPose.h:83
armarx::VariantType::FramedDirection
const VariantTypeId FramedDirection
Definition: FramedPose.h:38
memoryx::KBM::Vector3
Eigen::Vector3d Vector3
Definition: kbm.h:41
Logging.h
armarx::VariantType::FramedPosition
const VariantTypeId FramedPosition
Definition: FramedPose.h:39
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::filters::MedianDerivativeFilterV3::MedianDerivativeFilterV3
MedianDerivativeFilterV3(std::size_t offsetWindowSize=1001, std::size_t windowSize=11)
Definition: MedianDerivativeFilterV3.cpp:33