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
34TactileSensor::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
63void
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
74void
75TactileSensor::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 {
118 str(boost::format("Response ID (%02X) does not match submitted command ID (%02X)") %
119 (int)response.cmdId % (int)0x00));
120 }
121}
122
124TactileSensor::getPeriodicFrameData(Response* response)
125{
126 response->ensureMinLength(7);
127
128 unsigned int timestamp = response->getUInt(0);
129 int offset = 5;
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
147TactileSensor::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
168void
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
178void
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
208void
209TactileSensor::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
219void
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
235int
237{
238 return 0;
239}
240
241int
242TactileSensor::getAcquisitionMask(char** mask, int* mask_len)
243{
244 return 0;
245}
246
247void
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
258unsigned 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
267void
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
282unsigned 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
296std::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
309float
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
332void
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
345void
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
354std::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
364bool
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
380int
381TactileSensor::loop(char* data, int data_len)
382{
383 return 0;
384}
385
386std::string
388{
389 return interface->toString();
390}
391
392std::ostream&
393operator<<(std::ostream& strm, const TactileSensor& a)
394{
395 return strm << a.interface;
396}
std::string timestamp()
uint8_t data[1]
@ E_NOT_AVAILABLE
Definition Types.h:38
std::ostream & operator<<(std::ostream &strm, const TactileSensor &a)
std::string str(const T &t)
std::string getInterfaceInfo()
static void printMatrix(short *matrix, int width, int height)
int getAcquisitionMask(char **mask, int *mask_len)
void startPeriodicFrameAcquisition(unsigned short delay_ms)
tac_system_information_t getSystemInformation()
bool tryGetDeviceTag(std::string &tag)
float readDeviceTemperature()
PeriodicFrameData receicePeriodicFrame()
void setAquisitionWindow(unsigned char x1, unsigned char y1, unsigned char x2, unsigned char y2)
int loop(char *data, int data_len)
virtual ~TactileSensor()
unsigned short getThreshold()
void setThreshold(short threshold)
TactileSensor(std::shared_ptr< AbstractInterface > interface)
std::string getDeviceTag()
static void printMatrixInfo(tac_matrix_info_t *mi)
tac_matrix_info_t getMatrixInformation()
void tareSensorMatrix(unsigned char operation)
void setFrontEndGain(unsigned char gain)
int setAdvanvedAcquisitionMask(char *mask)
static void printSystemInformation(tac_system_information_t si)
void stopPeriodicFrameAcquisition(void)
std::string getSensorType()
FrameData readSingleFrame()
void setDeviceTag(std::string tag)
unsigned char getFrontEndGain()
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::shared_ptr< Value > value()
Definition cxxopts.hpp:855
unsigned short getShort(int index)
Definition Response.h:56
void ensureSuccess()
Definition Response.h:82
unsigned int getUInt(int index)
Definition Response.h:49
unsigned char cmdId
Definition Response.h:105
unsigned int len
Definition Response.h:108
unsigned char getByte(int index)
Definition Response.h:62
std::vector< unsigned char > data
Definition Response.h:107
void ensureMinLength(int len)
Definition Response.h:68
status_t status
Definition Response.h:106
unsigned short fw_version