-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathcamera.cpp
executable file
·381 lines (285 loc) · 12.4 KB
/
camera.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
#include "spinnaker_sdk_camera_driver/camera.h"
acquisition::Camera::~Camera() {
pCam_ = nullptr;
timestamp_ = 0;
}
acquisition::Camera::Camera(CameraPtr pCam) {
pCam_ = pCam;
if (pCam_->IsInitialized()) {
ROS_WARN_STREAM("Camera already initialized. Deinitializing...");
pCam_->EndAcquisition();
pCam_->DeInit();
}
lastFrameID_ = -1;
frameID_ = -1;
MASTER_ = false;
timestamp_ = 0;
GET_NEXT_IMAGE_TIMEOUT_ = EVENT_TIMEOUT_INFINITE;
}
void acquisition::Camera::init() {
pCam_->Init();
}
void acquisition::Camera::deinit() {
pCam_->DeInit();
}
ImagePtr acquisition::Camera::grab_frame() {
ImagePtr pResultImage;
try{
pResultImage = pCam_->GetNextImage(GET_NEXT_IMAGE_TIMEOUT_);
// Check if the Image is complete
if (pResultImage->IsIncomplete()) {
ROS_WARN_STREAM("Image incomplete with image status " << pResultImage->GetImageStatus() << "!");
} else {
timestamp_ = pResultImage->GetTimeStamp();
if (frameID_ >= 0) {
lastFrameID_ = frameID_;
frameID_ = pResultImage->GetFrameID();
ROS_WARN_STREAM_COND(frameID_ > lastFrameID_ + 1,"Frames are being skipped!");
} else {
frameID_ = pResultImage->GetFrameID();
ROS_ASSERT_MSG(frameID_ == 0 ,"First frame ID was not zero! Might cause sync issues later...");
}
}
ROS_DEBUG_STREAM("Grabbed frame from camera " << get_id() << " with timestamp " << timestamp_*1000);
return pResultImage;
}
catch(Spinnaker::Exception &e){
ROS_FATAL_STREAM(e.what()<<"\n Likely reason is that slaves are not triggered. Check GPIO cables\n");
}
return pResultImage;
}
// Returns last timestamp
string acquisition::Camera::get_time_stamp() {
stringstream ss;
ss<<timestamp_*1000;
return ss.str();
}
int acquisition::Camera::get_frame_id() {
return frameID_;
}
Mat acquisition::Camera::grab_mat_frame() {
try{
ImagePtr pResultImage = grab_frame();
return convert_to_mat(pResultImage);
}
catch(Spinnaker::Exception &e){
ros::shutdown();
}
}
Mat acquisition::Camera::convert_to_mat(ImagePtr pImage) {
ImagePtr convertedImage;
if (COLOR_)
{
// convertedImage = pImage->Convert(PixelFormat_BGR8); //, NEAREST_NEIGHBOR);
ImageProcessor processor;
convertedImage = processor.Convert(pImage, PixelFormat_BGR8);
}
else
{
ImageProcessor processor;
convertedImage = processor.Convert(pImage, PixelFormat_Mono8);
}
unsigned int XPadding = convertedImage->GetXPadding();
unsigned int YPadding = convertedImage->GetYPadding();
unsigned int rowsize = convertedImage->GetWidth();
unsigned int colsize = convertedImage->GetHeight();
//image data contains padding. When allocating Mat container size, you need to account for the X,Y image data padding.
Mat img;
if (COLOR_)
img = Mat(colsize + YPadding, rowsize + XPadding, CV_8UC3, convertedImage->GetData(), convertedImage->GetStride());
else
img = Mat(colsize + YPadding, rowsize + XPadding, CV_8UC1, convertedImage->GetData(), convertedImage->GetStride());
return img.clone();
// return img;
}
void acquisition::Camera::begin_acquisition() {
ROS_DEBUG_STREAM("Begin Acquisition...");
pCam_->BeginAcquisition();
}
void acquisition::Camera::end_acquisition() {
if (pCam_->GetNumImagesInUse())
ROS_WARN_STREAM("Some images still currently in use! Use image->Release() before deinitializing.");
ROS_DEBUG_STREAM("End Acquisition...");
pCam_->EndAcquisition();
}
void acquisition::Camera::setEnumValue(string setting, string value) {
INodeMap & nodeMap = pCam_->GetNodeMap();
// Retrieve enumeration node from nodemap
CEnumerationPtr ptr = nodeMap.GetNode(setting.c_str());
if (!IsAvailable(ptr) || !IsWritable(ptr))
ROS_FATAL_STREAM("Unable to set " << setting << " to " << value << " (enum retrieval). Aborting...");
// Retrieve entry node from enumeration node
CEnumEntryPtr ptrValue = ptr->GetEntryByName(value.c_str());
if (!IsAvailable(ptrValue) || !IsReadable(ptrValue))
ROS_FATAL_STREAM("Unable to set " << setting << " to " << value << " (entry retrieval). Aborting...");
// retrieve value from entry node
int64_t valueToSet = ptrValue->GetValue();
// Set value from entry node as new value of enumeration node
ptr->SetIntValue(valueToSet);
ROS_DEBUG_STREAM(setting << " set to " << value);
}
void acquisition::Camera::setIntValue(string setting, int val) {
INodeMap & nodeMap = pCam_->GetNodeMap();
CIntegerPtr ptr = nodeMap.GetNode(setting.c_str());
if (!IsAvailable(ptr) || !IsWritable(ptr)) {
ROS_FATAL_STREAM("Unable to set " << setting << " to " << val << " (ptr retrieval). Aborting...");
}
ptr->SetValue(val);
ROS_DEBUG_STREAM(setting << " set to " << val);
}
void acquisition::Camera::setFloatValue(string setting, float val) {
INodeMap & nodeMap = pCam_->GetNodeMap();
CFloatPtr ptr = nodeMap.GetNode(setting.c_str());
if (!IsAvailable(ptr) || !IsWritable(ptr)) {
ROS_FATAL_STREAM("Unable to set " << setting << " to " << val << " (ptr retrieval). Aborting...");
}
ptr->SetValue(val);
ROS_DEBUG_STREAM(setting << " set to " << val);
}
void acquisition::Camera::setBoolValue(string setting, bool val) {
INodeMap & nodeMap = pCam_->GetNodeMap();
CBooleanPtr ptr = nodeMap.GetNode(setting.c_str());
if (!IsAvailable(ptr) || !IsWritable(ptr)) {
ROS_FATAL_STREAM("Unable to set " << setting << " to " << val << " (ptr retrieval). Aborting...");
}
ptr->SetValue(val);
ROS_DEBUG_STREAM(setting << " set to " << val);
}
void acquisition::Camera::setResolutionPixels(int width, int height) {
CIntegerPtr ptrHeight=pCam_->GetNodeMap().GetNode("Height");
CIntegerPtr ptrWidth=pCam_->GetNodeMap().GetNode("Width");
if (!IsAvailable(ptrWidth) || !IsWritable(ptrWidth)){
ROS_FATAL_STREAM("Unable to set width" << "). Aborting...");
return ;
}
int64_t widthMax = ptrWidth->GetMax();
if(widthMax<width)
width=widthMax;
ptrWidth->SetValue(width);
ROS_DEBUG_STREAM("Set Width"<<width);
if (!IsAvailable(ptrHeight) || !IsWritable(ptrHeight)){
ROS_FATAL_STREAM("Unable to set height" << "). Aborting...");
return ;
}
int64_t heightMax = ptrHeight->GetMax();
if(heightMax<height)
height=heightMax;
ROS_DEBUG_STREAM("Set Height"<<height);
ptrHeight->SetValue(height);
}
void acquisition::Camera::adcBitDepth(gcstring bitDep) {
CEnumerationPtr ptrADC = pCam_->GetNodeMap().GetNode("AdcBitDepth");
if (!IsAvailable(ptrADC) || !IsWritable(ptrADC)){
ROS_FATAL_STREAM("Unable to set ADC Bit " << "). Aborting...");
return ;
}
CEnumEntryPtr ptrADCA = ptrADC->GetEntryByName(bitDep);
int currDepth=ptrADCA->GetValue();
ptrADC->SetIntValue(currDepth);
ROS_DEBUG_STREAM("BPS: "<<ptrADC->GetIntValue());
}
void acquisition::Camera::setBufferSize(int numBuf) {
INodeMap & sNodeMap = pCam_->GetTLStreamNodeMap();
CIntegerPtr StreamNode = sNodeMap.GetNode("StreamDefaultBufferCount");
int64_t bufferCount = StreamNode->GetValue();
if (!IsAvailable(StreamNode) || !IsWritable(StreamNode)){
ROS_FATAL_STREAM("Unable to set StreamMode " << "). Aborting...");
return;
}
StreamNode->SetValue(numBuf);
ROS_DEBUG_STREAM("Set Buf "<<numBuf<<endl);
}
void acquisition::Camera::setISPEnable() {
CBooleanPtr ptrISPEn=pCam_->GetNodeMap().GetNode("IspEnable");
if (!IsAvailable(ptrISPEn) || !IsWritable(ptrISPEn)){
ROS_FATAL_STREAM("Unable to set ISP Enable (node retrieval; camera " << "). Aborting...");
return;
}
ptrISPEn->SetValue("True");
}
void acquisition::Camera::setFREnable() {
CBooleanPtr ptrAcquisitionFrameRateEnable=pCam_->GetNodeMap().GetNode("AcquisitionFrameRateEnable");
if (!IsAvailable(ptrAcquisitionFrameRateEnable) || !IsWritable(ptrAcquisitionFrameRateEnable)){
ROS_FATAL_STREAM("Unable to set frameRateEnable (node retrieval; camera " << "). Aborting...");
return;
}
ptrAcquisitionFrameRateEnable->SetValue("True");
}
void acquisition::Camera::setPixelFormat(gcstring formatPic) {
CEnumerationPtr ptrPixelFormat = pCam_->GetNodeMap().GetNode(formatPic);
if ( !IsWritable(ptrPixelFormat)){
ROS_FATAL_STREAM("Unable to set Pixel Format " << "). Aborting...");
return ;
}
CEnumEntryPtr ptrPixelEnt = ptrPixelFormat->GetEntryByName("RGB8Packed");
if (!IsAvailable(ptrPixelEnt) || !IsReadable(ptrPixelEnt)){
ROS_FATAL_STREAM("Unable to set RGBPoint" << "). Aborting...");
return ;
}
int64_t colorNum = ptrPixelEnt->GetValue();
ptrPixelFormat->SetIntValue(colorNum);
ROS_DEBUG_STREAM( "Camera " << " set pixel format");
}
void acquisition::Camera::trigger() {
INodeMap & nodeMap = pCam_->GetNodeMap();
CCommandPtr ptr = nodeMap.GetNode("TriggerSoftware");
if (!IsAvailable(ptr) || !IsWritable(ptr))
ROS_FATAL_STREAM("Unable to execute trigger. Aborting...");
ROS_DEBUG_STREAM("Executing software trigger...");
ptr->Execute();
}
double acquisition::Camera::getFloatValueMax(string node_string) {
INodeMap& nodeMap = pCam_->GetNodeMap();
CFloatPtr ptrNodeValue = nodeMap.GetNode(node_string.c_str());
if (IsAvailable(ptrNodeValue)){
//cout << "\tMax " << ptrNodeValue->GetValue() << "..." << endl;
return ptrNodeValue->GetMax();
} else {
ROS_FATAL_STREAM("Node " << node_string << " not available" << endl);
return -1;
}
}
string acquisition::Camera::getTLNodeStringValue(string node_string) {
INodeMap& nodeMap = pCam_->GetTLDeviceNodeMap();
CStringPtr ptrNodeValue = nodeMap.GetNode(node_string.c_str());
if (IsReadable(ptrNodeValue)){
return string(ptrNodeValue->GetValue());
} else{
ROS_FATAL_STREAM("Node " << node_string << " not readable" << endl);
return "";
}
}
string acquisition::Camera::get_id() {
return getTLNodeStringValue("DeviceSerialNumber");
}
void acquisition::Camera::targetGreyValueTest() {
CFloatPtr ptrExpTest =pCam_->GetNodeMap().GetNode("AutoExposureTargetGreyValue");
//CFloatPtr ptrExpTest=pCam_->GetNodeMap().GetNode("ExposureTime");
if (!IsAvailable(ptrExpTest) || !IsReadable(ptrExpTest)){
ROS_FATAL_STREAM("Unable to set exposure " << "). Aborting..." << endl << endl);
return ;
}
ptrExpTest->SetValue(90.0);
ROS_INFO_STREAM("target grey mode Time: "<<ptrExpTest->GetValue()<<endl);
}
void acquisition::Camera::exposureTest() {
CFloatPtr ptrExpTest=pCam_->GetNodeMap().GetNode("ExposureTime");
if (!IsAvailable(ptrExpTest) || !IsReadable(ptrExpTest)){
ROS_FATAL_STREAM("Unable to set exposure " << "). Aborting..." << endl << endl);
return ;
}
float expTime=ptrExpTest->GetValue();
ROS_DEBUG_STREAM("Exposure Time: "<<expTime<<endl);
}
bool acquisition::Camera::verifyBinning(int binningDesired) {
int actualBinningX = (pCam_ ->SensorWidth())/(pCam_ ->Width());
int actualBinningY = (pCam_ ->SensorHeight())/(pCam_ ->Height());
if (binningDesired == actualBinningX) return true;
else return false;
}
void acquisition::Camera::calibrationParamsTest(int calibrationWidth, int calibrationHeight) {
if ( (pCam_ ->SensorWidth()) != calibrationWidth )
ROS_WARN_STREAM(" Looks like your calibration is not done at full Sensor Resolution for cam_id = "<<get_id()<<" , Sensor_Width = "<<(pCam_ ->SensorWidth()) <<" given cameraInfo params:width = "<<calibrationWidth);
if ( (pCam_ ->SensorHeight()) != calibrationHeight )
ROS_WARN_STREAM(" Looks like your calibration is not done at full Sensor Resolution for cam_id = "<<get_id()<<" , Sensor_Height= "<<(pCam_ ->SensorHeight()) <<" given cameraInfo params:height = "<<calibrationHeight);
}