From 2b2012ef1d7308f8d71ba4b152ada49b6666e831 Mon Sep 17 00:00:00 2001 From: Asbelos Date: Mon, 6 Jun 2022 17:37:23 +0100 Subject: [PATCH] float memory saver --- MotorDriver.cpp | 19 +++++++++++++------ MotorDriver.h | 11 +++++++++-- StringFormatter.cpp | 2 +- version.h | 1 + 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/MotorDriver.cpp b/MotorDriver.cpp index 92d8c88..b72cc67 100644 --- a/MotorDriver.cpp +++ b/MotorDriver.cpp @@ -67,16 +67,23 @@ MotorDriver::MotorDriver(VPIN power_pin, byte signal_pin, byte signal_pin2, int8 pinMode(faultPin, INPUT); } - senseFactor=sense_factor; + // This conversion performed at compile time so the remainder of the code never needs + // float calculations or libraray code. + senseFactorInternal=sense_factor * senseScale; tripMilliamps=trip_milliamps; - rawCurrentTripValue=(int)(trip_milliamps / sense_factor); + rawCurrentTripValue=mA2raw(trip_milliamps); if (currentPin==UNUSED_PIN) DIAG(F("MotorDriver ** WARNING ** No current or short detection")); - else + else { DIAG(F("MotorDriver currentPin=A%d, senseOffset=%d, rawCurrentTripValue(relative to offset)=%d"), currentPin-A0, senseOffset,rawCurrentTripValue); + // self testing diagnostic for the non-float converters... may be removed when happy + // DIAG(F("senseFactorInternal=%d raw2mA(1000)=%d mA2Raw(1000)=%d"), + // senseFactorInternal, raw2mA(1000),mA2raw(1000)); + } + // prepare values for current detection sampleDelay = 0; lastSampleTaken = millis(); @@ -164,10 +171,10 @@ int MotorDriver::getCurrentRawInInterrupt() { } unsigned int MotorDriver::raw2mA( int raw) { - return (unsigned int)(raw * senseFactor); + return (int32_t)raw * senseFactorInternal / senseScale; } -int MotorDriver::mA2raw( unsigned int mA) { - return (int)(mA / senseFactor); +unsigned int MotorDriver::mA2raw( unsigned int mA) { + return (int32_t)mA * senseScale / senseFactorInternal; } void MotorDriver::getFastPin(const FSH* type,int pin, bool input, FASTPIN & result) { diff --git a/MotorDriver.h b/MotorDriver.h index 283039c..1e4081e 100644 --- a/MotorDriver.h +++ b/MotorDriver.h @@ -79,7 +79,7 @@ class MotorDriver { virtual int getCurrentRaw(); virtual int getCurrentRawInInterrupt(); virtual unsigned int raw2mA( int raw); - virtual int mA2raw( unsigned int mA); + virtual unsigned int mA2raw( unsigned int mA); inline bool canBrake() { return brakePin!=UNUSED_PIN; } @@ -108,7 +108,14 @@ class MotorDriver { FASTPIN fastSignalPin, fastSignalPin2, fastBrakePin,fastFaultPin; bool dualSignal; // true to use signalPin2 bool invertBrake; // brake pin passed as negative means pin is inverted - float senseFactor; + + // Raw to milliamp conversion factors avoiding float data types. + // Milliamps=rawADCreading * sensefactorInternal / senseScale + // + // senseScale is chosen as 256 to give enough scale for 2 decimal place + // raw->mA conversion with an ultra fast optimised integer multiplication + int senseFactorInternal; // set to senseFactor * senseScale + static const int senseScale=256; int senseOffset; unsigned int tripMilliamps; int rawCurrentTripValue; diff --git a/StringFormatter.cpp b/StringFormatter.cpp index 73a910f..485d8fd 100644 --- a/StringFormatter.cpp +++ b/StringFormatter.cpp @@ -104,7 +104,7 @@ void StringFormatter::send2(Print * stream,const FSH* format, va_list args) { case 'b': stream->print(va_arg(args, int), BIN); break; case 'o': stream->print(va_arg(args, int), OCT); break; case 'x': stream->print(va_arg(args, int), HEX); break; - case 'f': stream->print(va_arg(args, double), 2); break; + //case 'f': stream->print(va_arg(args, double), 2); break; //format width prefix case '-': formatLeft=true; diff --git a/version.h b/version.h index e3ff37a..5ab7e44 100644 --- a/version.h +++ b/version.h @@ -6,6 +6,7 @@ #define VERSION "4.0.3" // 4.0.3 Track Manager additions: +// Float eliminated saving >1.5kb PROGMEM and speed. // SET_TRACK(track,mode) Functions (A-H, MAIN|PROG|DC|DCX|OFF) // New DC track function and DCX reverse polarity function // TrackManager DCC & DC up to 8 Districts Architecture