YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
UltraPythonDriver.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19#include "UltraPythonDriver.h"
20
22#include <yarp/dev/FrameGrabberInterfaces.h>
23#include <yarp/os/LogStream.h>
24#include <yarp/os/Semaphore.h>
25#include <yarp/os/Stamp.h>
26
27#include "InterfaceForCApi.h"
28#include "common.h"
29
30using namespace yarp::os;
31using namespace yarp::dev;
32
33YARP_LOG_COMPONENT(ULTRAPYTHON, "yarp.device.UltraPython")
34
35UltraPythonDriver::UltraPythonDriver() : pythonCameraHelper_(nullptr)
36{
37 yCTrace(ULTRAPYTHON) << "---------------------------------------------";
38 yCTrace(ULTRAPYTHON) << "------UltraPython device ready to start------";
39 yCTrace(ULTRAPYTHON) << "---------------------------------------------";
40
41 pythonCameraHelper_.setInjectedProcess([this](const void *pythonBuffer, size_t size) { pythonPreprocess(pythonBuffer, size); });
42 pythonCameraHelper_.setInjectedUnlock([this]() { mutex.post(); });
43 pythonCameraHelper_.setInjectedLock([this]() { mutex.wait(); });
44 pythonCameraHelper_.setInjectedLog([](const std::string &toLog, Severity severity) {
45 switch (severity)
46 {
47 case Severity::error:
49 break;
50 case Severity::info:
52 break;
53 case Severity::debug:
55 break;
58 break;
59 }
60 });
61}
62
67
69{
71 prop.fromString(config.toString());
72
73 yCTrace(ULTRAPYTHON) << "input params are " << config.toString();
74
75 if (!fromConfig(config))
76 {
77 return false;
78 }
79
80 if (!pythonCameraHelper_.openAll())
81 return false;
82 configured_ = true;
84 return true;
85}
86
88{
90
91 return pythonCameraHelper_.closeAll();
92}
93
95{
96 return pythonCameraHelper_.currentWidth_;
97}
98
100{
101 return pythonCameraHelper_.currentHeight_;
102}
103
109
111{
112 if (!configured_)
113 {
114 yCError(ULTRAPYTHON) << "unable to get the buffer, device uninitialized";
115 return false;
116 }
117
118 if ((image.width() != width()) || (image.height() != height()))
119 {
120 image.resize(width(), height());
121 }
122
123 mutex.wait();
124 if (pythonCameraHelper_.step(image.getRawImage()))
125 {
126 }
127 else
128 {
129 yCError(ULTRAPYTHON) << "Failed acquiring new frame";
130 }
131 mutex.post();
132 return true;
133}
134
136{
137 camera->busType = BUS_UNKNOWN;
138 camera->deviceDescription = "UltraPython camera";
139 return true;
140}
141
143{
144 if (!FeatureHelper::exists(feature))
145 {
146 yCError(ULTRAPYTHON) << "This feature is not supported:" << feature;
147 return false;
148 }
149 return true;
150}
151
153{
154 if (!FeatureHelper::existsForWrite(feature))
155 {
156 yCError(ULTRAPYTHON) << "This feature is not for write:" << feature;
157 return false;
158 }
159
160 bool absolute{false};
161 if (FeatureHelper::isAbsolute(feature))
162 {
163 absolute = true;
164 }
165 bool ret = pythonCameraHelper_.setControl(remapControlYARPtoXilinx(feature), value, absolute);
166 return ret;
167}
168
170{
171 if (!FeatureHelper::existsForRead(feature))
172 {
173 yCError(ULTRAPYTHON) << "This feature is not for read:" << feature;
174 return false;
175 }
176
177 bool absolute{false};
178 if (FeatureHelper::isAbsolute(feature))
179 {
180 absolute = true;
181 }
182
183 double tmp = 0.0;
184 tmp = pythonCameraHelper_.getControl(remapControlYARPtoXilinx(feature), absolute);
185 if (tmp == -1)
186 {
187 *value = 0;
188 return false;
189 }
190
191 *value = tmp;
192
193 yCDebug(ULTRAPYTHON) << "getControl feature:" << feature<<" value:"<<*value;
194 return true;
195}
196
198{
199 yCError(ULTRAPYTHON) << "getFeature double param - not supported";
200 return false;
201}
202
204{
205 yCError(ULTRAPYTHON) << "setFeature double param - not supported";
206 return false;
207}
208
210{
211 *_hasOnOff = false;
212 return true;
213}
214
216{
217 *_isActive = true;
218 return true;
219}
220
222{
223 *_hasAuto = false;
224 return true;
225}
226
228{
229 *_hasManual = false;
230 if (FeatureHelper::exists(feature))
231 *_hasManual = true;
232 return true;
233}
234
236{
237 yCError(ULTRAPYTHON) << "Feature " << feature << " does not support auto mode";
238 return false;
239}
240
242{
243 *mode = MODE_MANUAL;
244 return true;
245}
246
248{
249 yCError(ULTRAPYTHON) << "setActive - not supported";
250 return false;
251}
252
254{
255 yCError(ULTRAPYTHON) << "hasOnePush - not supported";
256 *_hasOnePush = false;
257 return false;
258}
259
261{
262 yCError(ULTRAPYTHON) << "setOnePush - not supported";
263 return false;
264}
265
266bool UltraPythonDriver::fromConfig(yarp::os::Searchable &config)
267{
268 if (config.check("verbose"))
269 {
270 // TODO
271 }
272
273 int period = 28;
274 if (config.check("period"))
275 {
276 auto tmp = config.find("period");
277 period = tmp.asInt32();
278 yCInfo(ULTRAPYTHON) << "Period used:" << period;
279 pythonCameraHelper_.setStepPeriod(period); // For exposition setting check
280 }
281
282 if (config.check("honorfps"))
283 {
284 bool honor;
285 auto tmp = config.find("honorfps");
286 honor = tmp.asBool();
287 yCInfo(ULTRAPYTHON) << "HonorFps:" << honor;
288 pythonCameraHelper_.setHonorFps(honor);
289 }
290
291 if (!config.check("subsampling"))
292 {
293 yCDebug(ULTRAPYTHON) << "Python cam full-sampling ";
294 pythonCameraHelper_.setSubsamplingProperty(false);
295 if (1000.0 / (double)period > UltraPythonCameraHelper::hiresFrameRate_)
296 {
297 yCWarning(ULTRAPYTHON) << "FPS exceed suggested FPS for hires:" << 1000.0 / (double)period << " suggested:" << UltraPythonCameraHelper::hiresFrameRate_;
298 }
299 }
300 else
301 {
302 yCDebug(ULTRAPYTHON) << "Python cam sub-sampling ";
303 pythonCameraHelper_.setSubsamplingProperty(true);
304 if (1000.0 / (double)period > UltraPythonCameraHelper::lowresFrameRate_)
305 {
306 yCWarning(ULTRAPYTHON) << "FPS exceed suggested FPS for lowres:" << 1000.0 / (double)period << " suggested:" << UltraPythonCameraHelper::lowresFrameRate_;
307 }
308 }
309
310 if (config.check("cammodel"))
311 {
312 yCError(ULTRAPYTHON) << "cammodel - param not supported.)";
313 }
314
315 if (config.check("d"))
316 {
317 yCError(ULTRAPYTHON) << "d - param not supported.)";
318 }
319
320 if (config.check("flip"))
321 {
322 yCError(ULTRAPYTHON, "flip - param not supported.");
323 }
324
325 if (config.check("crop"))
326 {
327 yCError(ULTRAPYTHON, "crop - param not supported.");
328 }
329
330 if (config.check("dual"))
331 {
332 yCError(ULTRAPYTHON, "dual - param not supported.");
333 }
334
335 if (config.check("framerate"))
336 {
337 yCWarning(ULTRAPYTHON) << "framerate - param not supported.";
338 }
339
340 if (config.check("pixelType"))
341 {
342 yCError(ULTRAPYTHON) << "pixelType - param not supported.";
343 }
344
345 if (config.check("horizontalFov"))
346 {
347 yCError(ULTRAPYTHON) << "horizontalFov - param not supported.";
348 }
349
350 if (config.check("verticalFov"))
351 {
352 yCError(ULTRAPYTHON) << "verticalFov - param not supported.";
353 }
354 if (config.check("principalPointX"))
355 {
356 yCError(ULTRAPYTHON) << "principalPointX - param not supported.";
357 }
358 if (config.check("principalPointY"))
359 {
360 yCError(ULTRAPYTHON) << "principalPointY - param not supported.";
361 }
362 if (config.check("retificationMatrix"))
363 {
364 yCError(ULTRAPYTHON) << "retificationMatrix - param not supported.";
365 }
366 if (config.check("distortionModel"))
367 {
368 yCError(ULTRAPYTHON) << "distortionModel - param not supported.";
369 }
370
371 yCDebug(ULTRAPYTHON) << "Ultrapython with the configuration: " << pythonCameraHelper_.currentWidth_ << "x" << pythonCameraHelper_.currentHeight_;
372 return true;
373}
374
375void UltraPythonDriver::pythonPreprocess(const void *pythonbuffer, size_t size)
376{
377 // Nothing to do
378}
379
380int UltraPythonDriver::remapControlYARPtoXilinx(int feature) const
381{
382 switch (feature)
383 {
386 yCDebug(ULTRAPYTHON) << "Feature " << feature << " does not support auto mode";
387 return V4L2_CID_BRIGHTNESS;
390 yCDebug(ULTRAPYTHON) << "remap contrast";
393 case YARP_FEATURE_EXPOSURE: // shutter used
394 case YARP_FEATURE_EXPOSURE_ABSOLUTE: // shutter used
395 yCDebug(ULTRAPYTHON) << "remap exposure";
399 yCDebug(ULTRAPYTHON) << "remap gain";
400 return V4L2_CID_GAIN;
403 yCDebug(ULTRAPYTHON) << "remap RED gain";
407 yCDebug(ULTRAPYTHON) << "remap GREEN gain";
411 yCDebug(ULTRAPYTHON) << "remap BLUE gain";
413 }
414 yCDebug(ULTRAPYTHON) << "not remapped feature:" << feature;
415 return feature;
416}
@ YARP_FEATURE_BRIGHTNESS
@ YARP_FEATURE_EXPOSURE
@ YARP_FEATURE_SHUTTER
@ YARP_FEATURE_GAIN
bool ret
const yarp::os::LogComponent & ULTRAPYTHON()
const yarp::os::LogComponent & ULTRAPYTHON()
static constexpr int hiresFrameRate_
static constexpr unsigned int V4L2_CONTRAST_ULTRA_PYTHON
static constexpr unsigned int V4L2_BLUEBALANCE_ULTRA_PYTHON
bool step(unsigned char *yarpbuffer)
static constexpr int lowresFrameRate_
static constexpr unsigned int V4L2_EXPOSURE_ULTRA_PYTHON
bool setControl(uint32_t v4lCtrl, double value, bool absolute)
static constexpr unsigned int V4L2_GREENBALANCE_ULTRA_PYTHON
double getControl(uint32_t v4lCtrl, bool absolute)
static constexpr unsigned int V4L2_REDBALANCE_ULTRA_PYTHON
bool getImage(yarp::sig::ImageOf< yarp::sig::PixelRgb > &image) override
Get an image from the frame grabber.
bool getActive(int feature, bool *isActive) override
Get the current status of the feature, on or off.
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 hasAuto(int feature, bool *hasAuto) override
Check if the requested feature has the 'auto' mode.
bool hasOnOff(int feature, bool *HasOnOff) override
Check if the camera has the ability to turn on/off the requested feature.
bool setMode(int feature, FeatureMode mode) override
Set the requested mode for the feature.
yarp::os::Stamp getLastInputStamp() override
Return the time stamp relative to the last acquisition.
int height() const override
Return the height of each frame.
bool setOnePush(int feature) override
Set the requested feature to a value (saturation, brightness ... )
int width() const override
Return the width of each frame.
bool hasManual(int feature, bool *hasManual) override
Check if the requested feature has the 'manual' mode.
bool hasOnePush(int feature, bool *hasOnePush) override
Check if the requested feature has the 'onePush' mode.
bool setFeature(int feature, double value) override
Set the requested feature to a value (saturation, brightness ... )
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
bool close() override
Close the DeviceDriver.
bool setActive(int feature, bool onoff) override
Set the requested feature on or off.
bool getFeature(int feature, double *value) override
Get the current value for the requested feature.
bool hasFeature(int feature, bool *hasFeature) override
Check if camera has the requested feature (saturation, brightness ... )
A mini-server for performing network communication in the background.
A class for storing options and configuration information.
Definition Property.h:33
void fromString(const std::string &txt, bool wipe=true)
Interprets a string as a list of properties.
A base class for nested structures that can be searched.
Definition Searchable.h:31
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
virtual std::string toString() const =0
Return a standard text representation of the content of the object.
virtual Value & find(const std::string &key) const =0
Gets a value corresponding to a given keyword.
void wait()
Decrement the counter, even if we must wait to do that.
Definition Semaphore.cpp:96
void post()
Increment the counter.
An abstraction for a time stamp and/or sequence number.
Definition Stamp.h:21
Typed image class.
Definition Image.h:605
size_t width() const
Gets width of image in pixels.
Definition Image.h:171
unsigned char * getRawImage() const
Access to the internal image buffer.
Definition Image.cpp:479
void resize(size_t imgWidth, size_t imgHeight)
Reallocate an image to be of a desired size, throwing away its current contents.
Definition Image.cpp:402
size_t height() const
Gets height of image in pixels.
Definition Image.h:177
#define yCInfo(component,...)
#define yCError(component,...)
#define yCTrace(component,...)
#define yCWarning(component,...)
#define yCDebug(component,...)
#define YARP_LOG_COMPONENT(name,...)
For streams capable of holding different kinds of content, check what they actually have.
void delay(double seconds)
Wait for a certain number of seconds.
Definition Time.cpp:111
An interface to the operating system, including Port based communication.