Subversion Repositories seema-scanner

Rev

Rev 157 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 jakw 1
#include "CameraPointGrey.h"
2
#include <cstring>
207 flgw 3
#include <assert.h>
1 jakw 4
 
26 jakw 5
//#include <sys/ioctl.h>
6
//#include <linux/usbdevice_fs.h>
7
 
1 jakw 8
void PrintError(FlyCapture2::Error error){
207 flgw 9
    //TODOassert(false);
1 jakw 10
    error.PrintErrorTrace();
11
}
12
 
13
vector<CameraInfo> CameraPointGrey::getCameraList(){
14
 
15
    FlyCapture2::Error error;
16
 
17
    FlyCapture2::BusManager busManager;
18
    unsigned int numCameras;
19
    error = busManager.GetNumOfCameras(&numCameras);
20
 
21
    vector<CameraInfo> ret;
22
 
23
    if (error != FlyCapture2::PGRERROR_OK){
24
        PrintError(error);
25
        return ret;
26
    }
27
 
28
    for (unsigned int i=0; i < numCameras; i++){
29
        FlyCapture2::PGRGuid guid;
30
        error = busManager.GetCameraFromIndex(i, &guid);
31
        if (error != FlyCapture2::PGRERROR_OK)
32
            PrintError(error);
33
 
34
        // Connect to camera
35
        FlyCapture2::Camera cam;
36
        error = cam.Connect(&guid);
37
        if (error != FlyCapture2::PGRERROR_OK)
38
            PrintError( error );
39
 
19 jakw 40
        // Get camera information
1 jakw 41
        FlyCapture2::CameraInfo camInfo;
42
        error = cam.GetCameraInfo(&camInfo);
43
        if (error != FlyCapture2::PGRERROR_OK)
44
            PrintError( error );
45
 
46
        CameraInfo camera;
53 jakw 47
        camera.vendor = "Point Grey";
48
        camera.model = camInfo.modelName;
1 jakw 49
        camera.busID = camInfo.nodeNumber;
53 jakw 50
        camera.serialNumber = camInfo.serialNumber;
1 jakw 51
 
52
        ret.push_back(camera);
53
    }
54
 
55
    return ret;
56
}
57
 
