OffsetFilter.cpp
Go to the documentation of this file.
1 #include "OffsetFilter.h"
2 
4 
7 
8 
9 namespace armarx::filters
10 {
11 
13  {
14  firstRun = true;
15  windowFilterSize = 1;
16  }
17 
18 
19  VariantBasePtr OffsetFilter::calculate(const Ice::Current&) const
20  {
21  std::unique_lock lock(historyMutex);
22 
23  VariantPtr newVariant;
24 
25  if (initialValue && dataHistory.size() > 0)
26  {
27  VariantTypeId type = dataHistory.begin()->second->getType();
28  VariantPtr currentValue = VariantPtr::dynamicCast(dataHistory.rbegin()->second);
29 
30  if (currentValue->getType() != initialValue->getType())
31  {
32  ARMARX_ERROR_S << "Types in OffsetFilter are different: " << Variant::typeToString(currentValue->getType()) << " and " << Variant::typeToString(initialValue->getType());
33  return nullptr;
34  }
35 
36  if (type == VariantType::Int)
37  {
38  int newValue = dataHistory.rbegin()->second->getInt() - initialValue->getInt();
39  newVariant = new Variant(newValue);
40  }
41  else if (type == VariantType::Long)
42  {
43  long newValue = dataHistory.rbegin()->second->getLong() - initialValue->getLong();
44  newVariant = new Variant(newValue);
45  }
46  else if (type == VariantType::Float)
47  {
48  float newValue = dataHistory.rbegin()->second->getFloat() - initialValue->getFloat();
49  newVariant = new Variant(newValue);
50  }
51  else if (type == VariantType::Double)
52  {
53  double newValue = dataHistory.rbegin()->second->getDouble() - initialValue->getDouble();
54  newVariant = new Variant(newValue);
55  }
56  else if (type == VariantType::FramedDirection)
57  {
58  FramedDirectionPtr vec = FramedDirectionPtr::dynamicCast(currentValue->get<FramedDirection>());
59  FramedDirectionPtr intialVec = FramedDirectionPtr::dynamicCast(initialValue->get<FramedDirection>());
60  Eigen::Vector3f newValue = vec->toEigen() - intialVec->toEigen();
61 
62  newVariant = new Variant(new FramedDirection(newValue, vec->frame, vec->agent));
63  }
64  else if (type == VariantType::FramedPosition)
65  {
66  FramedPositionPtr pos = FramedPositionPtr::dynamicCast(currentValue->get<FramedPosition>());
67  FramedPositionPtr intialPos = FramedPositionPtr::dynamicCast(initialValue->get<FramedPosition>());
68  Eigen::Vector3f newValue = pos->toEigen() - intialPos->toEigen();
69  newVariant = new Variant(new FramedPosition(newValue, pos->frame, pos->agent));
70  }
71  else if (type == VariantType::MatrixFloat)
72  {
73  MatrixFloatPtr matrix = MatrixFloatPtr::dynamicCast(currentValue->get<MatrixFloat>());
74  MatrixFloatPtr initialMatrix = MatrixFloatPtr::dynamicCast(initialValue->get<MatrixFloat>());
75  Eigen::MatrixXf newMatrix = matrix->toEigen() - initialMatrix->toEigen();
76  newVariant = new Variant(new MatrixFloat(newMatrix));
77  }
78  }
79 
80  return newVariant;
81 
82  }
83 
84  ParameterTypeList OffsetFilter::getSupportedTypes(const Ice::Current&) const
85  {
86  ParameterTypeList result;
87  result.push_back(VariantType::Int);
88  result.push_back(VariantType::Long);
89  result.push_back(VariantType::Float);
90  result.push_back(VariantType::Double);
91  result.push_back(VariantType::FramedDirection);
92  result.push_back(VariantType::FramedPosition);
93  result.push_back(VariantType::MatrixFloat);
94  return result;
95  }
96 
97  void OffsetFilter::update(Ice::Long timestamp, const VariantBasePtr& value, const Ice::Current& c)
98  {
99  DatafieldFilter::update(timestamp, value, c);
100 
101  if (firstRun)
102  {
103  std::unique_lock lock(historyMutex);
104 
105  if (dataHistory.size() == 0)
106  {
107  return;
108  }
109 
110  initialValue = VariantPtr::dynamicCast(dataHistory.begin()->second);
111  firstRun = false;
112  }
113  }
114 
115 }
armarx::Variant
The Variant class is described here: Variants.
Definition: Variant.h:224
armarx::VariantType::Float
const VariantTypeId Float
Definition: Variant.h:918
MatrixVariant.h
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
IceInternal::Handle<::armarx::VariantBase >
armarx::filters
Definition: AverageFilter.h:31
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::VariantType::Double
const VariantTypeId Double
Definition: Variant.h:919
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::filters::OffsetFilter::getSupportedTypes
ParameterTypeList getSupportedTypes(const Ice::Current &) const override
Definition: OffsetFilter.cpp:84
FramedPose.h
ARMARX_ERROR_S
#define ARMARX_ERROR_S
Definition: Logging.h:209
armarx::FramedPosition
The FramedPosition class.
Definition: FramedPose.h:142
armarx::MatrixFloat
The MatrixFloat class.
Definition: MatrixVariant.h:48
armarx::Variant::typeToString
static std::string typeToString(VariantTypeId typeId)
Return the name of the registered type typeId.
Definition: Variant.cpp:732
armarx::VariantType::Long
const VariantTypeId Long
Definition: Variant.h:917
armarx::VariantTypeId
Ice::Int VariantTypeId
Definition: Variant.h:44
OffsetFilter.h
armarx::FramedDirection
FramedDirection is a 3 dimensional direction vector with a reference frame. The reference frame can b...
Definition: FramedPose.h:83
armarx::DatafieldFilter::dataHistory
TimeVariantBaseMap dataHistory
Definition: DatafieldFilter.h:79
armarx::VariantType::FramedDirection
const VariantTypeId FramedDirection
Definition: FramedPose.h:38
armarx::VariantType::MatrixFloat
const VariantTypeId MatrixFloat
Definition: MatrixVariant.h:36
armarx::DatafieldFilter::historyMutex
std::mutex historyMutex
Definition: DatafieldFilter.h:80
armarx::VariantType::Int
const VariantTypeId Int
Definition: Variant.h:916
armarx::filters::OffsetFilter::calculate
VariantBasePtr calculate(const Ice::Current &=Ice::emptyCurrent) const override
Definition: OffsetFilter.cpp:19
Logging.h
armarx::VariantType::FramedPosition
const VariantTypeId FramedPosition
Definition: FramedPose.h:39
armarx::filters::OffsetFilter::update
void update(Ice::Long timestamp, const VariantBasePtr &value, const Ice::Current &c) override
Definition: OffsetFilter.cpp:97
armarx::filters::OffsetFilter::OffsetFilter
OffsetFilter()
Definition: OffsetFilter.cpp:12