Subversion Repositories seema-scanner

Rev

Rev 53 | Rev 110 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 53 Rev 74
1
#include "CameraPointGrey.h"
1
#include "CameraPointGrey.h"
2
#include <cstring>
2
#include <cstring>
3
 
3
 
4
//#include <sys/ioctl.h>
4
//#include <sys/ioctl.h>
5
//#include <linux/usbdevice_fs.h>
5
//#include <linux/usbdevice_fs.h>
6
 
6
 
7
void PrintError(FlyCapture2::Error error){
7
void PrintError(FlyCapture2::Error error){
8
    error.PrintErrorTrace();
8
    error.PrintErrorTrace();
9
}
9
}
10
 
10
 
11
vector<CameraInfo> CameraPointGrey::getCameraList(){
11
vector<CameraInfo> CameraPointGrey::getCameraList(){
12
    
12
    
13
    FlyCapture2::Error error;
13
    FlyCapture2::Error error;
14
 
14
 
15
    FlyCapture2::BusManager busManager;
15
    FlyCapture2::BusManager busManager;
16
    unsigned int numCameras;
16
    unsigned int numCameras;
17
    error = busManager.GetNumOfCameras(&numCameras);
17
    error = busManager.GetNumOfCameras(&numCameras);
18
 
18
 
19
    vector<CameraInfo> ret;
19
    vector<CameraInfo> ret;
20
 
20
 
21
    if (error != FlyCapture2::PGRERROR_OK){
21
    if (error != FlyCapture2::PGRERROR_OK){
22
        PrintError(error);
22
        PrintError(error);
23
        return ret;
23
        return ret;
24
    }
24
    }
25
 
25
 
26
    for (unsigned int i=0; i < numCameras; i++){
26
    for (unsigned int i=0; i < numCameras; i++){
27
        FlyCapture2::PGRGuid guid;
27
        FlyCapture2::PGRGuid guid;
28
        error = busManager.GetCameraFromIndex(i, &guid);
28
        error = busManager.GetCameraFromIndex(i, &guid);
29
        if (error != FlyCapture2::PGRERROR_OK)
29
        if (error != FlyCapture2::PGRERROR_OK)
30
            PrintError(error);
30
            PrintError(error);
31
 
31
 
32
        // Connect to camera
32
        // Connect to camera
33
        FlyCapture2::Camera cam;
33
        FlyCapture2::Camera cam;
34
        error = cam.Connect(&guid);
34
        error = cam.Connect(&guid);
35
        if (error != FlyCapture2::PGRERROR_OK)
35
        if (error != FlyCapture2::PGRERROR_OK)
36
            PrintError( error );
36
            PrintError( error );
37
 
37
 
38
        // Get camera information
38
        // Get camera information
39
        FlyCapture2::CameraInfo camInfo;
39
        FlyCapture2::CameraInfo camInfo;
40
        error = cam.GetCameraInfo(&camInfo);
40
        error = cam.GetCameraInfo(&camInfo);
41
        if (error != FlyCapture2::PGRERROR_OK)
41
        if (error != FlyCapture2::PGRERROR_OK)
42
            PrintError( error );
42
            PrintError( error );
43
 
43
 
44
        CameraInfo camera;
44
        CameraInfo camera;
45
        camera.vendor = "Point Grey";
45
        camera.vendor = "Point Grey";
46
        camera.model = camInfo.modelName;
46
        camera.model = camInfo.modelName;
47
        camera.busID = camInfo.nodeNumber;
47
        camera.busID = camInfo.nodeNumber;
48
        camera.serialNumber = camInfo.serialNumber;
48
        camera.serialNumber = camInfo.serialNumber;
49
 
49
 
50
        ret.push_back(camera);
50
        ret.push_back(camera);
51
    }
51
    }
52
 
52
 
53
    return ret;
53
    return ret;
54
}
54
}
55
 
55
 
