HysteresisTrigger

class

Hysteresis or Schmitt trigger function class.

This class implements a simple hysteresis check that allows you to continuously provide samples, that is checked against lower and upper boundaries. The class will call your provided callback methods whenever the hysteresis trigger changes from one boundary to another.

A hysteresis trigger is a sound way of change program states, on base of an analog signal value. This could be a light sensor or battery voltage monitoring. A hysteresis loop will trigger the state changes on the base signal value changes.

Hysteresis

This is a mechanism that defines an upper and lower threshold in signal values, say 1000 and 100 for example. Whenever the input signal rises above the upper threshold (1000 in this example), the hysteresis triggers a statw change to an upper state.

Low the signal value must fall below the lower threshold (100) to trigger an equivalent lower state change. This process repeats, now the signal must exceed the upper threshold again to trigger the upper state change.

Read more on hyteresis triggers here: https://en.wikipedia.org/wiki/Hysteresis#Control_systems

Example

Here we setup a hysteresis trigger object, that triggers the callbacks then a signal values exceed or fall under certia thresholds:

HysteresisTrigger sensorTrigger(10000, 3000);

sensorTrigger.setUpperTriggerCallback(this, &MyApp::clearErrorState);
sensorTrigger.setLowerTriggerCallback(this, &MyApp::setErrorState);

// call this in a loop
uint16_t value = getSampleValue(); // get some signal value
sensorTrigger.check(value);

See
FilteredAnalogIn

Public Functions

mono::io::HysteresisTrigger::HysteresisTrigger(int upper, int lower, TriggerTypes InitialState)

Construct a new hysteresis object with specific boundaries.

Setup a hysteresis object with defined upper and lower threshold values.

Parameters
  • upper -

    The upper value (ceil) that triggers a state change

  • lower -

    The lower value (floor) that triggers a state change

  • InitialState -

    Optional: The start state of the hysteresis object

bool mono::io::HysteresisTrigger::check(int value)

Provide a sample value to the hysteresis, that might trigger a state change.

Pass sample values of the signal source to this method, and it will compared against the upper an lower thredshold of the hyteresis.

If the value exceeds a threshold one of the state trigger hanlers is called.

Return
true if an state changewas triggered, false otherwise
Parameters
  • value -

    The signal sample to check against the threshold

TriggerTypes mono::io::HysteresisTrigger::NextTriggerType()
const

Return the current next state of the hyteresis loop.

This means if the signal was previously above the upper threshold the next state change will be from upper to lower, eg. TRIG_LOWER_NEXT

void mono::io::HysteresisTrigger::setNextTrigger(TriggerTypes next)

Set the hysteresis next state trigger.

You can force the hysteresis loop into a given state, by providing its next state varaiable here.

Parameters
  • next -

    The next state change that should be triggered

template <typename Context>
void mono::io::HysteresisTrigger::setUpperTriggerCallback(Context * cnxt, void(Context::*)(void) memptr)

Set the upper threshold callback.

Provide a method that gets called whenever the hysteresis loop triggers for value above the upper threshold

Parameters
  • cnxt -

    The context of the method to call, usually it is the thispointer.

  • memptr -

    Function pointer to the method on the context class.

template <typename Context>
void mono::io::HysteresisTrigger::setLowerTriggerCallback(Context * cnxt, void(Context::*)(void) memptr)

Set the lower threshold callback.

Provide a method that gets called whenever the hysteresis loop triggers for value above the lower threshold

Parameters
  • cnxt -

    The context of the method to call, usually it is the thispointer.

  • memptr -

    Function pointer to the method on the context class.