forked from ifm/libo3d3xx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex-exposure_times.cpp
164 lines (145 loc) · 5.01 KB
/
ex-exposure_times.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
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
* Copyright (C) 2016 Love Park Robotics, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distribted on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//
// ex-exposure_times.cpp
//
// Shows how to change imager exposure times on the fly while streaming in
// pixel data and validating the setting of the exposure times registered to
// the frame data.
//
#include <cstdint>
#include <iostream>
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>
#include <o3d3xx_camera.h>
#include <o3d3xx_framegrabber.h>
#include <o3d3xx_image.h>
int main(int argc, const char **argv)
{
o3d3xx::Logging::Init();
// example configuration for the camera we will use for exemplary purpose
// we will use a double exposure imager.
std::string json =
R"(
{
"o3d3xx":
{
"Device":
{
"ActiveApplication": "1"
},
"Apps":
[
{
"TriggerMode": "1",
"Index": "1",
"Imager":
{
"ExposureTime": "5000",
"ExposureTimeList": "125;5000",
"ExposureTimeRatio": "40",
"Type":"under5m_moderate"
}
}
]
}
}
)";
// instantiate the camera and set the configuration
o3d3xx::Camera::Ptr cam = std::make_shared<o3d3xx::Camera>();
std::cout << "Setting camera configuration: " << std::endl
<< json << std::endl;
cam->FromJSON(json);
// create our image buffer to hold frame data from the camera
o3d3xx::ImageBuffer::Ptr img = std::make_shared<o3d3xx::ImageBuffer>();
// instantiate our framegrabber and be sure to explicitly tell it to
// stream back the exposure times registered to the frame data
o3d3xx::FrameGrabber::Ptr fg =
std::make_shared<o3d3xx::FrameGrabber>(
cam, o3d3xx::DEFAULT_SCHEMA_MASK|o3d3xx::EXP_TIME);
// a vector to hold the exposure times (we will just print them to the
// screen)
std::vector<std::uint32_t> exposure_times;
// a map use to modulate the `ExposureTime` and `ExposureTimeRatio`
// on-the-fly. We seed it with data consistent with our config above
std::unordered_map<std::string, std::string> params =
{
{"imager_001/ExposureTime", "5000"},
{"imager_001/ExposureTimeRatio", "40"}
};
// create a session with the camera so we can modulate the exposure times
cam->RequestSession();
// set our session timeout --
//
// NOTE: I'm going to do nothing with this here. However, in a *real*
// application, you will have to send `Heartbeats` at least every `hb_secs`
// seconds to the camera. The best technique for doing that is left as an
// exercise for the reader.
int hb_secs = cam->Heartbeat(300);
// now we start looping over the image data, every 20 frames, we will
// change the exposure times, after 100 frames we will exit.
int i = 0;
while (true)
{
if (! fg->WaitForFrame(img.get(), 1000))
{
std::cerr << "Timeout waiting for camera!" << std::endl;
continue;
}
// get the exposure times registered to the frame data
exposure_times = img->ExposureTimes();
// depending on your imager config, you can have up to 3 exposure
// times. I'll print all three for exemplary purposes, but we know there
// are only two valid ones based on our double exposure imager
// configuration from above. We expect the third to be 0.
std::cout << "Exposure Time 0: " << exposure_times.at(0)
<< std::endl;
std::cout << "Exposure Time 1: " << exposure_times.at(1)
<< std::endl;
std::cout << "Exposure Time 2: " << exposure_times.at(2)
<< std::endl;
std::cout << "---" << std::endl;
i++;
if (i == 100)
{
break;
}
if (i % 20 == 0)
{
std::cout << "Setting long exposure time to: ";
if (exposure_times.at(1) == 5000)
{
std::cout << 10000 << std::endl;
params["ExposureTime"] = "10000";
}
else
{
std::cout << 5000 << std::endl;
params["ExposureTime"] = "5000";
}
cam->SetTemporaryApplicationParameters(params);
}
}
//
// In a long-running program, you will need to take care to
// clean up your session if necessary. Here we don't worry about it because
// the camera dtor will do that for us.
//
std::cout << "et voila." << std::endl;
return 0;
}