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
impl Controller
Sourcepub fn setpoint(
setpoint: f32,
hysteresis: f32,
cooling: bool,
safe_band: f32,
) -> Self
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-truefor an output that switches on above the band (a cooler),falsefor 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.
Sourcepub fn level(empty: f32, warn_within: u32) -> Self
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.
Sourcepub fn surge(rising: bool, limit: f32) -> Self
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.
Sourcepub fn monitor() -> Self
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.
Trait Implementations§
Source§impl Clone for Controller
impl Clone for Controller
Source§fn clone(&self) -> Controller
fn clone(&self) -> Controller
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more