-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathpylon_camera_usb.hpp
448 lines (406 loc) · 16 KB
/
pylon_camera_usb.hpp
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
/******************************************************************************
* Software License Agreement (BSD License)
*
* Copyright (C) 2016, Magazino GmbH. All rights reserved.
*
* Improved by drag and bot GmbH (www.dragandbot.com), 2019
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the names of Magazino GmbH nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
#ifndef PYLON_CAMERA_INTERNAL_USB_H_
#define PYLON_CAMERA_INTERNAL_USB_H_
#include <string>
#include <vector>
#include <pylon_camera/internal/pylon_camera.h>
#include <pylon/usb/BaslerUsbInstantCamera.h>
namespace pylon_camera
{
struct USBCameraTrait
{
typedef Pylon::CBaslerUsbInstantCamera CBaslerInstantCameraT;
typedef Basler_UsbCameraParams::ExposureAutoEnums ExposureAutoEnums;
typedef Basler_UsbCameraParams::GainAutoEnums GainAutoEnums;
typedef Basler_UsbCameraParams::PixelFormatEnums PixelFormatEnums;
typedef Basler_UsbCameraParams::PixelSizeEnums PixelSizeEnums;
typedef GenApi::IFloat AutoTargetBrightnessType;
typedef GenApi::IFloat GainType;
typedef double AutoTargetBrightnessValueType;
typedef Basler_UsbCameraParams::ShutterModeEnums ShutterModeEnums;
typedef Basler_UsbCameraParams::UserOutputSelectorEnums UserOutputSelectorEnums;
typedef Basler_UsbCameraParams::AcquisitionStatusSelectorEnums AcquisitionStatusSelectorEnums;
typedef Basler_UsbCameraParams::SensorReadoutModeEnums SensorReadoutModeEnums;
typedef Basler_UsbCameraParams::TriggerSelectorEnums TriggerSelectorEnums;
typedef Basler_UsbCameraParams::TriggerModeEnums TriggerModeEnums;
typedef Basler_UsbCameraParams::TriggerSourceEnums TriggerSourceEnums;
typedef Basler_UsbCameraParams::TriggerActivationEnums TriggerActivationEnums;
typedef Basler_UsbCameraParams::LineSelectorEnums LineSelectorEnums;
typedef Basler_UsbCameraParams::LineModeEnums LineModeEnums;
typedef Basler_UsbCameraParams::DeviceLinkThroughputLimitModeEnums DeviceLinkThroughputLimitModeEnums;
typedef Basler_UsbCameraParams::AutoFunctionROISelectorEnums AutoFunctionROISelectorEnums;
typedef Basler_UsbCameraParams::BalanceWhiteAutoEnums BalanceWhiteAutoEnums;
typedef Basler_UsbCameraParams::LightSourcePresetEnums LightSourcePresetEnums;
typedef Basler_UsbCameraParams::LineSourceEnums LineSourceEnums;
typedef Basler_UsbCameraParams::DemosaicingModeEnums DemosaicingModeEnums;
typedef Basler_UsbCameraParams::PgiModeEnums PgiModeEnums;
typedef Basler_UsbCameraParams::UserSetSelectorEnums UserSetSelectorEnums;
typedef Basler_UsbCameraParams::UserSetDefaultEnums UserSetDefaultSelectorEnums;
typedef Basler_UsbCameraParams::LineFormatEnums LineFormatEnums;
static inline AutoTargetBrightnessValueType convertBrightness(const int& value)
{
return value / 255.0;
}
};
typedef PylonCameraImpl<USBCameraTrait> PylonUSBCamera;
template <>
bool PylonUSBCamera::applyCamSpecificStartupSettings(const PylonCameraParameter& parameters)
{
try
{
if (parameters.startup_user_set_ == "Default")
{
// Remove all previous settings (sequencer etc.)
// Default Setting = Free-Running
cam_->UserSetSelector.SetValue(Basler_UsbCameraParams::UserSetSelector_Default);
cam_->UserSetLoad.Execute();
// UserSetSelector_Default overrides Software Trigger Mode !!
cam_->TriggerSource.SetValue(Basler_UsbCameraParams::TriggerSource_Software);
cam_->TriggerMode.SetValue(Basler_UsbCameraParams::TriggerMode_On);
/* Thresholds for the AutoExposure Functions:
* - lower limit can be used to get rid of changing light conditions
* due to 50Hz lamps (-> 20ms cycle duration)
* - upper limit is to prevent motion blur
*/
double upper_lim = std::min(parameters.auto_exp_upper_lim_,
cam_->ExposureTime.GetMax());
cam_->AutoExposureTimeLowerLimit.SetValue(cam_->ExposureTime.GetMin());
cam_->AutoExposureTimeUpperLimit.SetValue(upper_lim);
cam_->AutoGainLowerLimit.SetValue(cam_->Gain.GetMin());
cam_->AutoGainUpperLimit.SetValue(cam_->Gain.GetMax());
// The gain auto function and the exposure auto function can be used at the same time. In this case,
// however, you must also set the Auto Function Profile feature.
// cam_->AutoFunctionProfile.SetValue(Basler_UsbCameraParams::AutoFunctionProfile_MinimizeGain);
if ( GenApi::IsAvailable(cam_->BinningHorizontal) &&
GenApi::IsAvailable(cam_->BinningVertical) )
{
ROS_INFO_STREAM("Cam has binning range: x(hz) = ["
<< cam_->BinningHorizontal.GetMin() << " - "
<< cam_->BinningHorizontal.GetMax() << "], y(vt) = ["
<< cam_->BinningVertical.GetMin() << " - "
<< cam_->BinningVertical.GetMax() << "].");
}
else
{
ROS_INFO_STREAM("Cam does not support binning.");
}
ROS_INFO_STREAM("Cam has exposure time range: [" << cam_->ExposureTime.GetMin()
<< " - " << cam_->ExposureTime.GetMax()
<< "] measured in microseconds.");
ROS_INFO_STREAM("Cam has gain range: [" << cam_->Gain.GetMin()
<< " - " << cam_->Gain.GetMax()
<< "] measured in dB.");
ROS_INFO_STREAM("Cam has gammma range: ["
<< cam_->Gamma.GetMin() << " - "
<< cam_->Gamma.GetMax() << "].");
ROS_INFO_STREAM("Cam has pylon auto brightness range: ["
<< cam_->AutoTargetBrightness.GetMin() * 255 << " - "
<< cam_->AutoTargetBrightness.GetMax() * 255
<< "] which is the average pixel intensity.");
}
else if (parameters.startup_user_set_ == "UserSet1")
{
cam_->UserSetSelector.SetValue(Basler_UsbCameraParams::UserSetSelector_UserSet1);
cam_->UserSetLoad.Execute();
ROS_WARN("User Set 1 Loaded");
}
else if (parameters.startup_user_set_ == "UserSet2")
{
cam_->UserSetSelector.SetValue(Basler_UsbCameraParams::UserSetSelector_UserSet2);
cam_->UserSetLoad.Execute();
ROS_WARN("User Set 2 Loaded");
}
else if (parameters.startup_user_set_ == "UserSet3")
{
cam_->UserSetSelector.SetValue(Basler_UsbCameraParams::UserSetSelector_UserSet3);
cam_->UserSetLoad.Execute();
ROS_WARN("User Set 3 Loaded");
}
else if (parameters.startup_user_set_ == "CurrentSetting")
{
ROS_WARN("No User Set Is selected, Camera current setting will be used");
}
}
catch ( const GenICam::GenericException &e )
{
ROS_ERROR_STREAM("Error applying cam specific startup setting for USB cameras: "
<< e.GetDescription());
return false;
}
return true;
}
template <>
bool PylonUSBCamera::setupSequencer(const std::vector<float>& exposure_times,
std::vector<float>& exposure_times_set)
{
try
{
// Runtime Sequencer: cam_->IsGrabbing() ? cam_->StopGrabbing(); //10ms
if ( GenApi::IsWritable(cam_->SequencerMode) )
{
cam_->SequencerMode.SetValue(Basler_UsbCameraParams::SequencerMode_Off);
}
else
{
ROS_ERROR("Sequencer Mode not writable");
}
cam_->SequencerConfigurationMode.SetValue(Basler_UsbCameraParams::SequencerConfigurationMode_On);
// **** valid for all sets: reset on software signal 1 ****
int64_t initial_set = cam_->SequencerSetSelector.GetMin();
cam_->SequencerSetSelector.SetValue(initial_set);
cam_->SequencerPathSelector.SetValue(0);
cam_->SequencerSetNext.SetValue(initial_set);
cam_->SequencerTriggerSource.SetValue(Basler_UsbCameraParams::SequencerTriggerSource_SoftwareSignal1);
// advance on Frame Start
cam_->SequencerPathSelector.SetValue(1);
cam_->SequencerTriggerSource.SetValue(Basler_UsbCameraParams::SequencerTriggerSource_FrameStart);
// ********************************************************
for ( std::size_t i = 0; i < exposure_times.size(); ++i )
{
if ( i > 0 )
{
cam_->SequencerSetSelector.SetValue(i);
}
if ( i == exposure_times.size() - 1 ) // last frame
{
cam_->SequencerSetNext.SetValue(0);
}
else
{
cam_->SequencerSetNext.SetValue(i + 1);
}
float reached_exposure;
setExposure(exposure_times.at(i), reached_exposure);
exposure_times_set.push_back(reached_exposure / 1000000.);
cam_->SequencerSetSave.Execute();
}
// config finished
cam_->SequencerConfigurationMode.SetValue(Basler_UsbCameraParams::SequencerConfigurationMode_Off);
cam_->SequencerMode.SetValue(Basler_UsbCameraParams::SequencerMode_On);
}
catch ( const GenICam::GenericException &e )
{
ROS_ERROR_STREAM("ERROR while initializing pylon sequencer: "
<< e.GetDescription());
return false;
}
return true;
}
template <>
GenApi::IFloat& PylonUSBCamera::exposureTime()
{
if ( GenApi::IsAvailable(cam_->ExposureTime) )
{
return cam_->ExposureTime;
}
else
{
throw std::runtime_error("Error while accessing ExposureTime in PylonUSBCamera");
}
}
template <>
USBCameraTrait::GainType& PylonUSBCamera::gain()
{
if ( GenApi::IsAvailable(cam_->Gain) )
{
return cam_->Gain;
}
else
{
throw std::runtime_error("Error while accessing Gain in PylonUSBCamera");
}
}
template <>
bool PylonUSBCamera::setGamma(const float& target_gamma, float& reached_gamma)
{
if ( !GenApi::IsAvailable(cam_->Gamma) )
{
ROS_ERROR_STREAM("Error while trying to set gamma: cam.Gamma NodeMap is"
<< " not available!");
return false;
}
try
{
float gamma_to_set = target_gamma;
if ( gamma().GetMin() > gamma_to_set )
{
gamma_to_set = gamma().GetMin();
ROS_WARN_STREAM("Desired gamma unreachable! Setting to lower limit: "
<< gamma_to_set);
}
else if ( gamma().GetMax() < gamma_to_set )
{
gamma_to_set = gamma().GetMax();
ROS_WARN_STREAM("Desired gamma unreachable! Setting to upper limit: "
<< gamma_to_set);
}
gamma().SetValue(gamma_to_set);
reached_gamma = currentGamma();
}
catch ( const GenICam::GenericException &e )
{
ROS_ERROR_STREAM("An exception while setting target gamma to "
<< target_gamma << " occurred: " << e.GetDescription());
return false;
}
return true;
}
template <>
GenApi::IFloat& PylonUSBCamera::autoExposureTimeLowerLimit()
{
if ( GenApi::IsAvailable(cam_->AutoExposureTimeLowerLimit) )
{
return cam_->AutoExposureTimeLowerLimit;
}
else
{
throw std::runtime_error("Error while accessing AutoExposureTimeLowerLimit in PylonUSBCamera");
}
}
template <>
GenApi::IFloat& PylonUSBCamera::autoExposureTimeUpperLimit()
{
if ( GenApi::IsAvailable(cam_->AutoExposureTimeUpperLimit) )
{
return cam_->AutoExposureTimeUpperLimit;
}
else
{
throw std::runtime_error("Error while accessing AutoExposureTimeUpperLimit in PylonUSBCamera");
}
}
template <>
USBCameraTrait::GainType& PylonUSBCamera::autoGainLowerLimit()
{
if ( GenApi::IsAvailable(cam_->AutoGainLowerLimit) )
{
return cam_->AutoGainLowerLimit;
}
else
{
throw std::runtime_error("Error while accessing AutoGainLowerLimit in PylonUSBCamera");
}
}
template <>
USBCameraTrait::GainType& PylonUSBCamera::autoGainUpperLimit()
{
if ( GenApi::IsAvailable(cam_->AutoGainUpperLimit) )
{
return cam_->AutoGainUpperLimit;
}
else
{
throw std::runtime_error("Error while accessing AutoGainUpperLimit in PylonUSBCamera");
}
}
template <>
GenApi::IFloat& PylonUSBCamera::resultingFrameRate()
{
if ( GenApi::IsAvailable(cam_->ResultingFrameRate) )
{
return cam_->ResultingFrameRate;
}
else
{
throw std::runtime_error("Error while accessing ResultingFrameRate in PylonUSBCamera");
}
}
template <>
USBCameraTrait::AutoTargetBrightnessType& PylonUSBCamera::autoTargetBrightness()
{
if ( GenApi::IsAvailable(cam_->AutoTargetBrightness) )
{
return cam_->AutoTargetBrightness;
}
else
{
throw std::runtime_error("Error while accessing AutoTargetBrightness in PylonUSBCamera");
}
}
template <>
std::string PylonUSBCamera::typeName() const
{
return "USB";
}
template <>
std::string PylonUSBCamera::setAcquisitionFrameCount(const int& frameCount)
{
try
{
if ( GenApi::IsAvailable(cam_->AcquisitionBurstFrameCount) )
{
cam_->AcquisitionBurstFrameCount.SetValue(frameCount);
return "done";
}
else
{
ROS_ERROR_STREAM("Error while trying to change the Acquisition frame count. The connected Camera not supporting this feature");
return "The connected Camera not supporting this feature";
}
}
catch ( const GenICam::GenericException &e )
{
ROS_ERROR_STREAM("An exception while changing Acquisition frame count occurred:" << e.GetDescription());
return e.GetDescription();
}
}
template <>
int PylonUSBCamera::getAcquisitionFrameCount()
{
try
{
if ( GenApi::IsAvailable(cam_->AcquisitionBurstFrameCount) )
{
return static_cast<int>(cam_->AcquisitionBurstFrameCount.GetValue());
}
else
{
return -10000;
}
}
catch ( const GenICam::GenericException &e )
{
return -20000;
}
}
template <>
std::string PylonUSBCamera::setGammaSelector(const int& gammaSelector)
{
return "Error, the connect camera not supporting this feature";
}
template <>
std::string PylonUSBCamera::gammaEnable(const bool& enable)
{
return "Error, the connect camera not supporting this feature";
}
} // namespace pylon_camera
#endif // PYLON_CAMERA_INTERNAL_USB_H_