56
CameraPointGrey::CameraPointGrey(unsigned int camNum, CameraTriggerMode triggerMode) : Camera(triggerMode){
56
CameraPointGrey::CameraPointGrey(unsigned int camNum, CameraTriggerMode triggerMode) : Camera(triggerMode){
57
 
57
 
58
//    // Reset USB bus
58
//    // Reset USB bus
59
//    std::cout << "Resetting USB device %s\n";
59
//    std::cout << "Resetting USB device %s\n";
60
//    int rc = ioctl(fd, USBDEVFS_RESET, 0);
60
//    int rc = ioctl(fd, USBDEVFS_RESET, 0);
61
//    if (rc < 0)
61
//    if (rc < 0)
62
//        perror("Error in ioctl");
62
//        perror("Error in ioctl");
63
//    else
63
//    else
64
//        printf("Reset successful\n");
64
//        printf("Reset successful\n");
65
 
65
 
66
 
66
 
67
    FlyCapture2::Error error;
67
    FlyCapture2::Error error;
68
 
68
 
69
    // Connect to camera
69
    // Connect to camera
70
    FlyCapture2::BusManager busManager;
70
    FlyCapture2::BusManager busManager;
71
 
71
 
72
    FlyCapture2::PGRGuid camGuid;
72
    FlyCapture2::PGRGuid camGuid;
73
 
73
 
74
    busManager.GetCameraFromIndex(camNum, &camGuid);
74
    busManager.GetCameraFromIndex(camNum, &camGuid);
75
    error = cam.Connect(&camGuid);
75
    error = cam.Connect(&camGuid);
76
    if (error != FlyCapture2::PGRERROR_OK)
76
    if (error != FlyCapture2::PGRERROR_OK)
77
        PrintError(error);
77
        PrintError(error);
78
 
78
 
79
//    // Configure DCAM video mode and frame rate
79
//    // Configure DCAM video mode and frame rate
80
//    FlyCapture2::VideoMode videoMode = FlyCapture2::VIDEOMODE_1280x960Y8;
80
//    FlyCapture2::VideoMode videoMode = FlyCapture2::VIDEOMODE_1280x960Y8;
81
//    FlyCapture2::FrameRate frameRate = FlyCapture2::FRAMERATE_7_5;
81
//    FlyCapture2::FrameRate frameRate = FlyCapture2::FRAMERATE_7_5;
82
//    error = cam.SetVideoModeAndFrameRate(videoMode, frameRate);
82
//    error = cam.SetVideoModeAndFrameRate(videoMode, frameRate);
83
//    if (error != FlyCapture2::PGRERROR_OK)
83
//    if (error != FlyCapture2::PGRERROR_OK)
84
//        PrintError(error);
84
//        PrintError(error);
85
 
85
 
86
    // Configure Format7 mode
86
    // Configure Format7 mode
87
    FlyCapture2::Format7ImageSettings format7Settings;
87
    FlyCapture2::Format7ImageSettings format7Settings;
88
    format7Settings.mode = FlyCapture2::MODE_0;
88
    format7Settings.mode = FlyCapture2::MODE_0;
89
    format7Settings.pixelFormat = FlyCapture2::PIXEL_FORMAT_RAW16;
89
    format7Settings.pixelFormat = FlyCapture2::PIXEL_FORMAT_RAW16;
90
    format7Settings.width = 3376;
90
    format7Settings.width = 3376;
91
    format7Settings.height = 2704;
91
    format7Settings.height = 2704;
92
    format7Settings.offsetX = 0;
92
    format7Settings.offsetX = 0;
93
    format7Settings.offsetY = 0;
93
    format7Settings.offsetY = 0;
94
 
94
 
95
    // Validate and set mode
95
    // Validate and set mode
96
    FlyCapture2::Format7PacketInfo packetInfo;
96
    FlyCapture2::Format7PacketInfo packetInfo;
97
    bool valid;
97
    bool valid;
98
    error = cam.ValidateFormat7Settings(&format7Settings, &valid, &packetInfo);
98
    error = cam.ValidateFormat7Settings(&format7Settings, &valid, &packetInfo);
99
    if (error != FlyCapture2::PGRERROR_OK)
99
    if (error != FlyCapture2::PGRERROR_OK)
100
        PrintError(error);
100
        PrintError(error);
101
    // packetsize indirectly configures frame rate
101
    // packetsize indirectly configures frame rate
102
    error = cam.SetFormat7Configuration(&format7Settings, packetInfo.recommendedBytesPerPacket);
102
    error = cam.SetFormat7Configuration(&format7Settings, packetInfo.recommendedBytesPerPacket);
103
    if (error != FlyCapture2::PGRERROR_OK)
103
    if (error != FlyCapture2::PGRERROR_OK)
104
        PrintError(error);
104
        PrintError(error);
105
 
105
 
106
    // Configure general
106
    // Configure general
107
    FlyCapture2::FC2Config config;
107
    FlyCapture2::FC2Config config;
108
    config.numBuffers = 5;
108
    config.numBuffers = 5;
109
    config.grabTimeout = 1000; // retrieveBuffer() timeout in ms
109
    config.grabTimeout = 1000; // retrieveBuffer() timeout in ms
110
    config.grabMode = FlyCapture2::DROP_FRAMES;
110
    config.grabMode = FlyCapture2::DROP_FRAMES;
111
    config.isochBusSpeed = FlyCapture2::BUSSPEED_S_FASTEST;
111
    config.isochBusSpeed = FlyCapture2::BUSSPEED_S_FASTEST;
112
    config.highPerformanceRetrieveBuffer = true;
112
    config.highPerformanceRetrieveBuffer = true;
113
    //config.asyncBusSpeed = FlyCapture2::BUSSPEED_S_FASTEST;
113
    //config.asyncBusSpeed = FlyCapture2::BUSSPEED_S_FASTEST;
114
    error = cam.SetConfiguration(&config);
114
    error = cam.SetConfiguration(&config);
115
    if (error != FlyCapture2::PGRERROR_OK)
115
    if (error != FlyCapture2::PGRERROR_OK)
116
        PrintError(error);
116
        PrintError(error);
117
 
117
 
118
 
118
 
119
    // Get the camera information
119
    // Get the camera information
120
    FlyCapture2::CameraInfo camInfo;
120
    FlyCapture2::CameraInfo camInfo;
121
    error = cam.GetCameraInfo(&camInfo);
121
    error = cam.GetCameraInfo(&camInfo);
122
    if (error != FlyCapture2::PGRERROR_OK)
122
    if (error != FlyCapture2::PGRERROR_OK)
123
        PrintError(error);
123
        PrintError(error);
124
 
124
 
125
    std::cout << camInfo.vendorName << "  " << camInfo.modelName << "  " << camInfo.serialNumber << std::endl;
125
    std::cout << camInfo.vendorName << "  " << camInfo.modelName << "  " << camInfo.serialNumber << std::endl;
126
 
126
 
127
    // Set white balance appropriate for LED projector
127
    // Turn off auto settings
128
    FlyCapture2::Property property;
128
    FlyCapture2::Property property;
-
 
129
    property.type = FlyCapture2::AUTO_EXPOSURE;
-
 
130
    property.onOff = false;
-
 
131
    error = cam.SetProperty(&property);
-
 
132
    if (error != FlyCapture2::PGRERROR_OK)
-
 
133
        PrintError(error);
-
 
134
 
-
 
135
    property.type = FlyCapture2::GAMMA;
-
 
136
    property.onOff = true;
-
 
137
    property.absControl = true;
-
 
138
    property.absValue = 1.0;
-
 
139
    error = cam.SetProperty(&property);
-
 
140
    if (error != FlyCapture2::PGRERROR_OK)
-
 
141
        PrintError(error);
-
 
142
 
-
 
143
    // Set white balance appropriate for LED projector
-
 
144
    property.type = FlyCapture2::WHITE_BALANCE;
129
    property.onOff = true;
145
    property.onOff = true;
130
    property.absControl = false;
146
    property.absControl = false;
131
    property.autoManualMode = false;
147
    property.autoManualMode = false;
132
    property.type = FlyCapture2::WHITE_BALANCE;
-
 
133
    property.valueA = 666; //red
148
    property.valueA = 666; //red
134
    property.valueB = 777; //blue
149
    property.valueB = 777; //blue
135
    error = cam.SetProperty(&property);
150
    error = cam.SetProperty(&property);
136
    if (error != FlyCapture2::PGRERROR_OK)
151
    if (error != FlyCapture2::PGRERROR_OK)
137
        PrintError(error);
152
        PrintError(error);
138
 
153
 
139
    // Set reasonable default settings
154
    // Set reasonable default settings
140
    CameraSettings settings;
155
    CameraSettings settings;
141
    //settings.shutter = 8.33;
156
    //settings.shutter = 8.33;
142
    settings.shutter = 33.33;
157
    settings.shutter = 16.66;
143
    settings.gain = 0.0;
158
    settings.gain = 0.0;
144
    this->setCameraSettings(settings);
159
    this->setCameraSettings(settings);
145
 
160
 
146
    return;
161
    return;
147
}
162
}
148
 
