fix: 电压采样每次采样10次取平均值。Close #5.

This commit is contained in:
Ivan Li 2022-09-12 10:13:59 +08:00
parent 711306f2c1
commit 2aaa85af71

View File

@ -5,7 +5,7 @@ use std::{
};
use embedded_hal::{adc::Channel, prelude::_embedded_hal_adc_OneShot};
use embedded_svc::{event_bus::Postbox};
use embedded_svc::event_bus::Postbox;
use esp_idf_hal::{
adc::{
config::{self},
@ -13,11 +13,9 @@ use esp_idf_hal::{
},
gpio::{Gpio1, Gpio2, Gpio3, Input},
};
use esp_idf_svc::{
eventloop::{
use esp_idf_svc::eventloop::{
Background, EspBackgroundEventLoop, EspEventFetchData, EspEventLoop, EspEventPostData,
EspTypedEventDeserializer, EspTypedEventSerializer, EspTypedEventSource,
},
};
use esp_idf_sys::c_types;
use log::{debug, warn};
@ -75,6 +73,8 @@ impl VoltageDetection {
anyhow::anyhow!("Failed to set GPIO 3 as analog input. {}", err)
})?;
let mut adc = PoweredAdc::new(unsafe { ADC1::new() }, config::Config::new())?;
let mut worker = worker.lock().map_err(|err| {
anyhow::anyhow!("Lock VoltageDetection Worker Failed. {}", err)
})?;
@ -83,7 +83,12 @@ impl VoltageDetection {
loop {
last_runing_at = Time::new().get_time();
match worker.read_once(&mut adapter_pin, &mut battery_pin, &mut output_pin) {
match worker.read_once(
&mut adc,
&mut adapter_pin,
&mut battery_pin,
&mut output_pin,
) {
Ok(_) => debug!(
"Adapter: {},\tBattery: {},\t Output: {}",
worker.adapter_voltage, worker.battery_voltage, worker.output_voltage
@ -107,11 +112,11 @@ impl VoltageDetection {
}
let mut delta = Time::new().get_time() - last_runing_at;
if delta >= Duration::from_millis(1000) {
if delta >= Duration::from_millis(5000) {
delta = Duration::ZERO
}
sleep(Duration::from_millis(1000) - delta);
sleep(Duration::from_millis(5000) - delta);
}
};
@ -134,11 +139,12 @@ impl VoltageDetectionWorker {
pub fn read_once(
&mut self,
adc: &mut PoweredAdc<ADC1>,
adapter_pin: &mut Gpio1<Atten6dB<ADC1>>,
battery_pin: &mut Gpio2<Atten6dB<ADC1>>,
output_pin: &mut Gpio3<Atten6dB<ADC1>>,
) -> anyhow::Result<()> {
match self.read_pin_once(adapter_pin) {
match self.read_pin_once(adc, adapter_pin) {
Ok(voltage) => {
self.adapter_voltage = ((voltage as f32) * ADAPTER_OFFSET) as u16;
}
@ -147,7 +153,7 @@ impl VoltageDetectionWorker {
}
}
match self.read_pin_once(battery_pin) {
match self.read_pin_once(adc, battery_pin) {
Ok(voltage) => {
self.battery_voltage = ((voltage as f32) * BATTERY_OFFSET) as u16;
}
@ -155,7 +161,7 @@ impl VoltageDetectionWorker {
warn!("Adapter Voltage read failed: {:?}", err);
}
}
match self.read_pin_once(output_pin) {
match self.read_pin_once(adc, output_pin) {
Ok(voltage) => {
self.output_voltage = ((voltage as f32) * OUTPUT_OFFSET) as u16;
}
@ -168,16 +174,23 @@ impl VoltageDetectionWorker {
}
pub fn read_pin_once<AN: Analog<ADC1>, PIN: Channel<AN, ID = u8>>(
&mut self,
adc: &mut PoweredAdc<ADC1>,
pin: &mut PIN,
) -> anyhow::Result<u16> {
let mut adc = PoweredAdc::new(unsafe { ADC1::new() }, config::Config::new())?;
let mut avg_voltage: u16 = 0;
for _ in 0..10 {
let voltage = adc.read(pin);
match voltage {
Ok(voltage) => anyhow::Ok(voltage),
Ok(voltage) => {
avg_voltage += voltage;
}
Err(err) => {
anyhow::bail!("Adapter Voltage read failed: {:?}", err)
}
};
sleep(Duration::from_millis(100));
}
anyhow::Ok(avg_voltage / 10)
}
}