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 <mutex>
28 
33 
34 #include <RobotAPI/interface/units/HapticUnit.h>
35 
36 namespace armarx
37 {
38  /**
39  * \class HapticObserverPropertyDefinitions
40  * \brief
41  */
43  {
44  public:
46  {
47  defineOptionalProperty<std::string>(
48  "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
67  add(long timestamp)
68  {
69  long delta = timestamp - lastTimestamp;
70 
71  if (deltas.size() < count)
72  {
73  deltas.push_back(delta);
74  }
75  else
76  {
77  deltas.at(pos) = delta;
78  pos = (pos + 1) % count;
79  }
80 
81  lastTimestamp = timestamp;
82  }
83 
84  /*long average(long timestamp)
85  {
86  long sum = timestamp - lastTimestamp;
87  for(std::vector<long>::iterator it = deltas.begin(); it != deltas.end(); ++it)
88  {
89  sum += *it;
90  }
91  return sum / (deltas.size() + 1);
92  }*/
93 
94  long
96  {
97  if (deltas.size() == 0)
98  {
99  return 0;
100  }
101 
102  long sum = 0;
103 
104  for (std::vector<long>::iterator it = deltas.begin(); it != deltas.end(); ++it)
105  {
106  sum += *it;
107  }
108 
109  return sum / deltas.size();
110  }
111 
112  private:
113  long lastTimestamp;
114  unsigned int count;
115  int pos;
116  std::vector<long> deltas;
117  };
118 
119  /**
120  * \class HapticObserver
121  * \ingroup RobotAPI-SensorActorUnits-observers
122  * \brief Observer monitoring haptic sensor values
123  *
124  * The HapticObserver monitors haptic sensor values published by HapticUnit-implementations and offers condition checks on these values.
125  * Available condition checks are: *updated*, *larger*, *equals* and *smaller*.
126  */
127  class HapticObserver : virtual public Observer, virtual public HapticUnitObserverInterface
128  {
129  public:
130  HapticObserver();
131 
132  void setTopicName(std::string topicName);
133 
134  // framework hooks
135  std::string
136  getDefaultName() const override
137  {
138  return "HapticUnitObserver";
139  }
140 
141  void onInitObserver() override;
142  void onConnectObserver() override;
143  void onExitObserver() override;
144 
145  void reportSensorValues(const ::std::string& device,
146  const ::std::string& name,
147  const ::armarx::MatrixFloatBasePtr& values,
148  const ::armarx::TimestampBasePtr& timestamp,
149  const ::Ice::Current& = Ice::emptyCurrent) override;
150 
151  /**
152  * \see PropertyUser::createPropertyDefinitions()
153  */
155 
156  private:
157  std::mutex dataMutex;
158  std::string topicName;
160 
161  void updateStatistics();
162 
163  std::map<std::string, HapticSampleStatistics> statistics;
164  };
165 } // namespace armarx
armarx::HapticObserverPropertyDefinitions
Definition: HapticObserver.h:42
armarx::Observer
Baseclass for all ArmarX Observers.
Definition: Observer.h:84
armarx::HapticObserver::getDefaultName
std::string getDefaultName() const override
Retrieve default name of component.
Definition: HapticObserver.h:136
MatrixVariant.h
armarx::HapticObserver::onInitObserver
void onInitObserver() override
Framework hook.
Definition: HapticObserver.cpp:53
armarx::PropertyDefinitionContainer::prefix
std::string prefix
Prefix of the properties such as namespace, domain, component name, etc.
Definition: PropertyDefinitionContainer.h:345
ProsthesisInterface.values
values
Definition: ProsthesisInterface.py:190
PeriodicTask.h
armarx::HapticObserver::setTopicName
void setTopicName(std::string topicName)
Definition: HapticObserver.cpp:47
armarx::HapticObserverPropertyDefinitions::HapticObserverPropertyDefinitions
HapticObserverPropertyDefinitions(std::string prefix)
Definition: HapticObserver.h:45
Observer.h
armarx::HapticSampleStatistics::add
void add(long timestamp)
Definition: HapticObserver.h:67
armarx::HapticSampleStatistics::HapticSampleStatistics
HapticSampleStatistics(unsigned int count, long timestamp)
Definition: HapticObserver.h:59
armarx::HapticObserver::createPropertyDefinitions
PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: HapticObserver.cpp:169
armarx::HapticSampleStatistics
Definition: HapticObserver.h:56
armarx::HapticObserver::onConnectObserver
void onConnectObserver() override
Framework hook.
Definition: HapticObserver.cpp:72
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:84
armarx::deltas
Definition: BasicControllers.h:57
armarx::HapticSampleStatistics::average
long average()
Definition: HapticObserver.h:95
armarx::HapticObserver::onExitObserver
void onExitObserver() override
Framework hook.
Definition: HapticObserver.cpp:78
armarx::HapticObserver
Observer monitoring haptic sensor values.
Definition: HapticObserver.h:127
IceUtil::Handle
Definition: forward_declarations.h:30
armarx::ObserverPropertyDefinitions
Definition: Observer.h:49
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::HapticObserver::HapticObserver
HapticObserver()
Definition: HapticObserver.cpp:40