Subversion Repositories seema-scanner

Rev

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

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