Subversion Repositories seema-scanner

Rev

Rev 18 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 18 Rev 205
1
#include "CameraXIMEA.h"
1
#include "CameraXIMEA.h"
2
#include <cstdio>
2
#include <cstdio>
3
 
3
 
4
// Note: library headers conflict with IDS imaging headers
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)\n",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;
13
    stat = xiGetNumberDevices(&numCams);
13
    stat = xiGetNumberDevices(&numCams);
14
    HandleResult(stat, "xiGetNumberDevices");
14
    HandleResult(stat, "xiGetNumberDevices");
15
 
15
 
16
    std::vector<CameraInfo> ret(numCams);
16
    std::vector<CameraInfo> ret(numCams);
17
    for(unsigned int i=0; i<numCams; i++){
17
    for(unsigned int i=0; i<numCams; i++){
18
        CameraInfo info;
18
        CameraInfo info;
19
        info.vendor = "Ximea";
19
        info.vendor = "Ximea";
20
        char name[20];
20
        char name[20];
21
        xiGetDeviceInfoString(i, XI_PRM_DEVICE_NAME, name, 20);
21
        xiGetDeviceInfoString(i, XI_PRM_DEVICE_NAME, name, 20);
22
        info.model = name;
22
        info.model = name;
23
        info.busID = i;
23
        info.busID = i;
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, CameraTriggerMode triggerMode) : Camera(triggerMode), 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 (takes some seconds in initialization)
34
    // Disable auto bandwidth determination (takes some seconds in initialization)
35
    xiSetParamInt(0, XI_PRM_AUTO_BANDWIDTH_CALCULATION, XI_OFF);
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 unsafe buffers (prevents old buffers, memory leak)
41
    // Configure unsafe buffers (prevents old buffers, memory leak)
42
    xiSetParamInt(camera, XI_PRM_BUFFER_POLICY, XI_BP_UNSAFE);
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);
49
//    stat = xiSetParamInt(camera, XI_PRM_ACQ_BUFFER_SIZE, 128*1024);
50
//    HandleResult(stat,"xiSetParam (XI_PRM_ACQ_BUFFER_SIZE)");
50
//    HandleResult(stat,"xiSetParam (XI_PRM_ACQ_BUFFER_SIZE)");
51
//    stat = xiSetParamInt(camera, XI_PRM_BUFFERS_QUEUE_SIZE, 10);
51
//    stat = xiSetParamInt(camera, XI_PRM_BUFFERS_QUEUE_SIZE, 10);
52
//    HandleResult(stat,"xiSetParam (XI_PRM_BUFFERS_QUEUE_SIZE)");
52
//    HandleResult(stat,"xiSetParam (XI_PRM_BUFFERS_QUEUE_SIZE)");
53
 
53
 
54
    // 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)
55
    stat = xiSetParamInt(camera, XI_PRM_RECENT_FRAME, 0);
55
    stat = xiSetParamInt(camera, XI_PRM_RECENT_FRAME, 0);
56
    HandleResult(stat,"xiSetParam (XI_PRM_RECENT_FRAME)");
56
    HandleResult(stat,"xiSetParam (XI_PRM_RECENT_FRAME)");
57
 
57
 
58
    // Configure image type
58
    // Configure image type
59
    stat = xiSetParamInt(camera, XI_PRM_IMAGE_DATA_FORMAT, XI_MONO16);
59
    stat = xiSetParamInt(camera, XI_PRM_IMAGE_DATA_FORMAT, XI_MONO16);
60
    HandleResult(stat,"xiSetParam (XI_PRM_IMAGE_DATA_FORMAT)");
60
    HandleResult(stat,"xiSetParam (XI_PRM_IMAGE_DATA_FORMAT)");
61
 
61
 
62
    // Configure input pin 1 as trigger input
62
    // Configure input pin 1 as trigger input
63
    xiSetParamInt(camera, XI_PRM_GPI_SELECTOR, 1);
63
    xiSetParamInt(camera, XI_PRM_GPI_SELECTOR, 1);
64
    stat = xiSetParamInt(camera, XI_PRM_GPI_MODE, XI_GPI_TRIGGER);
64
    stat = xiSetParamInt(camera, XI_PRM_GPI_MODE, XI_GPI_TRIGGER);
