PoseMedianOffsetFilter.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 
25 #include "PoseMedianOffsetFilter.h"
26 
28 
29 using namespace armarx;
30 using namespace filters;
31 
33  : MedianFilter(windowSize)
34 {
35  this->windowFilterSize = windowSize;
36  this->dataIndex = -windowSize;
37  this->offset = Eigen::Vector3f::Zero();
38  this->currentValue = Eigen::Vector3f::Zero();
39 }
40 
42 {
43  std::unique_lock lock(historyMutex);
44 
45  if (dataHistory.size() == 0)
46  {
47  return NULL;
48  }
49 
50  VariantPtr var = VariantPtr::dynamicCast(dataHistory.begin()->second);
51  VariantTypeId type = var->getType();
52 
53  if (type == VariantType::Vector3)
54  {
55  Vector3Ptr vecVar = new Vector3(currentValue);
56  return new Variant(vecVar);
57  }
58  else if (type == VariantType::FramedDirection)
59  {
60  FramedDirectionPtr p = var->get<FramedDirection>();
61  FramedDirectionPtr vecVar = new FramedDirection(currentValue, p->frame, p->agent);
62  return new Variant(vecVar);
63  }
64  else if (type == VariantType::FramedPosition)
65  {
66  FramedPositionPtr p = var->get<FramedPosition>();
67  FramedPositionPtr vecVar = new FramedPosition(currentValue, p->frame, p->agent);
68  return new Variant(vecVar);
69  }
70  else
71  {
72  ARMARX_WARNING_S << "Unsupported Variant Type: " << var->getTypeName();
73  return NULL;
74  }
75 
76 }
77 
78 armarx::ParameterTypeList armarx::filters::PoseMedianOffsetFilter::getSupportedTypes(const Ice::Current& c) const
79 {
80  ParameterTypeList result = MedianFilter::getSupportedTypes(c);
81  result.push_back(VariantType::Vector3);
82  result.push_back(VariantType::FramedDirection);
83  result.push_back(VariantType::FramedPosition);
84  return result;
85 }
86 
87 float armarx::filters::PoseMedianOffsetFilter::median(std::vector<float>& values)
88 {
89  std::sort(values.begin(), values.end());
90  return values.size() % 2 == 0 ? (values.at(values.size() / 2 - 1) + values.at(values.size() / 2)) / 2 : values.at(values.size() / 2);
91 }
92 
93 Eigen::Vector3f armarx::filters::PoseMedianOffsetFilter::calculateMedian()
94 {
95  Eigen::Vector3f result;
96  for (int i = 0; i < 3; ++i)
97  {
98  std::vector<float> values;
99  values.reserve(data.size());
100 
101  for (const Eigen::Vector3f& v : data)
102  {
103  values.push_back(v(i));
104  }
105  result(i) = median(values);
106  }
107  return result;
108 }
109 
111 {
112  VariantTypeId type = value->getType();
114  {
115  Eigen::Vector3f currentValue = VariantPtr::dynamicCast(value)->get<Vector3>()->toEigen();
116  if (dataIndex < 0)
117  {
118  data.push_back(currentValue);
119  this->currentValue = Eigen::Vector3f::Zero();
120  dataIndex++;
121  if (dataIndex == 0)
122  {
123  offset = calculateMedian();
124  }
125  }
126  else
127  {
128  data.at(dataIndex) = currentValue;
129  dataIndex = (dataIndex + 1) % windowFilterSize;
130  this->currentValue = calculateMedian() - offset;
131  }
132  }
133  else
134  {
135  ARMARX_WARNING_S << "Unsupported Variant Type: " << value->getTypeName();
136  }
137  DatafieldFilter::update(timestamp, value, c);
138 }
armarx::Variant
The Variant class is described here: Variants.
Definition: Variant.h:224
armarx::viz::toEigen
Eigen::Matrix4f toEigen(data::GlobalPose const &pose)
Definition: Interaction.h:46
armarx::filters::PoseMedianOffsetFilter::getSupportedTypes
ParameterTypeList getSupportedTypes(const Ice::Current &c) const override
This filter supports: Vector3, FramedDirection, FramedPosition.
Definition: PoseMedianOffsetFilter.cpp:78
ProsthesisInterface.values
values
Definition: ProsthesisInterface.py:190
armarx::filters::PoseMedianOffsetFilter::update
void update(Ice::Long timestamp, const VariantBasePtr &value, const Ice::Current &c) override
Definition: PoseMedianOffsetFilter.cpp:110
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
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
armarx::FramedPosition
The FramedPosition class.
Definition: FramedPose.h:142
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::filters::PoseMedianOffsetFilter::PoseMedianOffsetFilter
PoseMedianOffsetFilter(int windowSize=11)
Definition: PoseMedianOffsetFilter.cpp:32
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
armarx::filters::PoseMedianOffsetFilter::calculate
VariantBasePtr calculate(const Ice::Current &c) const override
Definition: PoseMedianOffsetFilter.cpp:41
memoryx::KBM::Vector3
Eigen::Vector3d Vector3
Definition: kbm.h:41
Logging.h
armarx::VariantType::FramedPosition
const VariantTypeId FramedPosition
Definition: FramedPose.h:39
PoseMedianOffsetFilter.h
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28