HapticObserver.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 #include "HapticObserver.h"
25 
32 
34 #include <Eigen/Dense>
36 
37 namespace armarx
38 {
40  {
41  statisticsTask = new PeriodicTask<HapticObserver>(this, &HapticObserver::updateStatistics, 10, false);
42  }
43 
44  void HapticObserver::setTopicName(std::string topicName)
45  {
46  this->topicName = topicName;
47  }
48 
50  {
51  if (topicName.empty())
52  {
53  usingTopic(getProperty<std::string>("HapticTopicName").getValue());
54  }
55  else
56  {
57  usingTopic(topicName);
58  }
59 
60  // register all checks
65  }
66 
68  {
69  statisticsTask->start();
70  }
71 
73  {
74  statisticsTask->stop();
75  }
76 
77  void HapticObserver::reportSensorValues(const std::string& device, const std::string& name, const armarx::MatrixFloatBasePtr& values, const armarx::TimestampBasePtr& timestamp, const Ice::Current&)
78  {
79  std::unique_lock lock(dataMutex);
80 
81  MatrixFloatPtr matrix = MatrixFloatPtr::dynamicCast(values);
82  if (matrix->cols == 0)
83  {
84  // Empty matrix received, silently ignore
85  return;
86  }
87 
88  TimestampVariantPtr timestampPtr = TimestampVariantPtr::dynamicCast(timestamp);
89  Eigen::MatrixXf eigenMatrix = matrix->toEigen();
90  float max = eigenMatrix.maxCoeff();
91  float mean = eigenMatrix.mean();
92  std::string channelName = name;
93  Eigen::MatrixXf M = matrix->toEigen();
94 
95  if (!existsChannel(channelName))
96  {
97  offerChannel(channelName, "Haptic data");
98  offerDataFieldWithDefault(channelName, "device", Variant(device), "Device of the tactile sensor");
99  offerDataFieldWithDefault(channelName, "name", Variant(name), "Name of the tactile sensor");
100  offerDataFieldWithDefault(channelName, "matrix", matrix, "Raw tactile matrix data");
101  offerDataFieldWithDefault(channelName, "max", Variant(max), "Maximum value");
102  offerDataFieldWithDefault(channelName, "mean", Variant(mean), "Mean value");
103  offerDataFieldWithDefault(channelName, "timestamp", timestampPtr, "Timestamp");
104  offerDataFieldWithDefault(channelName, "rate", Variant(0.0f), "Sample rate");
105 
106  for (int i = 0; i < M.rows(); i++)
107  {
108  for (int j = 0; j < M.cols(); j++)
109  {
110  std::stringstream s;
111  s << "entry_" << i << "," << j;
112  offerDataFieldWithDefault(channelName, s.str(), Variant(M(i, j)), "Individual matrix entry");
113  }
114  }
115 
116  ARMARX_INFO << "Offering new channel: " << channelName;
117  }
118  else
119  {
120  setDataField(channelName, "device", Variant(device));
121  setDataField(channelName, "name", Variant(name));
122  setDataField(channelName, "matrix", matrix);
123  setDataField(channelName, "max", Variant(max));
124  setDataField(channelName, "mean", Variant(mean));
125  setDataField(channelName, "timestamp", timestampPtr);
126 
127  for (int i = 0; i < M.rows(); i++)
128  {
129  for (int j = 0; j < M.cols(); j++)
130  {
131  std::stringstream s;
132  s << "entry_" << i << "," << j;
133  setDataField(channelName, s.str(), Variant(M(i, j)));
134  }
135  }
136 
137  }
138 
139  /*if(statistics.count(device) > 0)
140  {
141  statistics.at(device).add(timestamp->timestamp);
142  HapticSampleStatistics stats = statistics.at(device);
143  long avg = stats.average();
144  float rate = avg == 0 ? 0 : 1000000.0f / (float)avg;
145  setDataField(device, "rate", Variant(rate));
146  }
147  else
148  {
149  statistics.insert(std::map<std::string,HapticSampleStatistics>::value_type(device, HapticSampleStatistics(100, timestamp->timestamp)));
150  }*/
151 
152  updateChannel(channelName);
153  }
154 
156  {
158  }
159 
160  void HapticObserver::updateStatistics()
161  {
162  /*std::unique_lock lock(dataMutex);
163  //ARMARX_LOG << "updateStatistics";
164  long now = TimestampVariant::nowLong();
165  for (std::map<std::string, HapticSampleStatistics>::iterator it = statistics.begin(); it != statistics.end(); ++it)
166  {
167  HapticSampleStatistics stats = it->second;
168  std::string device = it->first;
169  long avg = stats.average(now);
170  float rate = avg == 0 ? 0 : 1000000.0f / (float)avg;
171  setDataField(device, "rate", Variant(rate));
172  updateChannel(device);
173  }*/
174  }
175 }
armarx::Observer::updateChannel
void updateChannel(const std::string &channelName, const std::set< std::string > &updatedDatafields=std::set< std::string >())
Update all conditions for a channel.
Definition: Observer.cpp:715
armarx::Variant
The Variant class is described here: Variants.
Definition: Variant.h:224
armarx::HapticObserverPropertyDefinitions
Definition: HapticObserver.h:41
ConditionCheckEquals.h
armarx::HapticObserver::onInitObserver
void onInitObserver() override
Framework hook.
Definition: HapticObserver.cpp:49
ConditionCheckInRange.h
armarx::max
std::vector< T > max(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorHelpers.h:267
ProsthesisInterface.values
values
Definition: ProsthesisInterface.py:190
armarx::ConditionCheckSmaller
Definition: ConditionCheckSmaller.h:40
armarx::HapticObserver::setTopicName
void setTopicName(std::string topicName)
Definition: HapticObserver.cpp:44
armarx::Observer::existsChannel
bool existsChannel(const std::string &channelName) const
Definition: Observer.cpp:1433
RobotAPIObjectFactories.h
armarx::HapticObserver::createPropertyDefinitions
PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: HapticObserver.cpp:155
IceInternal::Handle
Definition: forward_declarations.h:8
armarx::ConditionCheckUpdated
Definition: ConditionCheckUpdated.h:41
armarx::mean
std::optional< float > mean(const boost::circular_buffer< NameValueMap > &buffer, const std::string &key)
Definition: KinematicUnitGuiPlugin.cpp:1615
armarx::HapticObserver::onConnectObserver
void onConnectObserver() override
Framework hook.
Definition: HapticObserver.cpp:67
HapticObserver.h
TimestampVariant.h
armarx::Observer::setDataField
void setDataField(const std::string &channelName, const std::string &datafieldName, const Variant &value, bool triggerFilterUpdate=true)
set datafield with datafieldName and in channel channelName
Definition: Observer.cpp:465
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::ManagedIceObject::usingTopic
void usingTopic(const std::string &name, bool orderedPublishing=false)
Registers a proxy for subscription after initialization.
Definition: ManagedIceObject.cpp:248
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:74
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::HapticObserver::onExitObserver
void onExitObserver() override
Framework hook.
Definition: HapticObserver.cpp:72
ConditionCheckSmaller.h
IceUtil::Handle< class PropertyDefinitionContainer >
armarx::ConditionCheckEquals
Definition: ConditionCheckEquals.h:46
armarx::Observer::offerConditionCheck
void offerConditionCheck(std::string checkName, ConditionCheck *conditionCheck)
Offer a condition check.
Definition: Observer.cpp:273
ConditionCheckMagnitudeChecks.h
armarx::PeriodicTask
Definition: ArmarXManager.h:70
ConditionCheckLarger.h
armarx::ConditionCheckLarger
Definition: ConditionCheckLarger.h:40
armarx::Observer::offerDataFieldWithDefault
void offerDataFieldWithDefault(std::string channelName, std::string datafieldName, const Variant &defaultValue, std::string description)
Offer a datafield with default value.
Definition: Observer.cpp:152
armarx::PropertyDefinitionsPtr
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
Definition: forward_declarations.h:34
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
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
ConditionCheckUpdated.h
armarx::Observer::offerChannel
void offerChannel(std::string channelName, std::string description)
Offer a channel.
Definition: Observer.cpp:126