HapticObserver.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2012-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 ArmarXCore::units
19  * @author Peter Kaiser <peter dot kaiser at kit dot edu>
20  * @date 2014
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
25 #pragma once
26 
27 #include <RobotAPI/interface/units/HapticUnit.h>
32 
33 #include <mutex>
34 
35 namespace armarx
36 {
37  /**
38  * \class HapticObserverPropertyDefinitions
39  * \brief
40  */
43  {
44  public:
47  {
48  defineOptionalProperty<std::string>("HapticTopicName", "HapticValues", "Name of the HapticUnit Topic");
49  }
50  };
51 
52  /**
53  * \class HapticSampleStatistics
54  * \brief
55  */
57  {
58  public:
59  HapticSampleStatistics(unsigned int count, long timestamp)
60  {
61  this->lastTimestamp = timestamp;
62  this->count = count;
63  this->pos = 0;
64  }
65 
66  void add(long timestamp)
67  {
68  long delta = timestamp - lastTimestamp;
69 
70  if (deltas.size() < count)
71  {
72  deltas.push_back(delta);
73  }
74  else
75  {
76  deltas.at(pos) = delta;
77  pos = (pos + 1) % count;
78  }
79 
80  lastTimestamp = timestamp;
81  }
82 
83  /*long average(long timestamp)
84  {
85  long sum = timestamp - lastTimestamp;
86  for(std::vector<long>::iterator it = deltas.begin(); it != deltas.end(); ++it)
87  {
88  sum += *it;
89  }
90  return sum / (deltas.size() + 1);
91  }*/
92 
93  long average()
94  {
95  if (deltas.size() == 0)
96  {
97  return 0;
98  }
99 
100  long sum = 0;
101 
102  for (std::vector<long>::iterator it = deltas.begin(); it != deltas.end(); ++it)
103  {
104  sum += *it;
105  }
106 
107  return sum / deltas.size();
108  }
109 
110  private:
111  long lastTimestamp;
112  unsigned int count;
113  int pos;
114  std::vector<long> deltas;
115  };
116 
117  /**
118  * \class HapticObserver
119  * \ingroup RobotAPI-SensorActorUnits-observers
120  * \brief Observer monitoring haptic sensor values
121  *
122  * The HapticObserver monitors haptic sensor values published by HapticUnit-implementations and offers condition checks on these values.
123  * Available condition checks are: *updated*, *larger*, *equals* and *smaller*.
124  */
126  virtual public Observer,
127  virtual public HapticUnitObserverInterface
128  {
129  public:
130  HapticObserver();
131 
132  void setTopicName(std::string topicName);
133 
134  // framework hooks
135  std::string getDefaultName() const override
136  {
137  return "HapticUnitObserver";
138  }
139  void onInitObserver() override;
140  void onConnectObserver() override;
141  void onExitObserver() override;
142 
143  void reportSensorValues(const ::std::string& device, const ::std::string& name, const ::armarx::MatrixFloatBasePtr& values, const ::armarx::TimestampBasePtr& timestamp, const ::Ice::Current& = Ice::emptyCurrent) override;
144 
145  /**
146  * \see PropertyUser::createPropertyDefinitions()
147  */
149  private:
150  std::mutex dataMutex;
151  std::string topicName;
153 
154  void updateStatistics();
155 
156  std::map<std::string, HapticSampleStatistics> statistics;
157 
158  };
159 }
160 
armarx::HapticObserverPropertyDefinitions
Definition: HapticObserver.h:41
armarx::Observer
Baseclass for all ArmarX Observers.
Definition: Observer.h:80
armarx::HapticObserver::getDefaultName
std::string getDefaultName() const override
Retrieve default name of component.
Definition: HapticObserver.h:135
MatrixVariant.h
armarx::HapticObserver::onInitObserver
void onInitObserver() override
Framework hook.
Definition: HapticObserver.cpp:49
armarx::PropertyDefinitionContainer::prefix
std::string prefix
Prefix of the properties such as namespace, domain, component name, etc.
Definition: PropertyDefinitionContainer.h:333
ProsthesisInterface.values
values
Definition: ProsthesisInterface.py:190
PeriodicTask.h
armarx::HapticObserver::setTopicName
void setTopicName(std::string topicName)
Definition: HapticObserver.cpp:44
armarx::HapticObserverPropertyDefinitions::HapticObserverPropertyDefinitions
HapticObserverPropertyDefinitions(std::string prefix)
Definition: HapticObserver.h:45
Observer.h
armarx::HapticSampleStatistics::add
void add(long timestamp)
Definition: HapticObserver.h:66
armarx::HapticSampleStatistics::HapticSampleStatistics
HapticSampleStatistics(unsigned int count, long timestamp)
Definition: HapticObserver.h:59
armarx::HapticObserver::createPropertyDefinitions
PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: HapticObserver.cpp:155
armarx::HapticSampleStatistics
Definition: HapticObserver.h:56
armarx::HapticObserver::onConnectObserver
void onConnectObserver() override
Framework hook.
Definition: HapticObserver.cpp:67
Eigen3VariantObjectFactories.h
armarx::HapticObserver::reportSensorValues
void reportSensorValues(const ::std::string &device, const ::std::string &name, const ::armarx::MatrixFloatBasePtr &values, const ::armarx::TimestampBasePtr &timestamp, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: HapticObserver.cpp:77
armarx::deltas
Definition: BasicControllers.h:57
armarx::HapticSampleStatistics::average
long average()
Definition: HapticObserver.h:93
armarx::HapticObserver::onExitObserver
void onExitObserver() override
Framework hook.
Definition: HapticObserver.cpp:72
armarx::HapticObserver
Observer monitoring haptic sensor values.
Definition: HapticObserver.h:125
IceUtil::Handle
Definition: forward_declarations.h:29
armarx::ObserverPropertyDefinitions
Definition: Observer.h:50
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::HapticObserver::HapticObserver
HapticObserver()
Definition: HapticObserver.cpp:39