YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
UltraPythonCameraHelper.h
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#pragma once
20
21#include <array>
22#include <cstring>
23#include <fstream>
24#include <functional>
25#include <map>
26#include <sstream>
27
28#include "InterfaceForCApi.h"
29#include "Statistics.h"
30
31struct v4l2_format;
32
34{
35 void *start;
36 size_t length;
37};
38enum class SpaceColor
39{
40 yuv,
41 rgb,
42 grgb
43};
44
45enum class Severity
46{
47 debug,
48 info,
49 warning,
50 error
51};
52
53// Helper for UltraPython functionalities through kernel modules
55{
56 public:
57 static constexpr int lowresFrameRate_{35};
58 static constexpr int hiresFrameRate_{14};
59
60 // V4l ctrl for UltraPython
61 static constexpr unsigned int V4L2_EXPOSURE_ULTRA_PYTHON{0x0098cb03}; // trg_l
62 static constexpr unsigned int V4L2_DEADTIME_ULTRA_PYTHON{0x0098cb02}; // trg_h
63 static constexpr unsigned int V4L2_EXTTRIGGER_ULTRA_PYTHON{0x0098cc03}; // ext_trigger
64 static constexpr unsigned int V4L2_REDBALANCE_ULTRA_PYTHON{0x0098c9a3}; // Red Balance
65 static constexpr unsigned int V4L2_GREENBALANCE_ULTRA_PYTHON{0x0098c9a4}; // Green Balance
66 static constexpr unsigned int V4L2_BLUEBALANCE_ULTRA_PYTHON{0x0098c9a5}; // Blue balance
67 static constexpr unsigned int V4L2_ANALOGGAIN_ULTRA_PYTHON{0x009e0903}; // Analog gain
68 static constexpr unsigned int V4L2_CONTRAST_ULTRA_PYTHON{0x0098c9a2}; // Contrast
69
70
71 private:
72 // Main properties
73 static constexpr unsigned int hiresWidth_{2560}; // HI RES width image
74 static constexpr unsigned int hiresHeight_{1024}; // HI RES height image
75 static constexpr unsigned int lowresWidth_{1280}; // LOW RES width image
76 static constexpr unsigned int lowresHeight_{512}; // LOW RES height image
77 static constexpr unsigned int hiresImageBufferSize_{hiresWidth_ * hiresHeight_ * 3}; // HI RES buffer size
78 static constexpr unsigned int lowresImageBufferSize_{lowresWidth_ * lowresHeight_ * 3}; // LOW RES buffer size
79 static constexpr unsigned int deadTime_{10};
80 static constexpr unsigned int minPermittedExposition_{15};
81 static constexpr unsigned int maxPermittedExposition_{100};
82
83 static constexpr const char *ultraPythonName = "ultrapython";
84
85 inline static constexpr const char *mediaName_ = "/dev/media0";
86
87 // Pipeline string
88 static constexpr const char *pipelineVideoName = "vcap_python output 0";
89 static constexpr const char *pipelinePythonName = "PYTHON1300";
90 static constexpr const char *pipelineTpgName = "v_tpg";
91 static constexpr const char *pipelineCscName = "v_proc_ss";
92 static constexpr const char *pipelinePacket32Name = "Packet32";
93 static constexpr const char *pipelineImgfusionName = "imgfusion";
94 static constexpr const char *pipelineRxifName = "PYTHON1300_RXIF";
95
96 // Buffers
97 static constexpr unsigned int requestBufferNumber_ = {4};
98 static constexpr unsigned int pipelineMaxLen = {16};
99
100 // Native resolution for cam
101 static constexpr unsigned int nativeWidth_{1280}; // Sensor HI RES width
102 static constexpr unsigned int nativeHeight_{1024}; // Sensor HI RES height
103
104 public:
105 // Main
106 bool openAll();
107 bool step(unsigned char *yarpbuffer); // Get current image
108 bool closeAll();
109
110 // Settings
111 void setSubsamplingProperty(bool value);
112 bool setControl(uint32_t v4lCtrl, double value,
113 bool absolute); // if absolute==false, normalized between 0-1
114 void setStepPeriod(double msec);
115 void setHonorFps(bool value);
116
117 // Getter
118 double getControl(uint32_t v4lCtrl, bool absolute); // if absolute==false, normalized between 0-1
119 bool getCropEnabledProperty() const;
120 bool getForceFormatProperty() const;
121 bool getHonorFps() const;
122 double getStepPeriod() const;
123 double getCurrentExposure() const;
124
125 // Inject function from out
126 void setInjectedProcess(std::function<void(const void *, int)> toinJect);
127 void setInjectedUnlock(std::function<void()> toinJect);
128 void setInjectedLock(std::function<void()> toinJect);
129 void setInjectedLog(std::function<void(const std::string &, Severity severity)> toinJect);
130
131 // General
132 explicit UltraPythonCameraHelper(InterfaceForCApi *interfaceCt);
138
141
142 void mapBufferFill(unsigned char *tofillWith, unsigned int index); // Test only
143
144 private:
145 InterfaceForCApi *interfaceCApi_; // Unittest purpouse c API interface
146 bool ownerCApi_{false};
147 bool openPipeline();
148 bool initDevice();
149 bool startCapturing();
150 bool setDefaultControl(); // Set some important default controls
151 bool setControl(uint32_t v4lCtrl, int fd, double value, bool absolute);
152 double getControl(uint32_t v4lCtrl, int fd, bool absolute);
153 bool setSubDevFormat(int width, int height);
154 bool setFormat();
155 bool setSubsampling();
156 bool crop(int top, int left, int w, int h, int mytry);
157 bool checkDevice(int mainSubdeviceFd);
158 int readFrame(unsigned char *yarpbuffer);
159 void processImage(const void *p, int size);
160 bool unInitDevice();
161 bool stopCapturing();
162 bool closePipeline();
163 bool initMmap();
164 bool cropCheck();
165 void log(const std::string &toBeLogged, Severity severity = Severity::debug);
166 bool checkIndex();
167 bool setGain(double value, bool absolute);
168 bool internalHasControl(uint32_t v4lCtrl) const;
169
170 // Property
171 bool subsamplingEnabledProperty_{true};
172 bool cropEnabledProperty_{false};
173 bool forceFormatProperty_{true}; // Overwrite preesistent format
174 double currentExposure_{0}; // Only for logging purpouse
175
176 // Image memory map
177 MmapBuffer mMapBuffers_[requestBufferNumber_];
178
179 // File descriptors and indexes
180 int mainSubdeviceFd_ = -1;
181 std::array<int, pipelineMaxLen> pipelineSubdeviceFd_ = {
182 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
183 };
184 int sourceSubDeviceIndex1_ = -1;
185 int sourceSubDeviceIndex2_ = -1;
186 int rxif1Index_ = -1;
187 int rxif2Index_ = -1;
188 int cscIndex_ = -1;
189 int tpgIndex_ = -1;
190 int imgfusionIndex_ = -1;
191 int packet32Index_ = -1;
192
193 double stepPeriod_{40}; // Interval for two step() call
194 bool honorfps_{false}; // Keep FPS stable
195
196 const SpaceColor spaceColor_{SpaceColor::rgb};
197
198 // Gain
199 double currentGainValue_{1};
200 const std::map<unsigned int, std::pair<unsigned int, unsigned int>> gainMap_{{1, {1, 1}}, {2, {1, 2}}, {3, {1, 3}}, {4, {1, 4}}, {5, {1, 5}}, {6, {2, 2}},
201 {7, {2, 3}}, {8, {2, 4}}, {9, {2, 5}}, {10, {2, 6}}, {11, {2, 7}}};
202
203 // Crop size
204 int cropLeft_{0};
205 int cropTop_{0};
206 int cropHeight_{0};
207 int cropWidth_{0};
208
209 // Other
210 Statistics statistics_{"frames read by YARP", 0};
211
212 // injected functionality
213 std::function<void(const void *, int)> injectedProcessImage_; // Process image external
214 std::function<void()> lock_; // Mutex injected method
215 std::function<void()> unlock_; // Mutex injected method
216 std::function<void(const std::string &, Severity)> log_; // Logging injected method
217
218 // Injected RAI mutex
219 class Locker
220 {
221 private:
222 const UltraPythonCameraHelper &parent_;
223
224 public:
225 explicit Locker(const UltraPythonCameraHelper &parent) : parent_(parent)
226 {
227 if (parent_.lock_)
228 {
229 parent_.lock_();
230 }
231 };
232 ~Locker()
233 {
234 if (parent_.unlock_)
235 {
236 parent_.unlock_();
237 }
238 };
239 };
240
241 // Injected RAI log
242 class Log
243 {
244 private:
245 const UltraPythonCameraHelper &parent_;
246 Severity severity_;
247 std::stringstream ss_;
248
249 public:
250 Log(const UltraPythonCameraHelper &parent, Severity severity) : parent_(parent), severity_(severity){};
251 ~Log()
252 {
253 if (parent_.log_)
254 {
255 parent_.log_(ss_.str(), severity_);
256 }
257 };
258
259 using streamtype = std::ostream &(std::ostream &);
260 Log &operator<<(streamtype); // std::endl only
261
262 template <typename T>
263 Log &operator<<(const T &data) // All other case
264 {
265 ss_ << data;
266 return *this;
267 };
268 };
269};
static constexpr unsigned int V4L2_ANALOGGAIN_ULTRA_PYTHON
static constexpr int hiresFrameRate_
void setInjectedUnlock(std::function< void()> toinJect)
void setInjectedLog(std::function< void(const std::string &, Severity severity)> toinJect)
void mapBufferFill(unsigned char *tofillWith, unsigned int index)
static constexpr unsigned int V4L2_EXTTRIGGER_ULTRA_PYTHON
static constexpr unsigned int V4L2_CONTRAST_ULTRA_PYTHON
static constexpr unsigned int V4L2_BLUEBALANCE_ULTRA_PYTHON
bool step(unsigned char *yarpbuffer)
void setInjectedLock(std::function< void()> toinJect)
void setInjectedProcess(std::function< void(const void *, int)> toinJect)
static constexpr unsigned int V4L2_DEADTIME_ULTRA_PYTHON
static constexpr int lowresFrameRate_
UltraPythonCameraHelper(const UltraPythonCameraHelper &)=delete
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
UltraPythonCameraHelper & operator=(const UltraPythonCameraHelper &)=delete
double getControl(uint32_t v4lCtrl, bool absolute)
static constexpr unsigned int V4L2_REDBALANCE_ULTRA_PYTHON
UltraPythonCameraHelper(UltraPythonCameraHelper &&)=delete
UltraPythonCameraHelper & operator=(UltraPythonCameraHelper &&)=delete
std::ostream & operator<<(std::ostream &os, StrStream &sstr)
Definition utility.cpp:85