65
    HandleResult(stat,"xiSetParam (XI_PRM_GPI_MODE)");
65
    HandleResult(stat,"xiSetParam (XI_PRM_GPI_MODE)");
66
 
66
 
67
//    // Downsample to half size
67
//    // Downsample to half size
68
//    stat = xiSetParamInt(camera, XI_PRM_DOWNSAMPLING_TYPE, XI_SKIPPING);
68
//    stat = xiSetParamInt(camera, XI_PRM_DOWNSAMPLING_TYPE, XI_SKIPPING);
69
//    HandleResult(stat,"xiSetParam (XI_PRM_DOWNSAMPLING_TYPE)");
69
//    HandleResult(stat,"xiSetParam (XI_PRM_DOWNSAMPLING_TYPE)");
70
//    stat = xiSetParamInt(camera, XI_PRM_DOWNSAMPLING, 2);
70
//    stat = xiSetParamInt(camera, XI_PRM_DOWNSAMPLING, 2);
71
//    HandleResult(stat,"xiSetParam (XI_PRM_DOWNSAMPLING)");
71
//    HandleResult(stat,"xiSetParam (XI_PRM_DOWNSAMPLING)");
72
 
72
 
73
//    // Configure frame rate
73
//    // Configure frame rate
74
//    stat = xiSetParamFloat(camera, XI_PRM_FRAMERATE, 10);
74
//    stat = xiSetParamFloat(camera, XI_PRM_FRAMERATE, 10);
75
//    HandleResult(stat,"xiSetParam (XI_PRM_FRAMERATE)");
75
//    HandleResult(stat,"xiSetParam (XI_PRM_FRAMERATE)");
76
 
76
 
77
    // Define ROI
77
    // Define ROI
78
    stat = xiSetParamInt(camera, XI_PRM_WIDTH, 640);
78
    stat = xiSetParamInt(camera, XI_PRM_WIDTH, 640);
79
    HandleResult(stat,"xiSetParam (XI_PRM_WIDTH)");
79
    HandleResult(stat,"xiSetParam (XI_PRM_WIDTH)");
80
    stat = xiSetParamInt(camera, XI_PRM_HEIGHT, 512);
80
    stat = xiSetParamInt(camera, XI_PRM_HEIGHT, 512);
81
    HandleResult(stat,"xiSetParam (XI_PRM_HEIGHT)");
81
    HandleResult(stat,"xiSetParam (XI_PRM_HEIGHT)");
82
    stat = xiSetParamInt(camera, XI_PRM_OFFSET_X, 320);
82
    stat = xiSetParamInt(camera, XI_PRM_OFFSET_X, 320);
83
    HandleResult(stat,"xiSetParam (XI_PRM_OFFSET_X)");
83
    HandleResult(stat,"xiSetParam (XI_PRM_OFFSET_X)");
84
    stat = xiSetParamInt(camera, XI_PRM_OFFSET_Y, 256);
84
    stat = xiSetParamInt(camera, XI_PRM_OFFSET_Y, 256);
85
    HandleResult(stat,"xiSetParam (XI_PRM_OFFSET_Y)");
85
    HandleResult(stat,"xiSetParam (XI_PRM_OFFSET_Y)");
86
 
86
 
87
    // Setting reasonable default settings
87
    // Setting reasonable default settings
88
    xiSetParamFloat(camera, XI_PRM_GAMMAY, 1.0);
88
    xiSetParamFloat(camera, XI_PRM_GAMMAY, 1.0);
89
    xiSetParamInt(camera, XI_PRM_EXPOSURE, 16666); //us
89
    xiSetParamInt(camera, XI_PRM_EXPOSURE, 16666); //us
90
    xiSetParamFloat(camera, XI_PRM_GAIN, 0);
90
    xiSetParamFloat(camera, XI_PRM_GAIN, 0);
91
 
91
 
92
}
92
}
93
 
93
 
