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 StartAt (const double tStart)
 
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.
 
bool LatenessWarningGiven () const
 True once the too-coarse-step warning has been given.
 

Static Public Member Functions

static SampleClock Never ()
 A clock that never fires: for a sensor that has no sample rate yet.
 

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; finite and positive.
firstSampleTimeThe time of the first sample, s. A clock for a run that starts at TStart is moved there with StartAt.
Exceptions
std::invalid_argumentif period is zero, negative, NaN or infinite: such a clock would never advance, or never fire.

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.

◆ StartAt()

void SampleClock::StartAt ( const double  tStart)
inline

Moves the first sample to the start of the run.

Called from FinalSetup(T, ...) with the run's start time, so that a run with TStart = 1000 s takes one sample on its first step instead of the 10 000 that fit between 0 and TStart. Does nothing for a Never clock.

Parameters
tStartThe simulation start time, s.

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