get more functions online
This commit is contained in:
@@ -8,99 +8,114 @@ use alloc::boxed::Box;
|
||||
use async_trait::async_trait;
|
||||
use embassy_embedded_hal::shared_bus::blocking::i2c::I2cDevice;
|
||||
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
|
||||
use esp_hal::Blocking;
|
||||
use esp_hal::{twai, Blocking};
|
||||
//use embedded_hal_bus::i2c::MutexDevice;
|
||||
use crate::bail;
|
||||
use crate::hal::v4_sensor::SensorImpl;
|
||||
use crate::FatError::{FatError, FatResult};
|
||||
use esp_hal::gpio::{Flex, Level, Output, OutputConfig};
|
||||
use esp_hal::gpio::{Flex, Input, InputConfig, Level, Output, OutputConfig};
|
||||
use esp_hal::i2c::master::I2c;
|
||||
use esp_hal::twai::{EspTwaiFrame, StandardId, TwaiMode};
|
||||
use esp_println::println;
|
||||
use ina219::address::{Address, Pin};
|
||||
use ina219::calibration::UnCalibrated;
|
||||
use ina219::configuration::{Configuration, OperatingMode, Resolution};
|
||||
use ina219::SyncIna219;
|
||||
use measurements::Resistance;
|
||||
use measurements::{Current, Voltage};
|
||||
use pca9535::{GPIOBank, Pca9535Immediate, StandardExpanderInterface};
|
||||
// pub enum Charger<'a> {
|
||||
// SolarMpptV1 {
|
||||
// mppt_ina: SyncIna219<MutexDevice<'a, I2cDriver<'a>>, UnCalibrated>,
|
||||
// solar_is_day: PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, esp_idf_hal::gpio::Input>,
|
||||
// charge_indicator: PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, InputOutput>,
|
||||
// },
|
||||
// ErrorInit {},
|
||||
// }
|
||||
//
|
||||
// impl Charger<'_> {
|
||||
// pub(crate) fn power_save(&mut self) {
|
||||
// match self {
|
||||
// Charger::SolarMpptV1 { mppt_ina, .. } => {
|
||||
// let _ = mppt_ina
|
||||
// .set_configuration(Configuration {
|
||||
// reset: Default::default(),
|
||||
// bus_voltage_range: Default::default(),
|
||||
// shunt_voltage_range: Default::default(),
|
||||
// bus_resolution: Default::default(),
|
||||
// shunt_resolution: Default::default(),
|
||||
// operating_mode: OperatingMode::PowerDown,
|
||||
// })
|
||||
// .map_err(|e| {
|
||||
// log::info!(
|
||||
// "Error setting ina mppt configuration during deep sleep preparation{:?}",
|
||||
// e
|
||||
// );
|
||||
// });
|
||||
// }
|
||||
// _ => {}
|
||||
// }
|
||||
// }
|
||||
// fn set_charge_indicator(&mut self, charging: bool) -> anyhow::Result<()> {
|
||||
// match self {
|
||||
// Self::SolarMpptV1 {
|
||||
// charge_indicator, ..
|
||||
// } => {
|
||||
// charge_indicator.set_state(charging.into())?;
|
||||
// }
|
||||
// _ => {}
|
||||
// }
|
||||
// Ok(())
|
||||
// }
|
||||
//
|
||||
// fn is_day(&self) -> bool {
|
||||
// match self {
|
||||
// Charger::SolarMpptV1 { solar_is_day, .. } => solar_is_day.get_level().into(),
|
||||
// _ => true,
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// fn get_mptt_voltage(&mut self) -> anyhow::Result<Voltage> {
|
||||
// let voltage = match self {
|
||||
// Charger::SolarMpptV1 { mppt_ina, .. } => mppt_ina
|
||||
// .bus_voltage()
|
||||
// .map(|v| Voltage::from_millivolts(v.voltage_mv() as f64))?,
|
||||
// _ => {
|
||||
// bail!("hardware error during init")
|
||||
// }
|
||||
// };
|
||||
// Ok(voltage)
|
||||
// }
|
||||
//
|
||||
// fn get_mptt_current(&mut self) -> anyhow::Result<Current> {
|
||||
// let current = match self {
|
||||
// Charger::SolarMpptV1 { mppt_ina, .. } => mppt_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)
|
||||
// })?,
|
||||
// _ => {
|
||||
// bail!("hardware error during init")
|
||||
// }
|
||||
// };
|
||||
// Ok(current)
|
||||
// }
|
||||
// }
|
||||
|
||||
const MPPT_CURRENT_SHUNT_OHMS: f64 = 0.05_f64;
|
||||
const TWAI_BAUDRATE: twai::BaudRate = twai::BaudRate::B125K;
|
||||
|
||||
pub enum Charger<'a> {
|
||||
SolarMpptV1 {
|
||||
mppt_ina: SyncIna219<
|
||||
I2cDevice<'a, CriticalSectionRawMutex, I2c<'static, Blocking>>,
|
||||
UnCalibrated,
|
||||
>,
|
||||
solar_is_day: Input<'a>,
|
||||
charge_indicator: Output<'a>,
|
||||
},
|
||||
ErrorInit {},
|
||||
}
|
||||
|
||||
impl<'a> Charger<'a> {
|
||||
pub(crate) fn get_mppt_current(&mut self) -> FatResult<Current> {
|
||||
match self {
|
||||
Charger::SolarMpptV1 { mppt_ina, .. } => {
|
||||
let v = mppt_ina.shunt_voltage()?;
|
||||
let shunt_voltage = Voltage::from_microvolts(v.shunt_voltage_uv().abs() as f64);
|
||||
let shut_value = Resistance::from_ohms(MPPT_CURRENT_SHUNT_OHMS);
|
||||
let current = shunt_voltage.as_volts() / shut_value.as_ohms();
|
||||
Ok(Current::from_amperes(current))
|
||||
}
|
||||
Charger::ErrorInit { .. } => {
|
||||
bail!("hardware error during init");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn get_mptt_voltage(&mut self) -> FatResult<Voltage> {
|
||||
match self {
|
||||
Charger::SolarMpptV1 { mppt_ina, .. } => {
|
||||
let v = mppt_ina.bus_voltage()?;
|
||||
Ok(Voltage::from_millivolts(v.voltage_mv() as f64))
|
||||
}
|
||||
Charger::ErrorInit { .. } => {
|
||||
bail!("hardware error during init");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Charger<'_> {
|
||||
pub(crate) fn power_save(&mut self) {
|
||||
match self {
|
||||
Charger::SolarMpptV1 { mppt_ina, .. } => {
|
||||
let _ = mppt_ina
|
||||
.set_configuration(Configuration {
|
||||
reset: Default::default(),
|
||||
bus_voltage_range: Default::default(),
|
||||
shunt_voltage_range: Default::default(),
|
||||
bus_resolution: Default::default(),
|
||||
shunt_resolution: Default::default(),
|
||||
operating_mode: OperatingMode::PowerDown,
|
||||
})
|
||||
.map_err(|e| {
|
||||
log::info!(
|
||||
"Error setting ina mppt configuration during deep sleep preparation{:?}",
|
||||
e
|
||||
);
|
||||
});
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
fn set_charge_indicator(&mut self, charging: bool) -> FatResult<()> {
|
||||
match self {
|
||||
Self::SolarMpptV1 {
|
||||
charge_indicator, ..
|
||||
} => {
|
||||
charge_indicator.set_level(charging.into());
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn is_day(&self) -> bool {
|
||||
match self {
|
||||
Charger::SolarMpptV1 { solar_is_day, .. } => solar_is_day.is_high(),
|
||||
_ => true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct V4<'a> {
|
||||
esp: Esp<'a>,
|
||||
tank_sensor: TankSensor<'a>,
|
||||
//charger: Charger<'a>,
|
||||
charger: Charger<'a>,
|
||||
rtc_module: Box<dyn RTCModuleInteraction + Send>,
|
||||
battery_monitor: Box<dyn BatteryInteraction + Send>,
|
||||
config: PlantControllerConfig,
|
||||
@@ -109,8 +124,10 @@ pub struct V4<'a> {
|
||||
light: Output<'a>,
|
||||
general_fault: Output<'a>,
|
||||
pump_expander: Pca9535Immediate<I2cDevice<'a, CriticalSectionRawMutex, I2c<'static, Blocking>>>,
|
||||
//pump_ina: Option<SyncIna219<MutexDevice<'a, I2cDriver<'a>>, UnCalibrated>>,
|
||||
//sensor: SensorImpl<'a>,
|
||||
pump_ina: Option<
|
||||
SyncIna219<I2cDevice<'a, CriticalSectionRawMutex, I2c<'static, Blocking>>, UnCalibrated>,
|
||||
>,
|
||||
sensor: SensorImpl,
|
||||
extra1: Output<'a>,
|
||||
extra2: Output<'a>,
|
||||
}
|
||||
@@ -148,73 +165,71 @@ pub(crate) async fn create_v4(
|
||||
//flow_sensor_pin,
|
||||
//peripherals.pcnt1,
|
||||
)?;
|
||||
//
|
||||
// let mut sensor_expander = Pca9535Immediate::new(MutexDevice::new(&I2C_DRIVER), 34);
|
||||
// let sensor = match sensor_expander.pin_into_output(GPIOBank::Bank0, 0) {
|
||||
// Ok(_) => {
|
||||
// log::info!("SensorExpander answered");
|
||||
// //pulse counter version
|
||||
// let mut signal_counter = PcntDriver::new(
|
||||
// peripherals.pcnt0,
|
||||
// Some(peripherals.gpio22),
|
||||
// Option::<AnyInputPin>::None,
|
||||
// Option::<AnyInputPin>::None,
|
||||
// Option::<AnyInputPin>::None,
|
||||
// )?;
|
||||
//
|
||||
// signal_counter.channel_config(
|
||||
// PcntChannel::Channel0,
|
||||
// PinIndex::Pin0,
|
||||
// PinIndex::Pin1,
|
||||
// &PcntChannelConfig {
|
||||
// lctrl_mode: PcntControlMode::Keep,
|
||||
// hctrl_mode: PcntControlMode::Keep,
|
||||
// pos_mode: PcntCountMode::Increment,
|
||||
// neg_mode: PcntCountMode::Hold,
|
||||
// counter_h_lim: i16::MAX,
|
||||
// counter_l_lim: 0,
|
||||
// },
|
||||
// )?;
|
||||
//
|
||||
// for pin in 0..8 {
|
||||
// let _ = sensor_expander.pin_into_output(GPIOBank::Bank0, pin);
|
||||
// let _ = sensor_expander.pin_into_output(GPIOBank::Bank1, pin);
|
||||
// let _ = sensor_expander.pin_set_low(GPIOBank::Bank0, pin);
|
||||
// let _ = sensor_expander.pin_set_low(GPIOBank::Bank1, pin);
|
||||
// }
|
||||
//
|
||||
// SensorImpl::PulseCounter {
|
||||
// signal_counter,
|
||||
// sensor_expander,
|
||||
// }
|
||||
// }
|
||||
// Err(_) => {
|
||||
// log::info!("Can bus mode ");
|
||||
// let timing = can::config::Timing::B25K;
|
||||
// let config = can::config::Config::new().timing(timing);
|
||||
// let can = can::CanDriver::new(
|
||||
// peripherals.can,
|
||||
// peripherals.gpio0,
|
||||
// peripherals.gpio2,
|
||||
// &config,
|
||||
// )
|
||||
// .unwrap();
|
||||
//
|
||||
// let frame = StandardId::new(0x042).unwrap();
|
||||
// let tx_frame = Frame::new(frame, &[0, 1, 2, 3, 4, 5, 6, 7]).unwrap();
|
||||
// can.transmit(&tx_frame, 1000).unwrap();
|
||||
//
|
||||
// if let Ok(rx_frame) = can.receive(1000) {
|
||||
// log::info!("rx {:}:", rx_frame);
|
||||
// }
|
||||
// //can bus version
|
||||
// SensorImpl::CanBus { can }
|
||||
// }
|
||||
// };
|
||||
|
||||
let mut solar_is_day = Output::new(peripherals.gpio7, Level::Low, Default::default());
|
||||
let mut light = Output::new(peripherals.gpio10, Level::Low, Default::default());
|
||||
let mut charge_indicator = Output::new(peripherals.gpio3, Level::Low, Default::default());
|
||||
let sensor_expander_device = I2cDevice::new(I2C_DRIVER.get().await);
|
||||
let mut sensor_expander = Pca9535Immediate::new(sensor_expander_device, 34);
|
||||
let sensor = match sensor_expander.pin_into_output(GPIOBank::Bank0, 0) {
|
||||
Ok(_) => {
|
||||
log::info!("SensorExpander answered");
|
||||
//pulse counter version
|
||||
// let mut signal_counter = PcntDriver::new(
|
||||
// peripherals.pcnt0,
|
||||
// Some(peripherals.gpio22),
|
||||
// Option::<AnyInputPin>::None,
|
||||
// Option::<AnyInputPin>::None,
|
||||
// Option::<AnyInputPin>::None,
|
||||
// )?;
|
||||
//
|
||||
// signal_counter.channel_config(
|
||||
// PcntChannel::Channel0,
|
||||
// PinIndex::Pin0,
|
||||
// PinIndex::Pin1,
|
||||
// &PcntChannelConfig {
|
||||
// lctrl_mode: PcntControlMode::Keep,
|
||||
// hctrl_mode: PcntControlMode::Keep,
|
||||
// pos_mode: PcntCountMode::Increment,
|
||||
// neg_mode: PcntCountMode::Hold,
|
||||
// counter_h_lim: i16::MAX,
|
||||
// counter_l_lim: 0,
|
||||
// },
|
||||
// )?;
|
||||
|
||||
for pin in 0..8 {
|
||||
let _ = sensor_expander.pin_into_output(GPIOBank::Bank0, pin);
|
||||
let _ = sensor_expander.pin_into_output(GPIOBank::Bank1, pin);
|
||||
let _ = sensor_expander.pin_set_low(GPIOBank::Bank0, pin);
|
||||
let _ = sensor_expander.pin_set_low(GPIOBank::Bank1, pin);
|
||||
}
|
||||
|
||||
SensorImpl::PulseCounter {
|
||||
// signal_counter,
|
||||
sensor_expander,
|
||||
}
|
||||
}
|
||||
Err(_) => {
|
||||
log::info!("Can bus mode ");
|
||||
let mut twai_config = twai::TwaiConfiguration::new(
|
||||
peripherals.twai,
|
||||
peripherals.gpio0,
|
||||
peripherals.gpio2,
|
||||
TWAI_BAUDRATE,
|
||||
TwaiMode::Normal,
|
||||
);
|
||||
|
||||
let mut twai = twai_config.start();
|
||||
let frame = EspTwaiFrame::new(StandardId::ZERO, &[1, 2, 3]).unwrap();
|
||||
twai.transmit(&frame).unwrap();
|
||||
|
||||
let frame = twai.receive().unwrap();
|
||||
println!("Received a frame: {frame:?}");
|
||||
//can bus version
|
||||
SensorImpl::CanBus { twai }
|
||||
}
|
||||
};
|
||||
|
||||
let solar_is_day = Input::new(peripherals.gpio7, InputConfig::default());
|
||||
let light = Output::new(peripherals.gpio10, Level::Low, Default::default());
|
||||
let charge_indicator = Output::new(peripherals.gpio3, Level::Low, Default::default());
|
||||
|
||||
let pump_device = I2cDevice::new(I2C_DRIVER.get().await);
|
||||
let mut pump_expander = Pca9535Immediate::new(pump_device, 32);
|
||||
@@ -226,38 +241,53 @@ pub(crate) async fn create_v4(
|
||||
}
|
||||
|
||||
let mppt_current = I2cDevice::new(I2C_DRIVER.get().await);
|
||||
let mppt_ina = SyncIna219::new(mppt_current, Address::from_pins(Pin::Vcc, Pin::Gnd));
|
||||
let mppt_ina = match SyncIna219::new(mppt_current, Address::from_pins(Pin::Vcc, Pin::Gnd)) {
|
||||
Ok(mut ina) => {
|
||||
// Prefer higher averaging for more stable readings
|
||||
let _ = ina.set_configuration(Configuration {
|
||||
reset: Default::default(),
|
||||
bus_voltage_range: Default::default(),
|
||||
shunt_voltage_range: Default::default(),
|
||||
bus_resolution: Default::default(),
|
||||
shunt_resolution: Resolution::Avg128,
|
||||
operating_mode: Default::default(),
|
||||
});
|
||||
Some(ina)
|
||||
}
|
||||
Err(err) => {
|
||||
log::info!("Error creating mppt ina: {:?}", err);
|
||||
None
|
||||
}
|
||||
};
|
||||
|
||||
// let charger = match mppt_ina {
|
||||
// Ok(mut mppt_ina) => {
|
||||
// mppt_ina.set_configuration(Configuration {
|
||||
// reset: Default::default(),
|
||||
// bus_voltage_range: Default::default(),
|
||||
// shunt_voltage_range: Default::default(),
|
||||
// bus_resolution: Default::default(),
|
||||
// shunt_resolution: ina219::configuration::Resolution::Avg128,
|
||||
// operating_mode: Default::default(),
|
||||
// })?;
|
||||
//
|
||||
// Charger::SolarMpptV1 {
|
||||
// mppt_ina,
|
||||
// solar_is_day,
|
||||
// charge_indicator,
|
||||
// }
|
||||
// }
|
||||
// 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) => {
|
||||
// log::info!("Error creating pump ina: {:?}", err);
|
||||
// None
|
||||
// }
|
||||
// };
|
||||
let pump_current_dev = I2cDevice::new(I2C_DRIVER.get().await);
|
||||
let pump_ina = match SyncIna219::new(pump_current_dev, Address::from_pins(Pin::Gnd, Pin::Sda)) {
|
||||
Ok(ina) => Some(ina),
|
||||
Err(err) => {
|
||||
log::info!("Error creating pump ina: {:?}", err);
|
||||
None
|
||||
}
|
||||
};
|
||||
|
||||
let charger = match mppt_ina {
|
||||
Some(mut mppt_ina) => {
|
||||
mppt_ina.set_configuration(Configuration {
|
||||
reset: Default::default(),
|
||||
bus_voltage_range: Default::default(),
|
||||
shunt_voltage_range: Default::default(),
|
||||
bus_resolution: Default::default(),
|
||||
shunt_resolution: ina219::configuration::Resolution::Avg128,
|
||||
operating_mode: Default::default(),
|
||||
})?;
|
||||
|
||||
Charger::SolarMpptV1 {
|
||||
mppt_ina,
|
||||
solar_is_day,
|
||||
charge_indicator,
|
||||
}
|
||||
}
|
||||
None => Charger::ErrorInit {},
|
||||
};
|
||||
|
||||
let v = V4 {
|
||||
rtc_module,
|
||||
@@ -270,10 +300,11 @@ pub(crate) async fn create_v4(
|
||||
pump_expander,
|
||||
config,
|
||||
battery_monitor,
|
||||
//charger,
|
||||
pump_ina,
|
||||
charger,
|
||||
extra1,
|
||||
extra2,
|
||||
//sensor,
|
||||
sensor,
|
||||
};
|
||||
Ok(Box::new(v))
|
||||
}
|
||||
@@ -336,22 +367,27 @@ impl<'a> BoardInteraction<'a> for V4<'a> {
|
||||
}
|
||||
|
||||
async fn pump_current(&mut self, _plant: usize) -> Result<Current, FatError> {
|
||||
bail!("not implemented");
|
||||
// //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)
|
||||
// }
|
||||
// }
|
||||
// sensor 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_err(|e| FatError::String {
|
||||
error: alloc::format!("{:?}", e),
|
||||
})
|
||||
.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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn fault(&mut self, plant: usize, enable: bool) -> FatResult<()> {
|
||||
@@ -419,12 +455,10 @@ impl<'a> BoardInteraction<'a> for V4<'a> {
|
||||
}
|
||||
|
||||
async fn get_mptt_voltage(&mut self) -> Result<Voltage, FatError> {
|
||||
bail!("not implemented");
|
||||
//self.charger.get_mptt_voltage()
|
||||
self.charger.get_mptt_voltage()
|
||||
}
|
||||
|
||||
async fn get_mptt_current(&mut self) -> Result<Current, FatError> {
|
||||
bail!("not implemented");
|
||||
//self.charger.get_mptt_current()
|
||||
self.charger.get_mppt_current()
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user