Subversion Repositories seema-scanner

Rev

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