Skip to content

uber-foo/bangbang

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

bangbang

Build Status Latest Version docs rustc 1.3.0+

This crate provides an abstraction of a bang-bang controller along with a simple reference implementation. Suitable for regular applications using the standard library and embedded applications using no_std.

Bang-bang controllers are relatively simple machines, able to flip-flop solely between two mutually exclusive states. A common example of a bang-bang controller is a thermostat—the furnace can be turned either on or off with no other states of operation between those two extremes.

Known Implementations

Implementation Purpose
OnOff Simple on/off reference implementation contained in this crate
TimeConstrainedOnOff Can enforce minimum times at each state before a transition is allowed

Example

use bangbang::prelude::*;

// Simple struct to hold our current state
struct FlipFlop {
    current_state: BangBangState,
}

// Simplest implementation of a bang-bang controller
impl BangBang for FlipFlop {
    // Return the current active state
    fn state(&self) -> BangBangState {
        self.current_state
    }
    
    // Change the current active state
    fn set(&mut self, new_state: BangBangState) -> Result<(), BangBangError> {
        // Normally there would be logic here to ensure a state transition is possible

        // For the example, we'll just simply change the state as requested
        self.current_state = new_state;

        // No failure cases in this example
        Ok(())
    }
}

fn run_example() -> Result<(), BangBangError> {
    // Create a new bang-bang controller with initial state set to `A`
    let mut flip_flop = FlipFlop { current_state: BangBangState::A };
    assert_eq!(flip_flop.state(), BangBangState::A);

    // Trigger the bang-bang controller, flipping the state to `B`
    flip_flop.bang()?;
    assert_eq!(flip_flop.state(), BangBangState::B);

    // Trigger the bang-bang controller, flipping the state back to `A`
    flip_flop.bang()?;
    assert_eq!(flip_flop.state(), BangBangState::A);

    Ok(())
}

run_example();

License

Licensed under either of the following, at your option:

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

About

bang-bang controller abstraction and reference implementation

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages