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>
14namespace {
15YARP_LOG_COMPONENT(FRAMEGRABBER_NWS_YARP, "yarp.device.frameGrabber_nws_yarp")
16} // namespace
17
18
19static constexpr double DEFAULT_THREAD_PERIOD = 0.033; // seconds
20
25
26
31
32
34{
35 detach();
36
37 pImg.interrupt();
38 pImg.close();
39
40 rpcPort.interrupt();
41 rpcPort.close();
42
43 delete img;
44 img = nullptr;
45
46 delete img_Raw;
47 img_Raw = nullptr;
48
49 return true;
50}
51
52
54{
55 if (!this->parseParams(config)) { return false; }
56
57 PeriodicThread::setPeriod(m_period);
58
59 // Check "capabilities" option
60 if (m_capabilities == "COLOR") { m_cap = COLOR; }
61 else if (m_capabilities == "RAW") { m_cap = RAW; }
62 else { yCError(FRAMEGRABBER_NWS_YARP) << "'capabilities' parameter unsupported value"; return false;}
63
64 // Check "name" option and open ports
65 std::string pImg_Name = m_name;
66 std::string rpcPort_Name = pImg_Name + "/rpc";
67
68 if (!rpcPort.open(rpcPort_Name)) {
69 yCError(FRAMEGRABBER_NWS_YARP) << "Unable to open rpc Port" << rpcPort_Name.c_str();
70 return false;
71 }
72 rpcPort.setReader(*this);
73
74 pImg.promiseType(yarp::os::Type::byName("yarp/image"));
75 pImg.setWriteOnly();
76 pImg.setStrict(m_no_drop);
77 if (!pImg.open(pImg_Name)) {
78 yCError(FRAMEGRABBER_NWS_YARP) << "Unable to open image streaming Port" << pImg_Name.c_str();
79 return false;
80 }
81 pImg.setReader(*this);
82
83 yCInfo(FRAMEGRABBER_NWS_YARP) << "Running, waiting for attach...";
84
85 return true;
86}
87
89{
90 std::lock_guard lock (m_mutex);
91
92 if (!poly->isValid())
93 {
94 yCError(FRAMEGRABBER_NWS_YARP) << "Device " << poly << " to attach to is not valid ... cannot proceed";
95 return false;
96 }
97
98 //Device Interfaces
99 poly->view(m_iRgbVisualParams);
100 poly->view(m_iFrameGrabberControls);
101 poly->view(m_iFrameGrabberControlsDC1394);
102 poly->view(m_iFrameGrabberImage); //optional: may be not implemented by device
103 poly->view(m_iFrameGrabberImageRaw); // optional: may be not implemented by device
104 poly->view(m_iPreciselyTimed); //optional: may be not implemented by device
105
106 switch (m_cap) {
107 case COLOR: {
108 if (m_iFrameGrabberImage == nullptr) {
109 yCError(FRAMEGRABBER_NWS_YARP) << "Capability \"COLOR\" required not supported";
110 return false;
111 }
112 } break;
113 case RAW: {
114 if (m_iFrameGrabberImageRaw == nullptr) {
115 yCError(FRAMEGRABBER_NWS_YARP) << "Capability \"RAW\" required not supported";
116 return false;
117 }
118 }
119 }
120
121 if (m_iRgbVisualParams == nullptr) {
122 yCWarning(FRAMEGRABBER_NWS_YARP) << "Targets has not IVisualParamInterface, some features cannot be available";
123 }
124
125 m_RPC_FrameGrabber = std::make_unique<FrameGrabberMsgsImpl>(m_iRgbVisualParams, m_iFrameGrabberControls, m_iFrameGrabberControlsDC1394, m_iFrameGrabberImage, m_iFrameGrabberImageRaw);
126
127 return PeriodicThread::start();
128}
129
130
132{
133 std::lock_guard lock (m_mutex);
134
137 }
138
139 m_iFrameGrabberImage = nullptr;
140 m_iFrameGrabberImageRaw = nullptr;
141 m_iPreciselyTimed = nullptr;
142 m_iRgbVisualParams = nullptr;
143 m_iFrameGrabberControls = nullptr;
144 m_iFrameGrabberControlsDC1394 = nullptr;
145
146 return true;
147}
148
150{
151 if (m_cap == COLOR) {
153 } else {
155 }
156
157 return true;
158}
159
160// Publish the images on the buffered port
162{
163 if (pImg.getOutputCount() == 0) {
164 // If no ports are connected, do not call getImage on the interface.
165 return;
166 }
167
168 yarp::sig::FlexImage& flex_i = pImg.prepare();
169
170 if (m_cap == COLOR)
171 {
172 if (m_iFrameGrabberImage != nullptr) {
173 if (m_iFrameGrabberImage->getImage(*img))
174 {flex_i.swap(*img);}
175 else
176 {yCError(FRAMEGRABBER_NWS_YARP) << "Image not captured (getImage failed). Check hardware configuration.";}
177 } else {
178 yCError(FRAMEGRABBER_NWS_YARP) << "Image not captured (invalid interface). Check hw/sw configuration.";
179 }
180 }
181
182 if (m_cap == RAW)
183 {
184 if (m_iFrameGrabberImageRaw != nullptr) {
185 if (m_iFrameGrabberImageRaw->getImage(*img_Raw))
186 {flex_i.swap(*img_Raw);}
187 else
188 {yCError(FRAMEGRABBER_NWS_YARP) << "Image not captured (getImage failed). Check hardware configuration.";}
189 } else {
190 yCError(FRAMEGRABBER_NWS_YARP) << "Image not captured (invalid interface). Check hw/sw configuration.";
191 }
192 }
193
194 if (m_iPreciselyTimed)
195 {
196 m_stamp = m_iPreciselyTimed->getLastInputStamp();
197 } else {
198 m_stamp.update(yarp::os::Time::now());
199 }
200 pImg.setEnvelope(m_stamp);
201
202 pImg.write();
203}
204
205// Respond to the RPC calls
207{
208 if (!connection.isValid()) { return false;}
209 if (!m_RPC_FrameGrabber) { return false;}
210
211 std::lock_guard<std::mutex> lock(m_mutex);
212
213 if (m_RPC_FrameGrabber->read(connection))
214 {
215 return true;
216 }
217
218 yCError(FRAMEGRABBER_NWS_YARP) << "read() Command failed";
219 return false;
220}
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.
virtual bool read(yarp::os::ConnectionReader &connection) override
Read this object from a network connection.
void run() override
Loop function.
bool close() override
Close the DeviceDriver.
bool attach(yarp::dev::PolyDriver *poly) override
Attach to another object.
bool view(T *&x)
Get an interface to the device driver.
virtual yarp::dev::ReturnValue 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.
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.
An interface for reading from a network connection.
virtual bool isValid() const =0
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
Image class with user control of representation details.
Definition Image.h:361
Typed image class.
Definition Image.h:603
bool swap(Image &alt)
swap operator.
Definition Image.cpp:659
#define yCInfo(component,...)
#define yCError(component,...)
#define yCWarning(component,...)
#define YARP_LOG_COMPONENT(name,...)
double now()
Return the current time in seconds, relative to an arbitrary starting point.
Definition Time.cpp:121