FhSim  3.1.0
Marine systems simulation
Loading...
Searching...
No Matches
SampleClock.h
1#pragma once
2
11#include <fhsim/NFhSim.h>
12#include <fhsim/PrintDuringExec.h>
13#include <string>
14
40{
41public:
47 explicit SampleClock(const double period, const double firstSampleTime = 0.0)
48 : m_period(period)
49 , m_next(firstSampleTime)
50 {
51 }
52
57 void SetLogger(PrintDuringExec* const print) { m_print = print; }
58
65 bool Due(const double t)
66 {
67 if (t < m_next)
68 return false;
69
70 WarnOnceIfLate(t);
71 m_next += m_period;
72 return true;
73 }
74
76 double NextSampleTime() const { return m_next; }
77
79 double Period() const { return m_period; }
80
81private:
86 void WarnOnceIfLate(const double t)
87 {
88 const double lateness = t - m_next;
89 if (lateness <= kLatenessFraction * m_period || m_latenessWarningGiven)
90 return;
91
92 m_latenessWarningGiven = true;
93 if (m_print == nullptr)
94 return;
95
96 m_print->WriteLog("In SampleClock. A sample due at t = " + std::to_string(m_next) + " s was taken " + std::to_string(lateness) + " s late, which is more than " + std::to_string(kLatenessFraction) + " of the sample period of " + std::to_string(m_period) + " s. The integrator step is too coarse for this "
97 "sample rate; set StepMax to " +
98 std::to_string(kLatenessFraction * m_period) + " s or less. This warning is given once.",
99 fhsim::LogLevel_Warning);
100 }
101
103 static constexpr double kLatenessFraction = 0.25;
104
105 double m_period;
106 double m_next;
107
108 PrintDuringExec* m_print = nullptr;
109 bool m_latenessWarningGiven = false;
110};
Definition SampleClock.h:40
bool Due(const double t)
Definition SampleClock.h:65
SampleClock(const double period, const double firstSampleTime=0.0)
Definition SampleClock.h:47
void SetLogger(PrintDuringExec *const print)
Definition SampleClock.h:57
double NextSampleTime() const
The time at which the next sample is due, s.
Definition SampleClock.h:76
double Period() const
The time between samples, s.
Definition SampleClock.h:79