-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlaubaslerusbcamera.cpp
232 lines (193 loc) · 9.16 KB
/
laubaslerusbcamera.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
#include "laubaslerusbcamera.h"
using namespace Pylon;
using namespace Basler_UsbCameraParams;
bool LAUBaslerUSBCamera::libraryInitializedFlag = false;
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
LAUBaslerUSBCamera::LAUBaslerUSBCamera(QObject *parent) : QObject(parent), numRows(0), numCols(0), bitsPerPixel(12), isConnected(false), camera(nullptr)
{
// INITIALIZE CAMERA LIBRARY AND UNLOAD IF ERROR
if (!libraryInitializedFlag) {
PylonInitialize();
libraryInitializedFlag = true;
}
// KEEP TRYING TO FIND CAMERAS WHILE LIBRARY SEARCHES NETWORK
errorString = QString("No cameras found.");
// GET A LIST OF AVAILABLE CAMERAS
QStringList availableCameralist = cameraList();
if (numAvailableCameras) {
// NOW SEE IF WE CAN CONNECT TO FIRST DETECTED CAMERA
if (connectToHost(availableCameralist.first())) {
errorString = QString();
isConnected = true;
} else {
disconnectFromHost();
isConnected = false;
}
}
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
LAUBaslerUSBCamera::~LAUBaslerUSBCamera()
{
// DISCONNECT FROM CAMERA
if (isConnected) {
disconnectFromHost();
}
PylonTerminate();
qDebug() << QString("LAUBaslerUSBCamera::~LAUBaslerUSBCamera()");
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
QStringList LAUBaslerUSBCamera::cameraList()
{
// ONLY LOOK FOR CAMERAS SUPPORTED BY CAMERA_T.
CDeviceInfo info;
info.SetDeviceClass(CBaslerUsbInstantCamera::DeviceClass());
// GET THE TRANSPORT LAYER FACTORY.
CTlFactory &tlFactory = CTlFactory::GetInstance();
// GET ALL ATTACHED DEVICES AND EXIT APPLICATION IF NO DEVICE IS FOUND.
DeviceInfoList_t devices;
numAvailableCameras = tlFactory.EnumerateDevices(devices);
// PRINT A LIST OF THE CONNECTED CAMERAS
QStringList stringList;
for (unsigned int i = 0; i < numAvailableCameras; i++) {
stringList << QString(devices[i].GetFriendlyName());
}
return (stringList);
}
/******************************************************************************/
/******************************************************************************/
/******************************************************************************/
bool LAUBaslerUSBCamera::connectToHost(QString idString)
{
// ONLY LOOK FOR CAMERAS SUPPORTED BY CAMERA_T.
CDeviceInfo info;
info.SetDeviceClass(CBaslerUsbInstantCamera::DeviceClass());
// GET THE TRANSPORT LAYER FACTORY.
CTlFactory &tlFactory = CTlFactory::GetInstance();
// GET ALL ATTACHED DEVICES AND EXIT APPLICATION IF NO DEVICE IS FOUND.
DeviceInfoList_t devices;
numAvailableCameras = tlFactory.EnumerateDevices(devices);
try {
// PRINT A LIST OF THE CONNECTED CAMERAS
for (unsigned int i = 0; i < numAvailableCameras; i++) {
if (QString(devices[i].GetFriendlyName()) == idString) {
// CREATE AN INSTANT CAMERA OBJECT WITH THE FIRST FOUND CAMERA DEVICE MATCHING THE SPECIFIED DEVICE CLASS.
camera = new CBaslerUsbInstantCamera(CTlFactory::GetInstance().CreateFirstDevice(info));
// OPEN THE CAMERA FOR ACCESSING THE PARAMETERS.
camera->Open();
if (camera->IsOpen()) {
// GET THE MAKE, MODEL, AND SERIAL NUMBER STRINGS
makeString = QString(camera->DeviceVendorName.GetValue());
modelString = QString(camera->DeviceModelName.GetValue());
serialString = QString(camera->DeviceFirmwareVersion.GetValue());
// SET THE REGION OF INTEREST TO THE FULL SENSOR
if (IsWritable(camera->OffsetX)) {
camera->OffsetX.SetValue(camera->OffsetX.GetMin());
}
if (IsWritable(camera->OffsetY)) {
camera->OffsetY.SetValue(camera->OffsetY.GetMin());
}
if (IsWritable(camera->Width)) {
camera->Width.SetValue(camera->Width.GetMax());
numCols = camera->Width.GetValue();
}
if (IsWritable(camera->Height)) {
camera->Height.SetValue(camera->Height.GetMax());
numRows = camera->Height.GetValue();
}
if (IsWritable(camera->PixelFormat)) {
camera->PixelFormat.SetValue(PixelFormat_Mono8);
bitsPerPixel = 10;
}
if (IsWritable(camera->ExposureAuto)) {
camera->ExposureAuto.SetValue(ExposureAuto_Continuous);
}
// THE PARAMETER MAXNUMBUFFER CAN BE USED TO CONTROL THE COUNT OF BUFFERS
// ALLOCATED FOR GRABBING. THE DEFAULT VALUE OF THIS PARAMETER IS 10.
camera->MaxNumBuffer = 8;
// PUT THE CAMERA IN FREE RUN MODE
camera->TriggerMode.SetValue(TriggerMode_Off);
// TELL THE USER WE HAVE SUCCESSFULLY CONNECTED TO THE CAMERA
return (true);
}
return (false);
}
}
} catch (const GenericException &e) {
errorString = QString("Pylon exception:").append(QString(e.GetDescription()));
emit emitError(errorString);
}
// IF WE MAKE IT THIS FAR, BUT WE ARE NOT CONNECTED, THEN THE CAMERA STRING WASN'T FOUND
return (false);
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
bool LAUBaslerUSBCamera::disconnectFromHost()
{
if (camera && camera->IsOpen()) {
camera->Close();
if (camera->IsOpen() == false) {
return (true);
}
}
return (true);
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
void LAUBaslerUSBCamera::setExposure(int microseconds)
{
// SET THE CAMERA'S EXPOSURE
if (camera && camera->IsOpen()) {
camera->ExposureTime.SetValue(microseconds);
}
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
void LAUBaslerUSBCamera::setFrame(LAUMemoryObject object)
{
if (object.isValid()) {
// RESET ERROR CODE
object.setElapsed(0);
// CALCULATE HOW MANY OF FRAMES WE NEED TO GRAB
unsigned int numFrmsToGrab = object.frames();
// THE PARAMETER MAXNUMBUFFER CAN BE USED TO CONTROL THE COUNT OF BUFFERS
// ALLOCATED FOR GRABBING. THE DEFAULT VALUE OF THIS PARAMETER IS 10.
camera->MaxNumBuffer = numFrmsToGrab;
try {
// KEEP TRACK OF HOW MANY FRAMES WE HAVE SO FAR COLLECTED
unsigned int counter = 0;
// START THE GRABBING OF NUMBTCS IMAGES.
camera->StartGrabbing(numFrmsToGrab);
// THIS SMART POINTER WILL RECEIVE THE GRAB RESULT DATA.
CGrabResultPtr ptrGrabResult;
// CAMERA.STOPGRABBING() IS CALLED AUTOMATICALLY BY THE RETRIEVERESULT() METHOD
// WHEN C_COUNTOFIMAGESTOGRAB IMAGES HAVE BEEN RETRIEVED.
while (camera->IsGrabbing() && counter < object.frames()) {
// WAIT FOR AN IMAGE AND THEN RETRIEVE IT. A TIMEOUT OF 5000 MS IS USED.
camera->RetrieveResult(5000, ptrGrabResult, TimeoutHandling_ThrowException);
// IMAGE GRABBED SUCCESSFULLY?
if (ptrGrabResult->GrabSucceeded()) {
memcpy(object.constFrame(counter), ptrGrabResult->GetBuffer(), object.length());
} else {
errorString = QString("Error grabbing frame:").append(QString(ptrGrabResult->GetErrorCode())).append(QString(ptrGrabResult->GetErrorDescription()));
object.setElapsed((unsigned int)(255));
}
// INCREMENT THE COUNTER FOR THE NEXT FRAME
counter++;
}
} catch (const GenericException &e) {
errorString = QString("Pylon exception:").append(QString(e.GetDescription()));
emit emitError(errorString);
}
}
// SEND MEMORY OBJECT BACK TO THE USER
emit emitFrame(object);
}