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