YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
pylonCameraDriver.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2006-2022 Istituto Italiano di Tecnologia (IIT)
3 * All rights reserved.
4 *
5 * This software may be modified and distributed under the terms of the
6 * BSD-3-Clause license. See the accompanying LICENSE file for details.
7 */
8
9#ifndef PYLON_DRIVER_H
10#define PYLON_DRIVER_H
11
12#include <pylon/PylonIncludes.h>
18#include <yarp/os/Stamp.h>
19#include <yarp/sig/Matrix.h>
20#include <yarp/sig/all.h>
21
22#include <cstring>
23#include <iostream>
24#include <map>
25#include <memory>
26#include <mutex>
27#include <typeinfo>
28
53namespace
54{
55YARP_LOG_COMPONENT(PYLON_CAMERA, "yarp.device.pylonCamera")
56}
57
59{
60 private:
61 using Stamp = yarp::os::Stamp;
64
65 public:
66 pylonCameraDriver() = default;
67 ~pylonCameraDriver() override = default;
68
69 // DeviceDriver
70 bool open(yarp::os::Searchable& config) override;
71 bool close() override;
72
73 // IRgbVisualParams
74 int getRgbHeight() override;
75 int getRgbWidth() override;
77 bool getRgbResolution(int& width, int& height) override;
78 bool setRgbResolution(int width, int height) override;
79 bool getRgbFOV(double& horizontalFov, double& verticalFov) override;
80 bool setRgbFOV(double horizontalFov, double verticalFov) override;
81 bool getRgbMirroring(bool& mirror) override;
82 bool setRgbMirroring(bool mirror) override;
83 bool getRgbIntrinsicParam(Property& intrinsic) override;
84
85 // IFrameGrabberControls
86 bool getCameraDescription(CameraDescriptor* camera) override;
87 bool hasFeature(int feature, bool* hasFeature) override;
88 bool setFeature(int feature, double value) override;
89 bool getFeature(int feature, double* value) override;
90 bool setFeature(int feature, double value1, double value2) override;
91 bool getFeature(int feature, double* value1, double* value2) override;
92 bool hasOnOff(int feature, bool* HasOnOff) override;
93 bool setActive(int feature, bool onoff) override;
94 bool getActive(int feature, bool* isActive) override;
95 bool hasAuto(int feature, bool* hasAuto) override;
96 bool hasManual(int feature, bool* hasManual) override;
97 bool hasOnePush(int feature, bool* hasOnePush) override;
98 bool setMode(int feature, FeatureMode mode) override;
99 bool getMode(int feature, FeatureMode* mode) override;
100 bool setOnePush(int feature) override;
101
102 // IFrameGrabberImage
104 int height() const override;
105 int width() const override;
106
107 private:
108 // method
109 // inline bool setParams();
110 bool setFramerate(const float _fps);
111 template <class T>
112 bool setOption(const std::string& option, T value, bool isEnum = false)
113 {
114 std::lock_guard<std::mutex> guard(m_mutex);
115 // in some cases it is not used, suppressing the warning
116 YARP_UNUSED(isEnum);
117 bool ok{true};
118 auto& node_map = m_camera_ptr->GetNodeMap();
119 stopCamera();
120 try
121 {
122 yCDebug(PYLON_CAMERA) << "Setting " << option << "to" << value;
123 if constexpr (std::is_same<T, float>::value || std::is_same<T, double>::value)
124 {
125 Pylon::CFloatParameter(node_map, option.c_str()).SetValue(value);
126 }
127 else if constexpr (std::is_same<T, bool>::value)
128 {
129 Pylon::CBooleanParameter(node_map, option.c_str()).SetValue(value);
130 }
131 else if constexpr (std::is_same<T, int>::value)
132 {
133 Pylon::CIntegerParameter(node_map, option.c_str()).SetValue(value);
134 }
135 else if constexpr (std::is_same<T, const char*>::value)
136 {
137 if (isEnum)
138 {
139 Pylon::CEnumParameter(node_map, option.c_str()).SetValue(value);
140 }
141 else
142 {
143 Pylon::CStringParameter(node_map, option.c_str()).SetValue(value);
144 }
145 }
146 else
147 {
148 yCError(PYLON_CAMERA) << "Option" << option << "has a type not supported, type" << typeid(T).name();
149 startCamera();
150 return false;
151 }
152 }
153 catch (const Pylon::GenericException& e)
154 {
155 // Error handling.
156 yCError(PYLON_CAMERA) << "Camera" << m_serial_number << "cannot set" << option << "to:" << value << "error:" << e.GetDescription();
157 ok = false;
158 }
159 return startCamera() && ok;
160 }
161
162 template <class T>
163 bool getOption(const std::string& option, T& value, bool isEnum = false)
164 {
165 auto& node_map = m_camera_ptr->GetNodeMap();
166 // in some cases it is not used, suppressing the warning
167 YARP_UNUSED(isEnum);
168 try
169 {
170 if constexpr (std::is_same<T, float*>::value || std::is_same<T, double*>::value)
171 {
172 *value = Pylon::CFloatParameter(node_map, option.c_str()).GetValue();
173 yCDebug(PYLON_CAMERA) << "Getting" << option << "value:" << *value;
174 }
175 else if constexpr (std::is_same<T, bool*>::value)
176 {
177 *value = Pylon::CBooleanParameter(node_map, option.c_str()).GetValue();
178 yCDebug(PYLON_CAMERA) << "Getting" << option << "value:" << *value;
179 }
180 else if constexpr (std::is_same<T, int*>::value)
181 {
182 *value = Pylon::CIntegerParameter(node_map, option.c_str()).GetValue();
183 yCDebug(PYLON_CAMERA) << "Getting" << option << "value:" << *value;
184 }
185 else if constexpr (std::is_same<T, std::string>::value)
186 {
187 if (isEnum)
188 {
189 value = Pylon::CEnumParameter(node_map, option.c_str()).GetValue();
190 yCDebug(PYLON_CAMERA) << "Getting" << option << "value:" << value;
191 }
192 else
193 {
194 value = Pylon::CStringParameter(node_map, option.c_str()).GetValue();
195 yCDebug(PYLON_CAMERA) << "Getting" << option << "value:" << value;
196 }
197 }
198 else
199 {
200 yCError(PYLON_CAMERA) << "Option" << option << "has a type not supported, type" << typeid(T).name();
201 return false;
202 }
203 }
204 catch (const Pylon::GenericException& e)
205 {
206 // Error handling.
207 yCError(PYLON_CAMERA) << "Camera" << m_serial_number << "cannot get" << option << "error:" << e.GetDescription();
208 return false;
209 }
210 return true;
211 }
212
213 bool startCamera();
214 bool stopCamera();
215
216 mutable std::mutex m_mutex;
217
218 yarp::os::Stamp m_rgb_stamp;
219 mutable std::string m_lastError{""};
220 bool m_verbose{false};
221 bool m_initialized{false};
222 float m_fps{30.0};
223 double m_rotation{0.0}; // degrees
224 uint32_t m_width{640};
225 uint32_t m_height{480};
226 Pylon::String_t m_serial_number{""};
227 std::unique_ptr<Pylon::CInstantCamera> m_camera_ptr;
228 bool m_rotationWithCrop{false};
229};
230#endif // PYLON_DRIVER_H
contains the definition of a Matrix type
int getRgbWidth() override
Return the width of each frame.
bool getActive(int feature, bool *isActive) override
Get the current status of the feature, on or off.
bool getRgbResolution(int &width, int &height) override
Get the resolution of the rgb image from the camera.
~pylonCameraDriver() override=default
bool setActive(int feature, bool onoff) override
Set the requested feature on or off.
bool setRgbMirroring(bool mirror) override
Set the mirroring setting of the sensor.
bool hasAuto(int feature, bool *hasAuto) override
Check if the requested feature has the 'auto' mode.
bool setOnePush(int feature) override
Set the requested feature to a value (saturation, brightness ... )
bool getRgbMirroring(bool &mirror) override
Get the mirroring setting of the sensor.
bool hasFeature(int feature, bool *hasFeature) override
Check if camera has the requested feature (saturation, brightness ... )
bool getRgbIntrinsicParam(Property &intrinsic) override
Get the intrinsic parameters of the rgb camera.
bool hasOnePush(int feature, bool *hasOnePush) override
Check if the requested feature has the 'onePush' mode.
bool close() override
Close the DeviceDriver.
bool getRgbSupportedConfigurations(yarp::sig::VectorOf< yarp::dev::CameraConfig > &configurations) override
Get the possible configurations of the camera.
int height() const override
Return the height of each frame.
bool hasOnOff(int feature, bool *HasOnOff) override
Check if the camera has the ability to turn on/off the requested feature.
bool getCameraDescription(CameraDescriptor *camera) override
Get a basic description of the camera hw.
bool getMode(int feature, FeatureMode *mode) override
Get the current mode for the feature.
bool hasManual(int feature, bool *hasManual) override
Check if the requested feature has the 'manual' mode.
bool setRgbResolution(int width, int height) override
Set the resolution of the rgb image from the camera.
int width() const override
Return the width of each frame.
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
bool getFeature(int feature, double *value) override
Get the current value for the requested feature.
bool setFeature(int feature, double value) override
Set the requested feature to a value (saturation, brightness ... )
int getRgbHeight() override
Return the height of each frame.
bool setMode(int feature, FeatureMode mode) override
Set the requested mode for the feature.
bool setRgbFOV(double horizontalFov, double verticalFov) override
Set the field of view (FOV) of the rgb camera.
pylonCameraDriver()=default
bool getImage(yarp::sig::ImageOf< yarp::sig::PixelRgb > &image) override
Get an image from the frame grabber.
bool getRgbFOV(double &horizontalFov, double &verticalFov) override
Get the field of view (FOV) of the rgb camera.
Interface implemented by all device drivers.
Control interface for frame grabber devices.
An interface for retrieving intrinsic parameter from a rgb camera.
A class for storing options and configuration information.
Definition Property.h:33
A base class for nested structures that can be searched.
Definition Searchable.h:31
An abstraction for a time stamp and/or sequence number.
Definition Stamp.h:21
Image class with user control of representation details.
Definition Image.h:363
Typed image class.
Definition Image.h:605
#define yCError(component,...)
#define yCDebug(component,...)
#define YARP_LOG_COMPONENT(name,...)
#define YARP_UNUSED(var)
Definition api.h:162