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)
13 YARP_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 
40 const int REPORT_TIME=5; //seconds
41 
42 namespace yarp {
43  namespace dev {
44  template <class T> class DataSource;
45  template <class T> class DataWriter;
46  template <class T1, class T2> class DataSource2;
47  template <class T1, class T2> class DataWriter2;
48  }
49 }
50 
51 template <class T>
52 class YARP_DEPRECATED yarp::dev::DataSource {
53 public:
54  virtual ~DataSource() {}
55  virtual bool getDatum(T& datum) = 0;
56 };
57 
58 template <class T>
59 class YARP_DEPRECATED yarp::dev::DataWriter : public yarp::os::Runnable {
60 private:
61  yarp::os::Port& port;
63  DataSource<T>& dater;
64  yarp::os::Stamp stamp;
65  IPreciselyTimed *pPrecTime;
66  bool canDrop;
67  bool addStamp;
68  int counter;
69  double timePrevious;
70  double cumulativeT;
71  double minT;
72  double maxT;
73  double lastSpoke;
74 public:
75  DataWriter(yarp::os::Port& port, DataSource<T>& dater,
76  bool canDrop=true,
77  bool addStamp=false,
78  IPreciselyTimed *pt=NULL) :
79  port(port),
80  dater(dater),
81  pPrecTime(pt),
82  canDrop(canDrop),
83  addStamp(addStamp),
84  counter(0),
85  timePrevious(0.0),
86  cumulativeT(0.0),
87  minT(1e10),
88  maxT(0.0),
89  lastSpoke(yarp::os::Time::now())
90  {
91  writer.attach(port);
92  }
93 
94  void run() override {
95 
97  double now=yarp::os::Time::now();
98  double deltaT=0.0;
99 
100  if (counter==0)
101  {
102  lastSpoke=now;
103  timePrevious=now;
104  cumulativeT=0.0;
105  }
106  else
107  {
108  deltaT=now-timePrevious;
109  cumulativeT+=deltaT;
110  if (deltaT > maxT) {
111  maxT = deltaT;
112  }
113  if (deltaT < minT) {
114  minT = deltaT;
115  }
116  timePrevious=now;
117  }
118 
119  counter++;
120 
121  // print report
122  if (now-lastSpoke>REPORT_TIME)
123  {
124  yInfo("Read [%d] frames in %d[s], average period %.2lf[ms], min %.2lf[ms], max %.2lf[ms]\n",
125  counter,
126  REPORT_TIME,
127  (cumulativeT/counter)*1000,
128  minT*1000, maxT*1000);
129  cumulativeT=0;
130  counter=0;
131  minT=1e10;
132  maxT=0.0;
133  lastSpoke=now;
134  }
136 
137  T& datum = writer.get();
138 
139  dater.getDatum(datum);
140  if (addStamp) {
141  if (pPrecTime)
142  {
143  stamp=pPrecTime->getLastInputStamp();
144  }
145  else
146  {
147  stamp.update();
148  }
149  port.setEnvelope(stamp);
150  }
151  writer.write(!canDrop);
152  }
153 };
154 
155 
156 
157 template <class T1, class T2>
158 class YARP_DEPRECATED yarp::dev::DataSource2 {
159 public:
160  virtual ~DataSource2() {}
161  virtual bool getDatum(T1& datum1, T2& datum2) = 0;
162 };
163 
164 
165 template <class T1, class T2>
166 class YARP_DEPRECATED yarp::dev::DataWriter2 : public yarp::os::Runnable {
167 private:
168  yarp::os::Port& port1;
169  yarp::os::Port& port2;
172  DataSource2<T1,T2>& dater;
173  bool canDrop;
174  bool addStamp;
175  yarp::os::Stamp stamp;
176 public:
177  DataWriter2(yarp::os::Port& port1,
178  yarp::os::Port& port2,
179  DataSource2<T1,T2>& dater,
180  bool canDrop=true,
181  bool addStamp=false) :
182  port1(port1), port2(port2), dater(dater), canDrop(canDrop),
183  addStamp(addStamp)
184  {
185  writer1.attach(port1);
186  writer2.attach(port2);
187  }
188 
189  void run() override {
190  T1& datum1 = writer1.get();
191  T2& datum2 = writer2.get();
192  dater.getDatum(datum1,datum2);
193  if (addStamp) {
194  stamp.update();
195  port1.setEnvelope(stamp);
196  port2.setEnvelope(stamp);
197  }
198  writer1.write(!canDrop);
199  writer2.write(!canDrop);
200  }
201 };
202 
203 
204 #endif // DOXYGEN_SHOULD_SKIP_THIS
205 
207 
208 #endif // YARP_NO_DEPRECATED
209 
210 #endif // YARP_DEV_DATASOURCE_H
#define yInfo(...)
Definition: Log.h:257
Buffer outgoing data to a port.
void write(bool forceStrict=false)
Try to write the last buffer returned by PortWriterBuffer::get.
T & get()
A synonym of PortWriterBuffer::prepare.
void attach(Port &port)
Set the Port to which objects will be written.
A mini-server for network communication.
Definition: Port.h:47
bool setEnvelope(PortWriter &envelope) override
Set an envelope (e.g., a timestamp) to the next message which will be sent.
Definition: Port.cpp:538
A class that can be managed by another thread.
Definition: Runnable.h:29
An abstraction for a time stamp and/or sequence number.
Definition: Stamp.h:22
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:2884
yarp::rosmsg::std_msgs::Time Time
Definition: Time.h:21
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:332
#define YARP_COMPILER_WARNING(x)
Generate a warning at build time on supported compilers.
Definition: system.h:109
#define YARP_WARNING_PUSH
Starts a temporary alteration of the enabled warnings.
Definition: system.h:331
#define YARP_DISABLE_DEPRECATED_WARNING
Disable deprecated warnings in the following code.
Definition: system.h:333