From e4ce0ce186b68497cfb14a400410fdd38aa93abc Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Thu, 19 Jan 2023 14:04:50 -0800 Subject: [PATCH] altos/easymotor: Move pressure conversion code to ao_motor_flight.c This inline function uses a pile of constants which aren't defined in ao_pins.h, so move it to ao_motor_flight.c where it is used Signed-off-by: Keith Packard --- src/easymotor-v3/ao_pins.h | 28 +++------------------------- src/kernel/ao_motor_flight.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 25 deletions(-) diff --git a/src/easymotor-v3/ao_pins.h b/src/easymotor-v3/ao_pins.h index b3be803e..a0e40bd7 100644 --- a/src/easymotor-v3/ao_pins.h +++ b/src/easymotor-v3/ao_pins.h @@ -156,36 +156,14 @@ typedef int16_t motor_pressure_t; -/* want about 50psi, or 344kPa */ +/* want about 50psi, or 344kPa for boost and 30psi for coast */ #define AO_FULL_SCALE_PRESSURE 11031612 /* 1600psi */ #define AO_BOOST_DETECT_PRESSURE 344000 /* 50psi */ #define AO_QUIET_DETECT_PRESSURE 207000 /* 30psi */ -static inline int16_t ao_delta_pressure_to_adc(uint32_t pressure) -{ - static const double volts_base = 0.5; - static const double volts_max = 4.5; - - /* Compute change in voltage from the sensor */ - double volts = (double) pressure / AO_FULL_SCALE_PRESSURE * (volts_max - volts_base); - - /* voltage divider in front of the ADC input to decivolts */ - double adc_dv = volts * 10 * 10.0/15.6; - - /* convert to ADC output value */ - double adc = adc_dv * AO_ADC_MAX / AO_ADC_REFERENCE_DV; - - if (adc > AO_ADC_MAX) - adc = AO_ADC_MAX; - if (adc < 0) - adc = 0; - - return (int16_t) adc; -} - -#define AO_BOOST_DETECT ao_delta_pressure_to_adc(AO_BOOST_DETECT_PRESSURE) -#define AO_QUIET_DETECT ao_delta_pressure_to_adc(AO_QUIET_DETECT_PRESSURE) +#define AO_PRESSURE_VOLTS_BASE 0.5 +#define AO_PRESSURE_VOLTS_MAX 4.5 struct ao_adc { int16_t v_batt; diff --git a/src/kernel/ao_motor_flight.c b/src/kernel/ao_motor_flight.c index f47f59b3..6fa68831 100644 --- a/src/kernel/ao_motor_flight.c +++ b/src/kernel/ao_motor_flight.c @@ -52,6 +52,35 @@ static AO_TICK_TYPE ao_interval_end; uint8_t ao_flight_force_idle; +/* Compute ADC value change given a defined pressure change in Pa */ + +static inline int16_t +ao_delta_pressure_to_adc(uint32_t pressure) +{ + static const double volts_base = AO_PRESSURE_VOLTS_BASE; + static const double volts_max = AO_PRESSURE_VOLTS_MAX; + + /* Compute change in voltage from the sensor */ + double volts = (double) pressure / AO_FULL_SCALE_PRESSURE * (volts_max - volts_base); + + /* voltage divider in front of the ADC input to decivolts */ + double adc_dv = volts * (10.0 * (double) AO_PRESSURE_DIV_MINUS / + ((double) AO_PRESSURE_DIV_PLUS + (double) AO_PRESSURE_DIV_MINUS)); + + /* convert to ADC output value */ + double adc = adc_dv * AO_ADC_MAX / AO_ADC_REFERENCE_DV; + + if (adc > AO_ADC_MAX) + adc = AO_ADC_MAX; + if (adc < 0) + adc = 0; + + return (int16_t) adc; +} + +#define AO_BOOST_DETECT ao_delta_pressure_to_adc(AO_BOOST_DETECT_PRESSURE) +#define AO_QUIET_DETECT ao_delta_pressure_to_adc(AO_QUIET_DETECT_PRESSURE) + /* * Landing is detected by getting constant readings from pressure sensor * for a fairly long time (AO_INTERVAL_TICKS), along with the max being -- 2.30.2