IRSOL
C++ code implementing socket server for interacting with Baumer camera.
irsol::camera::Interface Class Reference

High-level wrapper around the NeoAPI camera for synchronized access. More...

#include <interface.hpp>

Public Types

using image_t = NeoAPI::Image
 Alias for the image type returned by the NeoAPI.
 
using camera_param_t = std::variant< bool, int, int64_t, double, std::string, const char * >
 Union of all supported types for camera parameters.
 

Public Member Functions

 Interface (NeoAPI::Cam cam=irsol::utils::loadDefaultCamera())
 Constructs the Interface by loading the default camera.
 
 Interface (Interface &&other)
 Move constructor.
 
Interfaceoperator= (Interface &&other)
 Move assignment operator.
 
std::string cameraInfoAsString () const
 Get human-readable camera information.
 
std::string cameraStatusAsString () const
 Get current camera status.
 
NeoAPI::Cam & getNeoCam ()
 Access the underlying NeoAPI camera instance.
 
bool isConnected () const
 Check if the camera is currently connected.
 
void resetSensorArea ()
 Reset the sensor area to the full sensor dimensions.
 
irsol::types::duration_t getExposure () const
 Get the current exposure time from the camera.
 
irsol::types::duration_t setExposure (irsol::types::duration_t exposure)
 Set the exposure time of the camera.
 
template<typename T , std::enable_if_t< std::is_integral_v< T >||std::is_floating_point_v< T >||std::is_same_v< std::string, T >, int > = 0>
getParam (const std::string &param) const
 Retrieve a camera parameter of arbitrary type T.
 
std::string getParam (const std::string &param) const
 Retrieve a camera parameter and convert it to string.
 
template<typename T , std::enable_if_t< std::is_integral_v< std::decay_t< T > >||std::is_floating_point_v< std::decay_t< T > >||std::is_same_v< std::decay_t< T >, std::string >, int > = 0>
setParam (const std::string &param, T value)
 Set a camera parameter of arbitrary type T.
 
template<typename T , std::enable_if_t< std::is_same_v< std::decay_t< T >, const char * >, int > = 0>
std::string setParam (const std::string &param, T value)
 Specialization for setting const char* values as strings.
 
void setMultiParam (const std::unordered_map< std::string, camera_param_t > &params)
 Set multiple parameters in one call.
 
void trigger (const std::string &param)
 Trigger a camera feature (e.g., software trigger).
 
image_t captureImage (std::optional< irsol::types::duration_t > timeout=std::nullopt)
 Capture a single image from the camera.
 

Static Public Member Functions

static Interface FullResolution ()
 Factory method to create a camera interface using full sensor resolution.
 
static Interface HalfResolution ()
 Factory method to create a camera interface using half sensor resolution.
 

Static Public Attributes

static constexpr irsol::types::duration_t DEFAULT_EXPOSURE_TIME = std::chrono::milliseconds(2)
 Default exposure time (2 milliseconds) used to initialize the camera.
 

Private Member Functions

template<typename T , std::enable_if_t< std::is_integral_v< std::decay_t< T > >||std::is_floating_point_v< std::decay_t< T > >||std::is_same_v< std::decay_t< T >, std::string >||std::is_same_v< std::decay_t< T >, const char * >, int > = 0>
void setParamNonThreadSafe (const std::string &param, T value)
 Internal, non-thread-safe parameter setter used by setParam.
 

Private Attributes

std::mutex m_camMutex
 Mutex to protect access to camera parameters and image acquisition.
 
NeoAPI::Cam m_cam
 Internal camera instance from NeoAPI.
 
irsol::types::duration_t m_CachedExposureTime
 

Detailed Description

High-level wrapper around the NeoAPI camera for synchronized access.

The Interface class provides thread-safe control over camera acquisition and configuration via NeoAPI. It abstracts common camera operations such as setting exposure, retrieving and setting camera parameters, capturing images, and managing sensor state.

