Subversion Repositories seema-scanner

Rev

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

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