Subversion Repositories seema-scanner

Rev

Rev 1 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1 Rev 18
Line 1... Line 1...
1
#include "CameraXIMEA.h"
1
#include "CameraXIMEA.h"
2
#include <cstdio>
2
#include <cstdio>
3
 
3
 
4
// Note: library header conflicts, and should only be included in cpp files
4
// Note: library headers conflict with IDS imaging headers
5
#include <xiApi.h>
5
#include <xiApi.h>
6
 
6
 
7
#define HandleResult(res,place) if (res!=XI_OK) {printf("CameraXIMEA: Error at %s (%d)",place,res); fflush(stdout);}
7
#define HandleResult(res,place) if (res!=XI_OK) {printf("CameraXIMEA: Error at %s (%d)\n",place,res); fflush(stdout);}
8
 
8
 
9
std::vector<CameraInfo> CameraXIMEA::getCameraList(){
9
std::vector<CameraInfo> CameraXIMEA::getCameraList(){
10
 
10
 
11
    XI_RETURN stat = XI_OK;
11
    XI_RETURN stat = XI_OK;
12
    DWORD numCams;
12
    DWORD numCams;
Line 24... Line 24...
24
        ret[i] = info;
24
        ret[i] = info;
25
    }
25
    }
26
    return ret;
26
    return ret;
27
}
27
}
28
 
28
 
