forked from foxsi/SAAS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnap.cpp
151 lines (123 loc) · 4.16 KB
/
snap.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <iostream>
#include <signal.h>
#include <unistd.h> /* for sleep() */
#include "ImperxStream.hpp"
#include "compression.hpp"
#define TIMEOUT 20000 // milliseconds
#define SLEEP_CAMERA_CONNECT 1 // waits for errors while connecting to camera
long localExposure = 1000;
int16_t localPreampGain = -3;
uint16_t localAnalogGain = 300;
cv::Mat localFrame;
HeaderData localHeader;
sig_atomic_t volatile g_running = 1;
void sig_handler(int signum)
{
if ((signum == SIGINT) || (signum == SIGTERM))
{
if (signum == SIGINT) std::cerr << "Keyboard interrupt received (press enter)\n";
if (signum == SIGTERM) std::cerr << "Termination signal received (press enter)\n";
g_running = 0;
}
}
void ImageSave()
{
if(!localFrame.empty())
{
char timestamp[14];
char filename[128];
struct tm *capturetime;
capturetime = gmtime(&localHeader.captureTime.tv_sec);
strftime(timestamp,14,"%y%m%d_%H%M%S",capturetime);
sprintf(filename, "image_%s_%03d.fits", timestamp, (int)(localHeader.captureTime.tv_nsec/1000000l));
printf("Saving image %s: exposure %d us, analog gain %d, preamp gain %d, min %d, max %d\n", filename, localHeader.exposure, localHeader.analogGain, localHeader.preampGain, localHeader.imageMinMax[0], localHeader.imageMinMax[1]);
writeFITSImage(localFrame, localHeader, filename);
}
else
{
std::cerr << "Empty image!\n";
}
}
int main(int argc, char* argv[])
{
switch(argc) {
case 5:
localPreampGain = atoi(argv[4]);
case 4:
localAnalogGain = atoi(argv[3]);
case 3:
localExposure = atoi(argv[2]);
case 2:
break;
default:
std::cout << "Calling sequeunce: snap <camera IP> [exposure time (us)] [analog gain (0-1023)] [preamp gain (-3,0,3,6)]\n";
return 0;
}
// to catch a Ctrl-C or termination signal and clean up
signal(SIGINT, &sig_handler);
signal(SIGTERM, &sig_handler);
bool cameraReady;
ImperxStream camera;
int width, height;
cameraReady = false;
while(g_running)
{
if (!cameraReady)
{
if (camera.Connect(argv[1]) != 0)
{
std::cerr << "Error connecting to camera!\n";
sleep(SLEEP_CAMERA_CONNECT);
continue;
}
else
{
camera.ConfigureSnap();
camera.SetExposure(localExposure);
camera.SetAnalogGain(localAnalogGain);
camera.SetPreAmpGain(localPreampGain);
width = camera.GetROIWidth();
height = camera.GetROIHeight();
localFrame.create(height, width, CV_8UC1);
if(camera.Initialize() != 0)
{
std::cerr << "Error initializing camera!\n";
//may need disconnect here
sleep(SLEEP_CAMERA_CONNECT);
continue;
}
cameraReady = true;
}
}
else
{
if(!camera.Snap(localFrame, TIMEOUT))
{
memset(&localHeader, 0, sizeof(HeaderData));
timespec now;
clock_gettime(CLOCK_REALTIME, &now);
localHeader.captureTime = now;
uint8_t localMin, localMax;
localHeader.exposure = localExposure;
localHeader.preampGain = localPreampGain;
localHeader.analogGain = localAnalogGain;
double tempMin, tempMax;
cv::minMaxLoc(localFrame, &tempMin, &tempMax);
std::cout << "Image max: " << (int)tempMax << std::endl;
localHeader.imageMinMax[0] = localMin;
localHeader.imageMinMax[1] = localMax;
ImageSave();
}
else
{
std::cerr << "Image not taken\n";
}
}
std::cout << "Press return to snap\n";
std::string buffer;
std::getline(cin, buffer);
}
camera.Stop();
camera.Disconnect();
return 0;
}