94
CameraSettings CameraXIMEA::getCameraSettings(){
94
CameraSettings CameraXIMEA::getCameraSettings(){
95
 
95
 
96
    CameraSettings settings;
96
    CameraSettings settings;
97
 
97
 
98
    int shutter;
98
    int shutter;
99
    xiGetParamInt(camera, XI_PRM_EXPOSURE, &shutter);
99
    xiGetParamInt(camera, XI_PRM_EXPOSURE, &shutter);
100
    settings.shutter = shutter/1000.0; // from us to ms
100
    settings.shutter = shutter/1000.0; // from us to ms
101
    xiGetParamFloat(camera, XI_PRM_GAIN, &settings.gain);
101
    xiGetParamFloat(camera, XI_PRM_GAIN, &settings.gain);
102
 
102
 
103
    return settings;
103
    return settings;
104
}
104
}
105
 
105
 
106
void CameraXIMEA::setCameraSettings(CameraSettings settings){
106
void CameraXIMEA::setCameraSettings(CameraSettings settings){
107
 
107
 
108
    // Set shutter (in us)
108
    // Set shutter (in us)
109
    xiSetParamInt(camera, XI_PRM_EXPOSURE, settings.shutter*1000);
109
    xiSetParamInt(camera, XI_PRM_EXPOSURE, settings.shutter*1000);
110
    // Set gain (in dB)
110
    // Set gain (in dB)
111
    xiSetParamFloat(camera, XI_PRM_GAIN, settings.gain);
111
    xiSetParamFloat(camera, XI_PRM_GAIN, settings.gain);
112
 
112
 
113
    std::cout << "Setting camera parameters:" << std::endl
113
    std::cout << "Setting camera parameters:" << std::endl
114
              << "Shutter: " << settings.shutter << " ms" << std::endl
114
              << "Shutter: " << settings.shutter << " ms" << std::endl
115
              << "Gain: " << settings.gain << " dB" << std::endl;
115
              << "Gain: " << settings.gain << " dB" << std::endl;
116
}
116
}
117
 
117
 
118
void CameraXIMEA::startCapture(){
118
void CameraXIMEA::startCapture(){
119
 
119
 
120
    if(triggerMode == triggerModeHardware){
120
    if(triggerMode == triggerModeHardware){
121
        // Configure for hardware trigger
121
        // Configure for hardware trigger
122
        stat = xiSetParamInt(camera, XI_PRM_TRG_SOURCE, XI_TRG_EDGE_RISING);
122
        stat = xiSetParamInt(camera, XI_PRM_TRG_SOURCE, XI_TRG_EDGE_RISING);
123
        HandleResult(stat,"xiSetParam (XI_PRM_TRG_SOURCE)");
123
        HandleResult(stat,"xiSetParam (XI_PRM_TRG_SOURCE)");
124
    } else if(triggerMode == triggerModeSoftware){
124
    } else if(triggerMode == triggerModeSoftware){
125
        // Configure for software trigger (for getSingleFrame())
125
        // Configure for software trigger (for getSingleFrame())
126
        stat = xiSetParamInt(camera, XI_PRM_TRG_SOURCE, XI_TRG_SOFTWARE);
126
        stat = xiSetParamInt(camera, XI_PRM_TRG_SOURCE, XI_TRG_SOFTWARE);
127
        HandleResult(stat,"xiSetParam (XI_PRM_TRG_SOURCE)");
127
        HandleResult(stat,"xiSetParam (XI_PRM_TRG_SOURCE)");
128
    }
128
    }
129
 
129
 
130
    // Start aquistion
130
    // Start aquistion
131
    stat = xiStartAcquisition(camera);
131
    stat = xiStartAcquisition(camera);
132
    HandleResult(stat,"xiStartAcquisition");
132
    HandleResult(stat,"xiStartAcquisition");
133
 
133
 
134
    capturing = true;
134
    capturing = true;
135
 
135
 
136
}
136
}
137
 
137
 
138
void CameraXIMEA::stopCapture(){
138
void CameraXIMEA::stopCapture(){
139
 
139
 
140
    if(!capturing){
140
    if(!capturing){
141
        std::cerr << "CameraXIMEA: not capturing!" << std::endl;
141
        std::cerr << "CameraXIMEA: not capturing!" << std::endl;
142
        return;
142
        return;
143
    }
143
    }
144
 
144
 
145
}
145
}
146
 
146
 
