From bcd5752a12f5c24bbe18db85944bca51eba08717 Mon Sep 17 00:00:00 2001 From: "DESKTOP-JI9AKHT\\Dimand" Date: Tue, 12 Dec 2017 23:15:31 +1100 Subject: [PATCH] Changed the scaling factor of the curent sensor to be in the form of y=mx+b. Altered the scaling to be in mV/10A, this means that for you average 50mOhm shunt resistor you will have a scale factor of 500, plenty of integre adjustability and the default 400 used will be fine as a first guess in most cases. Offset is adjusted to mA to give greater adjustability as an integer value. Builds successfully to OMNIBUSF4SD. --- src/main/sensors/current.c | 5 +++-- src/main/sensors/current.h | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main/sensors/current.c b/src/main/sensors/current.c index f747c68137..27aa0b7746 100644 --- a/src/main/sensors/current.c +++ b/src/main/sensors/current.c @@ -107,9 +107,10 @@ static int32_t currentMeterADCToCentiamps(const uint16_t src) const currentSensorADCConfig_t *config = currentSensorADCConfig(); int32_t millivolts = ((uint32_t)src * ADCVREF) / 4096; - millivolts -= config->offset; + // y=x/m+b m is scale in (mV/10A) and b is offset in (mA) + int32_t centiAmps = (millivolts * 10000 / (int32_t)config->scale + (int32_t)config->offset) / 10; - return (millivolts * 1000) / (int32_t)config->scale; // current in 0.01A steps + return centiAmps; // Returns Centiamps to maintain compatability with the rest of the code } static void updateCurrentmAhDrawnState(currentMeterMAhDrawnState_t *state, int32_t amperageLatest, int32_t lastUpdateAt) diff --git a/src/main/sensors/current.h b/src/main/sensors/current.h index 48c0db2bab..8996e723ba 100644 --- a/src/main/sensors/current.h +++ b/src/main/sensors/current.h @@ -65,8 +65,8 @@ typedef struct currentMeterADCState_s { } currentMeterADCState_t; typedef struct currentSensorADCConfig_s { - int16_t scale; // scale the current sensor output voltage to milliamps. Value in 1/10th mV/A - int16_t offset; // offset of the current sensor in millivolt steps + int16_t scale; // scale the current sensor output voltage to milliamps. Value in mV/10A + int16_t offset; // offset of the current sensor in mA } currentSensorADCConfig_t; PG_DECLARE(currentSensorADCConfig_t, currentSensorADCConfig);