Subversion Repositories seema-scanner

Rev

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