-
Notifications
You must be signed in to change notification settings - Fork 0
/
EbusFlirInterface.cpp
299 lines (256 loc) · 8.66 KB
/
EbusFlirInterface.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
#include "EbusFlirInterface.h"
#include <libusb.h>
using namespace std;
EbusFlirInterface::EbusFlirInterface(int BUFFER_COUNT, int width, int height)
:lDeviceInfo(NULL),
lDevice(NULL),
lStream(NULL),
initSuccessful(false),
lSize(0),
acquisitionThread(0),
width(width),
height(height)
{
cout << libusb_get_version()->nano << endl;
isAcquisition.assignValue(false);
PvDeviceFinderWnd *lDeviceFinderWnd = new PvDeviceFinderWnd();
PvResult lResult = SelectDevice(lDeviceFinderWnd);
if( NULL != lDeviceFinderWnd )
{
delete lDeviceFinderWnd;
lDeviceFinderWnd = NULL;
}
if(lResult.IsOK())
{
lResult = Connect2Device();
if(lResult.IsOK())
{
lResult = OpenStream();
if(lResult.IsOK())
{
ConfigureStream();
CreateStreamBuffers(BUFFER_COUNT);
initSuccessful = true;
latestThermalIndex.assignValue(-1);
}
}
}
if(initSuccessful)
{
for(int i = 0; i < numBuffers; i++)
{
uint8_t * newThermal = (uint8_t *)malloc(lSize);
frameBuffers[i] = std::pair<uint8_t *, int64_t>(newThermal, 0);
}
}
}
void EbusFlirInterface::StartAcquire()
{
if(!initSuccessful)
return;
if(acquisitionThread || isAcquisition.getValue())
return;
PvGenParameterArray *lDeviceParams = lDevice->GetParameters();
PvGenCommand *lTimestampRest = dynamic_cast<PvGenCommand *>( lDeviceParams->Get( "GevTimestampControlReset" ) );
lTimestampRest->Execute();
isAcquisition.assignValue(true);
BufferList::iterator lIt = lBufferList.begin();
while ( lIt != lBufferList.end() )
{
lStream->QueueBuffer( *lIt );
lIt++;
}
acquisitionThread = new boost::thread(boost::bind(&EbusFlirInterface::AcquireThread,
this));
}
void EbusFlirInterface::StopAcquire()
{
if(!acquisitionThread || !isAcquisition.getValue())
return;
isAcquisition.assignValue(false);
acquisitionThread->join();
delete acquisitionThread;
acquisitionThread = 0;
}
EbusFlirInterface::~EbusFlirInterface()
{
if(!initSuccessful)
return;
StopAcquire();
FreeStreamBuffers();
// Close the stream
cout << "Closing stream" << endl;
lStream->Close();
PvStream::Free( lStream );
// Disconnect the device
cout << "Disconnecting device" << endl;
lDevice->Disconnect();
PvDevice::Free( lDevice );
if(initSuccessful)
{
for(int i = 0; i < numBuffers; i++)
{
free(frameBuffers[i].first);
}
}
}
PvResult EbusFlirInterface::SelectDevice( PvDeviceFinderWnd *aDeviceFinderWnd )
{
PvResult lResult;
if (NULL != aDeviceFinderWnd)
{
// Display list of GigE Vision and USB3 Vision devices
lResult = aDeviceFinderWnd->ShowModal();
if ( !lResult.IsOK() )
{
// User hit cancel
cout << "No device selected." << endl;
return lResult;
}
// Get the selected device information.
lDeviceInfo = aDeviceFinderWnd->GetSelected();
}
return lResult;
}
PvResult EbusFlirInterface::Connect2Device()
{
PvResult lResult;
// Connect to the GigE Vision or USB3 Vision device
cout << "Connecting to " << lDeviceInfo->GetDisplayID().GetAscii() << "." << endl;
lDevice = PvDevice::CreateAndConnect( lDeviceInfo, &lResult );
if ( lDevice == NULL )
{
cout << "Unable to connect to " << lDeviceInfo->GetDisplayID().GetAscii() << "." << endl;
}
return lResult;
}
PvResult EbusFlirInterface::OpenStream()
{
PvResult lResult;
// Open stream to the GigE Vision or USB3 Vision device
cout << "Opening stream to device." << endl;
lStream = PvStream::CreateAndOpen( lDeviceInfo->GetConnectionID(), &lResult );
if ( lStream == NULL )
{
cout << "Unable to stream from " << lDeviceInfo->GetDisplayID().GetAscii() << "." << endl;
}
return lResult;
}
void EbusFlirInterface::ConfigureStream()
{
// If this is a GigE Vision device, configure GigE Vision specific streaming parameters
PvDeviceGEV* lDeviceGEV = dynamic_cast<PvDeviceGEV *>( lDevice );
if ( lDeviceGEV != NULL )
{
PvStreamGEV *lStreamGEV = static_cast<PvStreamGEV *>( lStream );
// Negotiate packet size
lDeviceGEV->NegotiatePacketSize();
// Configure device streaming destination
lDeviceGEV->SetStreamDestination( lStreamGEV->GetLocalIPAddress(), lStreamGEV->GetLocalPort() );
}
}
void EbusFlirInterface::CreateStreamBuffers(int BUFFER_COUNT)
{
// Reading payload size from device
lSize = lDevice->GetPayloadSize();
// Use BUFFER_COUNT or the maximum number of buffers, whichever is smaller
uint32_t lBufferCount = ( lStream->GetQueuedBufferMaximum() < BUFFER_COUNT ) ?
lStream->GetQueuedBufferMaximum() :
BUFFER_COUNT;
// Allocate buffers
for ( uint32_t i = 0; i < lBufferCount; i++ )
{
// Create new buffer object
PvBuffer *lBuffer = new PvBuffer;
// Have the new buffer object allocate payload memory
lBuffer->Alloc( static_cast<uint32_t>( lSize ) );
// Add to external list - used to eventually release the buffers
lBufferList.push_back( lBuffer );
}
}
void EbusFlirInterface::FreeStreamBuffers()
{
// Go through the buffer list
BufferList::iterator lIt = lBufferList.begin();
while ( lIt != lBufferList.end() )
{
delete *lIt;
lIt++;
}
// Clear the buffer list
lBufferList.clear();
}
void EbusFlirInterface::AcquireThread()
{
// Get device parameters need to control streaming
PvGenParameterArray *lDeviceParams = lDevice->GetParameters();
// Map the GenICam AcquisitionStart and AcquisitionStop commands
PvGenCommand *lStart = dynamic_cast<PvGenCommand *>( lDeviceParams->Get( "AcquisitionStart" ) );
PvGenCommand *lStop = dynamic_cast<PvGenCommand *>( lDeviceParams->Get( "AcquisitionStop" ) );
// Enable streaming and send the AcquisitionStart command
cout << "Enabling streaming and sending AcquisitionStart command." << endl;
lDevice->StreamEnable();
lStart->Execute();
// Acquire images until the user instructs us to stop.
while ( isAcquisition.getValue() )
{
PvBuffer *lBuffer = NULL;
PvResult lOperationResult;
// Retrieve next buffer
PvResult lResult = lStream->RetrieveBuffer( &lBuffer, &lOperationResult, 1000 );
if ( lResult.IsOK() )
{
if ( lOperationResult.IsOK() )
{
PvPayloadType lType;
lType = lBuffer->GetPayloadType();
if ( lType == PvPayloadTypeImage )
{
// Get buffer pointer interface.
int bufferIndex = (latestThermalIndex.getValue() + 1) % numBuffers;
memcpy(frameBuffers[bufferIndex].first, lBuffer->GetDataPointer(), lSize);
boost::posix_time::ptime time = boost::posix_time::microsec_clock::local_time();
boost::posix_time::time_duration duration(time.time_of_day());
// frameBuffers[bufferIndex].second = float(lBuffer->GetTimestamp()) / 69513.0 * 33365.0;
frameBuffers[bufferIndex].second = duration.total_microseconds();
latestThermalIndex++;
}
else
{
cout << " (buffer does not contain image)";
}
}
else
{
// Non OK operational result
cout << lOperationResult.GetCodeString().GetAscii() << "\n";
}
// Re-queue the buffer in the stream object
lStream->QueueBuffer( lBuffer );
}
else
{
// Retrieve buffer failure
cout << lResult.GetCodeString().GetAscii() << "\n";
}
}
// Tell the device to stop sending images.
cout << "Sending AcquisitionStop command to the device" << endl;
lStop->Execute();
// Disable streaming on the device
cout << "Disable streaming on the controller." << endl;
lDevice->StreamDisable();
// Abort all buffers from the stream and dequeue
cout << "Aborting buffers still in stream" << endl;
lStream->AbortQueuedBuffers();
while ( lStream->GetQueuedBufferCount() > 0 )
{
PvBuffer *lBuffer = NULL;
PvResult lOperationResult;
lStream->RetrieveBuffer( &lBuffer, &lOperationResult );
}
}
bool EbusFlirInterface::IsOK()
{
return initSuccessful;
}