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