Subversion Repositories seema-scanner

Rev

Rev 18 | Rev 20 | 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
 
4
void PrintError(FlyCapture2::Error error){
5
    error.PrintErrorTrace();
6
}
7
 
8
vector<CameraInfo> CameraPointGrey::getCameraList(){
9
 
10
    FlyCapture2::Error error;
11
 
12
    FlyCapture2::BusManager busManager;
13
    unsigned int numCameras;
14
    error = busManager.GetNumOfCameras(&numCameras);
15
 
16
    vector<CameraInfo> ret;
17
 
18
    if (error != FlyCapture2::PGRERROR_OK){
19
        PrintError(error);
20
        return ret;
21
    }
22
 
23
    for (unsigned int i=0; i < numCameras; i++){
24
        FlyCapture2::PGRGuid guid;
25
        error = busManager.GetCameraFromIndex(i, &guid);
26
        if (error != FlyCapture2::PGRERROR_OK)
27
            PrintError(error);
28
 
29
        // Connect to camera
30
        FlyCapture2::Camera cam;
31
        error = cam.Connect(&guid);
32
        if (error != FlyCapture2::PGRERROR_OK)
33
            PrintError( error );
34
 
19 jakw 35
    //    // Configure DCAM video mode and frame rate
36
    //    FlyCapture2::VideoMode videoMode = FlyCapture2::VIDEOMODE_1280x960Y8;
37
    //    FlyCapture2::FrameRate frameRate = FlyCapture2::FRAMERATE_7_5;
38
    //    error = cam.SetVideoModeAndFrameRate(videoMode, frameRate);
39
    //    if (error != FlyCapture2::PGRERROR_OK)
40
    //        PrintError(error);
41
 
42
        // Configure Format7 mode
43
        FlyCapture2::Format7ImageSettings format7Settings;
44
        format7Settings.mode = FlyCapture2::MODE_0;
45
        format7Settings.pixelFormat = FlyCapture2::PIXEL_FORMAT_RAW8;
46
        format7Settings.width = 1296;
47
        format7Settings.height = 964;
48
        format7Settings.offsetX = 0;
49
        format7Settings.offsetY = 0;
50
 
51
        // Validate and set mode
52
        FlyCapture2::Format7PacketInfo packetInfo;
53
        bool valid;
54
        cam.ValidateFormat7Settings(&format7Settings, &valid, &packetInfo);
55
        // packetsize indirectly configures frame rate
56
        error = cam.SetFormat7Configuration(&format7Settings, packetInfo.unitBytesPerPacket*3);
57
        if (error != FlyCapture2::PGRERROR_OK)
58
            PrintError(error);
59
 
60
        // Configure general
61
        FlyCapture2::FC2Config config;
62
        config.numBuffers = 5;
63
        config.grabTimeout = 1000; // retrieveBuffer() timeout in ms
64
        config.grabMode = FlyCapture2::DROP_FRAMES;
65
        config.isochBusSpeed = FlyCapture2::BUSSPEED_S_FASTEST;
66
        config.highPerformanceRetrieveBuffer = true;
67
        //config.asyncBusSpeed = FlyCapture2::BUSSPEED_S_FASTEST;
68
        error = cam.SetConfiguration(&config);
69
        if (error != FlyCapture2::PGRERROR_OK)
70
            PrintError(error);
71
 
72
        // Get camera information
1 jakw 73
        FlyCapture2::CameraInfo camInfo;
74
        error = cam.GetCameraInfo(&camInfo);
75
        if (error != FlyCapture2::PGRERROR_OK)
76
            PrintError( error );
77
 
78
        CameraInfo camera;
79
        camera.busID = camInfo.nodeNumber;
80
        camera.model = camInfo.modelName;
81
        camera.vendor = "Point Grey Research";
82
 
83
        ret.push_back(camera);
84
    }
85
 
86
    return ret;
87
}
88
 
18 jakw 89
CameraPointGrey::CameraPointGrey(unsigned int camNum, CameraTriggerMode triggerMode) : Camera(triggerMode){
1 jakw 90
 
91
    FlyCapture2::Error error;
92
 
93
    // Connect to camera
94
    FlyCapture2::BusManager busManager;
95
    FlyCapture2::PGRGuid camGuid;
18 jakw 96
    busManager.GetCameraFromIndex(camNum, &camGuid);
1 jakw 97
    error = cam.Connect(&camGuid);
98
    if (error != FlyCapture2::PGRERROR_OK)
99
        PrintError(error);
100
 
4 jakw 101
    // Configure video mode and frame rate
18 jakw 102
    FlyCapture2::VideoMode videoMode = FlyCapture2::VIDEOMODE_640x480Y16;
103
    FlyCapture2::FrameRate frameRate = FlyCapture2::FRAMERATE_15;
4 jakw 104
    cam.SetVideoModeAndFrameRate(videoMode, frameRate);
105
 
1 jakw 106
    // Get the camera information
107
    FlyCapture2::CameraInfo camInfo;
108
    error = cam.GetCameraInfo(&camInfo);
109
    if (error != FlyCapture2::PGRERROR_OK)
110
        PrintError(error);
111
 
4 jakw 112
    std::cout << camInfo.vendorName << "  " << camInfo.modelName << "  " << camInfo.serialNumber << std::endl;
1 jakw 113
 
114
    // Set reasonable default settings
115
    CameraSettings settings;
18 jakw 116
    //settings.shutter = 8.33;
4 jakw 117
    settings.shutter = 33.33;
1 jakw 118
    settings.gain = 0.0;
119
    this->setCameraSettings(settings);
120
 
121
    // Start isochronous image transfer
122
    error = cam.StartCapture();
123
    if (error != FlyCapture2::PGRERROR_OK)
124
        PrintError(error);
125
 
126
    return;
127
}
18 jakw 128
 
