Skip to content

Commit

Permalink
adds abckflush routine
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbrain-za committed Dec 30, 2024
1 parent 0f51f64 commit 0979c10
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 6 deletions.
37 changes: 34 additions & 3 deletions src/components/pump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub enum Message {
OnForTimeAtPressure(Duration, Bar),
OnForYield { pressure: Bar, grams: Grams },
OnForHotWater,
Backflush(Duration),
Backflush,
}

pub type Mailbox = Sender<Message>;
Expand Down Expand Up @@ -45,12 +45,19 @@ impl Interface {
.send(Message::OnForYield { pressure, grams })
.unwrap();
}
pub fn turn_on_for_hot_water(&self) {
self.mailbox.send(Message::OnForHotWater).unwrap();
}
pub fn backflush(&self) {
self.mailbox.send(Message::Backflush).unwrap();
}
}

enum State {
On(Option<Instant>),
Off,
OnForYield { start: Grams, target: Grams },
Backflush,
}

pub struct Pump<PD: OutputPin, PE: OutputPin> {
Expand All @@ -59,6 +66,8 @@ pub struct Pump<PD: OutputPin, PE: OutputPin> {
pressure_probe: Arc<RwLock<Bar>>,
weight_probe: Arc<RwLock<Grams>>,
state: State,
backflush_cycle_start: Instant,
backflush_in_off_cycle: bool,
}

impl<PD, PE> Pump<PD, PE>
Expand All @@ -82,6 +91,8 @@ where
pressure_probe,
weight_probe,
state: State::Off,
backflush_cycle_start: Instant::now(),
backflush_in_off_cycle: true,
};
loop {
while let Ok(message) = rx.try_recv() {
Expand All @@ -98,6 +109,22 @@ where
my_pump.trasition(Message::Off);
}
}
State::Backflush => {
let elapsed = my_pump.backflush_cycle_start.elapsed();

if elapsed > config::BACKFLUSH_OFF_TIME + config::BACKFLUSH_ON_TIME {
my_pump.backflush_cycle_start = Instant::now();
my_pump.open_valve();
my_pump.set_pressure(config::MAX_PUMP_PRESSURE);
my_pump.backflush_in_off_cycle = false;
} else if elapsed > config::BACKFLUSH_ON_TIME
&& !my_pump.backflush_in_off_cycle
{
my_pump.backflush_in_off_cycle = true;
my_pump.close_valve();
my_pump.set_pressure(0.0);
}
}
_ => {}
}

Expand Down Expand Up @@ -168,8 +195,12 @@ where
self.close_valve();
self.set_pressure(config::MAX_PUMP_PRESSURE);
}
Message::Backflush(_) => {
todo!();
Message::Backflush => {
self.state = State::Backflush;
self.backflush_cycle_start = Instant::now();
self.backflush_in_off_cycle = false;
self.open_valve();
self.set_pressure(config::MAX_PUMP_PRESSURE);
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ pub const BOILER_PWM_PERIOD: Duration = Duration::from_millis(1000);
pub const PUMP_PWM_PERIOD: Duration = Duration::from_millis(100);
pub const OUTPUT_POLL_INTERVAL: Duration = Duration::from_millis(100);

pub const BACKFLUSH_ON_TIME: Duration = Duration::from_secs(10);
pub const BACKFLUSH_OFF_TIME: Duration = Duration::from_secs(10);

pub const PT_100_CALIBRATION_FACTOR: f32 = 2.209;

pub const LED_COUNT: usize = 32;
Expand Down
5 changes: 4 additions & 1 deletion src/gpio/switch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub enum SwitchesState {
HotWater,
Steam,
AutoTune,
Backflush,
}

impl std::fmt::Display for SwitchesState {
Expand All @@ -38,7 +39,8 @@ impl std::fmt::Display for SwitchesState {
Self::Brew => "Brew",
Self::HotWater => "Hot Water",
Self::Steam => "Steam",
Self::AutoTune => "Steam",
Self::AutoTune => "Autotune",
Self::Backflush => "Backflush",
};
write!(f, "{}", state)
}
Expand All @@ -48,6 +50,7 @@ impl SwitchesState {
fn update(&self, brew: SwitchState, hot_water: SwitchState, steam: SwitchState) -> Self {
match (brew, hot_water, steam) {
(SwitchState::Active, SwitchState::Active, SwitchState::Active) => Self::AutoTune,
(SwitchState::Active, SwitchState::Active, SwitchState::Released) => Self::AutoTune,
(SwitchState::Active, _, _) => Self::Brew,
(_, SwitchState::Active, _) => Self::HotWater,
(_, _, SwitchState::Active) => Self::Steam,
Expand Down
10 changes: 10 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ fn main() -> Result<()> {
SwitchesState::Idle => {
log::info!("Switched to idle");
boiler.send_message(BoilerMessage::SetMode(components::boiler::Mode::Off));
pump.turn_off();
}
SwitchesState::Brew => {
log::info!("Switched to brew");
Expand All @@ -242,17 +243,26 @@ fn main() -> Result<()> {
log::info!("Switched to hot water");
let mode = components::boiler::Mode::Mpc { target: 94.0 };
boiler.send_message(BoilerMessage::SetMode(mode));
pump.turn_on_for_hot_water();
}
SwitchesState::Steam => {
log::info!("Switched to steam");
let mode = components::boiler::Mode::BangBang {
upper_threshold: 140.0,
lower_threshold: 120.0,
};
pump.turn_off();
boiler.send_message(BoilerMessage::SetMode(mode));
}
SwitchesState::Backflush => {
log::info!("Switched to backflush");
let mode = components::boiler::Mode::Mpc { target: 70.0 };
boiler.send_message(BoilerMessage::SetMode(mode));
pump.backflush();
}
SwitchesState::AutoTune => {
log::info!("Switched to auto-tune");
pump.turn_off();
if let Err(e) = system.operational_state.lock().unwrap().transition(
crate::state_machines::operational_fsm::Transitions::StartAutoTune,
) {
Expand Down
4 changes: 2 additions & 2 deletions todo.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
- [ ] Review timer implementation (https://docs.esp-rs.org/esp-idf-svc/esp_idf_svc/timer/index.html)
- [ ] Tidy up the state stuff
- [ ] Tidy up the board stuff
- [X] Tidy up the board stuff
- [ ] Add periodic status reporting
- [ ] Add Availability
- [ ] Runtime WIFI config
- [ ] OTA
- [ ] consolidate the types
- [X] consolidate the types
- [ ] Fix up all the state machines/system state/operation state
- [ ] Maybe an observer patter?
- [X] Try out mspc channels again
Expand Down

0 comments on commit 0979c10

Please sign in to comment.