163
 
149
CameraSettings CameraPointGrey::getCameraSettings(){
164
CameraSettings CameraPointGrey::getCameraSettings(){
150
 
165
 
151
    FlyCapture2::Property property;
166
    FlyCapture2::Property property;
152
 
167
 
153
    // Get settings:
168
    // Get settings:
154
    CameraSettings settings;
169
    CameraSettings settings;
155
 
170
 
156
    property.type = FlyCapture2::SHUTTER;
171
    property.type = FlyCapture2::SHUTTER;
157
    cam.GetProperty(&property);
172
    cam.GetProperty(&property);
158
    settings.shutter = property.absValue;
173
    settings.shutter = property.absValue;
159
 
174
 
160
    property.type = FlyCapture2::GAIN;
175
    property.type = FlyCapture2::GAIN;
161
    cam.GetProperty(&property);
176
    cam.GetProperty(&property);
162
    settings.gain = property.absValue;
177
    settings.gain = property.absValue;
163
 
178
 
164
    return settings;
179
    return settings;
165
}
180
}
166
 
181
 
167
void CameraPointGrey::setCameraSettings(CameraSettings settings){
182
void CameraPointGrey::setCameraSettings(CameraSettings settings){
168
 
183
 
169
    FlyCapture2::Property property;
184
    FlyCapture2::Property property;
170
    property.onOff = true;
185
    property.onOff = true;
171
    property.absControl = true;
186
    property.absControl = true;
172
 
187
 
173
    property.type = FlyCapture2::SHUTTER;
188
    property.type = FlyCapture2::SHUTTER;
174
    property.absValue = settings.shutter;
189
    property.absValue = settings.shutter;
175
    cam.SetProperty(&property);
190
    cam.SetProperty(&property);
176
 
191
 
177
    property.type = FlyCapture2::GAIN;
192
    property.type = FlyCapture2::GAIN;
178
    property.absValue = settings.gain;
193
    property.absValue = settings.gain;
179
    cam.SetProperty(&property);
194
    cam.SetProperty(&property);
180
 
195
 
181
}
196
}
182
 
