Subversion Repositories seema-scanner

Rev

Rev 128 | Rev 156 | 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
 
134 jakw 95
//    format7Settings.width = 2496;
96
//    format7Settings.height = 1888;
97
//    format7Settings.offsetX = (3376-2496)/2;
98
//    format7Settings.offsetY = (2704-1888)/2;
99
 
20 jakw 100
    // Validate and set mode
101
    FlyCapture2::Format7PacketInfo packetInfo;
102
    bool valid;
103
    error = cam.ValidateFormat7Settings(&format7Settings, &valid, &packetInfo);
104
    if (error != FlyCapture2::PGRERROR_OK)
105
        PrintError(error);
110 jakw 106
    // packetsize configures maximum frame rate
20 jakw 107
    error = cam.SetFormat7Configuration(&format7Settings, packetInfo.recommendedBytesPerPacket);
108
    if (error != FlyCapture2::PGRERROR_OK)
109
        PrintError(error);
110
 
111
    // Configure general
112
    FlyCapture2::FC2Config config;
113
    config.numBuffers = 5;
114
    config.grabTimeout = 1000; // retrieveBuffer() timeout in ms
115
    config.grabMode = FlyCapture2::DROP_FRAMES;
116
    config.isochBusSpeed = FlyCapture2::BUSSPEED_S_FASTEST;
117
    config.highPerformanceRetrieveBuffer = true;
118
    //config.asyncBusSpeed = FlyCapture2::BUSSPEED_S_FASTEST;
119
    error = cam.SetConfiguration(&config);
120
    if (error != FlyCapture2::PGRERROR_OK)
121
        PrintError(error);
122
 
123
 
1 jakw 124
    // Get the camera information
125
    FlyCapture2::CameraInfo camInfo;
126
    error = cam.GetCameraInfo(&camInfo);
127
    if (error != FlyCapture2::PGRERROR_OK)
128
        PrintError(error);
129
 
4 jakw 130
    std::cout << camInfo.vendorName << "  " << camInfo.modelName << "  " << camInfo.serialNumber << std::endl;
1 jakw 131
 
74 jakw 132
    // Turn off auto settings
45 jakw 133
    FlyCapture2::Property property;
74 jakw 134
    property.type = FlyCapture2::AUTO_EXPOSURE;
135
    property.onOff = false;
136
    error = cam.SetProperty(&property);
137
    if (error != FlyCapture2::PGRERROR_OK)
138
        PrintError(error);
139
 
140
    property.type = FlyCapture2::GAMMA;
45 jakw 141
    property.onOff = true;
74 jakw 142
    property.absControl = true;
143
    property.absValue = 1.0;
144
    error = cam.SetProperty(&property);
145
    if (error != FlyCapture2::PGRERROR_OK)
146
        PrintError(error);
147
 
120 jakw 148
    // Set frame rate to 6
110 jakw 149
    property.type = FlyCapture2::FRAME_RATE;
150
    property.onOff = true;
151
    property.absControl = true;
152
    property.autoManualMode = false;
153
    property.absValue = 6.0;
154
    error = cam.SetProperty(&property);
155
    if (error != FlyCapture2::PGRERROR_OK)
156
        PrintError(error);
157
 
120 jakw 158
    // Set white balance to match LED projector light
74 jakw 159
    property.type = FlyCapture2::WHITE_BALANCE;
120 jakw 160
    property.onOff = true;
45 jakw 161
    property.absControl = false;
162
    property.autoManualMode = false;
163
    property.valueA = 666; //red
164
    property.valueB = 777; //blue
165
    error = cam.SetProperty(&property);
166
    if (error != FlyCapture2::PGRERROR_OK)
167
        PrintError(error);
168
 
1 jakw 169
    // Set reasonable default settings
170
    CameraSettings settings;
18 jakw 171
    //settings.shutter = 8.33;
128 jakw 172
    settings.shutter = 66.66;
1 jakw 173
    settings.gain = 0.0;
174
    this->setCameraSettings(settings);
175
 
176
    return;
177
}
18 jakw 178
 
179
CameraSettings CameraPointGrey::getCameraSettings(){
180
 
181
    FlyCapture2::Property property;
182
 
183
    // Get settings:
184
    CameraSettings settings;
185
 
186
    property.type = FlyCapture2::SHUTTER;
187
    cam.GetProperty(&property);
188
    settings.shutter = property.absValue;
189
 
190
    property.type = FlyCapture2::GAIN;
191
    cam.GetProperty(&property);
192
    settings.gain = property.absValue;
193
 
194
    return settings;
195
}
196
 
197
void CameraPointGrey::setCameraSettings(CameraSettings settings){
198
 
199
    FlyCapture2::Property property;
200
    property.onOff = true;
201
    property.absControl = true;
202
 
203
    property.type = FlyCapture2::SHUTTER;
204
    property.absValue = settings.shutter;
205
    cam.SetProperty(&property);
206
 
207
    property.type = FlyCapture2::GAIN;
208
    property.absValue = settings.gain;
209
    cam.SetProperty(&property);
210
 
211
}
212
 
1 jakw 213
void CameraPointGrey::startCapture(){
214
 
20 jakw 215
    FlyCapture2::Error error;
1 jakw 216
 
18 jakw 217
    CameraSettings settings = this->getCameraSettings();
218
    std::cout << "\tShutter: " << settings.shutter << "ms" << std::endl;
219
    std::cout << "\tGain: " << settings.gain << "dB" << std::endl;
1 jakw 220
 
18 jakw 221
    if(triggerMode == triggerModeHardware){
222
        // Configure for hardware trigger
223
        FlyCapture2::TriggerMode triggerMode;
224
        triggerMode.onOff = true;
225
        triggerMode.polarity = 0;
226
        triggerMode.source = 0;
227
        triggerMode.mode = 14;
228
        error = cam.SetTriggerMode(&triggerMode);
229
        if (error != FlyCapture2::PGRERROR_OK)
230
            PrintError(error);
1 jakw 231
 
18 jakw 232
    } else if(triggerMode == triggerModeSoftware){
233
        // Configure software trigger
234
        FlyCapture2::TriggerMode triggerMode;
235
        triggerMode.onOff = true;
236
        triggerMode.polarity = 0;
237
        triggerMode.source = 7; // software
238
        triggerMode.mode = 0;
239
        error = cam.SetTriggerMode(&triggerMode);
240
        if (error != FlyCapture2::PGRERROR_OK)
241
            PrintError(error);
242
    }
243
 
1 jakw 244
    // Set the trigger timeout to 1000 ms
245
    FlyCapture2::FC2Config config;
246
    config.grabTimeout = 1000;
247
    error = cam.SetConfiguration(&config);
248
    if (error != FlyCapture2::PGRERROR_OK)
249
        PrintError(error);
250
 
20 jakw 251
    error = cam.StartCapture();
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