Compare commits
No commits in common. "50b2e994ca624769b301bb265e7ff964c56d5339" and "d650358bab045cd7628523d79ae4b16407bc561d" have entirely different histories.
50b2e994ca
...
d650358bab
@ -18281,7 +18281,7 @@
|
||||
(type default)
|
||||
)
|
||||
(layer "F.SilkS")
|
||||
(uuid "46d24a82-3182-48c8-8e4a-4cd27dd2c4e0")
|
||||
(uuid "45f4b7ae-d94d-46a8-b133-6c94ff08cebe")
|
||||
)
|
||||
(fp_line
|
||||
(start -45 24)
|
||||
@ -18291,7 +18291,7 @@
|
||||
(type default)
|
||||
)
|
||||
(layer "F.SilkS")
|
||||
(uuid "1acd392a-edf0-453a-b4ea-80c286d57f01")
|
||||
(uuid "3f1ebb3a-e120-48a5-a2b3-0f3991ad318a")
|
||||
)
|
||||
(fp_line
|
||||
(start -44 24)
|
||||
@ -18301,7 +18301,7 @@
|
||||
(type default)
|
||||
)
|
||||
(layer "F.SilkS")
|
||||
(uuid "b21db6c0-4242-449b-87d0-a7705aa786df")
|
||||
(uuid "49c6ddd0-2438-484d-bdc7-b184dd56e005")
|
||||
)
|
||||
(fp_line
|
||||
(start 41 -18)
|
||||
@ -18311,7 +18311,7 @@
|
||||
(type default)
|
||||
)
|
||||
(layer "F.SilkS")
|
||||
(uuid "227b10d5-7b48-413f-84d8-72e74d474200")
|
||||
(uuid "3e55468a-fc5a-4912-8104-b18b5f9c809e")
|
||||
)
|
||||
(fp_line
|
||||
(start 41 24)
|
||||
@ -18321,7 +18321,7 @@
|
||||
(type default)
|
||||
)
|
||||
(layer "F.SilkS")
|
||||
(uuid "cbc4f5c3-2761-4d98-86ed-5202a0b3f2e9")
|
||||
(uuid "8ed77c24-2a0f-4e95-9927-23a96ccc1224")
|
||||
)
|
||||
(fp_text user "${REFERENCE}"
|
||||
(at -43 -16 90)
|
||||
@ -18424,8 +18424,8 @@
|
||||
)
|
||||
(pad "9" thru_hole rect
|
||||
(at 39.5 22.5)
|
||||
(size 1.7 1.7)
|
||||
(drill 1)
|
||||
(size 0.85 0.85)
|
||||
(drill 0.5)
|
||||
(layers "*.Cu" "*.Mask")
|
||||
(remove_unused_layers no)
|
||||
(net 48 "GND")
|
||||
@ -18435,8 +18435,8 @@
|
||||
)
|
||||
(pad "10" thru_hole rect
|
||||
(at -0.5 15)
|
||||
(size 1.7 1.7)
|
||||
(drill 1)
|
||||
(size 0.85 0.85)
|
||||
(drill 0.5)
|
||||
(layers "*.Cu" "*.Mask")
|
||||
(remove_unused_layers no)
|
||||
(net 48 "GND")
|
||||
@ -18446,8 +18446,8 @@
|
||||
)
|
||||
(pad "11" thru_hole rect
|
||||
(at -43 22.5)
|
||||
(size 1.7 1.7)
|
||||
(drill 1)
|
||||
(size 0.85 0.85)
|
||||
(drill 0.5)
|
||||
(layers "*.Cu" "*.Mask")
|
||||
(remove_unused_layers no)
|
||||
(net 48 "GND")
|
||||
@ -18457,8 +18457,8 @@
|
||||
)
|
||||
(pad "12" thru_hole rect
|
||||
(at -0.5 -12)
|
||||
(size 1.7 1.7)
|
||||
(drill 1)
|
||||
(size 0.85 0.85)
|
||||
(drill 0.5)
|
||||
(layers "*.Cu" "*.Mask")
|
||||
(remove_unused_layers no)
|
||||
(net 48 "GND")
|
||||
@ -18468,8 +18468,8 @@
|
||||
)
|
||||
(pad "13" thru_hole rect
|
||||
(at 39.5 -16)
|
||||
(size 1.7 1.7)
|
||||
(drill 1)
|
||||
(size 0.85 0.85)
|
||||
(drill 0.5)
|
||||
(layers "*.Cu" "*.Mask")
|
||||
(remove_unused_layers no)
|
||||
(net 48 "GND")
|
||||
@ -24623,18 +24623,6 @@
|
||||
(layer "Edge.Cuts")
|
||||
(uuid "cd80f150-3b94-43f9-b21f-3c1636daec3c")
|
||||
)
|
||||
(gr_text "GND Can+ Can- Prb SCL SDA VBat 3V3"
|
||||
(at 185.08 95.67 90)
|
||||
(layer "F.SilkS")
|
||||
(uuid "01f6cf85-f52a-4d4b-9dba-782fa63caa6e")
|
||||
(effects
|
||||
(font
|
||||
(size 0.9 0.6)
|
||||
(thickness 0.15)
|
||||
)
|
||||
(justify left bottom)
|
||||
)
|
||||
)
|
||||
(gr_text "3V3 S GND"
|
||||
(at 171.38 93.67 90)
|
||||
(layer "F.SilkS")
|
||||
@ -24746,7 +24734,7 @@
|
||||
(justify left bottom)
|
||||
)
|
||||
)
|
||||
(gr_text "PlantCtrl V4.0\nBackplane V1.3"
|
||||
(gr_text "PlantCtrl V4.0\nBackplane V1.2"
|
||||
(at 224.18 89.87 0)
|
||||
(layer "F.SilkS")
|
||||
(uuid "5ed408bf-a427-4794-bcf8-6f61a0d34323")
|
||||
|
@ -86,7 +86,6 @@ pca9535 = { version = "2.0.0", features = ["std"] }
|
||||
ina219 = { version = "0.2.0", features = ["std"] }
|
||||
embedded-storage = "=0.3.1"
|
||||
ekv = "1.0.0"
|
||||
embedded-can = "0.4.1"
|
||||
|
||||
|
||||
[patch.crates-io]
|
||||
|
@ -51,7 +51,6 @@ pub(crate) fn create_initial_board(
|
||||
config: PlantControllerConfig,
|
||||
esp: Esp<'static>,
|
||||
) -> Result<Box<dyn BoardInteraction<'static> + Send>> {
|
||||
println!("Start initial");
|
||||
let mut general_fault = PinDriver::input_output(free_pins.gpio6.downgrade())?;
|
||||
general_fault.set_pull(Pull::Floating)?;
|
||||
general_fault.set_low()?;
|
||||
|
@ -5,7 +5,6 @@ mod rtc;
|
||||
mod v3_hal;
|
||||
mod v4_hal;
|
||||
mod water;
|
||||
mod v4_sensor;
|
||||
|
||||
use crate::hal::rtc::{DS3231Module, RTCModuleInteraction};
|
||||
use crate::hal::water::TankSensor;
|
||||
@ -48,12 +47,11 @@ use once_cell::sync::Lazy;
|
||||
use std::result::Result::Ok as OkStd;
|
||||
use std::sync::Mutex;
|
||||
use std::time::Duration;
|
||||
use esp_idf_hal::can::CAN;
|
||||
use esp_idf_hal::pcnt::PCNT1;
|
||||
|
||||
//Only support for 8 right now!
|
||||
pub const PLANT_COUNT: usize = 8;
|
||||
|
||||
const REPEAT_MOIST_MEASURE: usize = 1;
|
||||
|
||||
const TANK_MULTI_SAMPLE: usize = 11;
|
||||
|
||||
@ -159,7 +157,6 @@ pub struct FreePeripherals {
|
||||
pub pcnt0: PCNT0,
|
||||
pub pcnt1: PCNT1,
|
||||
pub adc1: ADC1,
|
||||
pub can: CAN,
|
||||
}
|
||||
|
||||
impl PlantHal {
|
||||
@ -189,7 +186,6 @@ impl PlantHal {
|
||||
boot_button.set_pull(Pull::Floating)?;
|
||||
|
||||
let free_pins = FreePeripherals {
|
||||
can: peripherals.can,
|
||||
adc1: peripherals.adc1,
|
||||
pcnt0: peripherals.pcnt0,
|
||||
pcnt1: peripherals.pcnt1,
|
||||
|
@ -1,7 +1,7 @@
|
||||
use crate::hal::rtc::RTCModuleInteraction;
|
||||
use crate::hal::water::TankSensor;
|
||||
use crate::hal::{
|
||||
deep_sleep, BoardInteraction, FreePeripherals, Sensor, PLANT_COUNT,
|
||||
deep_sleep, BoardInteraction, FreePeripherals, Sensor, PLANT_COUNT, REPEAT_MOIST_MEASURE,
|
||||
};
|
||||
use crate::log::{log, LogMessage};
|
||||
use crate::{
|
||||
@ -64,9 +64,6 @@ const FAULT_4: usize = 21;
|
||||
const FAULT_1: usize = 22;
|
||||
const FAULT_2: usize = 23;
|
||||
|
||||
const REPEAT_MOIST_MEASURE: usize = 1;
|
||||
|
||||
|
||||
pub struct V3<'a> {
|
||||
config: PlantControllerConfig,
|
||||
battery_monitor: Box<dyn BatteryInteraction + Send>,
|
||||
@ -94,7 +91,6 @@ pub(crate) fn create_v3(
|
||||
battery_monitor: Box<dyn BatteryInteraction + Send>,
|
||||
rtc_module: Box<dyn RTCModuleInteraction + Send>,
|
||||
) -> Result<Box<dyn BoardInteraction<'static> + Send>> {
|
||||
println!("Start v3");
|
||||
let mut clock = PinDriver::input_output(peripherals.gpio15.downgrade())?;
|
||||
clock.set_pull(Pull::Floating)?;
|
||||
let mut latch = PinDriver::input_output(peripherals.gpio3.downgrade())?;
|
||||
|
@ -2,18 +2,18 @@ use crate::config::PlantControllerConfig;
|
||||
use crate::hal::battery::BatteryInteraction;
|
||||
use crate::hal::esp::Esp;
|
||||
use crate::hal::rtc::RTCModuleInteraction;
|
||||
use crate::hal::v4_sensor::SensorImpl;
|
||||
use crate::hal::v4_sensor::SensorInteraction;
|
||||
use crate::hal::water::TankSensor;
|
||||
use crate::hal::{
|
||||
deep_sleep, BoardInteraction, FreePeripherals, Sensor, I2C_DRIVER, PLANT_COUNT
|
||||
deep_sleep, BoardInteraction, FreePeripherals, Sensor, I2C_DRIVER, PLANT_COUNT,
|
||||
REPEAT_MOIST_MEASURE,
|
||||
};
|
||||
use crate::log::{log, LogMessage};
|
||||
use anyhow::bail;
|
||||
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,
|
||||
};
|
||||
@ -23,13 +23,15 @@ use ina219::calibration::UnCalibrated;
|
||||
use ina219::configuration::{Configuration, OperatingMode};
|
||||
use ina219::SyncIna219;
|
||||
use measurements::{Current, Resistance, Voltage};
|
||||
use pca9535::{ExpanderError, GPIOBank, Pca9535Immediate, StandardExpanderInterface};
|
||||
use pca9535::{GPIOBank, Pca9535Immediate, StandardExpanderInterface};
|
||||
use std::result::Result::Ok as OkStd;
|
||||
use embedded_can::nb::Can;
|
||||
use embedded_can::Frame;
|
||||
use embedded_can::StandardId;
|
||||
use esp_idf_hal::prelude::*;
|
||||
use esp_idf_hal::can;
|
||||
|
||||
const MS0: u8 = 1_u8;
|
||||
const MS1: u8 = 0_u8;
|
||||
const MS2: u8 = 3_u8;
|
||||
const MS3: u8 = 4_u8;
|
||||
const MS4: u8 = 2_u8;
|
||||
const SENSOR_ON: u8 = 5_u8;
|
||||
|
||||
pub enum Charger<'a> {
|
||||
SolarMpptV1 {
|
||||
@ -117,13 +119,13 @@ pub struct V4<'a> {
|
||||
rtc_module: Box<dyn RTCModuleInteraction + Send>,
|
||||
battery_monitor: Box<dyn BatteryInteraction + Send>,
|
||||
config: PlantControllerConfig,
|
||||
|
||||
signal_counter: PcntDriver<'a>,
|
||||
awake: PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, Output>,
|
||||
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: SensorImpl<'a>,
|
||||
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>,
|
||||
}
|
||||
@ -135,7 +137,6 @@ pub(crate) fn create_v4(
|
||||
battery_monitor: Box<dyn BatteryInteraction + Send>,
|
||||
rtc_module: Box<dyn RTCModuleInteraction + Send>,
|
||||
) -> anyhow::Result<Box<dyn BoardInteraction<'static> + Send + 'static>> {
|
||||
println!("Start v4");
|
||||
let mut awake = PinDriver::output(peripherals.gpio21.downgrade())?;
|
||||
awake.set_high()?;
|
||||
|
||||
@ -162,11 +163,6 @@ pub(crate) fn create_v4(
|
||||
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(_) => {
|
||||
println!("SensorExpander answered");
|
||||
//pulse counter version
|
||||
let mut signal_counter = PcntDriver::new(
|
||||
peripherals.pcnt0,
|
||||
Some(peripherals.gpio22),
|
||||
@ -189,42 +185,6 @@ pub(crate) fn create_v4(
|
||||
},
|
||||
)?;
|
||||
|
||||
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(_) => {
|
||||
println!("Can bus mode ");
|
||||
let timing = can::config::Timing::B25K;
|
||||
let config = can::config::Config::new().timing(timing);
|
||||
let mut 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) {
|
||||
println!("rx {:}:", rx_frame);
|
||||
}
|
||||
//can bus version
|
||||
SensorImpl::CanBus {
|
||||
can
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
let mut solar_is_day = PinDriver::input(peripherals.gpio7.downgrade())?;
|
||||
solar_is_day.set_pull(Pull::Floating)?;
|
||||
|
||||
@ -243,7 +203,13 @@ pub(crate) fn create_v4(
|
||||
let _ = pump_expander.pin_set_low(GPIOBank::Bank1, pin);
|
||||
}
|
||||
|
||||
|
||||
let mut sensor_expander = Pca9535Immediate::new(MutexDevice::new(&I2C_DRIVER), 34);
|
||||
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);
|
||||
}
|
||||
|
||||
let mppt_ina = SyncIna219::new(
|
||||
MutexDevice::new(&I2C_DRIVER),
|
||||
@ -286,16 +252,17 @@ pub(crate) fn create_v4(
|
||||
esp,
|
||||
awake,
|
||||
tank_sensor,
|
||||
signal_counter,
|
||||
light,
|
||||
general_fault,
|
||||
pump_ina,
|
||||
pump_expander,
|
||||
sensor_expander,
|
||||
config,
|
||||
battery_monitor,
|
||||
charger,
|
||||
extra1,
|
||||
extra2,
|
||||
sensor,
|
||||
};
|
||||
Ok(Box::new(v))
|
||||
}
|
||||
@ -383,7 +350,78 @@ impl<'a> BoardInteraction<'a> for V4<'a> {
|
||||
}
|
||||
|
||||
fn measure_moisture_hz(&mut self, plant: usize, sensor: Sensor) -> anyhow::Result<f32> {
|
||||
self.sensor.measure_moisture_hz(plant, sensor)
|
||||
let mut results = [0_f32; REPEAT_MOIST_MEASURE];
|
||||
for repeat in 0..REPEAT_MOIST_MEASURE {
|
||||
self.signal_counter.counter_pause()?;
|
||||
self.signal_counter.counter_clear()?;
|
||||
|
||||
//Disable all
|
||||
self.sensor_expander.pin_set_high(GPIOBank::Bank0, MS4)?;
|
||||
|
||||
let sensor_channel = match sensor {
|
||||
Sensor::A => plant as u32,
|
||||
Sensor::B => (15 - plant) as u32,
|
||||
};
|
||||
|
||||
let is_bit_set = |b: u8| -> bool { sensor_channel & (1 << b) != 0 };
|
||||
if is_bit_set(0) {
|
||||
self.sensor_expander.pin_set_high(GPIOBank::Bank0, MS0)?;
|
||||
} else {
|
||||
self.sensor_expander.pin_set_low(GPIOBank::Bank0, MS0)?;
|
||||
}
|
||||
if is_bit_set(1) {
|
||||
self.sensor_expander.pin_set_high(GPIOBank::Bank0, MS1)?;
|
||||
} else {
|
||||
self.sensor_expander.pin_set_low(GPIOBank::Bank0, MS1)?;
|
||||
}
|
||||
if is_bit_set(2) {
|
||||
self.sensor_expander.pin_set_high(GPIOBank::Bank0, MS2)?;
|
||||
} else {
|
||||
self.sensor_expander.pin_set_low(GPIOBank::Bank0, MS2)?;
|
||||
}
|
||||
if is_bit_set(3) {
|
||||
self.sensor_expander.pin_set_high(GPIOBank::Bank0, MS3)?;
|
||||
} else {
|
||||
self.sensor_expander.pin_set_low(GPIOBank::Bank0, MS3)?;
|
||||
}
|
||||
|
||||
self.sensor_expander.pin_set_low(GPIOBank::Bank0, MS4)?;
|
||||
self.sensor_expander
|
||||
.pin_set_high(GPIOBank::Bank0, SENSOR_ON)?;
|
||||
|
||||
let delay = Delay::new_default();
|
||||
let measurement = 100; // TODO what is this scaling factor? what is its purpose?
|
||||
let factor = 1000f32 / measurement as f32;
|
||||
|
||||
//give some time to stabilize
|
||||
delay.delay_ms(10);
|
||||
self.signal_counter.counter_resume()?;
|
||||
delay.delay_ms(measurement);
|
||||
self.signal_counter.counter_pause()?;
|
||||
self.sensor_expander.pin_set_high(GPIOBank::Bank0, MS4)?;
|
||||
self.sensor_expander
|
||||
.pin_set_low(GPIOBank::Bank0, SENSOR_ON)?;
|
||||
self.sensor_expander.pin_set_low(GPIOBank::Bank0, MS0)?;
|
||||
self.sensor_expander.pin_set_low(GPIOBank::Bank0, MS1)?;
|
||||
self.sensor_expander.pin_set_low(GPIOBank::Bank0, MS2)?;
|
||||
self.sensor_expander.pin_set_low(GPIOBank::Bank0, MS3)?;
|
||||
delay.delay_ms(10);
|
||||
let unscaled = self.signal_counter.get_counter_value()? as i32;
|
||||
let hz = unscaled as f32 * factor;
|
||||
log(
|
||||
LogMessage::RawMeasure,
|
||||
unscaled as u32,
|
||||
hz as u32,
|
||||
&plant.to_string(),
|
||||
&format!("{sensor:?}"),
|
||||
);
|
||||
results[repeat] = hz;
|
||||
}
|
||||
results.sort_by(|a, b| a.partial_cmp(b).unwrap()); // floats don't seem to implement total_ord
|
||||
|
||||
let mid = results.len() / 2;
|
||||
let median = results[mid];
|
||||
anyhow::Ok(median)
|
||||
}
|
||||
|
||||
fn general_fault(&mut self, enable: bool) {
|
||||
|
@ -1,115 +0,0 @@
|
||||
use embedded_hal_bus::i2c::MutexDevice;
|
||||
use esp_idf_hal::can::CanDriver;
|
||||
use esp_idf_hal::delay::Delay;
|
||||
use esp_idf_hal::i2c::I2cDriver;
|
||||
use esp_idf_hal::pcnt::PcntDriver;
|
||||
use pca9535::{GPIOBank, Pca9535Immediate, StandardExpanderInterface};
|
||||
use crate::hal::Sensor;
|
||||
use crate::log::{log, LogMessage};
|
||||
|
||||
const REPEAT_MOIST_MEASURE: usize = 10;
|
||||
|
||||
pub trait SensorInteraction {
|
||||
fn measure_moisture_hz(&mut self, plant: usize, sensor: Sensor) -> anyhow::Result<f32>;
|
||||
}
|
||||
|
||||
const MS0: u8 = 1_u8;
|
||||
const MS1: u8 = 0_u8;
|
||||
const MS2: u8 = 3_u8;
|
||||
const MS3: u8 = 4_u8;
|
||||
const MS4: u8 = 2_u8;
|
||||
const SENSOR_ON: u8 = 5_u8;
|
||||
|
||||
pub enum SensorImpl<'a> {
|
||||
PulseCounter{
|
||||
signal_counter: PcntDriver<'a>,
|
||||
sensor_expander: Pca9535Immediate<MutexDevice<'a, I2cDriver<'a>>>,
|
||||
},
|
||||
CanBus{
|
||||
can: CanDriver<'a>
|
||||
}
|
||||
}
|
||||
|
||||
impl SensorInteraction for SensorImpl<'_> {
|
||||
fn measure_moisture_hz(&mut self, plant: usize, sensor: Sensor) -> anyhow::Result<f32> {
|
||||
match self {
|
||||
SensorImpl::PulseCounter { signal_counter, sensor_expander, .. } => {
|
||||
let mut results = [0_f32; REPEAT_MOIST_MEASURE];
|
||||
for repeat in 0..REPEAT_MOIST_MEASURE {
|
||||
signal_counter.counter_pause()?;
|
||||
signal_counter.counter_clear()?;
|
||||
|
||||
//Disable all
|
||||
sensor_expander.pin_set_high(GPIOBank::Bank0, MS4)?;
|
||||
|
||||
let sensor_channel = match sensor {
|
||||
Sensor::A => plant as u32,
|
||||
Sensor::B => (15 - plant) as u32,
|
||||
};
|
||||
|
||||
let is_bit_set = |b: u8| -> bool { sensor_channel & (1 << b) != 0 };
|
||||
if is_bit_set(0) {
|
||||
sensor_expander.pin_set_high(GPIOBank::Bank0, MS0)?;
|
||||
} else {
|
||||
sensor_expander.pin_set_low(GPIOBank::Bank0, MS0)?;
|
||||
}
|
||||
if is_bit_set(1) {
|
||||
sensor_expander.pin_set_high(GPIOBank::Bank0, MS1)?;
|
||||
} else {
|
||||
sensor_expander.pin_set_low(GPIOBank::Bank0, MS1)?;
|
||||
}
|
||||
if is_bit_set(2) {
|
||||
sensor_expander.pin_set_high(GPIOBank::Bank0, MS2)?;
|
||||
} else {
|
||||
sensor_expander.pin_set_low(GPIOBank::Bank0, MS2)?;
|
||||
}
|
||||
if is_bit_set(3) {
|
||||
sensor_expander.pin_set_high(GPIOBank::Bank0, MS3)?;
|
||||
} else {
|
||||
sensor_expander.pin_set_low(GPIOBank::Bank0, MS3)?;
|
||||
}
|
||||
|
||||
sensor_expander.pin_set_low(GPIOBank::Bank0, MS4)?;
|
||||
sensor_expander
|
||||
.pin_set_high(GPIOBank::Bank0, SENSOR_ON)?;
|
||||
|
||||
let delay = Delay::new_default();
|
||||
let measurement = 100; // TODO what is this scaling factor? what is its purpose?
|
||||
let factor = 1000f32 / measurement as f32;
|
||||
|
||||
//give some time to stabilize
|
||||
delay.delay_ms(10);
|
||||
signal_counter.counter_resume()?;
|
||||
delay.delay_ms(measurement);
|
||||
signal_counter.counter_pause()?;
|
||||
sensor_expander.pin_set_high(GPIOBank::Bank0, MS4)?;
|
||||
sensor_expander
|
||||
.pin_set_low(GPIOBank::Bank0, SENSOR_ON)?;
|
||||
sensor_expander.pin_set_low(GPIOBank::Bank0, MS0)?;
|
||||
sensor_expander.pin_set_low(GPIOBank::Bank0, MS1)?;
|
||||
sensor_expander.pin_set_low(GPIOBank::Bank0, MS2)?;
|
||||
sensor_expander.pin_set_low(GPIOBank::Bank0, MS3)?;
|
||||
delay.delay_ms(10);
|
||||
let unscaled = signal_counter.get_counter_value()? as i32;
|
||||
let hz = unscaled as f32 * factor;
|
||||
log(
|
||||
LogMessage::RawMeasure,
|
||||
unscaled as u32,
|
||||
hz as u32,
|
||||
&plant.to_string(),
|
||||
&format!("{sensor:?}"),
|
||||
);
|
||||
results[repeat] = hz;
|
||||
}
|
||||
results.sort_by(|a, b| a.partial_cmp(b).unwrap()); // floats don't seem to implement total_ord
|
||||
|
||||
let mid = results.len() / 2;
|
||||
let median = results[mid];
|
||||
anyhow::Ok(median)
|
||||
}
|
||||
SensorImpl::CanBus { .. } => {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user