Subversion Repositories seema-scanner

Rev

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

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