23 class DelayEstimatorBase
26 DelayEstimatorBase(
double period) : adaptedPeriod(std::max(period, 0.0)) {}
27 virtual ~DelayEstimatorBase() =
default;
28 double getPeriod()
const {
return adaptedPeriod; }
29 virtual void setPeriod(
double period) { adaptedPeriod = std::max(period, 0.0); }
30 virtual void onInit() {};
31 virtual void onSchedule(
unsigned int count,
double now) {};
32 virtual double computeDelay(
unsigned int count,
double now,
double elapsed)
const = 0;
33 virtual void reset(
unsigned int count,
double now) {};
39 class AbsoluteDelayEstimator :
public DelayEstimatorBase
42 AbsoluteDelayEstimator(
double period) : DelayEstimatorBase(period), scheduledPeriod(period) {}
44 void onInit()
override
49 void setPeriod(
double period)
override
51 scheduledPeriod = period;
55 void onSchedule(
unsigned int count,
double now)
override
58 DelayEstimatorBase::setPeriod(scheduledPeriod);
63 double computeDelay(
unsigned int count,
double now,
double elapsed)
const override
65 return refTime + getPeriod() * (count - countOffset) -
now;
68 void reset(
unsigned int count,
double now)
override
72 scheduleAdapt =
false;
76 unsigned int countOffset {0};
78 double scheduledPeriod {0.0};
79 bool scheduleAdapt {
false};
82 class RelativeDelayEstimator :
public DelayEstimatorBase
85 using DelayEstimatorBase::DelayEstimatorBase;
87 double computeDelay(
unsigned int count,
double now,
double elapsed)
const override
89 return getPeriod() - elapsed;
99 mutable std::mutex mutex;
111 std::unique_ptr<DelayEstimatorBase> delayEstimator;
113 using NowFuncPtr = double (*)();
114 using DelayFuncPtr = void (*)(double);
115 const NowFuncPtr nowFunc;
116 const DelayFuncPtr delayFunc;
126 scheduleReset =
false;
140 scheduleReset(false),
145 delayEstimator = std::make_unique<RelativeDelayEstimator>(p);
147 delayEstimator = std::make_unique<AbsoluteDelayEstimator>(p);
153 scheduleReset =
true;
163 ret = (totalT / estPIt);
176 av = totalT / estPIt;
178 std = sqrt(((1.0 / (estPIt - 1)) * (sumTSq - estPIt * av * av)));
189 unsigned int ret = count;
201 ret = totalUsed / count;
214 av = totalUsed / count;
216 std = sqrt((1.0 / (count - 1)) * (sumUsedSq - count * av * av));
227 double currentRun = nowFunc();
228 delayEstimator->onSchedule(count, currentRun);
232 delayEstimator->reset(count, currentRun);
236 double dT = currentRun - previousRun;
242 previousRun = currentRun;
258 double now = nowFunc();
259 double elapsed =
now - currentRun;
260 double sleepPeriod = delayEstimator->computeDelay(count,
now, elapsed);
262 totalUsed += elapsed;
263 sumUsedSq += elapsed * elapsed;
266 delayFunc(sleepPeriod);
271 while (!isClosing()) {
278 delayEstimator->onInit();
290 delayEstimator->setPeriod(period);
297 return delayEstimator->getPeriod();
354 return mPriv->setPeriod(period);
359 return mPriv->getPeriod();
364 return mPriv->isSuspended();
384 return mPriv->start();
389 return mPriv->isRunning();
404 return mPriv->getIterations();
409 return mPriv->getEstimatedPeriod();
414 return mPriv->getEstimatedUsed();
419 mPriv->getEstimatedPeriod(av, std);
424 mPriv->getEstimatedUsed(av, std);
452 return mPriv->setPriority(priority, policy);
457 return mPriv->getPriority();
462 return mPriv->getPolicy();
void threadRelease() override
void getEstimatedUsed(double &av, double &std) const
bool setPeriod(double period)
double getEstimatedPeriod() const
void afterStart(bool s) override
double getEstimatedUsed() const
Private(PeriodicThread *owner, double p, ShouldUseSystemClock useSystemClock, PeriodicThreadClock clockAccuracy)
void beforeStart() override
bool threadInit() override
void getEstimatedPeriod(double &av, double &std) const
unsigned int getIterations() const
An abstraction for a periodic thread.
void resetStat()
Reset thread statistics.
bool setPeriod(double period)
Set the (new) period of the thread.
int getPriority() const
Query the current priority of the thread, if the OS supports that.
PeriodicThread(double period, ShouldUseSystemClock useSystemClock=ShouldUseSystemClock::No, PeriodicThreadClock clockAccuracy=PeriodicThreadClock::Relative)
Constructor.
virtual void run()=0
Loop function.
virtual void beforeStart()
Called just before a new thread starts.
unsigned int getIterations() const
Return the number of iterations performed since last reset.
bool isRunning() const
Returns true when the thread is started, false otherwise.
int getPolicy() const
Query the current scheduling policy of the thread, if the OS supports that.
bool isSuspended() const
Returns true when the thread is suspended, false otherwise.
virtual bool threadInit()
Initialization method.
void resume()
Resume the thread if previously suspended.
virtual void afterStart(bool success)
Called just after a new thread starts (or fails to start), this is executed by the same thread that c...
void suspend()
Suspend the thread, the thread keeps running by doLoop is never executed.
int setPriority(int priority, int policy=-1)
Set the priority and scheduling policy of the thread, if the OS supports that.
void askToStop()
Stop the thread.
double getEstimatedUsed() const
Return the estimated duration of the run() function since last reset.
double getEstimatedPeriod() const
Return estimated period since last reset.
bool start()
Call this to start the thread.
void step()
Call this to "step" the thread rather than starting it.
virtual ~PeriodicThread()
double getPeriod() const
Return the current period of the thread.
void stop()
Call this to stop the thread, this call blocks until the thread is terminated (and releaseThread() ca...
virtual void threadRelease()
Release method.
An abstraction for a thread of execution.
yarp::rosmsg::std_msgs::Time Time
void useSystemClock()
Configure YARP to use system time (this is the default).
void yield()
The calling thread releases its remaining quantum upon calling this function.
double now()
Return the current time in seconds, relative to an arbitrary starting point.
void delay(double seconds)
Wait for a certain number of seconds.
The components from which ports and connections are built.
An interface to the operating system, including Port based communication.
The main, catch-all namespace for YARP.