Subversion Repositories seema-scanner

Rev

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