Note
The underlying NeoAPI camera must be usable with a pixel format Mono12. Failure to run in this modality will lead to the Interface constructor to raise a fatal assertion.

Definition at line 60 of file interface.hpp.

Member Typedef Documentation

◆ camera_param_t

using irsol::camera::Interface::camera_param_t = std::variant<bool, int, int64_t, double, std::string, const char*>

Union of all supported types for camera parameters.

Definition at line 67 of file interface.hpp.

◆ image_t

using irsol::camera::Interface::image_t = NeoAPI::Image

Alias for the image type returned by the NeoAPI.

Definition at line 64 of file interface.hpp.

Constructor & Destructor Documentation

◆ Interface() [1/2]

irsol::camera::Interface::Interface ( NeoAPI::Cam  cam = irsol::utils::loadDefaultCamera())

Constructs the Interface by loading the default camera.

Initializes the camera by calling irsol::utils::loadDefaultCamera(), which automatically finds and opens the default device based on serial number.

Parameters
camOptional camera handle. Defaults to the result of irsol::utils::loadDefaultCamera().
Exceptions
std::runtime_errorif camera initialization fails.

Definition at line 13 of file interface.cpp.

13 : m_cam(cam)
14{
15
16 // Here we configure the camera settings so that we can trigger it on-demand via software
17 // triggers.
18 IRSOL_LOG_INFO("Configuring camera for manual trigger via software events");
19 setMultiParam({{"TriggerMode", {"On"}}, // Enable software trigger
20 {"AcquisitionMode", {"SingleFrame"}}, // Take a single frame, only when triggered
21 {"TriggerSource", {"Software"}}});
22
23 // Here we set the exposure parameters, so that they can be controlled via software
24 IRSOL_LOG_INFO("Configuring camera for manual exposure");
26 {"ExposureAuto", {"Off"}}, // Disable automatic exposure
27 {"ExposureMode", {"Timed"}}, // Exposure mode: timed
28 });
30 // Store the current exposure of the camera
32
33 // Here we configure the default bit-depth
34 IRSOL_LOG_INFO("Setting bit-depth to 12bits/pixel");
35 auto setPixelFormat = setParam("PixelFormat", "Mono12");
37 setPixelFormat == "Mono12", "Pixel format is not 'Mono12', but is %s", setPixelFormat.c_str());
38}
T setParam(const std::string &param, T value)
Set a camera parameter of arbitrary type T.
irsol::types::duration_t setExposure(irsol::types::duration_t exposure)
Set the exposure time of the camera.
static constexpr irsol::types::duration_t DEFAULT_EXPOSURE_TIME
Default exposure time (2 milliseconds) used to initialize the camera.
Definition interface.hpp:70
void setMultiParam(const std::unordered_map< std::string, camera_param_t > &params)
Set multiple parameters in one call.
NeoAPI::Cam m_cam
Internal camera instance from NeoAPI.
irsol::types::duration_t getExposure() const
Get the current exposure time from the camera.
irsol::types::duration_t m_CachedExposureTime
#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

◆ Interface() [2/2]

irsol::camera::Interface::Interface ( Interface &&  other)

Move constructor.

Definition at line 40 of file interface.cpp.

40: m_cam(other.m_cam) {}

Member Function Documentation

◆ cameraInfoAsString()

std::string irsol::camera::Interface::cameraInfoAsString ( ) const

Get human-readable camera information.

Collects fields from NeoAPI::CamInfo and formats them into a readable tabular string.

Returns
Formatted string with camera model, serial, etc.

Definition at line 79 of file interface.cpp.

