Subversion Repositories seema-scanner

Rev

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

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