YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
FrameGrabber_nws_yarp.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
8
10#include <yarp/os/LogStream.h>
11
12#include <yarp/dev/PolyDriver.h>
13
15
16namespace {
17YARP_LOG_COMPONENT(FRAMEGRABBER_NWS_YARP, "yarp.device.frameGrabber_nws_yarp")
18} // namespace
19
20
21static constexpr double DEFAULT_THREAD_PERIOD = 0.033; // seconds
22
27
28
33
34
36{
37 detach();
38
39 pImg.interrupt();
40 pImg.close();
41
42 rpcPort.interrupt();
43 rpcPort.close();
44
45 delete img;
46 img = nullptr;
47
48 delete img_Raw;
49 img_Raw = nullptr;
50
51 return true;
52}
53
54
56{
57 if (!this->parseParams(config)) { return false; }
58
59 PeriodicThread::setPeriod(m_period);
60
61 // Check "capabilities" option
62 if (m_capabilities == "COLOR") { m_cap = COLOR; }
63 else if (m_capabilities == "RAW") { m_cap = RAW; }
64 else { yCError(FRAMEGRABBER_NWS_YARP) << "'capabilities' parameter unsupported value"; return false;}
65
66 // Check "name" option and open ports
67 std::string pImg_Name = m_name;
68 std::string rpcPort_Name = pImg_Name + "/rpc";
69
70 if (!rpcPort.open(rpcPort_Name)) {
71 yCError(FRAMEGRABBER_NWS_YARP) << "Unable to open rpc Port" << rpcPort_Name.c_str();
72 return false;
73 }
74 rpcPort.setReader(*this);
75
76 pImg.promiseType(yarp::os::Type::byName("yarp/image"));
77 pImg.setWriteOnly();
78 pImg.setStrict(m_no_drop);
79 if (!pImg.open(pImg_Name)) {
80 yCError(FRAMEGRABBER_NWS_YARP) << "Unable to open image streaming Port" << pImg_Name.c_str();
81 return false;
82 }
83 pImg.setReader(*this);
84
85 yCInfo(FRAMEGRABBER_NWS_YARP) << "Running, waiting for attach...";
86
87 return true;
88}
89
91{
92 if (!poly->isValid()) {
93 yCError(FRAMEGRABBER_NWS_YARP) << "Device " << poly << " to attach to is not valid ... cannot proceed";
94 return false;
95 }
96
97 poly->view(iRgbVisualParams);
98 poly->view(iFrameGrabberImage);
99 poly->view(iFrameGrabberImageRaw);
100 poly->view(iFrameGrabberControls);
101 poly->view(iFrameGrabberControlsDC1394);
102 poly->view(iPreciselyTimed);
103
104 switch (m_cap) {
105 case COLOR: {
106 if (iFrameGrabberImage == nullptr) {
107 yCError(FRAMEGRABBER_NWS_YARP) << "Capability \"COLOR\" required not supported";
108 return false;
109 }
110 } break;
111 case RAW: {
112 if (iFrameGrabberImageRaw == nullptr) {
113 yCError(FRAMEGRABBER_NWS_YARP) << "Capability \"RAW\" required not supported";
114 return false;
115 }
116 }
117 }
118
119 if (iRgbVisualParams == nullptr) {
120 yCWarning(FRAMEGRABBER_NWS_YARP) << "Targets has not IVisualParamInterface, some features cannot be available";
121 }
122
123 // Configuring parsers
124 if (iFrameGrabberImage != nullptr) {
125 if (!(frameGrabberImage_Responder.configure(iFrameGrabberImage))) {
126 yCError(FRAMEGRABBER_NWS_YARP) << "Error configuring interfaces for parsers";
127 return false;
128 }
129 }
130
131 if (iFrameGrabberImageRaw != nullptr) {
132 if (!(frameGrabberImageRaw_Responder.configure(iFrameGrabberImageRaw))) {
133 yCError(FRAMEGRABBER_NWS_YARP) << "Error configuring interfaces for parsers";
134 return false;
135 }
136 }
137
138 if (iRgbVisualParams != nullptr) {
139 if (!(rgbVisualParams_Responder.configure(iRgbVisualParams))) {
140 yCError(FRAMEGRABBER_NWS_YARP) << "Error configuring interfaces for parsers";
141 return false;
142 }
143 }
144 if (iFrameGrabberControls != nullptr) {
145 if (!(frameGrabberControls_Responder.configure(iFrameGrabberControls))) {
146 yCError(FRAMEGRABBER_NWS_YARP) << "Error configuring interfaces for parsers";
147 return false;
148 }
149 }
150
151 if (iFrameGrabberControlsDC1394 != nullptr) {
152 if (!(frameGrabberControlsDC1394_Responder.configure(iFrameGrabberControlsDC1394))) {
153 yCError(FRAMEGRABBER_NWS_YARP) << "Error configuring interfaces for parsers";
154 return false;
155 }
156 }
157
158 return PeriodicThread::start();
159}
160
161
163{
166 }
167
168 iRgbVisualParams = nullptr;
169 iFrameGrabberImage = nullptr;
170 iFrameGrabberImageRaw = nullptr;
171 iFrameGrabberControls = nullptr;
172 iFrameGrabberControlsDC1394 = nullptr;
173 iPreciselyTimed = nullptr;
174
175 return true;
176}
177
179{
180 if (m_cap == COLOR) {
182 } else {
184 }
185
186 return true;
187}
188
189// Publish the images on the buffered port
191{
192 if (pImg.getOutputCount() == 0) {
193 // If no ports are connected, do not call getImage on the interface.
194 return;
195 }
196
197 yarp::sig::FlexImage& flex_i = pImg.prepare();
198
199 if (m_cap == COLOR) {
200 if (iFrameGrabberImage != nullptr) {
201 if (iFrameGrabberImage->getImage(*img))
202 {flex_i.swap(*img);}
203 else
204 {yCError(FRAMEGRABBER_NWS_YARP) << "Image not captured (getImage failed). Check hardware configuration.";}
205 } else {
206 yCError(FRAMEGRABBER_NWS_YARP) << "Image not captured (invalid interface). Check hw/sw configuration.";
207 }
208 }
209
210 if (m_cap == RAW) {
211 if (iFrameGrabberImageRaw != nullptr) {
212 if (iFrameGrabberImageRaw->getImage(*img_Raw))
213 {flex_i.swap(*img_Raw);}
214 else
215 {yCError(FRAMEGRABBER_NWS_YARP) << "Image not captured (getImage failed). Check hardware configuration.";}
216 } else {
217 yCError(FRAMEGRABBER_NWS_YARP) << "Image not captured (invalid interface). Check hw/sw configuration.";
218 }
219 }
220
221 if (iPreciselyTimed) {
222 m_stamp = iPreciselyTimed->getLastInputStamp();
223 } else {
224 m_stamp.update(yarp::os::Time::now());
225 }
226 pImg.setEnvelope(m_stamp);
227
228 pImg.write();
229}
230
231// Respond to the RPC calls
233 yarp::os::Bottle& reply)
234{
235 yarp::conf::vocab32_t code = command.get(0).asVocab32();
236 switch (code) {
238 return frameGrabberImage_Responder.respond(command, reply);
240 return frameGrabberImageRaw_Responder.respond(command, reply);
242 return frameGrabberControls_Responder.respond(command, reply);
244 return rgbVisualParams_Responder.respond(command, reply);
246 return frameGrabberControlsDC1394_Responder.respond(command, reply);
247 default:
248 yCError(FRAMEGRABBER_NWS_YARP) << "Command not recognized" << command.toString();
249 return false;
250 }
251
252 return false;
253}
constexpr yarp::conf::vocab32_t VOCAB_RGB_VISUAL_PARAMS
constexpr yarp::conf::vocab32_t VOCAB_FRAMEGRABBER_CONTROL_DC1394
constexpr yarp::conf::vocab32_t VOCAB_FRAMEGRABBER_IMAGERAW
constexpr yarp::conf::vocab32_t VOCAB_FRAMEGRABBER_IMAGE
constexpr yarp::conf::vocab32_t VOCAB_FRAMEGRABBER_CONTROL
static constexpr double DEFAULT_THREAD_PERIOD
bool parseParams(const yarp::os::Searchable &config) override
Parse the DeviceDriver parameters.
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
bool detach() override
Detach the object (you must have first called attach).
bool threadInit() override
Initialization method.
void run() override
Loop function.
bool close() override
Close the DeviceDriver.
bool attach(yarp::dev::PolyDriver *poly) override
Attach to another object.
bool respond(const yarp::os::Bottle &command, yarp::os::Bottle &reply) override
Respond to a message.
bool view(T *&x)
Get an interface to the device driver.
virtual bool getImage(ImageType &image)=0
Get an image from the frame grabber.
virtual yarp::os::Stamp getLastInputStamp()=0
Return the time stamp relative to the last acquisition.
A container for a device driver.
Definition PolyDriver.h:23
bool isValid() const
Check if device is valid.
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
void setReader(PortReader &reader) override
Set an external reader for port data.
void close() override
Stop port activity.
void interrupt() override
Interrupt any current reads or writes attached to the port.
A simple collection of objects that can be described and transmitted in a portable way.
Definition Bottle.h:64
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Definition Bottle.cpp:246
std::string toString() const override
Gives a human-readable textual representation of the bottle.
Definition Bottle.cpp:211
void promiseType(const Type &typ) override
Commit the port to a particular type of data.
void close() override
Stop port activity.
bool setEnvelope(PortWriter &envelope) override
Set an envelope (e.g., a timestamp) to the next message which will be sent.
void setReader(PortReader &reader) override
Set an external reader for port data.
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
void interrupt() override
Interrupt any current reads or writes attached to the port.
void setStrict(bool strict=true) override
Call this to strictly keep all messages, or allow old ones to be quietly dropped.
int getOutputCount() override
Determine how many output connections this port has.
void write(bool forceStrict=false)
Write the current object being returned by BufferedPort::prepare.
T & prepare()
Access the object which will be transmitted by the next call to yarp::os::BufferedPort::write.
void setWriteOnly()
Shorthand for setInputMode(false), setOutputMode(true), setRpcMode(false)
bool isRunning() const
Returns true when the thread is started, false otherwise.
void stop()
Call this to stop the thread, this call blocks until the thread is terminated (and releaseThread() ca...
A base class for nested structures that can be searched.
Definition Searchable.h:31
void update()
Set the timestamp to the current time, and increment the sequence number (wrapping to 0 if the sequen...
Definition Stamp.cpp:124
static Type byName(const char *name)
Definition Type.cpp:171
virtual yarp::conf::vocab32_t asVocab32() const
Get vocabulary identifier as an integer.
Definition Value.cpp:228
bool respond(const yarp::os::Bottle &cmd, yarp::os::Bottle &response) override
Respond to a message.
bool respond(const yarp::os::Bottle &cmd, yarp::os::Bottle &response) override
Respond to a message.
bool configure(yarp::dev::IFrameGrabberControls *interface)
bool configure(yarp::dev::IFrameGrabberOf< ImageType > *interface)
bool respond(const yarp::os::Bottle &cmd, yarp::os::Bottle &response) override
Respond to a message.
bool configure(yarp::dev::IRgbVisualParams *interface)
bool respond(const yarp::os::Bottle &cmd, yarp::os::Bottle &response) override
Respond to a message.
Image class with user control of representation details.
Definition Image.h:363
Typed image class.
Definition Image.h:605
bool swap(Image &alt)
swap operator.
Definition Image.cpp:676
#define yCInfo(component,...)
#define yCError(component,...)
#define yCWarning(component,...)
#define YARP_LOG_COMPONENT(name,...)
std::int32_t vocab32_t
Definition numeric.h:78
double now()
Return the current time in seconds, relative to an arbitrary starting point.
Definition Time.cpp:121