80{
81 auto info = m_cam.GetInfo();
82 const auto model = info.GetModelName();
83 const auto camId = info.GetId();
84 const auto serial = info.GetSerialNumber();
85 const auto tlType = info.GetTLType();
86 const auto vendor = info.GetVendorName();
87 const auto usb3VisionGuid = info.GetUSB3VisionGUID();
88 const auto usbPortId = info.GetUSBPortID();
89 const auto gevIpAddress = info.GetGevIpAddress();
90 const auto gevSubnetMask = info.GetGevSubnetMask();
91 const auto gevGateway = info.GetGevGateway();
92 const auto gevMacAddress = info.GetGevMACAddress();
93
94 tabulate::Table camInfo;
95 camInfo.add_row({"Name", "Value"});
96 camInfo.add_row({"Camera Model Name", model.c_str()});
97 camInfo.add_row({"Camera ID", camId.c_str()});
98 camInfo.add_row({"Camera Serial Number", serial.c_str()});
99 camInfo.add_row({"Camera Transport Layer Type", tlType.c_str()});
100 camInfo.add_row({"Camera Vendor Name", vendor.c_str()});
101 camInfo.add_row({"Camera USB3 Vision GUID", usb3VisionGuid.c_str()});
102 camInfo.add_row({"Camera USB Port ID", usbPortId.c_str()});
103 camInfo.add_row({"Camera GEV IP Address", gevIpAddress.c_str()});
104 camInfo.add_row({"Camera GEV Subnet Mask", gevSubnetMask.c_str()});
105 camInfo.add_row({"Camera GEV Gateway", gevGateway.c_str()});
106 camInfo.add_row({"Camera GEV MAC Address", gevMacAddress.c_str()});
107 camInfo.add_row({"Is connectable", info.IsConnectable() ? "true" : "false"});
108
109 camInfo.column(0).format().font_align(tabulate::FontAlign::right);
110 return camInfo.str();
111}

◆ cameraStatusAsString()

std::string irsol::camera::Interface::cameraStatusAsString ( ) const

Get current camera status.

Reads camera state such as resolution, exposure, and other operational flags and returns a descriptive tabular string.

Returns
Formatted string describing current state of the camera.

Definition at line 114 of file interface.cpp.

115{
116 static const char* const FEATURE_NAMES[] = {"AcquisitionMode",
117 "BinningHorizontalMode",
118 "BinningHorizontal",
119 "BinningVerticalMode",
120 "BinningVertical",
121 "DeviceTemperatureStatus",
122 "DeviceTemperature",
123 "ExposureAuto",
124 "ExposureMode",
125 "ExposureTime",
126 "FrameCounter",
127 "Height",
128 "HeightMax",
129 "OffsetX",
130 "OffsetY",
131 "PayloadSize",
132 "PixelFormat",
133 "ReadOutTime",
134 "ReverseX",
135 "ReverseY",
136 "TriggerMode",
137 "TriggerOverlap",
138 "Width",
139 "WidthMax"};
140
141 tabulate::Table featureInfo;
142 featureInfo.add_row({"Feature", "Value"});
143
144 for(const auto featureName : FEATURE_NAMES) {
145 std::string featureValue = getParam(featureName);
146 featureInfo.add_row({featureName, featureValue});
147 }
148 featureInfo.column(0).format().font_align(tabulate::FontAlign::right);
149 return featureInfo.str();
150}
T getParam(const std::string &param) const
Retrieve a camera parameter of arbitrary type T.

◆ captureImage()

Interface::image_t irsol::camera::Interface::captureImage ( std::optional< irsol::types::duration_t timeout = std::nullopt)

Capture a single image from the camera.

Waits up to a specified timeout (or the cached exposure time if not provided as argument). Thread-safe.

Parameters
timeoutOptional duration to wait for a frame.
Returns
Captured image.
Exceptions
std::runtime_erroron timeout or camera error.

Definition at line 232 of file interface.cpp.