197
 
183
void CameraPointGrey::startCapture(){
198
void CameraPointGrey::startCapture(){
184
 
199
 
185
    FlyCapture2::Error error;
200
    FlyCapture2::Error error;
186
 
201
 
187
    CameraSettings settings = this->getCameraSettings();
202
    CameraSettings settings = this->getCameraSettings();
188
    std::cout << "\tShutter: " << settings.shutter << "ms" << std::endl;
203
    std::cout << "\tShutter: " << settings.shutter << "ms" << std::endl;
189
    std::cout << "\tGain: " << settings.gain << "dB" << std::endl;
204
    std::cout << "\tGain: " << settings.gain << "dB" << std::endl;
190
 
205
 
191
    if(triggerMode == triggerModeHardware){
206
    if(triggerMode == triggerModeHardware){
192
        // Configure for hardware trigger
207
        // Configure for hardware trigger
193
        FlyCapture2::TriggerMode triggerMode;
208
        FlyCapture2::TriggerMode triggerMode;
194
        triggerMode.onOff = true;
209
        triggerMode.onOff = true;
195
        triggerMode.polarity = 0;
210
        triggerMode.polarity = 0;
196
        triggerMode.source = 0;
211
        triggerMode.source = 0;
197
        triggerMode.mode = 14;
212
        triggerMode.mode = 14;
198
        error = cam.SetTriggerMode(&triggerMode);
213
        error = cam.SetTriggerMode(&triggerMode);
199
        if (error != FlyCapture2::PGRERROR_OK)
214
        if (error != FlyCapture2::PGRERROR_OK)
200
            PrintError(error);
215
            PrintError(error);
201
 
216
 
202
    } else if(triggerMode == triggerModeSoftware){
217
    } else if(triggerMode == triggerModeSoftware){
203
        // Configure software trigger
218
        // Configure software trigger
204
        FlyCapture2::TriggerMode triggerMode;
219
        FlyCapture2::TriggerMode triggerMode;
205
        triggerMode.onOff = true;
220
        triggerMode.onOff = true;
206
        triggerMode.polarity = 0;
221
        triggerMode.polarity = 0;
207
        triggerMode.source = 7; // software
222
        triggerMode.source = 7; // software
208
        triggerMode.mode = 0;
223
        triggerMode.mode = 0;
209
        error = cam.SetTriggerMode(&triggerMode);
224
        error = cam.SetTriggerMode(&triggerMode);
210
        if (error != FlyCapture2::PGRERROR_OK)
225
        if (error != FlyCapture2::PGRERROR_OK)
211
            PrintError(error);
226
            PrintError(error);
212
    }
227
    }
213
 
228
 
214
    // Set the trigger timeout to 1000 ms
229
    // Set the trigger timeout to 1000 ms
215
    FlyCapture2::FC2Config config;
230
    FlyCapture2::FC2Config config;
216
    config.grabTimeout = 1000;
231
    config.grabTimeout = 1000;
217
    error = cam.SetConfiguration(&config);
232
    error = cam.SetConfiguration(&config);
218
    if (error != FlyCapture2::PGRERROR_OK)
233
    if (error != FlyCapture2::PGRERROR_OK)
219
        PrintError(error);
234
        PrintError(error);
220
 
235
 
221
    error = cam.StartCapture();
236
    error = cam.StartCapture();
222
    if (error != FlyCapture2::PGRERROR_OK)
237
    if (error != FlyCapture2::PGRERROR_OK)
223
        PrintError(error);
238
        PrintError(error);
224
 
239
 
225
    capturing = true;
240
    capturing = true;
226
}
241
}
227
 
