Subversion Repositories seema-scanner

Rev

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