Subversion Repositories seema-scanner

Rev

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