242
 
228
void CameraPointGrey::stopCapture(){
243
void CameraPointGrey::stopCapture(){
229
 
244
 
230
    FlyCapture2::Error error = cam.StopCapture();
245
    FlyCapture2::Error error = cam.StopCapture();
231
    if (error != FlyCapture2::PGRERROR_OK)
246
    if (error != FlyCapture2::PGRERROR_OK)
232
        PrintError(error);
247
        PrintError(error);
233
 
248
 
234
    capturing = false;
249
    capturing = false;
235
}
250
}
236
 
251
 
237
void CameraPointGrey::trigger(){
252
void CameraPointGrey::trigger(){
238
 
253
 
239
    FlyCapture2::Error error;
254
    FlyCapture2::Error error;
240
 
255
 
241
    // Fire software trigger
256
    // Fire software trigger
242
    // broadcasting not supported on some platforms
257
    // broadcasting not supported on some platforms
243
    error = cam.FireSoftwareTrigger(false);
258
    error = cam.FireSoftwareTrigger(false);
244
 
259
 
245
    if (error != FlyCapture2::PGRERROR_OK)
260
    if (error != FlyCapture2::PGRERROR_OK)
246
        PrintError(error);
261
        PrintError(error);
247
 
262
 
248
}
263
}
249
 
264
 
