Subversion Repositories seema-scanner

Rev

Rev 53 | Rev 110 | 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;
89
    format7Settings.pixelFormat = FlyCapture2::PIXEL_FORMAT_RAW16;
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);
101
    // packetsize indirectly configures frame rate
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
 
143
    // Set white balance appropriate for LED projector
144
    property.type = FlyCapture2::WHITE_BALANCE;
145
    property.onOff = true;
45 jakw 146
    property.absControl = false;
147
    property.autoManualMode = false;
148
    property.valueA = 666; //red
149
    property.valueB = 777; //blue
150
    error = cam.SetProperty(&property);
151
    if (error != FlyCapture2::PGRERROR_OK)
152
        PrintError(error);
153
 
1 jakw 154
    // Set reasonable default settings
155
    CameraSettings settings;
18 jakw 156
    //settings.shutter = 8.33;
74 jakw 157
    settings.shutter = 16.66;
1 jakw 158
    settings.gain = 0.0;
159
    this->setCameraSettings(settings);
160
 
161
    return;
162
}
18 jakw 163
 
164
CameraSettings CameraPointGrey::getCameraSettings(){
165
 
166
    FlyCapture2::Property property;
167
 
168
    // Get settings:
169
    CameraSettings settings;
170
 
171
    property.type = FlyCapture2::SHUTTER;
172
    cam.GetProperty(&property);
173
    settings.shutter = property.absValue;
174
 
175
    property.type = FlyCapture2::GAIN;
176
    cam.GetProperty(&property);
177
    settings.gain = property.absValue;
178
 
179
    return settings;
180
}
181
 
182
void CameraPointGrey::setCameraSettings(CameraSettings settings){
183
 
184
    FlyCapture2::Property property;
185
    property.onOff = true;
186
    property.absControl = true;
187
 
188
    property.type = FlyCapture2::SHUTTER;
189
    property.absValue = settings.shutter;
190
    cam.SetProperty(&property);
191
 
192
    property.type = FlyCapture2::GAIN;
193
    property.absValue = settings.gain;
194
    cam.SetProperty(&property);
195
 
196
}
197
 
1 jakw 198
void CameraPointGrey::startCapture(){
199
 
20 jakw 200
    FlyCapture2::Error error;
1 jakw 201
 
18 jakw 202
    CameraSettings settings = this->getCameraSettings();
203
    std::cout << "\tShutter: " << settings.shutter << "ms" << std::endl;
204
    std::cout << "\tGain: " << settings.gain << "dB" << std::endl;
1 jakw 205
 
18 jakw 206
    if(triggerMode == triggerModeHardware){
207
        // Configure for hardware trigger
208
        FlyCapture2::TriggerMode triggerMode;
209
        triggerMode.onOff = true;
210
        triggerMode.polarity = 0;
211
        triggerMode.source = 0;
212
        triggerMode.mode = 14;
213
        error = cam.SetTriggerMode(&triggerMode);
214
        if (error != FlyCapture2::PGRERROR_OK)
215
            PrintError(error);
1 jakw 216
 
18 jakw 217
    } else if(triggerMode == triggerModeSoftware){
218
        // Configure software trigger
219
        FlyCapture2::TriggerMode triggerMode;
220
        triggerMode.onOff = true;
221
        triggerMode.polarity = 0;
222
        triggerMode.source = 7; // software
223
        triggerMode.mode = 0;
224
        error = cam.SetTriggerMode(&triggerMode);
225
        if (error != FlyCapture2::PGRERROR_OK)
226
            PrintError(error);
227
    }
228
 
1 jakw 229
    // Set the trigger timeout to 1000 ms
230
    FlyCapture2::FC2Config config;
231
    config.grabTimeout = 1000;
232
    error = cam.SetConfiguration(&config);
233
    if (error != FlyCapture2::PGRERROR_OK)
234
        PrintError(error);
235
 
20 jakw 236
    error = cam.StartCapture();
237
    if (error != FlyCapture2::PGRERROR_OK)
238
        PrintError(error);
239
 
1 jakw 240
    capturing = true;
241
}
242
 
243
void CameraPointGrey::stopCapture(){
244
 
18 jakw 245
    FlyCapture2::Error error = cam.StopCapture();
1 jakw 246
    if (error != FlyCapture2::PGRERROR_OK)
247
        PrintError(error);
248
 
18 jakw 249
    capturing = false;
1 jakw 250
}
251
 
23 jakw 252
void CameraPointGrey::trigger(){
253
 
254
    FlyCapture2::Error error;
255
 
256
    // Fire software trigger
257
    // broadcasting not supported on some platforms
258
    error = cam.FireSoftwareTrigger(false);
259
 
260
    if (error != FlyCapture2::PGRERROR_OK)
261
        PrintError(error);
262
 
263
}
264
 
18 jakw 265
CameraFrame CameraPointGrey::getFrame(){
1 jakw 266
 
267
    FlyCapture2::Error error;
268
 
269
    // Retrieve the image
270
    FlyCapture2::Image rawImage;
271
    error = cam.RetrieveBuffer(&rawImage);
272
    if (error != FlyCapture2::PGRERROR_OK)
273
        PrintError(error);
274
 
20 jakw 275
    rawImage.SetColorProcessing(FlyCapture2::IPP);
276
 
277
    // de-Bayer
23 jakw 278
    rawImage.Convert(FlyCapture2::PIXEL_FORMAT_RGB8, &currentImage);
20 jakw 279
 
1 jakw 280
    CameraFrame frame;
281
 
20 jakw 282
    frame.timeStamp = currentImage.GetTimeStamp().cycleCount;
283
    frame.height = currentImage.GetRows();
284
    frame.width = currentImage.GetCols();
23 jakw 285
    frame.bitDepth = 8;
286
    frame.channels = 3;
20 jakw 287
    frame.memory = (unsigned short*)currentImage.GetData();
1 jakw 288
 
289
    return frame;
290
}
291
 
292
 
293
size_t CameraPointGrey::getFrameSizeBytes(){
294
 
28 jakw 295
    FlyCapture2::Format7ImageSettings format7Settings;
296
    cam.GetFormat7Configuration(&format7Settings, NULL, NULL);
297
 
298
    return format7Settings.height*format7Settings.width*3;
1 jakw 299
}
300
 
18 jakw 301
size_t CameraPointGrey::getFrameWidth(){
302
 
28 jakw 303
    FlyCapture2::Format7ImageSettings format7Settings;
304
    cam.GetFormat7Configuration(&format7Settings, NULL, NULL);
1 jakw 305
 
28 jakw 306
    return format7Settings.width;
1 jakw 307
}
308
 
18 jakw 309
size_t CameraPointGrey::getFrameHeight(){
1 jakw 310
 
28 jakw 311
    FlyCapture2::Format7ImageSettings format7Settings;
312
    cam.GetFormat7Configuration(&format7Settings, NULL, NULL);
1 jakw 313
 
28 jakw 314
    return format7Settings.height;
315
 
1 jakw 316
}
317
 
318
 
319
CameraPointGrey::~CameraPointGrey(){
320
 
23 jakw 321
    if(capturing){
18 jakw 322
        // Stop camera transmission
23 jakw 323
        stopCapture();
18 jakw 324
    }
1 jakw 325
 
326
    // Gracefulle destruct the camera
18 jakw 327
    cam.Disconnect();
1 jakw 328
 
23 jakw 329
    std::cout << "Closed camera!" << std::endl << std::flush;
1 jakw 330
}
331
 
332
 
333