29
CameraXIMEA::CameraXIMEA(unsigned int camNum) : camera(NULL){
29
CameraXIMEA::CameraXIMEA(unsigned int camNum, CameraTriggerMode triggerMode) : Camera(triggerMode), camera(NULL){
30
 
30
 
31
    // Set debugging level
31
    // Set debugging level
32
    xiSetParamInt(0, XI_PRM_DEBUG_LEVEL, XI_DL_FATAL);
32
    xiSetParamInt(0, XI_PRM_DEBUG_LEVEL, XI_DL_FATAL);
33
 
33
 
34
    // Disable auto bandwidth determination
34
    // Disable auto bandwidth determination (takes some seconds in initialization)
35
    xiSetParamInt(0, XI_PRM_AUTO_BANDWIDTH_CALCULATION, XI_ON);
35
    xiSetParamInt(0, XI_PRM_AUTO_BANDWIDTH_CALCULATION, XI_OFF);
36
 
36
 
37
    // Retrieve a handle to the camera device
37
    // Retrieve a handle to the camera device
38
    stat = xiOpenDevice(camNum, &camera);
38
    stat = xiOpenDevice(camNum, &camera);
39
    HandleResult(stat,"xiOpenDevice");
39
    HandleResult(stat,"xiOpenDevice");
40
 
40
 
41
    // Configure safe buffers (copies data)
41
    // Configure unsafe buffers (prevents old buffers, memory leak)
42
    xiSetParamInt(camera, XI_PRM_BUFFER_POLICY, XI_BP_SAFE);
42
    xiSetParamInt(camera, XI_PRM_BUFFER_POLICY, XI_BP_UNSAFE);
43
 
43
 
44
//    // Output frame signal
44
//    // Output frame signal
45
//    xiSetParamInt(camera, XI_PRM_GPO_SELECTOR, 1);
45
//    xiSetParamInt(camera, XI_PRM_GPO_SELECTOR, 1);
46
//    xiSetParamInt(camera, XI_PRM_GPO_MODE, XI_GPO_ON);
46
//    xiSetParamInt(camera, XI_PRM_GPO_MODE, XI_GPO_ON);
47
 
47
 
48
    // Configure buffer size
48
    // Configure buffer size
-
 
49
//    stat = xiSetParamInt(camera, XI_PRM_ACQ_BUFFER_SIZE, 128*1024);
-
 
50
//    HandleResult(stat,"xiSetParam (XI_PRM_ACQ_BUFFER_SIZE)");
49
    stat = xiSetParamInt(camera, XI_PRM_BUFFERS_QUEUE_SIZE, 10);
51
//    stat = xiSetParamInt(camera, XI_PRM_BUFFERS_QUEUE_SIZE, 10);
50
    HandleResult(stat,"xiSetParam (XI_PRM_BUFFERS_QUEUE_SIZE)");
52
//    HandleResult(stat,"xiSetParam (XI_PRM_BUFFERS_QUEUE_SIZE)");
51
 
53
 
52
    // Configure queue mode (0 = next frame in queue, 1 = most recent frame)
54
    // Configure queue mode (0 = next frame in queue, 1 = most recent frame)
53
    stat = xiSetParamInt(camera, XI_PRM_RECENT_FRAME, 1);
55
    stat = xiSetParamInt(camera, XI_PRM_RECENT_FRAME, 0);
54
    HandleResult(stat,"xiSetParam (XI_PRM_RECENT_FRAME)");
56
    HandleResult(stat,"xiSetParam (XI_PRM_RECENT_FRAME)");
55
 
57
 
56
    // Configure image type
58
    // Configure image type
57
    stat = xiSetParamInt(camera, XI_PRM_IMAGE_DATA_FORMAT, XI_MONO8);
59
    stat = xiSetParamInt(camera, XI_PRM_IMAGE_DATA_FORMAT, XI_MONO16);
58
    HandleResult(stat,"xiSetParam (XI_PRM_IMAGE_DATA_FORMAT)");
60
    HandleResult(stat,"xiSetParam (XI_PRM_IMAGE_DATA_FORMAT)");
59
 
61
 
60
    // Configure ouput pin 1 as trigger input
62
    // Configure input pin 1 as trigger input
61
    xiSetParamInt(camera, XI_PRM_GPI_SELECTOR, 1);
63
    xiSetParamInt(camera, XI_PRM_GPI_SELECTOR, 1);
62
    stat = xiSetParamInt(camera, XI_PRM_GPI_MODE, XI_GPI_TRIGGER);
64
    stat = xiSetParamInt(camera, XI_PRM_GPI_MODE, XI_GPI_TRIGGER);
63
    HandleResult(stat,"xiSetParam (XI_PRM_GPI_MODE)");
65
    HandleResult(stat,"xiSetParam (XI_PRM_GPI_MODE)");
64
 
66
 
65
//    // Downsample to half size
67
//    // Downsample to half size
66
//    stat = xiSetParamInt(camera, XI_PRM_DOWNSAMPLING_TYPE, XI_BINNING);
68
//    stat = xiSetParamInt(camera, XI_PRM_DOWNSAMPLING_TYPE, XI_SKIPPING);
67
//    HandleResult(stat,"xiSetParam (XI_PRM_DOWNSAMPLING_TYPE)");
69
//    HandleResult(stat,"xiSetParam (XI_PRM_DOWNSAMPLING_TYPE)");
68
//    stat = xiSetParamInt(camera, XI_PRM_DOWNSAMPLING, 2);
70
//    stat = xiSetParamInt(camera, XI_PRM_DOWNSAMPLING, 2);
69
//    HandleResult(stat,"xiSetParam (XI_PRM_DOWNSAMPLING)");
71
//    HandleResult(stat,"xiSetParam (XI_PRM_DOWNSAMPLING)");
70
 
72
 
71
//    // Configure frame rate
73
//    // Configure frame rate
Line 85... Line 87...
85
    // Setting reasonable default settings
87
    // Setting reasonable default settings
86
    xiSetParamFloat(camera, XI_PRM_GAMMAY, 1.0);
88
    xiSetParamFloat(camera, XI_PRM_GAMMAY, 1.0);
87
    xiSetParamInt(camera, XI_PRM_EXPOSURE, 16666); //us
89
    xiSetParamInt(camera, XI_PRM_EXPOSURE, 16666); //us
88
    xiSetParamFloat(camera, XI_PRM_GAIN, 0);
90
    xiSetParamFloat(camera, XI_PRM_GAIN, 0);
89
 
91
 
90
    // Configure for software trigger (for getSingleFrame())
-
 
91
    stat = xiSetParamInt(camera, XI_PRM_TRG_SOURCE, XI_TRG_SOFTWARE);
-
 
92
    HandleResult(stat,"xiSetParam (XI_PRM_TRG_SOURCE)");
-
 
93
 
-
 
94
    // Start aquisition
-
 
95
    stat = xiStartAcquisition(camera);
-
 
96
    HandleResult(stat,"xiStartAcquisition");
-
 
97
}
92
}
98
 
93
 
99
void CameraXIMEA::startCapture(){
94
CameraSettings CameraXIMEA::getCameraSettings(){
100
 
95
 
101
    // Configure for hardware trigger
96
    CameraSettings settings;
102
    stat = xiSetParamInt(camera, XI_PRM_TRG_SOURCE, XI_TRG_EDGE_FALLING);
-
 
103
    HandleResult(stat,"xiSetParam (XI_PRM_TRG_SOURCE)");
-
 
104
 
97
 
105
    capturing = true;
98
    int shutter;
-
 
99
    xiGetParamInt(camera, XI_PRM_EXPOSURE, &shutter);
-
 
100
    settings.shutter = shutter/1000.0; // from us to ms
-
 
101
    xiGetParamFloat(camera, XI_PRM_GAIN, &settings.gain);
106
 
102
 
-
 
103
    return settings;
107
}
104
}
108
 
