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 
43 {
44  std::unique_lock lock(historyMutex);
45 
46  if (dataHistory.size() == 0)
47  {
48  return NULL;
49  }
50 
51  VariantPtr var = VariantPtr::dynamicCast(dataHistory.begin()->second);
52  VariantTypeId type = var->getType();
53 
54  if (type == VariantType::Vector3)
55  {
56  Vector3Ptr vecVar = new Vector3(currentValue);
57  return new Variant(vecVar);
58  }
59  else if (type == VariantType::FramedDirection)
60  {
61  FramedDirectionPtr p = var->get<FramedDirection>();
62  FramedDirectionPtr vecVar = new FramedDirection(currentValue, p->frame, p->agent);
63  return new Variant(vecVar);
64  }
65  else if (type == VariantType::FramedPosition)
66  {
67  FramedPositionPtr p = var->get<FramedPosition>();
68  FramedPositionPtr vecVar = new FramedPosition(currentValue, p->frame, p->agent);
69  return new Variant(vecVar);
70  }
71  else
72  {
73  ARMARX_WARNING_S << "Unsupported Variant Type: " << var->getTypeName();
74  return NULL;
75  }
76 }
77 
78 armarx::ParameterTypeList
80 {
81  ParameterTypeList result = MedianFilter::getSupportedTypes(c);
82  result.push_back(VariantType::Vector3);
83  result.push_back(VariantType::FramedDirection);
84  result.push_back(VariantType::FramedPosition);
85  return result;
86 }
87 
88 float
89 armarx::filters::PoseMedianOffsetFilter::median(std::vector<float>& values)
90 {
91  std::sort(values.begin(), values.end());
92  return values.size() % 2 == 0
93  ? (values.at(values.size() / 2 - 1) + values.at(values.size() / 2)) / 2
94  : values.at(values.size() / 2);
95 }
96 
97 Eigen::Vector3f
98 armarx::filters::PoseMedianOffsetFilter::calculateMedian()
99 {
100  Eigen::Vector3f result;
101  for (int i = 0; i < 3; ++i)
102  {
103  std::vector<float> values;
104  values.reserve(data.size());
105 
106  for (const Eigen::Vector3f& v : data)
107  {
108  values.push_back(v(i));
109  }
110  result(i) = median(values);
111  }
112  return result;
113 }
114 
115 void
118  const Ice::Current& c)
119 {
120  VariantTypeId type = value->getType();
121  if (type == VariantType::Vector3 || type == VariantType::FramedDirection ||
123  {
124  Eigen::Vector3f currentValue = VariantPtr::dynamicCast(value)->get<Vector3>()->toEigen();
125  if (dataIndex < 0)
126  {
127  data.push_back(currentValue);
128  this->currentValue = Eigen::Vector3f::Zero();
129  dataIndex++;
130  if (dataIndex == 0)
131  {
132  offset = calculateMedian();
133  }
134  }
135  else
136  {
137  data.at(dataIndex) = currentValue;
138  dataIndex = (dataIndex + 1) % windowFilterSize;
139  this->currentValue = calculateMedian() - offset;
140  }
141  }
142  else
143  {
144  ARMARX_WARNING_S << "Unsupported Variant Type: " << value->getTypeName();
145  }
147 }
armarx::Variant
The Variant class is described here: Variants.
Definition: Variant.h:223
armarx::filters::PoseMedianOffsetFilter::getSupportedTypes
ParameterTypeList getSupportedTypes(const Ice::Current &c) const override
This filter supports: Vector3, FramedDirection, FramedPosition.
Definition: PoseMedianOffsetFilter.cpp:79
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:116
armarx::VariantType::Vector3
const VariantTypeId Vector3
Definition: Pose.h:38
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:46
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:41
armarx::filters::MedianFilter
The MedianFilter class provides an implementation for a median for datafields of type float,...
Definition: MedianFilter.h:47
armarx::filters::MedianFilter::getSupportedTypes
ParameterTypeList getSupportedTypes(const Ice::Current &c=Ice::emptyCurrent) const override
This filter supports: Int, Long, Float, Double.
Definition: MedianFilter.cpp:69
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:855
armarx::toEigen
Eigen::Vector3f toEigen(const pcl::PointXYZ &pt)
Definition: PointToModelICP.h:67
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
armarx::FramedPosition
The FramedPosition class.
Definition: FramedPose.h:157
armarx::Vector3
The Vector3 class.
Definition: Pose.h:112
armarx::VariantType::Long
const VariantTypeId Long
Definition: Variant.h:918
timestamp
std::string timestamp()
Definition: CartographerAdapter.cpp:85
armarx::VariantTypeId
Ice::Int VariantTypeId
Definition: Variant.h:43
ARMARX_WARNING_S
#define ARMARX_WARNING_S
Definition: Logging.h:213
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:86
armarx::VariantType::FramedDirection
const VariantTypeId FramedDirection
Definition: FramedPose.h:37
armarx::filters::PoseMedianOffsetFilter::calculate
VariantBasePtr calculate(const Ice::Current &c) const override
Definition: PoseMedianOffsetFilter.cpp:42
memoryx::KBM::Vector3
Eigen::Vector3d Vector3
Definition: kbm.h:43
Logging.h
armarx::VariantType::FramedPosition
const VariantTypeId FramedPosition
Definition: FramedPose.h:38
PoseMedianOffsetFilter.h
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27