Subversion Repositories seema-scanner

Rev

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