Subversion Repositories seema-scanner

Rev

Rev 156 | Rev 207 | 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
    if(triggerMode == triggerModeHardware){
218
        // Configure for hardware trigger
219
        FlyCapture2::TriggerMode triggerMode;
220
        triggerMode.onOff = true;
221
        triggerMode.polarity = 0;
222
        triggerMode.source = 0;
223
        triggerMode.mode = 14;
224
        error = cam.SetTriggerMode(&triggerMode);
225
        if (error != FlyCapture2::PGRERROR_OK)
226
            PrintError(error);
1 jakw 227
 
18 jakw 228
    } else if(triggerMode == triggerModeSoftware){
229
        // Configure software trigger
230
        FlyCapture2::TriggerMode triggerMode;
231
        triggerMode.onOff = true;
232
        triggerMode.polarity = 0;
233
        triggerMode.source = 7; // software
234
        triggerMode.mode = 0;
235
        error = cam.SetTriggerMode(&triggerMode);
236
        if (error != FlyCapture2::PGRERROR_OK)
237
            PrintError(error);
238
    }
239
 
1 jakw 240
    // Set the trigger timeout to 1000 ms
241
    FlyCapture2::FC2Config config;
242
    config.grabTimeout = 1000;
243
    error = cam.SetConfiguration(&config);
244
    if (error != FlyCapture2::PGRERROR_OK)
245
        PrintError(error);
246
 
20 jakw 247
    error = cam.StartCapture();
248
    if (error != FlyCapture2::PGRERROR_OK)
249
        PrintError(error);
250
 
1 jakw 251
    capturing = true;
252
}
253
 
254
void CameraPointGrey::stopCapture(){
255
 
18 jakw 256
    FlyCapture2::Error error = cam.StopCapture();
1 jakw 257
    if (error != FlyCapture2::PGRERROR_OK)
258
        PrintError(error);
259
 
18 jakw 260
    capturing = false;
1 jakw 261
}
262
 
23 jakw 263
void CameraPointGrey::trigger(){
264
 
265
    FlyCapture2::Error error;
266
 
267
    // Fire software trigger
268
    // broadcasting not supported on some platforms
269
    error = cam.FireSoftwareTrigger(false);
270
 
271
    if (error != FlyCapture2::PGRERROR_OK)
272
        PrintError(error);
273
 
274
}
275
 
18 jakw 276
CameraFrame CameraPointGrey::getFrame(){
1 jakw 277
 
278
    FlyCapture2::Error error;
279
 
280
    // Retrieve the image
281
    FlyCapture2::Image rawImage;
282
    error = cam.RetrieveBuffer(&rawImage);
283
    if (error != FlyCapture2::PGRERROR_OK)
284
        PrintError(error);
285
 
113 jakw 286
//    rawImage.SetColorProcessing(FlyCapture2::IPP);
20 jakw 287
 
113 jakw 288
//    // de-Bayer
289
//    rawImage.Convert(FlyCapture2::PIXEL_FORMAT_RGB8, &currentImage);
20 jakw 290
 
113 jakw 291
//    CameraFrame frame;
292
 
293
//    frame.timeStamp = currentImage.GetTimeStamp().cycleCount;
294
//    frame.height = currentImage.GetRows();
295
//    frame.width = currentImage.GetCols();
296
//    frame.bitDepth = 8;
297
//    frame.channels = 3;
298
//    frame.memory = (unsigned short*)currentImage.GetData();
299
 
1 jakw 300
    CameraFrame frame;
301
 
113 jakw 302
    frame.timeStamp = rawImage.GetTimeStamp().cycleCount;
303
    frame.height = rawImage.GetRows();
304
    frame.width = rawImage.GetCols();
134 jakw 305
    frame.bitDepth = 8;
113 jakw 306
    frame.channels = 1;
120 jakw 307
    frame.memory = rawImage.GetData();
1 jakw 308
 
309
    return frame;
310
}
311
 
312
 
313
size_t CameraPointGrey::getFrameSizeBytes(){
314
 
28 jakw 315
    FlyCapture2::Format7ImageSettings format7Settings;
316
    cam.GetFormat7Configuration(&format7Settings, NULL, NULL);
317
 
120 jakw 318
    return format7Settings.height*format7Settings.width*3*2;
1 jakw 319
}
320
 
18 jakw 321
size_t CameraPointGrey::getFrameWidth(){
322
 
28 jakw 323
    FlyCapture2::Format7ImageSettings format7Settings;
114 jakw 324
    unsigned int dummy1;
325
    float dummy2;
326
    FlyCapture2::Error error = cam.GetFormat7Configuration(&format7Settings, &dummy1, &dummy2);
327
    if (error != FlyCapture2::PGRERROR_OK)
328
        PrintError(error);
1 jakw 329
 
28 jakw 330
    return format7Settings.width;
1 jakw 331
}
332
 
18 jakw 333
size_t CameraPointGrey::getFrameHeight(){
1 jakw 334
 
28 jakw 335
    FlyCapture2::Format7ImageSettings format7Settings;
114 jakw 336
    unsigned int dummy1;
337
    float dummy2;
338
    FlyCapture2::Error error = cam.GetFormat7Configuration(&format7Settings, &dummy1, &dummy2);
339
    if (error != FlyCapture2::PGRERROR_OK)
340
        PrintError(error);
1 jakw 341
 
28 jakw 342
    return format7Settings.height;
343
 
114 jakw 344
 
1 jakw 345
}
346
 
347
 
348
CameraPointGrey::~CameraPointGrey(){
349
 
23 jakw 350
    if(capturing){
18 jakw 351
        // Stop camera transmission
23 jakw 352
        stopCapture();
18 jakw 353
    }
1 jakw 354
 
355
    // Gracefulle destruct the camera
18 jakw 356
    cam.Disconnect();
1 jakw 357
 
23 jakw 358
    std::cout << "Closed camera!" << std::endl << std::flush;
1 jakw 359
}
360
 
361
 
362