IRSOL
C++ code implementing socket server for interacting with Baumer camera.
main.cpp
Go to the documentation of this file.
1
13
14#include "irsol/irsol.hpp"
15
16#include <chrono>
17#include <unordered_map>
18
21const std::string
23{
24#ifndef PROGRAM_NAME
25#define PROGRAM_NAME "loading-images-demo"
26#endif
27 return PROGRAM_NAME;
28}
29
31struct Params
32{
34 spdlog::level::level_enum logLevel{spdlog::level::info};
35
37 uint64_t numFrames{10};
38
40 double fps = -1;
41};
42
54getParams(int argc, char** argv)
55{
56 args::ArgumentParser parser(getProgramName());
57 args::HelpFlag help(parser, "help", "Display this help menu", {'h', "help"});
58
59 args::MapFlag<std::string, spdlog::level::level_enum> logLevelArg(
60 parser, "level", "Log level", {'l', "log-level"}, irsol::levelNameToLevelMap);
61
62 args::ValueFlag<uint64_t> numFramesArg(
63 parser, "#frames", "Number of frames to iterate over", {'n', "frames-count"});
64
65 args::ValueFlag<double> fpsArg(parser, "fps", "Desired FPS for capturing image", {'f', "fps"});
66
67 try {
68 parser.ParseCLI(argc, argv);
69 } catch(args::Help) {
70 std::cerr << parser.Help() << std::endl;
71 std::exit(0);
72 } catch(args::ParseError e) {
73 std::cerr << "Error parsing command-line arguments:\n" << e.what() << "\n\n" << parser.Help();
74 std::exit(1);
75 }
76
77 Params params{};
78 if(logLevelArg) {
79 params.logLevel = args::get(logLevelArg);
80 }
81 if(numFramesArg) {
82 params.numFrames = args::get(numFramesArg);
83 }
84 if(fpsArg) {
85 params.fps = args::get(fpsArg);
86 }
87 return params;
88}
89
94void
96{
97 auto image = cam.captureImage();
98
99 if(image.IsEmpty()) {
100 IRSOL_LOG_WARN("Image is empty.");
101 }
102
103 IRSOL_LOG_INFO("{}x{}, {} bytes", image.GetHeight(), image.GetWidth(), image.GetSize());
104}
105
112int
113main(int argc, char** argv)
114{
115 // Parse command-line parameters
116 auto params = getParams(argc, argv);
117
118 // Construct log file path based on program name
119 std::string logPath = "logs/" + getProgramName() + ".log";
120 irsol::initLogging(logPath.c_str(), params.logLevel);
121
122 // Enable custom assertion handler
124
125 // Choose camera resolution based on build type
126#ifdef DEBUG
127 auto cameraInterface = irsol::camera::Interface::FullResolution();
128#else
129 auto cameraInterface = irsol::camera::Interface::HalfResolution();
130#endif
131
132 // Ensure the camera is connected before continuing
133 IRSOL_ASSERT_FATAL(cameraInterface.isConnected(), "Camera is not connected");
134
135 // Log camera metadata for debugging purposes
136 IRSOL_LOG_INFO("\n{}", cameraInterface.cameraInfoAsString());
137 IRSOL_LOG_INFO("\n{}", cameraInterface.cameraStatusAsString());
138
139 // Start a monitoring thread to log camera status
140 irsol::camera::StatusMonitor monitor(cameraInterface, std::chrono::milliseconds(500));
141 monitor.start();
142
143 // Capture and log a sequence of frames
144 auto lastTick = irsol::types::clock_t::now();
145 const auto desiredDt =
146 params.fps > 0.0 ? std::chrono::microseconds(static_cast<uint64_t>(1000000.0 / params.fps))
147 : std::chrono::microseconds(0);
148 for(size_t i = 0; i < params.numFrames; ++i) {
149 const auto t0 = irsol::types::clock_t::now();
150 runCapture(cameraInterface);
151 const auto nextTick = lastTick + desiredDt;
152 lastTick = nextTick;
153 std::this_thread::sleep_until(nextTick);
154 const auto loopDuration = irsol::types::clock_t::now() - t0;
155 const auto measuredFps =
156 1000000.0 / std::chrono::duration_cast<std::chrono::microseconds>(loopDuration).count();
157 IRSOL_LOG_INFO("Desired FPS: {:.4f}, measured FPS: {:.4f}", params.fps, measuredFps);
158 }
159
160 return 0;
161}
High-level wrapper around the NeoAPI camera for synchronized access.
Definition interface.hpp:61
image_t captureImage(std::optional< irsol::types::duration_t > timeout=std::nullopt)
Capture a single image from the camera.
static Interface HalfResolution()
Factory method to create a camera interface using half sensor resolution.
Definition interface.cpp:63
static Interface FullResolution()
Factory method to create a camera interface using full sensor resolution.
Definition interface.cpp:49
Class that offers an easy way to run a background monitorin thread that periodically inspects and rep...
Definition monitor.hpp:44
void start()
Starts the monitoring thread. If the monitor is already running, this call has no effect.
Definition monitor.cpp:42
const std::string getProgramName()
Returns the program name, typically used for logging. If PROGRAM_NAME is not defined at compile time,...
Definition main.cpp:12
#define PROGRAM_NAME
int main()
Definition main.cpp:90
Params getParams(int argc, char **argv)
Parses command-line arguments using args library.
Definition main.cpp:54
void runCapture(irsol::camera::Interface &cam)
Captures a single image using the provided camera interface, logs metadata and performance stats.
Definition main.cpp:95
void initAssertHandler()
Initializes the assertion handler system.
Definition assert.cpp:14
#define IRSOL_ASSERT_FATAL
Fatal-level assertion macro.
Definition assert.hpp:135
#define IRSOL_LOG_INFO(...)
Logs an info-level message using the default logger.
Definition logging.hpp:92
#define IRSOL_LOG_WARN(...)
Logs a warning-level message using the default logger.
Definition logging.hpp:93
void initLogging(const char *fileSinkFilename="logs/irsol.log", std::optional< spdlog::level::level_enum > minLogLevel=std::nullopt)
Initializes the irsol logging system.
Definition logging.cpp:108
const std::unordered_map< std::string, spdlog::level::level_enum > levelNameToLevelMap
Runtime map from string names to spdlog log levels.
Definition logging.hpp:254
Command-line parameters for the program.
double fps
FPS at which to capture frames from the camera (negative means as fast as you can)
uint64_t numFrames
Number of frames to capture from the camera (default: 10)
spdlog::level::level_enum logLevel
Logging verbosity level (default: info)