Skip to main content

Controller

Struct Controller 

Source
pub struct Controller { /* private fields */ }
Expand description

The assembled, stateful decision logic of a profile.

A controller is what a Profile turns its ControlSpec into: the live loop that maps each reading to a Reaction. It composes the pamoja-kit helpers - a Thermostat for on/off control, a Depletion predictor for level alerts, and a Surge alarm for rapid change - so the same field-tested math drives every profile. The logic is synchronous and hardware-free, so a profile’s whole control policy is unit-testable with no devices and no network.

§Examples

use pamoja_profile::{Alert, Controller};

// Hold a fridge near 5 C, alerting if it strays more than 3 C from target.
let mut control = Controller::setpoint(5.0, 0.5, true, 3.0);

let reaction = control.evaluate(9.0); // warm and out of the safe band
assert_eq!(reaction.actuator, Some(true));
assert!(matches!(reaction.alert, Some(Alert::OutOfRange { .. })));

Implementations§

Source§

impl Controller

Source

pub fn setpoint( setpoint: f32, hysteresis: f32, cooling: bool, safe_band: f32, ) -> Self

Builds a controller that holds a reading near a setpoint.

This is the policy behind “keep a temperature” and “keep the soil watered”: it switches an output on and off around the setpoint and raises an Alert::OutOfRange when the reading strays beyond safe_band.

§Arguments
  • setpoint - the target reading.
  • hysteresis - half the deadband width around the setpoint, which stops the output chattering at the threshold.
  • cooling - true for an output that switches on above the band (a cooler), false for one that switches on below it (a heater or an irrigation valve).
  • safe_band - how far the reading may stray from the setpoint before an alert fires.
§Returns

A controller whose output starts off.

Source

pub fn level(empty: f32, warn_within: u32) -> Self

Builds a controller that warns before a falling level runs out.

This is the policy behind “warn before a tank runs dry”: it watches a level fall and raises an Alert::RunningOut once it is estimated to reach empty within warn_within more samples.

§Arguments
  • empty - the level treated as empty, such as a dry tank.
  • warn_within - warn once empty is this many samples away or nearer.
§Returns

A controller awaiting its first two readings.

Source

pub fn surge(rising: bool, limit: f32) -> Self

Builds a controller that warns when a reading changes too fast.

This is the policy behind “warn me before it is too late”: it watches the change between samples and raises an Alert::ChangingFast when a reading moves more than limit per sample in the watched direction, such as a river level rising into a flash flood.

§Arguments
  • rising - watch a rapid rise (true) or a rapid fall (false).
  • limit - the largest safe change per sample.
§Returns

A controller awaiting its first reading.

Source

pub fn monitor() -> Self

Builds a controller that reports readings without driving an output.

§Returns

A controller that never commands an actuator and never alerts.

Source

pub fn evaluate(&mut self, reading: f32) -> Reaction

Evaluates one reading and returns the action and any alert it calls for.

§Arguments
  • reading - the latest measured value, in real-world units.
§Returns

The Reaction for this reading: the actuator setting (if the profile drives one) and any alert the reading raised.

Trait Implementations§

Source§

impl Clone for Controller

Source§

fn clone(&self) -> Controller

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Controller

Source§

impl Debug for Controller

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.