18 jakw 58
CameraPointGrey::CameraPointGrey(unsigned int camNum, CameraTriggerMode triggerMode) : Camera(triggerMode){
1 jakw 59
 
26 jakw 60
//    // Reset USB bus
61
//    std::cout << "Resetting USB device %s\n";
62
//    int rc = ioctl(fd, USBDEVFS_RESET, 0);
63
//    if (rc < 0)
64
//        perror("Error in ioctl");
65
//    else
66
//        printf("Reset successful\n");
67
 
68
 
1 jakw 69
    FlyCapture2::Error error;
70
 
71
    // Connect to camera
72
    FlyCapture2::BusManager busManager;
26 jakw 73
 
1 jakw 74
    FlyCapture2::PGRGuid camGuid;
23 jakw 75
 
18 jakw 76
    busManager.GetCameraFromIndex(camNum, &camGuid);
1 jakw 77
    error = cam.Connect(&camGuid);
78
    if (error != FlyCapture2::PGRERROR_OK)
79
        PrintError(error);
80
 
20 jakw 81
//    // Configure DCAM video mode and frame rate
82
//    FlyCapture2::VideoMode videoMode = FlyCapture2::VIDEOMODE_1280x960Y8;
83
//    FlyCapture2::FrameRate frameRate = FlyCapture2::FRAMERATE_7_5;
84
//    error = cam.SetVideoModeAndFrameRate(videoMode, frameRate);
85
//    if (error != FlyCapture2::PGRERROR_OK)
86
//        PrintError(error);
4 jakw 87
 
20 jakw 88
    // Configure Format7 mode
89
    FlyCapture2::Format7ImageSettings format7Settings;
90
    format7Settings.mode = FlyCapture2::MODE_0;
121 jakw 91
    format7Settings.pixelFormat = FlyCapture2::PIXEL_FORMAT_RAW8;
20 jakw 92
    format7Settings.width = 3376;
93
    format7Settings.height = 2704;
94
    format7Settings.offsetX = 0;
95
    format7Settings.offsetY = 0;
96
 
134 jakw 97
//    format7Settings.width = 2496;
98
//    format7Settings.height = 1888;
99
//    format7Settings.offsetX = (3376-2496)/2;
100
//    format7Settings.offsetY = (2704-1888)/2;
101
 
20 jakw 102
    // Validate and set mode
103
    FlyCapture2::Format7PacketInfo packetInfo;
104
    bool valid;
105
    error = cam.ValidateFormat7Settings(&format7Settings, &valid, &packetInfo);
106
    if (error != FlyCapture2::PGRERROR_OK)
107
        PrintError(error);
110 jakw 108
    // packetsize configures maximum frame rate
20 jakw 109
    error = cam.SetFormat7Configuration(&format7Settings, packetInfo.recommendedBytesPerPacket);
110
    if (error != FlyCapture2::PGRERROR_OK)
111
        PrintError(error);
112
 
113
    // Configure general
114
    FlyCapture2::FC2Config config;
115
    config.numBuffers = 5;
116
    config.grabTimeout = 1000; // retrieveBuffer() timeout in ms
117
    config.grabMode = FlyCapture2::DROP_FRAMES;
118
    config.isochBusSpeed = FlyCapture2::BUSSPEED_S_FASTEST;
119
    config.highPerformanceRetrieveBuffer = true;
120
    //config.asyncBusSpeed = FlyCapture2::BUSSPEED_S_FASTEST;
121
    error = cam.SetConfiguration(&config);
122
    if (error != FlyCapture2::PGRERROR_OK)
123
        PrintError(error);
124
 
125
 
1 jakw 126
    // Get the camera information
127
    FlyCapture2::CameraInfo camInfo;
128
    error = cam.GetCameraInfo(&camInfo);
129
    if (error != FlyCapture2::PGRERROR_OK)
130
        PrintError(error);
131
 
4 jakw 132
    std::cout << camInfo.vendorName << "  " << camInfo.modelName << "  " << camInfo.serialNumber << std::endl;
1 jakw 133
 
74 jakw 134
    // Turn off auto settings
45 jakw 135
    FlyCapture2::Property property;
74 jakw 136
    property.type = FlyCapture2::AUTO_EXPOSURE;
137
    property.onOff = false;
138
    error = cam.SetProperty(&property);
139
    if (error != FlyCapture2::PGRERROR_OK)
140
        PrintError(error);
141
 
142
    property.type = FlyCapture2::GAMMA;
45 jakw 143
    property.onOff = true;
74 jakw 144
    property.absControl = true;
145
    property.absValue = 1.0;
146
    error = cam.SetProperty(&property);
147
    if (error != FlyCapture2::PGRERROR_OK)
148
        PrintError(error);
149
 
120 jakw 150
    // Set frame rate to 6
110 jakw 151
    property.type = FlyCapture2::FRAME_RATE;
152
    property.onOff = true;
153
    property.absControl = true;
154
    property.autoManualMode = false;
155
    property.absValue = 6.0;
156
    error = cam.SetProperty(&property);
157
    if (error != FlyCapture2::PGRERROR_OK)
158
        PrintError(error);
159
 
120 jakw 160
    // Set white balance to match LED projector light
74 jakw 161
    property.type = FlyCapture2::WHITE_BALANCE;
120 jakw 162
    property.onOff = true;
45 jakw 163
    property.absControl = false;
164
    property.autoManualMode = false;
165
    property.valueA = 666; //red
166
    property.valueB = 777; //blue
167
    error = cam.SetProperty(&property);
168
    if (error != FlyCapture2::PGRERROR_OK)
169
        PrintError(error);
170
 
1 jakw 171
    // Set reasonable default settings
172
    CameraSettings settings;
18 jakw 173
    //settings.shutter = 8.33;
128 jakw 174
    settings.shutter = 66.66;
1 jakw 175
    settings.gain = 0.0;
176
    this->setCameraSettings(settings);
177
 
178
    return;
179
}
18 jakw 180
 
181
CameraSettings CameraPointGrey::getCameraSettings(){
182
 
183
    FlyCapture2::Property property;
184
 
185
    // Get settings:
186
    CameraSettings settings;
187
 
188
    property.type = FlyCapture2::SHUTTER;
189
    cam.GetProperty(&property);
190
    settings.shutter = property.absValue;
191
 
192
    property.type = FlyCapture2::GAIN;
193
    cam.GetProperty(&property);
194
    settings.gain = property.absValue;
195
 
196
    return settings;
197
}
198
 
199
void CameraPointGrey::setCameraSettings(CameraSettings settings){
200
 
201
    FlyCapture2::Property property;
202
    property.onOff = true;
203
    property.absControl = true;
204
 
205
    property.type = FlyCapture2::SHUTTER;
206
    property.absValue = settings.shutter;
207
    cam.SetProperty(&property);
208
 
209
    property.type = FlyCapture2::GAIN;
210
    property.absValue = settings.gain;
211
    cam.SetProperty(&property);
212
 
213
}
214
 
1 jakw 215
void CameraPointGrey::startCapture(){
216
 
20 jakw 217
    FlyCapture2::Error error;
1 jakw 218
 
18 jakw 219
    if(triggerMode == triggerModeHardware){
220
        // Configure for hardware trigger
221
        FlyCapture2::TriggerMode triggerMode;
222
        triggerMode.onOff = true;
223
        triggerMode.polarity = 0;
224
        triggerMode.source = 0;
225
        triggerMode.mode = 14;
226
        error = cam.SetTriggerMode(&triggerMode);
227
        if (error != FlyCapture2::PGRERROR_OK)
228
            PrintError(error);
1 jakw 229
 
18 jakw 230
    } else if(triggerMode == triggerModeSoftware){
231
        // Configure software trigger
232
        FlyCapture2::TriggerMode triggerMode;
233
        triggerMode.onOff = true;
234
        triggerMode.polarity = 0;
235
        triggerMode.source = 7; // software
236
        triggerMode.mode = 0;
237
        error = cam.SetTriggerMode(&triggerMode);
238
        if (error != FlyCapture2::PGRERROR_OK)
239
            PrintError(error);
240
    }
241
 
1 jakw 242
    // Set the trigger timeout to 1000 ms
243
    FlyCapture2::FC2Config config;
244
    config.grabTimeout = 1000;
245
    error = cam.SetConfiguration(&config);
246
    if (error != FlyCapture2::PGRERROR_OK)
247
        PrintError(error);
248
 
207 flgw 249
    // TODO this segfaults if another instance is running
250
    if(!capturing)
251
        error = cam.StartCapture();
20 jakw 252
    if (error != FlyCapture2::PGRERROR_OK)
253
        PrintError(error);
254
 
1 jakw 255
    capturing = true;
256
}
257
 
