TactileSensor.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 "TactileSensor.h"
25 
26 #include <stdio.h>
27 
28 #include <string>
29 
30 #include "AbstractInterface.h"
31 #include "Response.h"
32 #include "Types.h"
33 
34 TactileSensor::TactileSensor(std::shared_ptr<AbstractInterface> interface)
35 {
36  this->interface = interface;
37 }
38 
40 {
41  // TODO Auto-generated destructor stub
42 }
43 
46 {
47  Response response = interface->submitCmd(0x30, nullptr, 0, false);
48  response.ensureMinLength(12);
49  response.ensureSuccess();
50 
51  tac_matrix_info_t matrix_info;
52 
53  matrix_info.res_x = response.getShort(2);
54  matrix_info.res_y = response.getShort(4);
55  matrix_info.cell_width = response.getShort(6);
56  matrix_info.cell_height = response.getShort(8);
57  matrix_info.fullscale = response.getShort(10);
58 
59  //delete response;
60  return matrix_info;
61 }
62 
63 void
65 {
66  printf("res_x = %d, res_y = %d, cell_width = %d, cell_height = %d, fullscale = %X\n",
67  mi->res_x,
68  mi->res_y,
69  mi->cell_width,
70  mi->cell_height,
71  mi->fullscale);
72 }
73 
74 void
75 TactileSensor::printMatrix(short* matrix, int width, int height)
76 {
77  int x, y;
78 
79  for (y = 0; y < height; y++)
80  {
81  printf("%03X", matrix[y * width]);
82 
83  for (x = 1; x < width; x++)
84  {
85  printf(", %03X", matrix[y * width + x]);
86  }
87 
88  printf("\n");
89  }
90 }
91 
94 {
95  unsigned char payload[1];
96  payload[0] = 0x00; // FLAGS = 0
97  Response response = interface->submitCmd(0x20, payload, sizeof(payload), false);
98  return getFrameData(&response);
99 }
100 
103 {
104  Response response = interface->receiveWithoutChecks();
105 
106  if (response.cmdId == 0x21)
107  {
108  response = interface->receiveWithoutChecks();
109  }
110 
111  if (response.cmdId == 0x00)
112  {
113  return getPeriodicFrameData(&response);
114  }
115  else
116  {
117  throw TransmissionException(
118  str(boost::format("Response ID (%02X) does not match submitted command ID (%02X)") %
119  (int)response.cmdId % (int)0x00));
120  }
121 }
122 
124 TactileSensor::getPeriodicFrameData(Response* response)
125 {
126  response->ensureMinLength(7);
127 
128  unsigned int timestamp = response->getUInt(0);
129  int offset = 5;
130 
131  int count = (response->len - offset) / 2;
132  int i;
133  std::shared_ptr<std::vector<short>> data;
134  data.reset(new std::vector<short>(count, 0));
135 
136  //short* data = new short[ count ];
137  for (i = 0; i < count; i++)
138  {
139  short value = response->getShort(i * 2 + offset);
140  (*data)[i] = value;
141  }
142 
143  return PeriodicFrameData(data, count, timestamp);
144 }
145 
146 FrameData
147 TactileSensor::getFrameData(Response* response)
148 {
149  response->ensureMinLength(7);
150  response->ensureSuccess();
151 
152  int offset = 5 + 2;
153 
154  int count = (response->len - offset) / 2;
155  int i;
156  std::shared_ptr<std::vector<short>> data;
157  data.reset(new std::vector<short>(count, 0));
158 
159  for (i = 0; i < count; i++)
160  {
161  short value = response->getShort(i * 2 + offset);
162  (*data)[i] = value;
163  }
164 
165  return FrameData(data, count);
166 }
167 
168 void
170 {
171  unsigned char payload[3];
172  payload[0] = 0x00; // FLAGS = 0
173  payload[1] = delay_ms & 0xFF;
174  payload[2] = (delay_ms >> 8) & 0xFF;
175  interface->fireAndForgetCmd(0x21, payload, sizeof(payload), false);
176 }
177 
178 void
180 {
181  while (1)
182  {
183  interface->fireAndForgetCmd(0x22, nullptr, 0, false);
184  int waitCount = 10;
185 
186  while (waitCount > 0)
187  {
188  Response response = interface->receiveWithoutChecks();
189 
190  if (response.cmdId == 0x22)
191  {
192  return;
193  }
194  else
195  {
196  std::cout
197  << boost::format(
198  "stopPeriodicFrameAcquisition :: Discarding Response with ID 0x%02X") %
199  (int)response.cmdId
200  << std::endl;
201  }
202 
203  waitCount--;
204  }
205  }
206 }
207 
208 void
209 TactileSensor::tareSensorMatrix(unsigned char operation)
210 {
211  unsigned char payload[1];
212  payload[0] =
213  operation; // OPERATION: 0 = un-tare the sensor matrix using the currently set threshold value, 1 = tare the sensor matrix
214  Response response = interface->submitCmd(0x23, payload, sizeof(payload), false);
215  response.ensureMinLength(2);
216  response.ensureSuccess();
217 }
218 
219 void
221  unsigned char y1,
222  unsigned char x2,
223  unsigned char y2)
224 {
225  unsigned char payload[4];
226  payload[0] = x1;
227  payload[1] = y1;
228  payload[2] = x2;
229  payload[3] = y2;
230  Response response = interface->submitCmd(0x31, payload, sizeof(payload), false);
231  response.ensureMinLength(2);
232  response.ensureSuccess();
233 }
234 
235 int
237 {
238  return 0;
239 }
240 
241 int
242 TactileSensor::getAcquisitionMask(char** mask, int* mask_len)
243 {
244  return 0;
245 }
246 
247 void
249 {
250  unsigned char payload[2];
251  payload[0] = threshold & 0xFF;
252  payload[1] = (threshold >> 8) & 0xFF;
253  Response response = interface->submitCmd(0x34, payload, sizeof(payload), false);
254  response.ensureMinLength(2);
255  response.ensureSuccess();
256 }
257 
258 unsigned short
260 {
261  Response response = interface->submitCmd(0x35, nullptr, 0, false);
262  response.ensureMinLength(2);
263  response.ensureSuccess();
264  return response.getShort(2);
265 }
266 
267 void
269 {
270  /*
271  * Adjust the pressure sensitivity of a matrix by setting the gain of the Analog Front-End. The gain can
272  * be set using an integer value in the range of 0 to 255, where 0 is the most insensitive (lowest gain)
273  * and 255 is the most sensitive (highest gain) setting.
274  */
275  unsigned char payload[1];
276  payload[0] = gain;
277  Response response = interface->submitCmd(0x36, payload, sizeof(payload), false);
278  response.ensureMinLength(2);
279  response.ensureSuccess();
280 }
281 
282 unsigned char
284 {
285  /*
286  * Get the currently set analog front-end gain value. The gain is as an integer value ranging from 0 to
287  * 255, where 0 is the most insensitive (lowest gain) and 255 is the most sensitive (highest gain) setting.
288  */
289  Response response = interface->submitCmd(0x37, nullptr, 0, false);
290  response.ensureMinLength(3);
291  response.ensureSuccess();
292  unsigned char gain = response.getByte(2);
293  return gain;
294 }
295 
296 std::string
298 {
299  /*
300  * Return a string containing the sensor type.
301  */
302  Response response = interface->submitCmd(0x38, nullptr, 0, false);
303  response.ensureMinLength(2);
304  response.ensureSuccess();
305  std::string type = std::string((char*)response.data.data() + 2, response.len - 2);
306  return type;
307 }
308 
309 float
311 {
312  Response response = interface->submitCmd(0x46, nullptr, 0, false);
313  response.ensureMinLength(2);
314  short value = (short)response.getShort(2);
315  return value * 0.1f;
316 }
317 
320 {
321  Response response = interface->submitCmd(0x50, nullptr, 0, false);
322  response.ensureMinLength(9);
323  response.ensureSuccess();
325  si.type = response.getByte(2);
326  si.hw_rev = response.getByte(3);
327  si.fw_version = response.getShort(4);
328  si.sn = response.getShort(6);
329  return si;
330 }
331 
332 void
334 {
335  int v1 = (si.fw_version & 0xF000) >> 12;
336  int v2 = (si.fw_version & 0x0F00) >> 8;
337  int v3 = (si.fw_version & 0x00F0) >> 4;
338  int v4 = (si.fw_version & 0x000F) >> 0;
339  std::cout << boost::format("System Type=%1%, Hardware Revision=%2%, Firmware "
340  "Version=%3%.%4%.%5%.%6% (0x%7$04X), Serial Number=%8%") %
341  (int)si.type % (int)si.hw_rev % v1 % v2 % v3 % v4 % si.fw_version % si.sn
342  << std::endl;
343 }
344 
345 void
347 {
348  unsigned char* payload = (unsigned char*)tag.c_str();
349  Response response = interface->submitCmd(0x51, payload, tag.length(), false);
350  response.ensureMinLength(2);
351  response.ensureSuccess();
352 }
353 
354 std::string
356 {
357  Response response = interface->submitCmd(0x52, nullptr, 0, false);
358  response.ensureMinLength(2);
359  response.ensureSuccess();
360  std::string tag = std::string((char*)response.data.data() + 2, response.len - 2);
361  return tag;
362 }
363 
364 bool
366 {
367  Response response = interface->submitCmd(0x52, nullptr, 0, false);
368  response.ensureMinLength(2);
369 
370  if (response.status == E_NOT_AVAILABLE)
371  {
372  return false;
373  }
374 
375  response.ensureSuccess();
376  tag = std::string((char*)response.data.data() + 2, response.len - 2);
377  return true;
378 }
379 
380 int
381 TactileSensor::loop(char* data, int data_len)
382 {
383  return 0;
384 }
385 
386 std::string
388 {
389  return interface->toString();
390 }
391 
392 std::ostream&
393 operator<<(std::ostream& strm, const TactileSensor& a)
394 {
395  return strm << a.interface;
396 }
TactileSensor::setDeviceTag
void setDeviceTag(std::string tag)
Definition: TactileSensor.cpp:346
Response::len
unsigned int len
Definition: Response.h:108
TactileSensor::printSystemInformation
static void printSystemInformation(tac_system_information_t si)
Definition: TactileSensor.cpp:333
Response::getByte
unsigned char getByte(int index)
Definition: Response.h:62
FrameData
Definition: TactileSensor.h:66
str
std::string str(const T &t)
Definition: UserAssistedSegmenterGuiWidgetController.cpp:43
Response::cmdId
unsigned char cmdId
Definition: Response.h:105
Response::data
std::vector< unsigned char > data
Definition: Response.h:107
TactileSensor::getAcquisitionMask
int getAcquisitionMask(char **mask, int *mask_len)
Definition: TactileSensor.cpp:242
TactileSensor::readSingleFrame
FrameData readSingleFrame()
Definition: TactileSensor.cpp:93
TactileSensor::startPeriodicFrameAcquisition
void startPeriodicFrameAcquisition(unsigned short delay_ms)
Definition: TactileSensor.cpp:169
tac_matrix_info_t::fullscale
int fullscale
Definition: TactileSensor.h:54
Response.h
TactileSensor::setAquisitionWindow
void setAquisitionWindow(unsigned char x1, unsigned char y1, unsigned char x2, unsigned char y2)
Definition: TactileSensor.cpp:220
tac_system_information_t
Definition: TactileSensor.h:57
TactileSensor::setFrontEndGain
void setFrontEndGain(unsigned char gain)
Definition: TactileSensor.cpp:268
TactileSensor::stopPeriodicFrameAcquisition
void stopPeriodicFrameAcquisition(void)
Definition: TactileSensor.cpp:179
TactileSensor::printMatrix
static void printMatrix(short *matrix, int width, int height)
Definition: TactileSensor.cpp:75
TactileSensor
Definition: TactileSensor.h:90
Response::ensureMinLength
void ensureMinLength(int len)
Definition: Response.h:68
tac_matrix_info_t::res_x
int res_x
Definition: TactileSensor.h:50
armarx::ctrlutil::a
double a(double t, double a0, double j)
Definition: CtrlUtil.h:45
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:855
operator<<
std::ostream & operator<<(std::ostream &strm, const TactileSensor &a)
Definition: TactileSensor.cpp:393
Response::status
status_t status
Definition: Response.h:106
AbstractInterface.h
tac_system_information_t::fw_version
unsigned short fw_version
Definition: TactileSensor.h:62
tac_matrix_info_t::cell_width
int cell_width
Definition: TactileSensor.h:52
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
Response::getUInt
unsigned int getUInt(int index)
Definition: Response.h:49
TactileSensor::getSystemInformation
tac_system_information_t getSystemInformation()
Definition: TactileSensor.cpp:319
TactileSensor::tareSensorMatrix
void tareSensorMatrix(unsigned char operation)
Definition: TactileSensor.cpp:209
E_NOT_AVAILABLE
@ E_NOT_AVAILABLE
Definition: Types.h:38
TactileSensor::getThreshold
unsigned short getThreshold()
Definition: TactileSensor.cpp:259
TactileSensor::getFrontEndGain
unsigned char getFrontEndGain()
Definition: TactileSensor.cpp:283
timestamp
std::string timestamp()
Definition: CartographerAdapter.cpp:85
TactileSensor::setThreshold
void setThreshold(short threshold)
Definition: TactileSensor.cpp:248
TactileSensor::tryGetDeviceTag
bool tryGetDeviceTag(std::string &tag)
Definition: TactileSensor.cpp:365
TactileSensor::printMatrixInfo
static void printMatrixInfo(tac_matrix_info_t *mi)
Definition: TactileSensor.cpp:64
TactileSensor::~TactileSensor
virtual ~TactileSensor()
Definition: TactileSensor.cpp:39
tac_matrix_info_t::cell_height
int cell_height
Definition: TactileSensor.h:53
TactileSensor::readDeviceTemperature
float readDeviceTemperature()
Definition: TactileSensor.cpp:310
Response::ensureSuccess
void ensureSuccess()
Definition: Response.h:82
TactileSensor::getMatrixInformation
tac_matrix_info_t getMatrixInformation()
Definition: TactileSensor.cpp:45
tac_system_information_t::type
unsigned char type
Definition: TactileSensor.h:59
Response
Definition: Response.h:36
TactileSensor::receicePeriodicFrame
PeriodicFrameData receicePeriodicFrame()
Definition: TactileSensor.cpp:102
TactileSensor::setAdvanvedAcquisitionMask
int setAdvanvedAcquisitionMask(char *mask)
Definition: TactileSensor.cpp:236
Response::getShort
unsigned short getShort(int index)
Definition: Response.h:56
tac_matrix_info_t::res_y
int res_y
Definition: TactileSensor.h:51
TactileSensor::loop
int loop(char *data, int data_len)
Definition: TactileSensor.cpp:381
TransmissionException
Definition: TransmissionException.h:28
TactileSensor::getSensorType
std::string getSensorType()
Definition: TactileSensor.cpp:297
tac_system_information_t::sn
unsigned short sn
Definition: TactileSensor.h:63
tac_system_information_t::hw_rev
unsigned char hw_rev
Definition: TactileSensor.h:60
PeriodicFrameData
Definition: TactileSensor.h:77
TactileSensor.h
TactileSensor::getDeviceTag
std::string getDeviceTag()
Definition: TactileSensor.cpp:355
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
Types.h
tac_matrix_info_t
Definition: TactileSensor.h:48
TactileSensor::TactileSensor
TactileSensor(std::shared_ptr< AbstractInterface > interface)
Definition: TactileSensor.cpp:34
TactileSensor::getInterfaceInfo
std::string getInterfaceInfo()
Definition: TactileSensor.cpp:387