forked from KitwareMedical/IntersonManager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IntersonManagerTest.cpp
403 lines (331 loc) · 10.5 KB
/
IntersonManagerTest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//
// Created by Matthieu Heitz on 6/16/15.
// Copyright (c) 2015 Kitware Inc. All rights reserved.
//
#include "IntersonManagerTest.h"
#include <iostream>
#include <stdio.h>
#include <unistd.h>
#include "libusb.h"
#include "IntersonManager.h"
IntersonManagerTest::IntersonManagerTest(IntersonManager * manager)
{
if(manager != NULL)
{
this->m_IntMng = manager;
this->m_IntMng->setVerbose(true);
this->m_IntMng->initializeProbe();
}
else
{
std::cerr<<"Null pointer passed. Initialize the IntersonManager before passing it."<<std::endl;
}
}
IntersonManagerTest::~IntersonManagerTest()
{
}
void IntersonManagerTest::printDevices(libusb_device ** deviceList)
{
libusb_device *dev;
int i = 0, j = 0;
uint8_t path[8];
uint8_t string_index[3]; // indexes of the string descriptors
char string[128];
while ((dev = deviceList[i]) != NULL)
{
printf("\n");
struct libusb_device_descriptor desc;
int r = libusb_get_device_descriptor(dev, &desc);
if (r < 0)
{
fprintf(stderr, "failed to get device descriptor");
return;
}
string_index[0] = desc.iManufacturer;
string_index[1] = desc.iProduct;
string_index[2] = desc.iSerialNumber;
printf("%04x:%04x (bus %d, device %d)",
desc.idVendor, desc.idProduct,
libusb_get_bus_number(dev), libusb_get_device_address(dev));
r = libusb_get_port_numbers(dev, path, sizeof(path));
if (r > 0) {
printf(" path: %d", path[0]);
for (j = 1; j < r; j++)
printf(".%d", path[j]);
}
printf("\n");
libusb_device_handle *handle;
r = libusb_open(dev, &handle);
if(r == LIBUSB_ERROR_ACCESS)
{
std::cout<<" Insufficient permission"<<std::endl;
}
if (r == 0 && handle != NULL)
{
for (int k = 0; k < 3; k++)
{
if (string_index[k] == 0)
{
continue;
}
if (libusb_get_string_descriptor_ascii(handle, string_index[k], (unsigned char *) string, 128) >= 0)
{
printf("String Desc(0x%02X): \"%s\"\n", string_index[k], string);
}
}
libusb_close(handle);
}
i++;
}
}
int IntersonManagerTest::TestHardButton()
{
// Testing HardButton - using usleep()
for (int i = 0; i < 30; ++i)
{
uInt8 state = 0xAC;
this->m_IntMng->readHardButton(&state);
std::cout<<"HardButton state = "<<+state<<std::endl;
usleep(300000);
}
return 0;
}
int IntersonManagerTest::TestMotor1Speed()
{
// Testing Motor - 1 speed
uInt8 motorSpeed15[] = {0x7A, 0x0D, 0x00, 0x00, 0x80, 0xA4, 0x32}; // 15.0 fps
this->m_IntMng->initializeMotorSpeed(motorSpeed15);
this->m_IntMng->setStartMotor(true);
sleep(1);
this->m_IntMng->setStartMotor(false);
return 0;
}
int IntersonManagerTest::TestMotor3Speeds()
{
// Testing Motor - 3 speeds
uInt8 motorSpeed10[] = {0xB9, 0x04, 0x00, 0x00, 0xB7, 0xA4, 0x32}; // 10.0 fps
this->m_IntMng->initializeMotorSpeed(motorSpeed10);
this->m_IntMng->setStartMotor(true);
sleep(1);
this->m_IntMng->setStartMotor(false);
uInt8 motorSpeed12_5[] = {0x93, 0x07, 0x00, 0x00, 0x93, 0xA4, 0x32}; // 12.5 fps
this->m_IntMng->initializeMotorSpeed(motorSpeed12_5);
this->m_IntMng->setStartMotor(true);
sleep(1);
this->m_IntMng->setStartMotor(false);
uInt8 motorSpeed15[] = {0x7A, 0x0D, 0x00, 0x00, 0x80, 0xA4, 0x32}; // 15.0 fps
this->m_IntMng->initializeMotorSpeed(motorSpeed15);
this->m_IntMng->setStartMotor(true);
sleep(1);
this->m_IntMng->setStartMotor(false);
return 0;
}
int IntersonManagerTest::TestAcquisition()
{
// Testing Acquisition - B-Mode
this->m_IntMng->startAcquisitionRoutine(0);
sleep(1);
this->m_IntMng->stopAcquisitionRoutine();
return 0;
}
int IntersonManagerTest::TestSyncBulk_FrameByFrame_Overview()
{
// Testing Receiving Data : B-Mode, Synchronous Bulk
uInt8 motorSpeed10[] = {0xB9, 0x04, 0x00, 0x00, 0xB7, 0xA4, 0x32}; // 10.0 fps
uInt8 motorSpeed15[] = {0x7A, 0x0D, 0x00, 0x00, 0x80, 0xA4, 0x32}; // 15.0 fps
this->m_IntMng->initializeMotorSpeed(motorSpeed10);
libusb_set_debug(NULL, LIBUSB_LOG_LEVEL_NONE);
std::cout<<std::dec;
this->m_IntMng->startAcquisitionRoutine(0);
int r;
int nRows = 241;
int nColumn = 1024;
for (int i = 0; i < 20; ++i) // 20 frames
{
int transferSize = nRows*nColumn;
unsigned char endpoint = 0x82;
unsigned char * buffer = new unsigned char[transferSize];
int transferred = 1;
unsigned int BULK_TRANSFER_TIMEOUT = 1000;
libusb_device_handle * handle = this->m_IntMng->getHandle();
r = libusb_bulk_transfer(handle, endpoint, buffer, transferSize, &transferred, BULK_TRANSFER_TIMEOUT);
// Terminal max size
int maxTermRow = 25;
int maxTermColumn = 25;
// Overview of the frame
for (int k = 0; k < nRows * nColumn; k += nColumn*(nRows/maxTermRow))
{
for (int j = 0; j < nColumn; j+= nColumn/maxTermColumn)
{
std::cout << +buffer[k+j] << " ";
}
std::cout << std::endl;
}
std::cout << std::endl;
}
this->m_IntMng->stopAcquisitionRoutine();
return 0;
}
int IntersonManagerTest::TestSyncBulk_FrameByFrame_CheckFrameNumbers()
{
// Testing Receiving Data : B-Mode, Synchronous Bulk
uInt8 motorSpeed10[] = {0xB9, 0x04, 0x00, 0x00, 0xB7, 0xA4, 0x32}; // 10.0 fps
uInt8 motorSpeed15[] = {0x7A, 0x0D, 0x00, 0x00, 0x80, 0xA4, 0x32}; // 15.0 fps
this->m_IntMng->initializeMotorSpeed(motorSpeed15);
libusb_set_debug(NULL, LIBUSB_LOG_LEVEL_NONE);
std::cout<<std::dec;
this->m_IntMng->startAcquisitionRoutine(0);
int r;
for (int i = 0; i < 30; ++i) // 30 frames
{
int transferSize = 241 * 1024;
unsigned char endpoint = 0x82;
unsigned char *buffer = new unsigned char[transferSize];
int transferred = 1;
unsigned int BULK_TRANSFER_TIMEOUT = 1000;
libusb_device_handle *handle = this->m_IntMng->getHandle();
r = libusb_bulk_transfer(handle, endpoint, buffer, transferSize, &transferred, BULK_TRANSFER_TIMEOUT);
std::cout << "Frame numbers:" << std::hex << std::endl;
for (int k = 0; k < 241 * 1024; k += 1024)
{
std::cout << +buffer[k] << " ";
}
std::cout << std::dec << std::endl;
}
this->m_IntMng->stopAcquisitionRoutine();
return 0;
}
int IntersonManagerTest::TestSyncBulk_LineByLine_Overview()
{
// Testing Receiving Data : B-Mode, Synchronous Bulk
uInt8 motorSpeed10[] = {0xB9, 0x04, 0x00, 0x00, 0xB7, 0xA4, 0x32}; // 10.0 fps
uInt8 motorSpeed15[] = {0x7A, 0x0D, 0x00, 0x00, 0x80, 0xA4, 0x32}; // 15.0 fps
this->m_IntMng->initializeMotorSpeed(motorSpeed15);
libusb_set_debug(NULL, LIBUSB_LOG_LEVEL_NONE);
this->m_IntMng->startAcquisitionRoutine(0);
std::cout<<std::dec;
int r;
int nRows = 241;
int fps = 10;
int sec = 1;
int frameNumber = -1;
int linesPerFrame = 0;
for (int i = 0 ; i < nRows*fps*sec ; ++i)
{
int transferSize = 1024;
unsigned char endpoint = 0x82;
unsigned char * buffer = new unsigned char[transferSize];
int transferred = 1;
int BULK_TRANSFER_TIMEOUT = 1000;
libusb_device_handle * handle = this->m_IntMng->getHandle();
r = libusb_bulk_transfer(handle, endpoint, buffer, transferSize, &transferred, BULK_TRANSFER_TIMEOUT);
// Terminal Max Size
int maxTermRow = 25;
int maxTermColumn = 25;
// Overview of the frame
if(i%(nRows/maxTermRow) == 0)
{
if(buffer[0] != frameNumber)
{
frameNumber = buffer[0];
std::cout<<std::endl;
}
// Overview of the line
for(int k = 0 ; k < transferSize ; k+= transferSize/maxTermColumn)
{
std::cout<<+buffer[k]<<" ";
}
std::cout<<std::endl;
// Lines header
// for(int k = 0 ; k < maxTermColumn ; k+= 1)
// {
// std::cout<<+buffer[k]<<" ";
// }
// std::cout<<std::endl;
}
}
this->m_IntMng->stopAcquisitionRoutine();
return 0;
}
int IntersonManagerTest::TestSyncBulk_LineByLine_CheckFrameNumbers()
{
// Testing Receiving Data : B-Mode, Synchronous Bulk
uInt8 motorSpeed10[] = {0xB9, 0x04, 0x00, 0x00, 0xB7, 0xA4, 0x32}; // 10.0 fps
uInt8 motorSpeed15[] = {0x7A, 0x0D, 0x00, 0x00, 0x80, 0xA4, 0x32}; // 15.0 fps
this->m_IntMng->initializeMotorSpeed(motorSpeed15);
libusb_set_debug(NULL, LIBUSB_LOG_LEVEL_NONE);
this->m_IntMng->startAcquisitionRoutine(0);
std::cout<<std::dec;
int r;
int nRows = 241;
int fps = 10;
int sec = 3;
int frameNumber = 0;
int linesPerFrame = 0;
for (int i = 0 ; i < nRows*fps*sec ; ++i)
{
int transferSize = 1024;
unsigned char endpoint = 0x82;
unsigned char * buffer = new unsigned char[transferSize];
int transferred = 1;
int BULK_TRANSFER_TIMEOUT = 1000;
libusb_device_handle * handle = this->m_IntMng->getHandle();
r = libusb_bulk_transfer(handle, endpoint, buffer, transferSize, &transferred, BULK_TRANSFER_TIMEOUT);
linesPerFrame++;
if(buffer[0] != frameNumber)
{
frameNumber = buffer[0];
if(frameNumber <= 15 /*&& linesPerFrame > 10*/)
{
// std::cout<<frameNumber<<":"<<linesPerFrame<<" ";
std::cout<<"frame # "<<frameNumber<<" : "<<linesPerFrame<<" lines";
std::cout << std::endl;
}
linesPerFrame = 0;
}
}
this->m_IntMng->stopAcquisitionRoutine();
return 0;
}
int IntersonManagerTest::TestSyncBulk_LineByLine_CheckLengths()
{
// Testing Receiving Data : B-Mode, Synchronous Bulk
uInt8 motorSpeed10[] = {0xB9, 0x04, 0x00, 0x00, 0xB7, 0xA4, 0x32}; // 10.0 fps
uInt8 motorSpeed15[] = {0x7A, 0x0D, 0x00, 0x00, 0x80, 0xA4, 0x32}; // 15.0 fps
this->m_IntMng->initializeMotorSpeed(motorSpeed15);
libusb_set_debug(NULL, LIBUSB_LOG_LEVEL_NONE);
std::cout<<std::dec;
this->m_IntMng->startAcquisitionRoutine(0);
int r;
int nRows = 241;
for (int i = 0; i < 241*2; ++i) // Ask for 2 frames
{
int transferSize = 1024;
unsigned char endpoint = 0x82;
unsigned char * buffer = new unsigned char[transferSize];
int transferred = 1;
unsigned int BULK_TRANSFER_TIMEOUT = 1000;
libusb_device_handle * handle = this->m_IntMng->getHandle();
r = libusb_bulk_transfer(handle, endpoint, buffer, transferSize, &transferred, BULK_TRANSFER_TIMEOUT);
// Looking for last non-zero value
int lnz = transferSize;
for(int j = transferSize ; j >= 0 ; j--)
{
if (buffer[j] != 0)
{
lnz = j;
break;
}
}
std::cout<<"i="<<i<<", r="<<r<<", t= "<<transferred<<", lnz="<<lnz<<std::endl;
// Lines header
// for(int k = 0 ; k < 5 ; k+= 1)
// {
// std::cout<<+buffer[k]<<" "<<std::endl;
// }
// std::cout<<std::endl;
// }
}
this->m_IntMng->stopAcquisitionRoutine();
return 0;
}