Subversion Repositories seema-scanner

Rev

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