FhSim  3.1.0
Marine systems simulation
Loading...
Searching...
No Matches
SampleClock Class Reference

#include <SampleClock.h>

Public Member Functions

 SampleClock (const double period, const double firstSampleTime=0.0)
 
void SetLogger (PrintDuringExec *const print)
 
bool Due (const double t)
 
double NextSampleTime () const
 The time at which the next sample is due, s.
 
double Period () const
 The time between samples, s.
 

Detailed Description

Decides when a fixed-rate sensor should take its next sample.

FhSim has no discrete event scheduler, so a sensor that samples at a fixed rate has to schedule itself from the accepted simulation time. The idiom is the one used by Signal/Samplifier in fhsim_base: hold the time of the next sample, and advance it by one period whenever it is reached.

The integrator step is not synchronised with the sample period, so more than one sample can fall inside a single accepted step. Due therefore advances by exactly one period per call and is meant to be used in a loop:

while (m_clock.Due(T))
TakeOneSample(T, X);

A sample fired later than kLatenessFraction of a period after it was due means the integrator step is too coarse for the sample rate, which otherwise shows up only as a quietly aliased signal. The clock says so once, naming the StepMax the user would need, and then stays quiet.

Note
This is a helper, not a SimObject, so it has no @group block and appears in no model list.

Constructor & Destructor Documentation

◆ SampleClock()

SampleClock::SampleClock ( const double  period,
const double  firstSampleTime = 0.0 
)
inlineexplicit

Creates a clock with the given rate.

Parameters
periodThe time between samples, s.
firstSampleTimeThe time of the first sample, s.

Member Function Documentation

◆ Due()

bool SampleClock::Due ( const double  t)
inline

Reports whether a sample is due at the given time, and consumes it.

Parameters
tThe current simulation time, s.
Returns
True when a sample is due, in which case the clock has advanced one period and should be asked again.

◆ SetLogger()

void SampleClock::SetLogger ( PrintDuringExec *const  print)
inline

Sets the logger used for the too-coarse-step warning.

Parameters
printThe simulation logger, or null to log nothing.

The documentation for this class was generated from the following file: