WeissHapticSensor.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
25#include "WeissHapticSensor.h"
26
27#include <boost/format.hpp>
28#include <boost/regex.hpp>
29
31
32using namespace armarx;
33
34WeissHapticSensor::WeissHapticSensor(std::string device, int minimumReportIntervalMs) :
35 device(device),
36 connected(false),
37 setDeviceTagScheduled(false),
38 minimumReportIntervalMs(minimumReportIntervalMs)
39{
40 sensorTask =
41 new RunningTask<WeissHapticSensor>(this, &WeissHapticSensor::frameAcquisitionTaskLoop);
42 boost::smatch match;
43 boost::regex_search(device, match, boost::regex("\\w+$"));
44 this->deviceFileName = match[0];
45}
46
47void
49{
50
51 ARMARX_INFO << "Open Serial" << std::endl;
52 this->interface.reset(new SerialInterface(device.c_str(), 115200));
53 //interface->startLogging(deviceFileName + ".transmission.log");
54 interface->open();
55 //cout << *interface << endl;
56 this->sensor.reset(new TactileSensor(interface));
57
58 jsWriter.reset(new TextWriter(deviceFileName + ".js"));
59
60 //cout << "Stop Periodic Frame Acquisition" << endl;
61 sensor->stopPeriodicFrameAcquisition();
62
63 //string sensorType = sensor->getSensorType();
64 //cout << boost::format("Sensor Type = %1%") % sensorType << endl;
65
66 //tac_system_information_t si = sensor->getSystemInformation();
67 //TactileSensor::printSystemInformation(si);
68
69 //sensor->setDeviceTag("Test Tag");
70 std::string tag;
71
72 if (sensor->tryGetDeviceTag(tag))
73 {
74 ARMARX_INFO << "[" << device << "] Got Device Tag: " << tag;
75 this->tag = tag;
76 }
77 else
78 {
79 ARMARX_WARNING << "[" << device << "] No device tag present using device name instead";
80 this->tag = deviceFileName;
81 }
82
83 //cout << boost::format("Sensor Tag = %1%") % tag << endl;
84
85 jsWriter->writeLine(str(boost::format("%s = {};") % deviceFileName));
86 jsWriter->writeLine(str(boost::format("sensors.push(%s);") % deviceFileName));
87 jsWriter->writeLine(str(boost::format("%s.device = \"%s\";") % deviceFileName % device));
88 jsWriter->writeLine(str(boost::format("%s.tag = \"%s\";") % deviceFileName % tag));
89 jsWriter->writeLine(str(boost::format("%s.data = [];") % deviceFileName));
90
91 //cout << "Tare Sensor Matrix" << endl;
92 //sensor->tareSensorMatrix(1);
93
94 //cout << "Get Matrix Information" << endl;
95 this->mi = sensor->getMatrixInformation();
97
98 sensor->setAquisitionWindow(1, 1, mi.res_x, mi.res_y);
99
100 sensor->setFrontEndGain(255);
101 ARMARX_LOG << "[" << device << "] Front end gain set to " << (int)sensor->getFrontEndGain();
102
103 sensor->setThreshold(0);
104 ARMARX_LOG << "[" << device << "] threshold set to " << (int)sensor->getThreshold();
105
106 connected = true;
107 ARMARX_LOG << device << ": Connect done, Interface=" << sensor->getInterfaceInfo();
108}
109
110void
112{
113 connected = false;
114 sensorTask->stop(true);
115}
116
117void
118WeissHapticSensor::setListenerPrx(HapticUnitListenerPrx listenerPrx)
119{
120 this->listenerPrx = listenerPrx;
121}
122
123void
125{
126 ARMARX_LOG << device << ": startSampling" << std::endl;
127 sensorTask->start();
128}
129
130std::string
132{
133 return device;
134}
135
136void
138{
139 std::unique_lock lock(mutex);
140 setDeviceTagValue = tag;
141 setDeviceTagScheduled = true;
142}
143
144void
145WeissHapticSensor::frameAcquisitionTaskLoop()
146{
147 ARMARX_LOG << device << ": readAndReportSensorValues";
148 //bool periodic = false;
149 ARMARX_LOG << device << ": Interface Info: " << sensor->getInterfaceInfo();
150
151 ARMARX_LOG << device << this << ": startPeriodicFrameAcquisition";
152 sensor->startPeriodicFrameAcquisition(0);
153
154 IceUtil::Time lastFrameTime = IceUtil::Time::now();
155
157 mi.res_x * mi.res_y, 21); // inter sample dely ~= 3,7ms, 11 samples ~== 40ms delay
158
159
160 while (!sensorTask->isStopped())
161 {
162 //ARMARX_INFO << deactivateSpam(1) << this << ": receicePeriodicFrame";
163
164 try
165 {
166 //long start = TimestampVariant::nowLong();
167 PeriodicFrameData data = sensor->receicePeriodicFrame();
168 //long end = TimestampVariant::nowLong();
169 //cout << end - start << endl;
170
171 std::vector<float> sensorValues;
172
173 for (int i = 0; i < mi.res_x * mi.res_y; i++)
174 {
175 sensorValues.push_back(data.data->at(i));
176 }
177
178 slidingMedian.addEntry(sensorValues);
179
180 MatrixFloatPtr matrix = new MatrixFloat(mi.res_y, mi.res_x);
181 std::vector<float> filteredSensorValues = slidingMedian.getMedian();
182
183 for (int y = 0; y < mi.res_y; y++)
184 {
185 for (int x = 0; x < mi.res_x; x++)
186 {
187 (*matrix)(y, x) = filteredSensorValues.at(y * mi.res_x + x);
188 }
189 }
190
191
192 /*
193 MatrixFloatPtr matrix = new MatrixFloat(mi.res_y, mi.res_x);
194 for (int y = 0; y < mi.res_y; y++)
195 {
196 for (int x = 0; x < mi.res_x; x++)
197 {
198 short val = (*data.data)[y * mi.res_x + x];
199 (*matrix)(y, x) = val;
200 }
201 }
202 */
203
204 IceUtil::Time now = IceUtil::Time::now();
205
206 TimestampVariantPtr nowTimestamp = new TimestampVariant(now);
207 writeMatrixToJs(matrix, nowTimestamp);
208
209 IceUtil::Time interval = now - lastFrameTime;
210
211 if (interval.toMilliSeconds() >= minimumReportIntervalMs)
212 {
213 listenerPrx->reportSensorValues(device, tag, matrix, nowTimestamp);
214 lastFrameTime = now;
215 }
216 }
217 catch (ChecksumErrorException&)
218 {
219 ARMARX_WARNING << "Caught ChecksumErrorException on " << device << ", skipping frame";
220 }
221
222 if (setDeviceTagScheduled)
223 {
224 std::unique_lock lock(mutex);
225 setDeviceTagScheduled = false;
226
227 ARMARX_INFO << "[" << device
228 << "] Stopping periodic frame aquisition to set new device tag";
229 sensor->stopPeriodicFrameAcquisition();
230
231 ARMARX_IMPORTANT << "[" << device << "] Setting new device tag '" << setDeviceTagValue
232 << "'";
233 sensor->setDeviceTag(setDeviceTagValue);
234 this->tag = setDeviceTagValue;
235
236 ARMARX_INFO << "[" << device << "] Starting periodic frame aquisition";
237 sensor->startPeriodicFrameAcquisition(0);
238 }
239 }
240
241 ARMARX_LOG << device << ": stopPeriodicFrameAcquisition";
242 sensor->stopPeriodicFrameAcquisition();
243}
244
245void
246WeissHapticSensor::writeMatrixToJs(MatrixFloatPtr matrix, TimestampVariantPtr timestamp)
247{
248 //std::cout << "writeMatrixToJs" << std::endl;
249 if (jsWriter != NULL)
250 {
251 jsWriter->writeLine(str(boost::format("%s.data.push([%i, %s]);") % deviceFileName %
252 timestamp->getTimestamp() % matrix->toJsonRowMajor()));
253 }
254}
std::string timestamp()
std::string str(const T &t)
static void printMatrixInfo(tac_matrix_info_t *mi)
The MatrixFloat class.
WeissHapticSensor(std::string device, int minimumReportIntervalMs)
void setListenerPrx(HapticUnitListenerPrx listenerPrx)
void scheduleSetDeviceTag(std::string tag)
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
#define ARMARX_IMPORTANT
The logging level for always important information, but expected behaviour (in contrast to ARMARX_WAR...
Definition Logging.h:190
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
#define ARMARX_LOG
Definition Logging.h:165
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceInternal::Handle< MatrixFloat > MatrixFloatPtr
Interval< T > interval(T lo, T hi)
IceInternal::Handle< TimestampVariant > TimestampVariantPtr