105
 
109
void CameraXIMEA::stopCapture(){
106
void CameraXIMEA::setCameraSettings(CameraSettings settings){
110
 
107
 
111
    if(!capturing){
108
    // Set shutter (in us)
112
        std::cerr << "CameraXIMEA: not capturing!" << std::endl;
109
    xiSetParamInt(camera, XI_PRM_EXPOSURE, settings.shutter*1000);
113
        return;
110
    // Set gain (in dB)
-
 
111
    xiSetParamFloat(camera, XI_PRM_GAIN, settings.gain);
-
 
112
 
-
 
113
    std::cout << "Setting camera parameters:" << std::endl
-
 
114
              << "Shutter: " << settings.shutter << " ms" << std::endl
-
 
115
              << "Gain: " << settings.gain << " dB" << std::endl;
114
    }
116
}
115
 
117
 
-
 
118
void CameraXIMEA::startCapture(){
-
 
119
 
-
 
120
    if(triggerMode == triggerModeHardware){
-
 
121
        // Configure for hardware trigger
-
 
122
        stat = xiSetParamInt(camera, XI_PRM_TRG_SOURCE, XI_TRG_EDGE_RISING);
-
 
123
        HandleResult(stat,"xiSetParam (XI_PRM_TRG_SOURCE)");
-
 
124
    } else if(triggerMode == triggerModeSoftware){
116
    // Configure for software trigger (for getSingleFrame())
125
        // Configure for software trigger (for getSingleFrame())
117
    stat = xiSetParamInt(camera, XI_PRM_TRG_SOURCE, XI_TRG_SOFTWARE);
126
        stat = xiSetParamInt(camera, XI_PRM_TRG_SOURCE, XI_TRG_SOFTWARE);
118
    HandleResult(stat,"xiSetParam (XI_PRM_TRG_SOURCE)");
127
        HandleResult(stat,"xiSetParam (XI_PRM_TRG_SOURCE)");
119
 
-
 
120
}
128
    }
121
 
129
 
122
CameraFrame CameraXIMEA::lockFrame(){
-
 
123
 
-
 
124
    // Create single image buffer
-
 
125
    XI_IMG image;
-
 
126
    image.bp = NULL;
130
    // Start aquistion
127
    image.bp_size = 0;
-
 
128
 
-
 
129
    // getting image from camera
-
 
130
    stat = xiGetImage(camera, 1000, &image);
131
    stat = xiStartAcquisition(camera);
131
    HandleResult(stat,"xiGetImage");
132
    HandleResult(stat,"xiStartAcquisition");
132
 
133
 
133
    CameraFrame frame;
134
    capturing = true;
134
    frame.height = image.height;
-
 
135
    frame.width = image.width;
-
 
136
    frame.memory = (unsigned char*)image.bp;
-
 
137
    frame.timeStamp = image.tsUSec;
-
 
138
    frame.sizeBytes = image.bp_size;
-
 
139
 
135
 
140
    return frame;
-
 
141
}
136
}
142
 
137
 
143
void CameraXIMEA::unlockFrame(){
138
void CameraXIMEA::stopCapture(){
144
 
139
 
-
 
140
    if(!capturing){
-
 
141
        std::cerr << "CameraXIMEA: not capturing!" << std::endl;
-
 
142
        return;
-
 
143
    }
145
 
144
 
146
}
145
}
147
 
146
 