233{
234 std::scoped_lock<std::mutex> lock(m_camMutex);
235
236 // Send software trigger to get an image
237 trigger("AcquisitionStart");
238 trigger("TriggerSoftware");
239 // Wait for image, either using the current cached exposure time with a small buffer
240 // or using the user-provided timeout
241 irsol::types::duration_t actualTimeout = m_CachedExposureTime + std::chrono::milliseconds(200);
242 if(timeout.has_value()) {
244 "User provided a custom timeout of capturing camera of {}",
246 actualTimeout = *timeout;
247 } else {
249 "Using exposure from camera {} with buffer", irsol::utils::durationToString(actualTimeout));
250 }
251
252 uint32_t timeoutMs = static_cast<uint32_t>(
253 std::chrono::duration_cast<std::chrono::milliseconds>(actualTimeout).count());
254 auto image = m_cam.GetImage(timeoutMs);
255 if(image.IsEmpty() || image.GetSize() == 0) {
256 IRSOL_LOG_WARN("Timeout or empty image received.");
257 }
258 trigger("AcquisitionStop");
259 return image;
260}
std::mutex m_camMutex
Mutex to protect access to camera parameters and image acquisition.
void trigger(const std::string &param)
Trigger a camera feature (e.g., software trigger).
#define IRSOL_LOG_WARN(...)
Logs a warning-level message using the default logger.
Definition logging.hpp:93
#define IRSOL_LOG_DEBUG(...)
Logs a debug-level message using the default logger.
Definition logging.hpp:91
clock_t::duration duration_t
Alias for a duration of time as defined by clock_t.
Definition types.hpp:128
std::string durationToString(irsol::types::duration_t dr)
Converts a duration to a human-readable string.
Definition utils.cpp:133

◆ FullResolution()

Interface irsol::camera::Interface::FullResolution ( )
static

Factory method to create a camera interface using full sensor resolution.

Returns
Interface instance initialized at full resolution.

Definition at line 49 of file interface.cpp.

50{
52
53 Interface interface(cam);
54 interface.setMultiParam({{"BinningVertical", {1}},
55 {"BinningVerticalMode", {"Sum"}},
56 {"BinningHorizontal", {1}},
57 {"BinningHorizontalMode", {"Sum"}}});
58 interface.resetSensorArea();
59 return interface;
60}
Interface(NeoAPI::Cam cam=irsol::utils::loadDefaultCamera())
Constructs the Interface by loading the default camera.
Definition interface.cpp:13
NeoAPI::Cam loadDefaultCamera()
Loads the default camera device.
Definition utils.cpp:196

◆ getExposure()

irsol::types::duration_t irsol::camera::Interface::getExposure ( ) const

Get the current exposure time from the camera.

Returns
Current exposure duration.

Definition at line 170 of file interface.cpp.

171{
172 auto exposureInMicroSeconds = getParam<int64_t>("ExposureTime");
173 return std::chrono::microseconds(exposureInMicroSeconds);
174}

◆ getNeoCam()

NeoAPI::Cam & irsol::camera::Interface::getNeoCam ( )

Access the underlying NeoAPI camera instance.

Returns
Reference to the underlying NeoAPI::Cam object.

Definition at line 153 of file interface.cpp.

154{
155 return m_cam;
156}

◆ getParam() [1/2]

template<typename T , std::enable_if_t< std::is_integral_v< T >||std::is_floating_point_v< T >||std::is_same_v< std::string, T >, int > = 0>
T irsol::camera::Interface::getParam ( const std::string &  param) const

Retrieve a camera parameter of arbitrary type T.

Uses the NeoAPI API to retrieve the value and cast to T. Supported types:

  • std::string
  • bool
  • integral types (e.g., int, int64_t)
  • floating-point types (e.g., double)

Thread-safe: locks internal mutex.

Template Parameters
TDesired return type.
Parameters
paramName of the camera parameter.
Returns
Value of type T or a default on read error.

◆ getParam() [2/2]

std::string irsol::camera::Interface::getParam ( const std::string &  param) const

Retrieve a camera parameter and convert it to string.

Thread-safe: locks internal mutex.

Parameters
paramName of the camera parameter.
Returns
Value as string or "Unknown" on error.