129
CameraSettings CameraPointGrey::getCameraSettings(){
130
 
131
    FlyCapture2::Property property;
132
 
133
    // Get settings:
134
    CameraSettings settings;
135
 
136
    property.type = FlyCapture2::SHUTTER;
137
    cam.GetProperty(&property);
138
    settings.shutter = property.absValue;
139
 
140
    property.type = FlyCapture2::GAIN;
141
    cam.GetProperty(&property);
142
    settings.gain = property.absValue;
143
 
144
    return settings;
145
}
146
 
147
void CameraPointGrey::setCameraSettings(CameraSettings settings){
148
 
149
    FlyCapture2::Property property;
150
    property.onOff = true;
151
    property.absControl = true;
152
 
153
    property.type = FlyCapture2::SHUTTER;
154
    property.absValue = settings.shutter;
155
    cam.SetProperty(&property);
156
 
157
    property.type = FlyCapture2::GAIN;
158
    property.absValue = settings.gain;
159
    cam.SetProperty(&property);
160
 
161
}
162
 
1 jakw 163
void CameraPointGrey::startCapture(){
164
 
18 jakw 165
    FlyCapture2::Error error;\
1 jakw 166
 
18 jakw 167
    CameraSettings settings = this->getCameraSettings();
168
    std::cout << "\tShutter: " << settings.shutter << "ms" << std::endl;
169
    std::cout << "\tGain: " << settings.gain << "dB" << std::endl;
1 jakw 170
 
18 jakw 171
    if(triggerMode == triggerModeHardware){
172
        // Configure for hardware trigger
173
        FlyCapture2::TriggerMode triggerMode;
174
        triggerMode.onOff = true;
175
        triggerMode.polarity = 0;
176
        triggerMode.source = 0;
177
        triggerMode.mode = 14;
178
        error = cam.SetTriggerMode(&triggerMode);
179
        if (error != FlyCapture2::PGRERROR_OK)
180
            PrintError(error);
1 jakw 181
 
18 jakw 182
        error = cam.StartCapture();
183
        if (error != FlyCapture2::PGRERROR_OK)
184
            PrintError(error);
185
 
186
    } else if(triggerMode == triggerModeSoftware){
187
        // Configure software trigger
188
        FlyCapture2::TriggerMode triggerMode;
189
        triggerMode.onOff = true;
190
        triggerMode.polarity = 0;
191
        triggerMode.source = 7; // software
192
        triggerMode.mode = 0;
193
        error = cam.SetTriggerMode(&triggerMode);
194
        if (error != FlyCapture2::PGRERROR_OK)
195
            PrintError(error);
196
    }
197
 
1 jakw 198
    // Set the trigger timeout to 1000 ms
199
    FlyCapture2::FC2Config config;
200
    config.grabTimeout = 1000;
201
    error = cam.SetConfiguration(&config);
202
    if (error != FlyCapture2::PGRERROR_OK)
203
        PrintError(error);
204
 
205
    capturing = true;
206
}
207
 
208
void CameraPointGrey::stopCapture(){
209
 
18 jakw 210
    FlyCapture2::Error error = cam.StopCapture();
1 jakw 211
    if (error != FlyCapture2::PGRERROR_OK)
212
        PrintError(error);
213
 
18 jakw 214
    capturing = false;
1 jakw 215
}
216
 
18 jakw 217
CameraFrame CameraPointGrey::getFrame(){
1 jakw 218
 
219
    FlyCapture2::Error error;
220
 
18 jakw 221
    if(triggerMode == triggerModeSoftware){
222
        // Fire software trigger
223
        // broadcasting not supported on some platforms
224
        cam.FireSoftwareTrigger(false);
225
    }
1 jakw 226
 
227
    // Retrieve the image
228
    FlyCapture2::Image rawImage;
229
    error = cam.RetrieveBuffer(&rawImage);
230
    if (error != FlyCapture2::PGRERROR_OK)
231
        PrintError(error);
232
 
233
    CameraFrame frame;
234
 
235
    frame.timeStamp = rawImage.GetTimeStamp().cycleCount;
236
    frame.height = rawImage.GetRows();
237
    frame.width = rawImage.GetCols();
18 jakw 238
    frame.bitDepth = 16;
239
    frame.memory = (unsigned short*)rawImage.GetData();
1 jakw 240
 
241
    return frame;
242
}
243
 
244
 
245
size_t CameraPointGrey::getFrameSizeBytes(){
246
 
247
    return 0;
248
}
249
 
18 jakw 250
size_t CameraPointGrey::getFrameWidth(){
251
 
1 jakw 252
    // How do we poll this from the camera?
18 jakw 253
    return 640;
1 jakw 254
 
255
}
256
 
18 jakw 257
size_t CameraPointGrey::getFrameHeight(){
1 jakw 258
 
18 jakw 259
    // How do we poll this from the camera?
260
    return 480;
1 jakw 261
 
262
}
263
 
264
 
265
CameraPointGrey::~CameraPointGrey(){
266
 
18 jakw 267
    if(capturing && triggerMode == triggerModeHardware){
268
        // Stop camera transmission
269
        cam.StopCapture();
270
    }
1 jakw 271
 
272
    // Gracefulle destruct the camera
18 jakw 273
    cam.Disconnect();
1 jakw 274
 
275
}
276
 
277
 
278