1 |
jakw |
1 |
#include "CameraXIMEA.h"
|
|
|
2 |
#include <cstdio>
|
|
|
3 |
|
|
|
4 |
// Note: library header conflicts, and should only be included in cpp files
|
|
|
5 |
#include <xiApi.h>
|
|
|
6 |
|
|
|
7 |
#define HandleResult(res,place) if (res!=XI_OK) {printf("CameraXIMEA: Error at %s (%d)",place,res); fflush(stdout);}
|
|
|
8 |
|
|
|
9 |
std::vector<CameraInfo> CameraXIMEA::getCameraList(){
|
|
|
10 |
|
|
|
11 |
XI_RETURN stat = XI_OK;
|
|
|
12 |
DWORD numCams;
|
|
|
13 |
stat = xiGetNumberDevices(&numCams);
|
|
|
14 |
HandleResult(stat, "xiGetNumberDevices");
|
|
|
15 |
|
|
|
16 |
std::vector<CameraInfo> ret(numCams);
|
|
|
17 |
for(unsigned int i=0; i<numCams; i++){
|
|
|
18 |
CameraInfo info;
|
|
|
19 |
info.vendor = "Ximea";
|
|
|
20 |
char name[20];
|
|
|
21 |
xiGetDeviceInfoString(i, XI_PRM_DEVICE_NAME, name, 20);
|
|
|
22 |
info.model = name;
|
|
|
23 |
info.busID = i;
|
|
|
24 |
ret[i] = info;
|
|
|
25 |
}
|
|
|
26 |
return ret;
|
|
|
27 |
}
|
|
|
28 |
|
|
|
29 |
CameraXIMEA::CameraXIMEA(unsigned int camNum) : camera(NULL){
|
|
|
30 |
|
|
|
31 |
// Set debugging level
|
|
|
32 |
xiSetParamInt(0, XI_PRM_DEBUG_LEVEL, XI_DL_FATAL);
|
|
|
33 |
|
|
|
34 |
// Disable auto bandwidth determination
|
|
|
35 |
xiSetParamInt(0, XI_PRM_AUTO_BANDWIDTH_CALCULATION, XI_ON);
|
|
|
36 |
|
|
|
37 |
// Retrieve a handle to the camera device
|
|
|
38 |
stat = xiOpenDevice(camNum, &camera);
|
|
|
39 |
HandleResult(stat,"xiOpenDevice");
|
|
|
40 |
|
|
|
41 |
// Configure safe buffers (copies data)
|
|
|
42 |
xiSetParamInt(camera, XI_PRM_BUFFER_POLICY, XI_BP_SAFE);
|
|
|
43 |
|
|
|
44 |
// // Output frame signal
|
|
|
45 |
// xiSetParamInt(camera, XI_PRM_GPO_SELECTOR, 1);
|
|
|
46 |
// xiSetParamInt(camera, XI_PRM_GPO_MODE, XI_GPO_ON);
|
|
|
47 |
|
|
|
48 |
// Configure buffer size
|
|
|
49 |
stat = xiSetParamInt(camera, XI_PRM_BUFFERS_QUEUE_SIZE, 10);
|
|
|
50 |
HandleResult(stat,"xiSetParam (XI_PRM_BUFFERS_QUEUE_SIZE)");
|
|
|
51 |
|
|
|
52 |
// Configure queue mode (0 = next frame in queue, 1 = most recent frame)
|
|
|
53 |
stat = xiSetParamInt(camera, XI_PRM_RECENT_FRAME, 1);
|
|
|
54 |
HandleResult(stat,"xiSetParam (XI_PRM_RECENT_FRAME)");
|
|
|
55 |
|
|
|
56 |
// Configure image type
|
|
|
57 |
stat = xiSetParamInt(camera, XI_PRM_IMAGE_DATA_FORMAT, XI_MONO8);
|
|
|
58 |
HandleResult(stat,"xiSetParam (XI_PRM_IMAGE_DATA_FORMAT)");
|
|
|
59 |
|
|
|
60 |
// Configure ouput pin 1 as trigger input
|
|
|
61 |
xiSetParamInt(camera, XI_PRM_GPI_SELECTOR, 1);
|
|
|
62 |
stat = xiSetParamInt(camera, XI_PRM_GPI_MODE, XI_GPI_TRIGGER);
|
|
|
63 |
HandleResult(stat,"xiSetParam (XI_PRM_GPI_MODE)");
|
|
|
64 |
|
|
|
65 |
// // Downsample to half size
|
|
|
66 |
// stat = xiSetParamInt(camera, XI_PRM_DOWNSAMPLING_TYPE, XI_BINNING);
|
|
|
67 |
// HandleResult(stat,"xiSetParam (XI_PRM_DOWNSAMPLING_TYPE)");
|
|
|
68 |
// stat = xiSetParamInt(camera, XI_PRM_DOWNSAMPLING, 2);
|
|
|
69 |
// HandleResult(stat,"xiSetParam (XI_PRM_DOWNSAMPLING)");
|
|
|
70 |
|
|
|
71 |
// // Configure frame rate
|
|
|
72 |
// stat = xiSetParamFloat(camera, XI_PRM_FRAMERATE, 10);
|
|
|
73 |
// HandleResult(stat,"xiSetParam (XI_PRM_FRAMERATE)");
|
|
|
74 |
|
|
|
75 |
// Define ROI
|
|
|
76 |
stat = xiSetParamInt(camera, XI_PRM_WIDTH, 640);
|
|
|
77 |
HandleResult(stat,"xiSetParam (XI_PRM_WIDTH)");
|
|
|
78 |
stat = xiSetParamInt(camera, XI_PRM_HEIGHT, 512);
|
|
|
79 |
HandleResult(stat,"xiSetParam (XI_PRM_HEIGHT)");
|
|
|
80 |
stat = xiSetParamInt(camera, XI_PRM_OFFSET_X, 320);
|
|
|
81 |
HandleResult(stat,"xiSetParam (XI_PRM_OFFSET_X)");
|
|
|
82 |
stat = xiSetParamInt(camera, XI_PRM_OFFSET_Y, 256);
|
|
|
83 |
HandleResult(stat,"xiSetParam (XI_PRM_OFFSET_Y)");
|
|
|
84 |
|
|
|
85 |
// Setting reasonable default settings
|
|
|
86 |
xiSetParamFloat(camera, XI_PRM_GAMMAY, 1.0);
|
|
|
87 |
xiSetParamInt(camera, XI_PRM_EXPOSURE, 16666); //us
|
|
|
88 |
xiSetParamFloat(camera, XI_PRM_GAIN, 0);
|
|
|
89 |
|
|
|
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 |
}
|
|
|
98 |
|
|
|
99 |
void CameraXIMEA::startCapture(){
|
|
|
100 |
|
|
|
101 |
// Configure for hardware trigger
|
|
|
102 |
stat = xiSetParamInt(camera, XI_PRM_TRG_SOURCE, XI_TRG_EDGE_FALLING);
|
|
|
103 |
HandleResult(stat,"xiSetParam (XI_PRM_TRG_SOURCE)");
|
|
|
104 |
|
|
|
105 |
capturing = true;
|
|
|
106 |
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
void CameraXIMEA::stopCapture(){
|
|
|
110 |
|
|
|
111 |
if(!capturing){
|
|
|
112 |
std::cerr << "CameraXIMEA: not capturing!" << std::endl;
|
|
|
113 |
return;
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
// Configure for software trigger (for getSingleFrame())
|
|
|
117 |
stat = xiSetParamInt(camera, XI_PRM_TRG_SOURCE, XI_TRG_SOFTWARE);
|
|
|
118 |
HandleResult(stat,"xiSetParam (XI_PRM_TRG_SOURCE)");
|
|
|
119 |
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
CameraFrame CameraXIMEA::lockFrame(){
|
|
|
123 |
|
|
|
124 |
// Create single image buffer
|
|
|
125 |
XI_IMG image;
|
|
|
126 |
image.bp = NULL;
|
|
|
127 |
image.bp_size = 0;
|
|
|
128 |
|
|
|
129 |
// getting image from camera
|
|
|
130 |
stat = xiGetImage(camera, 1000, &image);
|
|
|
131 |
HandleResult(stat,"xiGetImage");
|
|
|
132 |
|
|
|
133 |
CameraFrame frame;
|
|
|
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 |
|
|
|
140 |
return frame;
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
void CameraXIMEA::unlockFrame(){
|
|
|
144 |
|
|
|
145 |
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
CameraFrame CameraXIMEA::getSingleFrame(){
|
|
|
149 |
|
|
|
150 |
XI_IMG image;
|
|
|
151 |
image.bp = NULL;
|
|
|
152 |
image.bp_size = 0;
|
|
|
153 |
|
|
|
154 |
// Fire software trigger
|
|
|
155 |
stat = xiSetParamInt(camera, XI_PRM_TRG_SOFTWARE, 0);
|
|
|
156 |
HandleResult(stat,"xiSetParam (XI_PRM_TRG_SOFTWARE)");
|
|
|
157 |
|
|
|
158 |
stat = xiGetImage(camera, 1000, &image);
|
|
|
159 |
HandleResult(stat,"xiGetImage");
|
|
|
160 |
|
|
|
161 |
CameraFrame frame;
|
|
|
162 |
frame.height = image.height;
|
|
|
163 |
frame.width = image.width;
|
|
|
164 |
frame.memory = (unsigned char*)image.bp;
|
|
|
165 |
frame.timeStamp = image.tsUSec;
|
|
|
166 |
frame.sizeBytes = image.bp_size;
|
|
|
167 |
|
|
|
168 |
return frame;
|
|
|
169 |
}
|
|
|
170 |
|
|
|
171 |
|
|
|
172 |
size_t CameraXIMEA::getFrameSizeBytes(){
|
|
|
173 |
return 0;
|
|
|
174 |
}
|
|
|
175 |
|
|
|
176 |
void CameraXIMEA::getFrameWidthHeight(unsigned int *width, unsigned int *height){
|
|
|
177 |
int w, h;
|
|
|
178 |
xiGetParamInt(camera, XI_PRM_WIDTH, &w);
|
|
|
179 |
xiGetParamInt(camera, XI_PRM_HEIGHT, &h);
|
|
|
180 |
*width = w;
|
|
|
181 |
*height = h;
|
|
|
182 |
}
|
|
|
183 |
|
|
|
184 |
|
|
|
185 |
void CameraXIMEA::setCameraSettings(CameraSettings settings){
|
|
|
186 |
|
|
|
187 |
// Set shutter (in us)
|
|
|
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 |
}
|
|
|
196 |
|
|
|
197 |
CameraSettings CameraXIMEA::getCameraSettings(){
|
|
|
198 |
|
|
|
199 |
CameraSettings settings;
|
|
|
200 |
|
|
|
201 |
int shutter;
|
|
|
202 |
xiGetParamInt(camera, XI_PRM_EXPOSURE, &shutter);
|
|
|
203 |
settings.shutter = shutter/1000.0; // from us to ms
|
|
|
204 |
xiGetParamFloat(camera, XI_PRM_GAIN, &settings.gain);
|
|
|
205 |
|
|
|
206 |
return settings;
|
|
|
207 |
|
|
|
208 |
}
|
|
|
209 |
|
|
|
210 |
CameraXIMEA::~CameraXIMEA(){
|
|
|
211 |
|
|
|
212 |
// Stop acquisition
|
|
|
213 |
stat = xiStopAcquisition(camera);
|
|
|
214 |
HandleResult(stat,"xiStopAcquisition");
|
|
|
215 |
|
|
|
216 |
// Close device
|
|
|
217 |
xiCloseDevice(camera);
|
|
|
218 |
}
|
|
|
219 |
|
|
|
220 |
|