250
CameraFrame CameraPointGrey::getFrame(){
265
CameraFrame CameraPointGrey::getFrame(){
251
 
266
 
252
    FlyCapture2::Error error;
267
    FlyCapture2::Error error;
253
 
268
 
254
    // Retrieve the image
269
    // Retrieve the image
255
    FlyCapture2::Image rawImage;
270
    FlyCapture2::Image rawImage;
256
    error = cam.RetrieveBuffer(&rawImage);
271
    error = cam.RetrieveBuffer(&rawImage);
257
    if (error != FlyCapture2::PGRERROR_OK)
272
    if (error != FlyCapture2::PGRERROR_OK)
258
        PrintError(error);
273
        PrintError(error);
259
 
274
 
260
    rawImage.SetColorProcessing(FlyCapture2::IPP);
275
    rawImage.SetColorProcessing(FlyCapture2::IPP);
261
 
276
 
262
    // de-Bayer
277
    // de-Bayer
263
    rawImage.Convert(FlyCapture2::PIXEL_FORMAT_RGB8, &currentImage);
278
    rawImage.Convert(FlyCapture2::PIXEL_FORMAT_RGB8, &currentImage);
264
 
279
 
265
    CameraFrame frame;
280
    CameraFrame frame;
266
 
281
 
267
    frame.timeStamp = currentImage.GetTimeStamp().cycleCount;
282
    frame.timeStamp = currentImage.GetTimeStamp().cycleCount;
268
    frame.height = currentImage.GetRows();
283
    frame.height = currentImage.GetRows();
269
    frame.width = currentImage.GetCols();
284
    frame.width = currentImage.GetCols();
270
    frame.bitDepth = 8;
285
    frame.bitDepth = 8;
271
    frame.channels = 3;
286
    frame.channels = 3;
272
    frame.memory = (unsigned short*)currentImage.GetData();
287
    frame.memory = (unsigned short*)currentImage.GetData();
273
 
288
 
274
    return frame;
289
    return frame;
275
}
290
}
276
 
291
 
277
 
292
 
278
size_t CameraPointGrey::getFrameSizeBytes(){
293
size_t CameraPointGrey::getFrameSizeBytes(){
279
    
294
    
280
    FlyCapture2::Format7ImageSettings format7Settings;
295
    FlyCapture2::Format7ImageSettings format7Settings;
281
    cam.GetFormat7Configuration(&format7Settings, NULL, NULL);
296
    cam.GetFormat7Configuration(&format7Settings, NULL, NULL);
282
 
297
 
283
    return format7Settings.height*format7Settings.width*3;
298
    return format7Settings.height*format7Settings.width*3;
284
}
299
}
285
 
300
 
286
size_t CameraPointGrey::getFrameWidth(){
301
size_t CameraPointGrey::getFrameWidth(){
287
 
302
 
288
    FlyCapture2::Format7ImageSettings format7Settings;
303
    FlyCapture2::Format7ImageSettings format7Settings;
289
    cam.GetFormat7Configuration(&format7Settings, NULL, NULL);
304
    cam.GetFormat7Configuration(&format7Settings, NULL, NULL);
290
 
305
 
291
    return format7Settings.width;
306
    return format7Settings.width;
292
}
307
}
293
 
308
 
294
size_t CameraPointGrey::getFrameHeight(){
309
size_t CameraPointGrey::getFrameHeight(){
295
 
310
 
296
    FlyCapture2::Format7ImageSettings format7Settings;
311
    FlyCapture2::Format7ImageSettings format7Settings;
297
    cam.GetFormat7Configuration(&format7Settings, NULL, NULL);
312
    cam.GetFormat7Configuration(&format7Settings, NULL, NULL);
298
 
313
 
299
    return format7Settings.height;
314
    return format7Settings.height;
300
 
315
 
301
}
316
}
302
 
317
 
303
 
318
 
304
CameraPointGrey::~CameraPointGrey(){
319
CameraPointGrey::~CameraPointGrey(){
305
 
320
 
306
    if(capturing){
321
    if(capturing){
307
        // Stop camera transmission
322
        // Stop camera transmission
308
        stopCapture();
323
        stopCapture();
309
    }
324
    }
310
 
325
 
311
    // Gracefulle destruct the camera
326
    // Gracefulle destruct the camera
312
    cam.Disconnect();
327
    cam.Disconnect();
313
 
328
 
314
    std::cout << "Closed camera!" << std::endl << std::flush;
329
    std::cout << "Closed camera!" << std::endl << std::flush;
315
}
330
}
316
 
331
 
317
 
332
 
318
 
333
 
319
 
334