258
void CameraPointGrey::stopCapture(){
259
 
18 jakw 260
    FlyCapture2::Error error = cam.StopCapture();
1 jakw 261
    if (error != FlyCapture2::PGRERROR_OK)
262
        PrintError(error);
263
 
18 jakw 264
    capturing = false;
1 jakw 265
}
266
 
23 jakw 267
void CameraPointGrey::trigger(){
268
 
269
    FlyCapture2::Error error;
270
 
271
    // Fire software trigger
272
    // broadcasting not supported on some platforms
273
    error = cam.FireSoftwareTrigger(false);
274
 
275
    if (error != FlyCapture2::PGRERROR_OK)
276
        PrintError(error);
277
 
278
}
279
 
18 jakw 280
CameraFrame CameraPointGrey::getFrame(){
1 jakw 281
 
282
    FlyCapture2::Error error;
283
 
284
    // Retrieve the image
285
    FlyCapture2::Image rawImage;
286
    error = cam.RetrieveBuffer(&rawImage);
287
    if (error != FlyCapture2::PGRERROR_OK)
288
        PrintError(error);
289
 
113 jakw 290
//    rawImage.SetColorProcessing(FlyCapture2::IPP);
20 jakw 291
 
113 jakw 292
//    // de-Bayer
293
//    rawImage.Convert(FlyCapture2::PIXEL_FORMAT_RGB8, &currentImage);
20 jakw 294
 
113 jakw 295
//    CameraFrame frame;
296
 
297
//    frame.timeStamp = currentImage.GetTimeStamp().cycleCount;
298
//    frame.height = currentImage.GetRows();
299
//    frame.width = currentImage.GetCols();
300
//    frame.bitDepth = 8;
301
//    frame.channels = 3;
302
//    frame.memory = (unsigned short*)currentImage.GetData();
303
 
1 jakw 304
    CameraFrame frame;
305
 
113 jakw 306
    frame.timeStamp = rawImage.GetTimeStamp().cycleCount;
307
    frame.height = rawImage.GetRows();
308
    frame.width = rawImage.GetCols();
134 jakw 309
    frame.bitDepth = 8;
113 jakw 310
    frame.channels = 1;
120 jakw 311
    frame.memory = rawImage.GetData();
1 jakw 312
 
313
    return frame;
314
}
315
 
316
 
317
size_t CameraPointGrey::getFrameSizeBytes(){
318
 
28 jakw 319
    FlyCapture2::Format7ImageSettings format7Settings;
320
    cam.GetFormat7Configuration(&format7Settings, NULL, NULL);
321
 
120 jakw 322
    return format7Settings.height*format7Settings.width*3*2;
1 jakw 323
}
324
 
18 jakw 325
size_t CameraPointGrey::getFrameWidth(){
326
 
28 jakw 327
    FlyCapture2::Format7ImageSettings format7Settings;
114 jakw 328
    unsigned int dummy1;
329
    float dummy2;
330
    FlyCapture2::Error error = cam.GetFormat7Configuration(&format7Settings, &dummy1, &dummy2);
331
    if (error != FlyCapture2::PGRERROR_OK)
332
        PrintError(error);
1 jakw 333
 
28 jakw 334
    return format7Settings.width;
1 jakw 335
}
336
 
18 jakw 337
size_t CameraPointGrey::getFrameHeight(){
1 jakw 338
 
28 jakw 339
    FlyCapture2::Format7ImageSettings format7Settings;
114 jakw 340
    unsigned int dummy1;
341
    float dummy2;
342
    FlyCapture2::Error error = cam.GetFormat7Configuration(&format7Settings, &dummy1, &dummy2);
343
    if (error != FlyCapture2::PGRERROR_OK)
344
        PrintError(error);
1 jakw 345
 
28 jakw 346
    return format7Settings.height;
347
 
114 jakw 348
 
1 jakw 349
}
350
 
351
 
352
CameraPointGrey::~CameraPointGrey(){
353
 
23 jakw 354
    if(capturing){
18 jakw 355
        // Stop camera transmission
23 jakw 356
        stopCapture();
18 jakw 357
    }
1 jakw 358
 
359
    // Gracefulle destruct the camera
18 jakw 360
    cam.Disconnect();
1 jakw 361
 
23 jakw 362
    std::cout << "Closed camera!" << std::endl << std::flush;
1 jakw 363
}
364
 
365
 
366