add pump current configuration

This commit is contained in:
2025-06-26 02:39:32 +02:00
parent 577c38d026
commit 5d91daf23d
12 changed files with 492 additions and 275 deletions

View File

@@ -13,7 +13,7 @@ use embedded_hal::digital::OutputPin;
use embedded_hal_bus::i2c::MutexDevice;
use esp_idf_hal::delay::Delay;
use esp_idf_hal::gpio::{AnyInputPin, IOPin, InputOutput, Output, PinDriver, Pull};
use esp_idf_hal::i2c::{I2cDriver, I2cError};
use esp_idf_hal::i2c::I2cDriver;
use esp_idf_hal::pcnt::{
PcntChannel, PcntChannelConfig, PcntControlMode, PcntCountMode, PcntDriver, PinIndex,
};
@@ -21,7 +21,6 @@ use esp_idf_sys::{gpio_hold_dis, gpio_hold_en};
use ina219::address::{Address, Pin};
use ina219::calibration::UnCalibrated;
use ina219::configuration::{Configuration, OperatingMode};
use ina219::errors::InitializationError;
use ina219::SyncIna219;
use measurements::{Current, Resistance, Voltage};
use pca9535::{GPIOBank, Pca9535Immediate, StandardExpanderInterface};
@@ -125,6 +124,7 @@ pub struct V4<'a> {
light: PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, InputOutput>,
general_fault: PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, InputOutput>,
pump_expander: Pca9535Immediate<MutexDevice<'a, I2cDriver<'a>>>,
pump_ina: Option<SyncIna219<MutexDevice<'a, I2cDriver<'a>>, UnCalibrated>>,
sensor_expander: Pca9535Immediate<MutexDevice<'a, I2cDriver<'a>>>,
extra1: PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, Output>,
extra2: PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, Output>,
@@ -208,7 +208,7 @@ pub(crate) fn create_v4(
let _ = sensor_expander.pin_set_low(GPIOBank::Bank1, pin);
}
let mut mppt_ina = SyncIna219::new(
let mppt_ina = SyncIna219::new(
MutexDevice::new(&I2C_DRIVER),
Address::from_pins(Pin::Vcc, Pin::Gnd),
);
@@ -224,15 +224,6 @@ pub(crate) fn create_v4(
operating_mode: Default::default(),
})?;
//TODO this is probably already done until we are ready first time?, maybe add startup time comparison on access?
esp.delay.delay_ms(
mppt_ina
.configuration()?
.conversion_time()
.unwrap()
.as_millis() as u32,
);
Charger::SolarMpptV1 {
mppt_ina,
solar_is_day,
@@ -242,6 +233,17 @@ pub(crate) fn create_v4(
Err(_) => Charger::ErrorInit {},
};
let pump_ina = match SyncIna219::new(
MutexDevice::new(&I2C_DRIVER),
Address::from_pins(Pin::Gnd, Pin::Sda),
) {
Ok(pump_ina) => Some(pump_ina),
Err(err) => {
println!("Error creating pump ina: {:?}", err);
None
}
};
let v = V4 {
rtc_module,
esp,
@@ -250,6 +252,7 @@ pub(crate) fn create_v4(
signal_counter,
light,
general_fault,
pump_ina,
pump_expander,
sensor_expander,
config,
@@ -314,6 +317,24 @@ impl<'a> BoardInteraction<'a> for V4<'a> {
anyhow::Ok(())
}
fn pump_current(&mut self, _plant: usize) -> anyhow::Result<Current> {
//sensore is shared for all pumps, ignore plant id
match self.pump_ina.as_mut() {
None => {
bail!("pump current sensor not available");
}
Some(pump_ina) => {
let v = pump_ina.shunt_voltage().map(|v| {
let shunt_voltage = Voltage::from_microvolts(v.shunt_voltage_uv().abs() as f64);
let shut_value = Resistance::from_ohms(0.05_f64);
let current = shunt_voltage.as_volts() / shut_value.as_ohms();
Current::from_amperes(current)
})?;
Ok(v)
}
}
}
fn fault(&mut self, plant: usize, enable: bool) -> anyhow::Result<()> {
if enable {
self.pump_expander
@@ -406,13 +427,6 @@ impl<'a> BoardInteraction<'a> for V4<'a> {
unsafe { gpio_hold_en(self.general_fault.pin()) };
}
fn test_pump(&mut self, plant: usize) -> anyhow::Result<()> {
self.pump(plant, true)?;
self.esp.delay.delay_ms(30000);
self.pump(plant, false)?;
anyhow::Ok(())
}
fn test(&mut self) -> anyhow::Result<()> {
self.general_fault(true);
self.esp.delay.delay_ms(100);