From 28e4cb24e0f0ee5abf66c5a0466edfd0e31f4df0 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Thu, 19 Jan 2023 13:20:56 -0800 Subject: [PATCH] easymotor-v3: Fix pressure to adc conversion to use reference voltage Wasn't taking reference voltage into account, which meant the computed values were 3.3 times too high. Signed-off-by: Keith Packard --- src/easymotor-v3/ao_pins.h | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/easymotor-v3/ao_pins.h b/src/easymotor-v3/ao_pins.h index 5cc95eb9..b3be803e 100644 --- a/src/easymotor-v3/ao_pins.h +++ b/src/easymotor-v3/ao_pins.h @@ -167,14 +167,20 @@ 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); - double adc_volts = volts * 10.0/15.6; /* voltage divider in front of the ADC input */ - if (adc_volts > 1.0) - adc_volts = 1.0; - double adc = adc_volts * 32767.0; + /* 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; } -- 2.30.2