Definition at line 189 of file interface.cpp.

190{
191 IRSOL_LOG_DEBUG("Getting parameter '{}'", param);
192 try {
193 NeoAPI::NeoString neoParam(param.c_str());
194 auto feature = m_cam.GetFeature(neoParam);
195 return NeoAPI::NeoString(feature).c_str();
196 } catch(const std::exception& e) {
197 IRSOL_LOG_ERROR("Failed to get parameter '{}': {}", param, e.what());
198 return "Unknown";
199 }
200}
#define IRSOL_LOG_ERROR(...)
Logs an error-level message using the default logger.
Definition logging.hpp:94

◆ HalfResolution()

Interface irsol::camera::Interface::HalfResolution ( )
static

Factory method to create a camera interface using half sensor resolution.

Uses hardware binning to reduce resolution by averaging pixels.

Returns
Interface instance initialized at half resolution.

Definition at line 63 of file interface.cpp.

64{
66
67 Interface interface(cam);
68 // For 'HalfResolution' we bin in both vertical and horizontal direction.
69 interface.setMultiParam({{"BinningVertical", {2}},
70 {"BinningVerticalMode", {"Average"}},
71 {"BinningHorizontal", {2}},
72 {"BinningHorizontalMode", {"Average"}}});
73 interface.resetSensorArea();
74
75 return interface;
76}

◆ isConnected()

bool irsol::camera::Interface::isConnected ( ) const
inline

Check if the camera is currently connected.

Uses NeoAPI::Cam::IsConnected() internally.

Returns
true if camera is connected; false otherwise.

Definition at line 137 of file interface.hpp.

138 {
139 return m_cam.IsConnected();
140 }

◆ operator=()

Interface & irsol::camera::Interface::operator= ( Interface &&  other)

Move assignment operator.

Definition at line 42 of file interface.cpp.

43{
44 m_cam = other.m_cam;
45 return *this;
46}

◆ resetSensorArea()

void irsol::camera::Interface::resetSensorArea ( )

Reset the sensor area to the full sensor dimensions.

Useful if a previous program limited the sensor region and full dimensions need to be restored.

Definition at line 159 of file interface.cpp.

160{
161 IRSOL_LOG_INFO("Resetting sensor area");
162 int maxWidth = getParam<int>("WidthMax");
163 int maxHeight = getParam<int>("HeightMax");
164
166 {{"Width", {maxWidth}}, {"Height", {maxHeight}}, {"OffsetX", {0}}, {"OffsetY", {0}}});
167}

◆ setExposure()

irsol::types::duration_t irsol::camera::Interface::setExposure ( irsol::types::duration_t  exposure)

Set the exposure time of the camera.

Parameters
exposureNew exposure duration.
Returns
The actual exposure set on the camera (may differ slightly).

Definition at line 177 of file interface.cpp.

178{
179 IRSOL_ASSERT_ERROR(exposure.count() > 0, "Cannot set non-positive exposure");
180 auto exposureInMicroseconds =
181 std::chrono::duration_cast<std::chrono::microseconds>(exposure).count();
182 auto setExposureInMicroseconds =
183 setParam("ExposureTime", static_cast<int64_t>(exposureInMicroseconds));
184 m_CachedExposureTime = std::chrono::microseconds(setExposureInMicroseconds);
186}
#define IRSOL_ASSERT_ERROR
Error-level assertion macro.
Definition assert.hpp:134

◆ setMultiParam()

void irsol::camera::Interface::setMultiParam ( const std::unordered_map< std::string, camera_param_t > &  params)

Set multiple parameters in one call.

Iterates through a map of key-value pairs and sets each camera parameter accordingly. Internally dispatches based on type.

Parameters
paramsMap of parameter names and values.
See also
Interface::setParam()

Definition at line 203 of file interface.cpp.

