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
36namespace armarx
37{
38 /**
39 * \class HapticObserverPropertyDefinitions
40 * \brief
41 */
43 {
44 public:
46 {
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
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:
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
std::string timestamp()
HapticObserverPropertyDefinitions(std::string prefix)
void onConnectObserver() override
Framework hook.
void setTopicName(std::string topicName)
void onExitObserver() override
Framework hook.
void reportSensorValues(const ::std::string &device, const ::std::string &name, const ::armarx::MatrixFloatBasePtr &values, const ::armarx::TimestampBasePtr &timestamp, const ::Ice::Current &=Ice::emptyCurrent) override
PropertyDefinitionsPtr createPropertyDefinitions() override
void onInitObserver() override
Framework hook.
std::string getDefaultName() const override
Retrieve default name of component.
HapticSampleStatistics(unsigned int count, long timestamp)
ObserverPropertyDefinitions(std::string prefix)
Definition Observer.h:52
IceUtil::Handle< PeriodicTask< T > > pointer_type
Shared pointer type for convenience.
std::string prefix
Prefix of the properties such as namespace, domain, component name, etc.
PropertyDefinition< PropertyType > & defineOptionalProperty(const std::string &name, PropertyType defaultValue, const std::string &description="", PropertyDefinitionBase::PropertyConstness constness=PropertyDefinitionBase::eConstant)
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.