forked from foxsi/SAAS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImperxStream.cpp
620 lines (550 loc) · 16.7 KB
/
ImperxStream.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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
#include "ImperxStream.hpp"
#include <iostream>
ImperxStream::ImperxStream()
: lStream()
, lPipeline( &lStream )
{
lDeviceInfo = NULL;
lDeviceParams = NULL;
lStreamParams = NULL;
}
ImperxStream::~ImperxStream()
{
Stop();
Disconnect();
}
int ImperxStream::Connect()
{
std::cout << "ImperxStream::Connect starting" << std::endl;
PvResult lResult;
// Find all GEV Devices on the network.
lSystem.SetDetectionTimeout( 2000 );
lResult = lSystem.Find();
if( !lResult.IsOK() )
{
printf( "PvSystem::Find Error: %s", lResult.GetCodeString().GetAscii() );
return -1;
}
// Get the number of GEV Interfaces that were found using GetInterfaceCount.
uint32_t lInterfaceCount = lSystem.GetInterfaceCount();
// Display information about all found interface
// For each interface, display information about all devices.
for( uint32_t x = 0; x < lInterfaceCount; x++ )
{
// get pointer to each of interface
PvInterface * lInterface = lSystem.GetInterface( x );
printf( "Interface %i\nMAC Address: %s\nIP Address: %s\nSubnet Mask: %s\n\n",
x,
lInterface->GetMACAddress().GetAscii(),
lInterface->GetIPAddress().GetAscii(),
lInterface->GetSubnetMask().GetAscii() );
// Get the number of GEV devices that were found using GetDeviceCount.
uint32_t lDeviceCount = lInterface->GetDeviceCount();
for( uint32_t y = 0; y < lDeviceCount ; y++ )
{
lDeviceInfo = lInterface->GetDeviceInfo( y );
printf( "Device %i\nMAC Address: %s\nIP Address: %s\nSerial number: %s\n\n",
y,
lDeviceInfo->GetMACAddress().GetAscii(),
lDeviceInfo->GetIPAddress().GetAscii(),
lDeviceInfo->GetSerialNumber().GetAscii() );
}
}
// Connect to the last GEV Device found.
if( lDeviceInfo != NULL )
{
printf( "ImpxerStream::Connect Connecting to %s\n",
lDeviceInfo->GetMACAddress().GetAscii() );
lResult = lDevice.Connect( lDeviceInfo );
if ( !lResult.IsOK() )
{
printf( "ImpxerStream::Connect Unable to connect to %s\n",
lDeviceInfo->GetMACAddress().GetAscii() );
return -1;
}
else
{
printf( "ImpexStream::Connect Successfully connected to %s\n",
lDeviceInfo->GetMACAddress().GetAscii() );
}
}
else
{
printf( "ImperxStream::Connect No device found\n" );
return -1;
}
// Get device parameters need to control streaming
lDeviceParams = lDevice.GetGenParameters();
std::cout << "ImperxStream::Connect Exiting." << std::endl;
return 0;
}
int ImperxStream::Connect(const std::string &IP)
{
PvResult lResult;
// Find all GEV Devices on the network.
lSystem.SetDetectionTimeout( 2000 );
lResult = lSystem.Find();
if( !lResult.IsOK() )
{
//Failed to find PvAnything
printf( "PvSystem::Find Error: %s", lResult.GetCodeString().GetAscii() );
return -1;
}
// Get the number of GEV Interfaces that were found using GetInterfaceCount.
uint32_t lInterfaceCount = lSystem.GetInterfaceCount();
// Search through interfaces for any devices
// Check devices for correct target IP
for( uint32_t x = 0; x < lInterfaceCount; x++ )
{
// get pointer to each of interface
PvInterface * lInterface = lSystem.GetInterface( x );
// Get the number of GEV devices that were found using GetDeviceCount.
uint32_t lDeviceCount = lInterface->GetDeviceCount();
for( uint32_t y = 0; y < lDeviceCount ; y++ )
{
PvDeviceInfo *tDeviceInfo = lInterface->GetDeviceInfo( y );
std::string laddress(tDeviceInfo->GetIPAddress().GetAscii());
if (!laddress.compare(IP))
{
lDeviceInfo = tDeviceInfo;
printf( "Interface %i\nMAC Address: %s\nIP Address: %s\nSubnet Mask: %s\n\n",
x,
lInterface->GetMACAddress().GetAscii(),
lInterface->GetIPAddress().GetAscii(),
lInterface->GetSubnetMask().GetAscii() );
printf( "Device %i\nMAC Address: %s\nIP Address: %s\nSerial number: %s\n\n",
y,
tDeviceInfo->GetMACAddress().GetAscii(),
tDeviceInfo->GetIPAddress().GetAscii(),
tDeviceInfo->GetSerialNumber().GetAscii() );
}
}
}
// Connect to the last GEV Device found.
if( lDeviceInfo != NULL )
{
printf( "Connecting to %s\n",
lDeviceInfo->GetMACAddress().GetAscii() );
lResult = lDevice.Connect( lDeviceInfo );
if ( !lResult.IsOK() )
{
printf( "Unable to connect to %s\n",
lDeviceInfo->GetMACAddress().GetAscii() );
return -1;
}
else
{
printf( "Successfully connected to %s\n",
lDeviceInfo->GetMACAddress().GetAscii() );
}
}
else
{
printf( "No device found\n" );
return -1;
}
// Get device parameters need to control streaming
lDeviceParams = lDevice.GetGenParameters();
return 0;
}
int ImperxStream::Initialize()
{
std::cout << "ImperxStream::Initialize starting" << std::endl;
if(lDeviceInfo == NULL)
{
std::cout << "ImperxStream::Initialize No device connected!" << std::endl;
return -1;
}
// Negotiate streaming packet size
lDevice.NegotiatePacketSize();
// Open stream - have the PvDevice do it for us
std::cout << "ImperxStream::Initialize Opening Stream to Device" << std::endl;
PvResult lResult = lStream.Open( lDeviceInfo->GetIPAddress() );
if(!lResult.IsOK()) {
std::cout << "ImperxStream::Initialize error on opening stream: " << lResult << std::endl;
return -1;
}
// Reading payload size from device
int64_t lSize = 0;
lDeviceParams->GetIntegerValue( "PayloadSize", lSize );
// Set the Buffer size and the Buffer count
std::cout << "ImperxStream::Initialize Setting Buffer Size" << std::endl;
lPipeline.SetBufferSize( static_cast<uint32_t>( lSize ) );
lPipeline.SetBufferCount( 16 ); // Increase for high frame rate without missing block IDs
// Have to set the Device IP destination to the Stream
lDevice.SetStreamDestination( lStream.GetLocalIPAddress(), lStream.GetLocalPort() );
// IMPORTANT: the pipeline needs to be "armed", or started before
// we instruct the device to send us images
std::cout << "ImperxStream::Initialize Starting Pipeline" << std::endl;
lPipeline.Start();
// Get stream parameters/stats
std::cout << "ImperxStream::Initialize Get Stream Parameters" << std::endl;
lStreamParams = lStream.GetParameters();
(lStreamParams)->SetBooleanValue("EnableMissingPacketsList",1);
// TLParamsLocked is optional but when present, it MUST be set to 1
// before sending the AcquisitionStart command
lDeviceParams->SetIntegerValue( "TLParamsLocked", 1 );
std::cout << "ImperxStream::Initialize Resetting timestamp counter..." << std::endl;
lDeviceParams->ExecuteCommand( "GevTimestampControlReset" );
std::cout << "ImperxStream::Initialize Exiting" << std::endl;
return 0;
}
int ImperxStream::Snap(cv::Mat &frame)
{
return Snap(frame, 1000);
}
int ImperxStream::Snap(cv::Mat &frame, timespec timeout)
{
int timeout_int = (int) (timeout.tv_sec*1000L + timeout.tv_nsec/1000000L);
return Snap(frame, timeout_int);
}
int ImperxStream::Snap(cv::Mat &frame, int timeout)
{
// std::cout << "ImperxStream::Snap starting" << std::endl;
// The pipeline is already "armed", we just have to tell the device
// to start sending us images
lDeviceParams->ExecuteCommand( "AcquisitionStart" );
int lWidth, lHeight, result = 0;
uint32_t dropCount;
// Retrieve next buffer
PvBuffer *lBuffer = NULL;
PvResult lOperationResult;
PvResult lResult = lPipeline.RetrieveNextBuffer( &lBuffer, timeout, &lOperationResult );
if ( lResult.IsOK() )
{
if ( lOperationResult.IsOK() )
{
// Process Buffer
if ( lBuffer->GetPayloadType() == PvPayloadTypeImage )
{
// std::cout << "ImperxStream::Snap Copying frame" << std::endl;
// Get image specific buffer interface
PvImage *lImage = lBuffer->GetImage();
// Read width, height
lWidth = (int) lImage->GetWidth();
lHeight = (int) lImage->GetHeight();
unsigned char *img = lImage->GetDataPointer();
cv::Mat lframe(lHeight,lWidth,CV_8UC1,img, cv::Mat::AUTO_STEP);
lframe.copyTo(frame);
result = 0;
}
else
{
std::cout << "ImperxStream::Snap No image in buffer" << std::endl;
result = 1;
}
}
else
{
std::cout << "ImperxStream::Snap Operation result: " << lOperationResult << std::endl;
lBuffer->GetMissingPacketIdsCount(dropCount);
std::cout << "ImperxStream::Snap Dropped " << (int) dropCount << " packets!" << std::endl;
result = 1;
}
// We have an image - do some processing (...) and VERY IMPORTANT,
// release the buffer back to the pipeline
}
else
{
std::cout << "ImperxStream::Snap Timeout: " << lResult << std::endl;
result = 1;
}
lPipeline.ReleaseBuffer( lBuffer );
// std::cout << "ImperxStream::Snap Exiting" << std::endl;
return result;
}
float ImperxStream::getTemperature()
{
long long int lTempValue = -512;
lDevice.GetGenParameters()->GetIntegerValue( "GetTemperature", lTempValue );
if (lTempValue >= 512) lTempValue = lTempValue-1024;
return (float)lTempValue/4.;
}
void ImperxStream::Stop()
{
if (lDeviceParams != NULL)
{
// Tell the device to stop sending images
std::cout << "Stop: Send AcquisitionStop\n";
lDeviceParams->ExecuteCommand( "AcquisitionStop" );
// If present reset TLParamsLocked to 0. Must be done AFTER the
// streaming has been stopped
std::cout << "Stop: set TLParamsLocked to 0\n";
lDeviceParams->SetIntegerValue( "TLParamsLocked", 0 );
}
// We stop the pipeline - letting the object lapse out of
// scope would have had the destructor do the same, but we do it anyway
if(lPipeline.IsStarted())
{
std::cout << "Stop: Stop pipeline\n";
lPipeline.Stop();
}
// Now close the stream. Also optionnal but nice to have
if(lStream.IsOpen())
{
std::cout << "Stop: Closing stream\n";
lStream.Close();
}
}
void ImperxStream::Disconnect()
{
if(lDevice.IsConnected())
{
printf( "Disconnecting device\n" );
lDevice.Disconnect();
}
}
void ImperxStream::ConfigureSnap()
{
lDeviceParams->SetEnumValue("AcquisitionMode","SingleFrame");
lDeviceParams->SetEnumValue("ExposureMode","Timed");
lDeviceParams->SetEnumValue("PixelFormat","Mono8");
lDeviceParams->SetBooleanValue("AecEnable", false);
lDeviceParams->SetBooleanValue("AgcEnable", false);
}
int ImperxStream::SetExposure(int exposureTime)
{
PvResult outcome;
int64_t temp;
if (exposureTime >= 5 && exposureTime <= 38221)
{
lDeviceParams->SetBooleanValue("ProgFrameTimeEnable", false);
outcome = lDeviceParams->SetIntegerValue("ExposureTimeRaw", exposureTime);
if (outcome.IsSuccess())
{
return 0;
}
} else if (exposureTime > 38221) {
lDeviceParams->SetBooleanValue("ProgFrameTimeEnable", true);
lDeviceParams->SetIntegerValue("ProgFrameTimeAbs", exposureTime);
//It can take a while for MaxExposure to update properly
do {
lDeviceParams->GetIntegerValue("MaxExposure", temp);
} while (exposureTime - temp > 100);
outcome = lDeviceParams->SetIntegerValue("ExposureTimeRaw", temp);
if (outcome.IsSuccess())
{
return 0;
}
}
return -1;
}
int ImperxStream::SetROISize(cv::Size size)
{
return SetROISize(size.width, size.height);
}
int ImperxStream::SetROISize(int width, int height)
{
int outcomes[2];
outcomes[0] = SetROIHeight(height);
outcomes[1] = SetROIWidth(width);
if (outcomes[0] == 0 && outcomes[1] == 0)
{
return 0;
}
return -1;
}
int ImperxStream::SetROIHeight(int height)
{
PvResult outcome;
if (height >= 1 && height <= 966)
{
outcome = lDeviceParams->SetIntegerValue("Height", height);
if (outcome.IsSuccess())
{
return 0;
}
}
return -1;
}
int ImperxStream::SetROIWidth(int width)
{
PvResult outcome;
if (width >= 8 && width <= 1296 && (width % 8) == 0)
{
outcome = lDeviceParams->SetIntegerValue("Width", width);
if (outcome.IsSuccess())
{
return 0;
}
}
return -1;
}
int ImperxStream::SetROIOffset(cv::Point offset)
{
return SetROIOffset(offset.x, offset.y);
}
int ImperxStream::SetROIOffset(int x, int y)
{
int outcomes[2];
outcomes[0] = SetROIOffsetX(x);
outcomes[1] = SetROIOffsetY(y);
if (outcomes[0] == 0 && outcomes[1] == 0)
{
return 0;
}
else
{
return -1;
}
}
int ImperxStream::SetROIOffsetX(int x)
{
PvResult outcome;
if (x >= 0 && x <= 965)
{
outcome = lDeviceParams->SetIntegerValue("OffsetX", x);
if (outcome.IsSuccess())
{
return 0;
}
}
return -1;
}
int ImperxStream::SetROIOffsetY(int y)
{
PvResult outcome;
if (y >= 0 && y <= 1295)
{
outcome = lDeviceParams->SetIntegerValue("OffsetY", y);
if (outcome.IsSuccess())
{
return 0;
}
}
return -1;
}
int ImperxStream::SetAnalogGain(int gain)
{
PvResult outcome;
if (gain >= 0 && gain <= 1023)
{
outcome = lDeviceParams->SetIntegerValue("GainRaw",gain);
if (outcome.IsSuccess())
{
return 0;
}
}
return -1;
}
int ImperxStream::SetPreAmpGain(int gain)
{
PvResult outcome;
PvString value;
switch(gain)
{
case -3:
value = "minus3dB";
break;
case 0:
value = "zero_dB";
break;
case 3:
value = "plus3dB";
break;
case 6:
value = "plus6dB";
break;
default:
return -1;
}
outcome = lDeviceParams->SetEnumValue("PreAmpRaw",value);
if (outcome.IsSuccess())
{
return 0;
}
return -1;
}
int ImperxStream::SetBlackLevel(int black)
{
PvResult outcome;
if (black >= 0 && black <= 1023)
{
outcome = lDeviceParams->SetIntegerValue("BlackLevelRaw",black);
if (outcome.IsSuccess())
{
return 0;
}
}
return -1;
}
int ImperxStream::GetExposure()
{
int64_t exposure;
lDeviceParams->GetIntegerValue("ExposureTimeRaw", exposure);
return (int) exposure;
}
cv::Size ImperxStream::GetROISize()
{
int width, height;
width = GetROIWidth();
height = GetROIHeight();
return cv::Size(width, height);
}
cv::Point ImperxStream::GetROIOffset()
{
int x, y;
x = GetROIOffsetX();
y = GetROIOffsetY();
return cv::Point(x,y);
}
int ImperxStream::GetROIHeight()
{
int64_t height;
lDeviceParams->GetIntegerValue("Height", height);
return (int) height;
}
int ImperxStream::GetROIWidth()
{
int64_t width;
lDeviceParams->GetIntegerValue("Width", width);
return (int) width;
}
int ImperxStream::GetROIOffsetX()
{
int64_t x;
lDeviceParams->GetIntegerValue("OffsetX", x);
return (int) x;
}
int ImperxStream::GetROIOffsetY()
{
int64_t y;
lDeviceParams->GetIntegerValue("OffsetY", y);
return (int) y;
}
int ImperxStream::GetAnalogGain()
{
int64_t gain;
lDeviceParams->GetIntegerValue("GainRaw", gain);
return (int) gain;
}
int ImperxStream::GetBlackLevel()
{
int64_t black;
lDeviceParams->GetIntegerValue("BlackLevelRaw", black);
return (int) black;
}
int ImperxStream::GetPreAmpGain()
{
int gain;
int64_t name;
lDeviceParams->GetEnumValue("PreAmpRaw", name);
switch((int) name)
{
case 0:
gain = -3;
break;
case 1:
gain = 0;
break;
case 2:
gain = 3;
break;
case 3:
gain = 6;
break;
default:
gain = name;
}
return gain;
}