Subversion Repositories seema-scanner

Rev

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

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