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