148
CameraFrame CameraXIMEA::getSingleFrame(){
147
CameraFrame CameraXIMEA::getFrame(){
149
 
148
 
-
 
149
    // Create single image buffer
150
    XI_IMG image;
150
    XI_IMG image;
-
 
151
    image.size = SIZE_XI_IMG_V2; // must be initialized
151
    image.bp = NULL;
152
    image.bp = NULL;
152
    image.bp_size = 0;
153
    image.bp_size = 0;
153
 
154
 
-
 
155
    if(triggerMode == triggerModeSoftware){
154
    // Fire software trigger
156
        // Fire software trigger
155
    stat = xiSetParamInt(camera, XI_PRM_TRG_SOFTWARE, 0);
157
        stat = xiSetParamInt(camera, XI_PRM_TRG_SOFTWARE, 0);
156
    HandleResult(stat,"xiSetParam (XI_PRM_TRG_SOFTWARE)");
158
        HandleResult(stat,"xiSetParam (XI_PRM_TRG_SOFTWARE)");
157
 
159
 
-
 
160
        // Retrieve image from camera
158
    stat = xiGetImage(camera, 1000, &image);
161
        stat = xiGetImage(camera, 1000, &image);
159
    HandleResult(stat,"xiGetImage");
162
        HandleResult(stat,"xiGetImage");
-
 
163
    } else {
-
 
164
 
-
 
165
        // Retrieve image from camera
-
 
166
        stat = xiGetImage(camera, 50, &image);
-
 
167
        HandleResult(stat,"xiGetImage");
-
 
168
    }
-
 
169
 
-
 
170
    // Empty buffer
-
 
171
    while(xiGetImage(camera, 1, &image) == XI_OK){
-
 
172
        std::cerr << "drop!" << std::endl;
-
 
173
        continue;
-
 
174
    }
160
 
175
 
161
    CameraFrame frame;
176
    CameraFrame frame;
162
    frame.height = image.height;
177
    frame.height = image.height;
163
    frame.width = image.width;
178
    frame.width = image.width;
164
    frame.memory = (unsigned char*)image.bp;
179
    frame.memory = (unsigned short*)image.bp;
-
 
180
    frame.bitDepth = 10;
165
    frame.timeStamp = image.tsUSec;
181
    frame.timeStamp = image.tsUSec;
166
    frame.sizeBytes = image.bp_size;
182
    frame.sizeBytes = image.bp_size;
167
 
183
 
168
    return frame;
184
    return frame;
169
}
185
}
Line 171... Line 187...
171
 
187
 
172
size_t CameraXIMEA::getFrameSizeBytes(){
188
size_t CameraXIMEA::getFrameSizeBytes(){
173
    return 0;
189
    return 0;
174
}
190
}
175
 
191
 
176
void CameraXIMEA::getFrameWidthHeight(unsigned int *width, unsigned int *height){
192
size_t CameraXIMEA::getFrameWidth(){
177
    int w, h;
193
    int w;
178
    xiGetParamInt(camera, XI_PRM_WIDTH, &w);
194
    xiGetParamInt(camera, XI_PRM_WIDTH, &w);
179
    xiGetParamInt(camera, XI_PRM_HEIGHT, &h);
-
 
180
    *width = w;
-
 
181
    *height = h;
-
 
182
}
-
 
183
 
-
 
184
 
195
 
185
void CameraXIMEA::setCameraSettings(CameraSettings settings){
-
 
186
 
-
 
187
    // Set shutter (in us)
196
    return w;
188
    xiSetParamInt(camera, XI_PRM_EXPOSURE, settings.shutter*1000);
-
 
189
    // Set gain (in dB)
-
 
190
    xiSetParamFloat(camera, XI_PRM_GAIN, settings.gain);
-
 
191
 
-
 
192
    std::cout << "Setting camera parameters:" << std::endl
-
 
193
              << "Shutter: " << settings.shutter << " ms" << std::endl
-
 
194
              << "Gain: " << settings.gain << " dB" << std::endl;
-
 
195
}
197
}
196
 
198
 
197
CameraSettings CameraXIMEA::getCameraSettings(){
199
size_t CameraXIMEA::getFrameHeight(){
198
 
-
 
199
    CameraSettings settings;
-
 
200
 
-
 
201
    int shutter;
200
    int h;
202
    xiGetParamInt(camera, XI_PRM_EXPOSURE, &shutter);
201
    xiGetParamInt(camera, XI_PRM_HEIGHT, &h);
203
    settings.shutter = shutter/1000.0; // from us to ms
-
 
204
    xiGetParamFloat(camera, XI_PRM_GAIN, &settings.gain);
-
 
205
 
-
 
206
    return settings;
-
 
207
 
202
 
-
 
203
    return h;
208
}
204
}
209
 
205
 
210
CameraXIMEA::~CameraXIMEA(){
206
CameraXIMEA::~CameraXIMEA(){
211
 
207
 
-
 
208
    if(capturing){
212
    // Stop acquisition
209
        // Stop acquisition
213
    stat = xiStopAcquisition(camera);
210
        stat = xiStopAcquisition(camera);
214
    HandleResult(stat,"xiStopAcquisition");
211
        HandleResult(stat,"xiStopAcquisition");
-
 
212
    }
215
 
213
 
216
    // Close device
214
    // Close device
217
    xiCloseDevice(camera);
215
    xiCloseDevice(camera);
218
}
216
}
219
 
217