147
CameraFrame CameraXIMEA::getFrame(){
147
CameraFrame CameraXIMEA::getFrame(){
148
 
148
 
149
    // Create single image buffer
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.size = SIZE_XI_IMG_V2; // must be initialized
152
    image.bp = NULL;
152
    image.bp = NULL;
153
    image.bp_size = 0;
153
    image.bp_size = 0;
154
 
154
 
155
    if(triggerMode == triggerModeSoftware){
155
    if(triggerMode == triggerModeSoftware){
156
        // Fire software trigger
156
        // Fire software trigger
157
        stat = xiSetParamInt(camera, XI_PRM_TRG_SOFTWARE, 0);
157
        stat = xiSetParamInt(camera, XI_PRM_TRG_SOFTWARE, 0);
158
        HandleResult(stat,"xiSetParam (XI_PRM_TRG_SOFTWARE)");
158
        HandleResult(stat,"xiSetParam (XI_PRM_TRG_SOFTWARE)");
159
 
159
 
160
        // Retrieve image from camera
160
        // Retrieve image from camera
161
        stat = xiGetImage(camera, 1000, &image);
161
        stat = xiGetImage(camera, 1000, &image);
162
        HandleResult(stat,"xiGetImage");
162
        HandleResult(stat,"xiGetImage");
163
    } else {
163
    } else {
164
 
164
 
165
        // Retrieve image from camera
165
        // Retrieve image from camera
166
        stat = xiGetImage(camera, 50, &image);
166
        stat = xiGetImage(camera, 50, &image);
167
        HandleResult(stat,"xiGetImage");
167
        HandleResult(stat,"xiGetImage");
168
    }
168
    }
169
 
169
 
170
    // Empty buffer
170
    // Empty buffer
171
    while(xiGetImage(camera, 1, &image) == XI_OK){
171
    while(xiGetImage(camera, 1, &image) == XI_OK){
172
        std::cerr << "drop!" << std::endl;
172
        std::cerr << "drop!" << std::endl;
173
        continue;
173
        continue;
174
    }
174
    }
175
 
175
 
176
    CameraFrame frame;
176
    CameraFrame frame;
177
    frame.height = image.height;
177
    frame.height = image.height;
178
    frame.width = image.width;
178
    frame.width = image.width;
179
    frame.memory = (unsigned short*)image.bp;
179
    frame.memory = (unsigned short*)image.bp;
180
    frame.bitDepth = 10;
180
    frame.bitDepth = 10;
181
    frame.timeStamp = image.tsUSec;
181
    frame.timeStamp = image.tsUSec;
182
    frame.sizeBytes = image.bp_size;
182
    frame.sizeBytes = image.bp_size;
183
 
183
 
184
    return frame;
184
    return frame;
185
}
185
}
186
 
186
 
187
 
187
 
188
size_t CameraXIMEA::getFrameSizeBytes(){
188
size_t CameraXIMEA::getFrameSizeBytes(){
189
    return 0;
189
    return 0;
190
}
190
}
191
 
191
 
192
size_t CameraXIMEA::getFrameWidth(){
192
size_t CameraXIMEA::getFrameWidth(){
193
    int w;
193
    int w;
194
    xiGetParamInt(camera, XI_PRM_WIDTH, &w);
194
    xiGetParamInt(camera, XI_PRM_WIDTH, &w);
195
 
195
 
196
    return w;
196
    return w;
197
}
197
}
198
 
198
 
199
size_t CameraXIMEA::getFrameHeight(){
199
size_t CameraXIMEA::getFrameHeight(){
200
    int h;
200
    int h;
201
    xiGetParamInt(camera, XI_PRM_HEIGHT, &h);
201
    xiGetParamInt(camera, XI_PRM_HEIGHT, &h);
202
 
202
 
203
    return h;
203
    return h;
204
}
204
}
205
 
205
 
206
CameraXIMEA::~CameraXIMEA(){
206
CameraXIMEA::~CameraXIMEA(){
207
 
207
 
208
    if(capturing){
208
    if(capturing){
209
        // Stop acquisition
209
        // Stop acquisition
210
        stat = xiStopAcquisition(camera);
210
        stat = xiStopAcquisition(camera);
211
        HandleResult(stat,"xiStopAcquisition");
211
        HandleResult(stat,"xiStopAcquisition");
212
    }
212
    }
213
 
213
 
214
    // Close device
214
    // Close device
215
    xiCloseDevice(camera);
215
    xiCloseDevice(camera);
216
}
216
}
217
 
217
 
218
 
218
 
219
 
219