204{
205 IRSOL_LOG_DEBUG("Setting multiple parameters");
206 std::scoped_lock<std::mutex> lock(m_camMutex);
207 for(const auto& [param, value] : params) {
208 std::visit(
209 [&param, this](auto&& arg) {
210 using U = std::decay_t<decltype(arg)>;
211 IRSOL_LOG_TRACE("Setting parameter '{}' to value '{}'", param, arg);
212 setParamNonThreadSafe<U>(param, arg);
213 },
214 value);
215 }
216}
#define IRSOL_LOG_TRACE(...)
Logs a trace-level message using the default logger.
Definition logging.hpp:90
auto value

◆ setParam() [1/2]

template<typename T , std::enable_if_t< std::is_integral_v< std::decay_t< T > >||std::is_floating_point_v< std::decay_t< T > >||std::is_same_v< std::decay_t< T >, std::string >, int > = 0>
T irsol::camera::Interface::setParam ( const std::string &  param,
value 
)

Set a camera parameter of arbitrary type T.

Thread-safe. Supported types:

  • std::string
  • const char*
  • bool
  • integral types (converted to int64_t)
  • floating-point types (converted to double)
Template Parameters
TValue type.
Parameters
paramParameter name.
valueValue to set.
Returns
The set value (after conversion/rounding, if any).
See also
Interface::setMultiParam()

◆ setParam() [2/2]

template<typename T , std::enable_if_t< std::is_same_v< std::decay_t< T >, const char * >, int > = 0>
std::string irsol::camera::Interface::setParam ( const std::string &  param,
value 
)

Specialization for setting const char* values as strings.

Parameters
paramParameter name.
valueC-string value.
Returns
Set value as std::string.
See also
Interface::setMultiParam()

◆ setParamNonThreadSafe()

template<typename T , std::enable_if_t< std::is_integral_v< std::decay_t< T > >||std::is_floating_point_v< std::decay_t< T > >||std::is_same_v< std::decay_t< T >, std::string >||std::is_same_v< std::decay_t< T >, const char * >, int > = 0>
void irsol::camera::Interface::setParamNonThreadSafe ( const std::string &  param,
value 
)
private

Internal, non-thread-safe parameter setter used by setParam.

Template Parameters
TParameter value type.
Parameters
paramName of the parameter.
valueValue to assign.

◆ trigger()

void irsol::camera::Interface::trigger ( const std::string &  param)

Trigger a camera feature (e.g., software trigger).

Parameters
paramName of the triggerable feature.

Definition at line 219 of file interface.cpp.

220{
221 IRSOL_LOG_TRACE("Triggering camera with parameter '{}'", param);
222 try {
223 NeoAPI::NeoString neoParam(param.c_str());
224 auto feature = m_cam.GetFeature(neoParam);
225 feature.Execute();
226 } catch(const std::exception& e) {
227 IRSOL_LOG_ERROR("Failed to trigger camera with parameter '{}': {}", param, e.what());
228 }
229}

Member Data Documentation

◆ DEFAULT_EXPOSURE_TIME

constexpr irsol::types::duration_t irsol::camera::Interface::DEFAULT_EXPOSURE_TIME = std::chrono::milliseconds(2)
staticconstexpr

Default exposure time (2 milliseconds) used to initialize the camera.

Definition at line 70 of file interface.hpp.

◆ m_CachedExposureTime

irsol::types::duration_t irsol::camera::Interface::m_CachedExposureTime
private

Cached exposure value used for implicit timeout behavior in irsol::camera::Interface::captureImage().

Definition at line 270 of file interface.hpp.

◆ m_cam

NeoAPI::Cam irsol::camera::Interface::m_cam
private

Internal camera instance from NeoAPI.

Definition at line 266 of file interface.hpp.

◆ m_camMutex

std::mutex irsol::camera::Interface::m_camMutex
mutableprivate

Mutex to protect access to camera parameters and image acquisition.

Definition at line 263 of file interface.hpp.


The documentation for this class was generated from the following files: