YARP
Yet Another Robot Platform
DataSource.h
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
7#ifndef YARP_DEV_DATASOURCE_H
8#define YARP_DEV_DATASOURCE_H
9
10#include <yarp/conf/system.h>
11
12#if !defined(YARP_INCLUDING_DEPRECATED_HEADER_YARP_DEV_DATASOURCE_H_ON_PURPOSE)
13YARP_COMPILER_WARNING("<yarp/dev/DataSource.h> file is deprecated")
14#endif
15
16#ifndef YARP_NO_DEPRECATED // Since YARP 3.3
17
20
21#include <yarp/os/Port.h>
23#include <yarp/os/Stamp.h>
24#include <yarp/os/Time.h>
25#include <yarp/os/Log.h>
26
27// #define YARP_INCLUDING_DEPRECATED_HEADER_YARP_OS_RUNNABLE_H_ON_PURPOSE
28#define YARP_INCLUDING_DEPRECATED_HEADER_ON_PURPOSE
29#include <yarp/os/Runnable.h>
30#undef YARP_INCLUDING_DEPRECATED_HEADER_ON_PURPOSE
31// #undef YARP_INCLUDING_DEPRECATED_HEADER_YARP_OS_RUNNABLE_H_ON_PURPOSE
32
33
35
36// These classes are part of the YARP library implementation,
37// rather than its user interface
38#ifndef DOXYGEN_SHOULD_SKIP_THIS
39
40const int REPORT_TIME=5; //seconds
41
42namespace yarp::dev {
43template <class T>
44class DataSource;
45template <class T>
46class DataWriter;
47template <class T1, class T2>
48class DataSource2;
49template <class T1, class T2>
50class DataWriter2;
51}
52
53template <class T>
54class YARP_DEPRECATED yarp::dev::DataSource {
55public:
56 virtual ~DataSource() {}
57 virtual bool getDatum(T& datum) = 0;
58};
59
60template <class T>
61class YARP_DEPRECATED yarp::dev::DataWriter : public yarp::os::Runnable {
62private:
63 yarp::os::Port& port;
65 DataSource<T>& dater;
66 yarp::os::Stamp stamp;
67 IPreciselyTimed *pPrecTime;
68 bool canDrop;
69 bool addStamp;
70 int counter;
71 double timePrevious;
72 double cumulativeT;
73 double minT;
74 double maxT;
75 double lastSpoke;
76public:
77 DataWriter(yarp::os::Port& port, DataSource<T>& dater,
78 bool canDrop=true,
79 bool addStamp=false,
80 IPreciselyTimed *pt=NULL) :
81 port(port),
82 dater(dater),
83 pPrecTime(pt),
84 canDrop(canDrop),
85 addStamp(addStamp),
86 counter(0),
87 timePrevious(0.0),
88 cumulativeT(0.0),
89 minT(1e10),
90 maxT(0.0),
91 lastSpoke(yarp::os::Time::now())
92 {
93 writer.attach(port);
94 }
95
96 void run() override {
97
99 double now=yarp::os::Time::now();
100 double deltaT=0.0;
101
102 if (counter==0)
103 {
104 lastSpoke=now;
105 timePrevious=now;
106 cumulativeT=0.0;
107 }
108 else
109 {
110 deltaT=now-timePrevious;
111 cumulativeT+=deltaT;
112 if (deltaT > maxT) {
113 maxT = deltaT;
114 }
115 if (deltaT < minT) {
116 minT = deltaT;
117 }
118 timePrevious=now;
119 }
120
121 counter++;
122
123 // print report
124 if (now-lastSpoke>REPORT_TIME)
125 {
126 yInfo("Read [%d] frames in %d[s], average period %.2lf[ms], min %.2lf[ms], max %.2lf[ms]\n",
127 counter,
128 REPORT_TIME,
129 (cumulativeT/counter)*1000,
130 minT*1000, maxT*1000);
131 cumulativeT=0;
132 counter=0;
133 minT=1e10;
134 maxT=0.0;
135 lastSpoke=now;
136 }
138
139 T& datum = writer.get();
140
141 dater.getDatum(datum);
142 if (addStamp) {
143 if (pPrecTime)
144 {
145 stamp=pPrecTime->getLastInputStamp();
146 }
147 else
148 {
149 stamp.update();
150 }
151 port.setEnvelope(stamp);
152 }
153 writer.write(!canDrop);
154 }
155};
156
157
158
159template <class T1, class T2>
160class YARP_DEPRECATED yarp::dev::DataSource2 {
161public:
162 virtual ~DataSource2() {}
163 virtual bool getDatum(T1& datum1, T2& datum2) = 0;
164};
165
166
167template <class T1, class T2>
168class YARP_DEPRECATED yarp::dev::DataWriter2 : public yarp::os::Runnable {
169private:
170 yarp::os::Port& port1;
171 yarp::os::Port& port2;
174 DataSource2<T1,T2>& dater;
175 bool canDrop;
176 bool addStamp;
177 yarp::os::Stamp stamp;
178public:
179 DataWriter2(yarp::os::Port& port1,
180 yarp::os::Port& port2,
181 DataSource2<T1,T2>& dater,
182 bool canDrop=true,
183 bool addStamp=false) :
184 port1(port1), port2(port2), dater(dater), canDrop(canDrop),
185 addStamp(addStamp)
186 {
187 writer1.attach(port1);
188 writer2.attach(port2);
189 }
190
191 void run() override {
192 T1& datum1 = writer1.get();
193 T2& datum2 = writer2.get();
194 dater.getDatum(datum1,datum2);
195 if (addStamp) {
196 stamp.update();
197 port1.setEnvelope(stamp);
198 port2.setEnvelope(stamp);
199 }
200 writer1.write(!canDrop);
201 writer2.write(!canDrop);
202 }
203};
204
205
206#endif // DOXYGEN_SHOULD_SKIP_THIS
207
209
210#endif // YARP_NO_DEPRECATED
211
212#endif // YARP_DEV_DATASOURCE_H
#define yInfo(...)
Definition: Log.h:314
Buffer outgoing data to a port.
T & get()
A synonym of PortWriterBuffer::prepare.
void write(bool forceStrict=false)
Try to write the last buffer returned by PortWriterBuffer::get.
void attach(Port &port)
Set the Port to which objects will be written.
A mini-server for network communication.
Definition: Port.h:46
bool setEnvelope(PortWriter &envelope) override
Set an envelope (e.g., a timestamp) to the next message which will be sent.
Definition: Port.cpp:547
A class that can be managed by another thread.
Definition: Runnable.h:28
An abstraction for a time stamp and/or sequence number.
Definition: Stamp.h:21
void update()
Set the timestamp to the current time, and increment the sequence number (wrapping to 0 if the sequen...
Definition: Stamp.cpp:124
#define YARP_DEPRECATED
Expands to either the standard [[deprecated]] attribute or a compiler-specific decorator such as __at...
Definition: compiler.h:2894
yarp::rosmsg::std_msgs::Time Time
Definition: Time.h:21
For streams capable of holding different kinds of content, check what they actually have.
double now()
Return the current time in seconds, relative to an arbitrary starting point.
Definition: Time.cpp:121
The main, catch-all namespace for YARP.
Definition: dirs.h:16
#define YARP_WARNING_POP
Ends a temporary alteration of the enabled warnings.
Definition: system.h:334
#define YARP_COMPILER_WARNING(x)
Generate a warning at build time on supported compilers.
Definition: system.h:111
#define YARP_WARNING_PUSH
Starts a temporary alteration of the enabled warnings.
Definition: system.h:333
#define YARP_DISABLE_DEPRECATED_WARNING
Disable deprecated warnings in the following code.
Definition: system.h:335