Subversion Repositories seema-scanner

Rev

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

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