diff --git a/MotorDriver.cpp b/MotorDriver.cpp index a6a1766..7415b36 100644 --- a/MotorDriver.cpp +++ b/MotorDriver.cpp @@ -38,7 +38,7 @@ volatile portreg_t shadowPORTB; volatile portreg_t shadowPORTC; MotorDriver::MotorDriver(int16_t power_pin, byte signal_pin, byte signal_pin2, int8_t brake_pin, - byte current_pin, float sense_factor, unsigned int trip_milliamps, byte fault_pin) { + byte current_pin, float sense_factor, unsigned int trip_milliamps, int8_t fault_pin) { powerPin=power_pin; invertPower=power_pin < 0; if (invertPower) { @@ -95,6 +95,9 @@ MotorDriver::MotorDriver(int16_t power_pin, byte signal_pin, byte signal_pin2, i faultPin=fault_pin; if (faultPin != UNUSED_PIN) { + invertFault=fault_pin < 0; + faultPin=invertFault ? 0-fault_pin : fault_pin; + DIAG(F("Fault pin = %d invert %d"), faultPin, invertFault); getFastPin(F("FAULT"),faultPin, 1 /*input*/, fastFaultPin); pinMode(faultPin, INPUT); } @@ -196,8 +199,12 @@ int MotorDriver::getCurrentRaw(bool fromISR) { int current; current = ADCee::read(currentPin, fromISR)-senseOffset; if (current<0) current=0-current; - if ((faultPin != UNUSED_PIN) && isLOW(fastFaultPin) && powerMode==POWERMODE::ON) + if ((faultPin != UNUSED_PIN) && powerMode==POWERMODE::ON) { + if (invertFault && isLOW(fastFaultPin)) return (current == 0 ? -1 : -current); + if (!invertFault && !isLOW(fastFaultPin)) + return (current == 0 ? -1 : -current); + } return current; } diff --git a/MotorDriver.h b/MotorDriver.h index 51f7654..5f36184 100644 --- a/MotorDriver.h +++ b/MotorDriver.h @@ -112,7 +112,7 @@ class MotorDriver { public: MotorDriver(int16_t power_pin, byte signal_pin, byte signal_pin2, int8_t brake_pin, - byte current_pin, float senseFactor, unsigned int tripMilliamps, byte faultPin); + byte current_pin, float senseFactor, unsigned int tripMilliamps, int8_t fault_pin); void setPower( POWERMODE mode); POWERMODE getPower() { return powerMode;} // as the port registers can be shadowed to get syncronized DCC signals @@ -198,6 +198,7 @@ class MotorDriver { bool dualSignal; // true to use signalPin2 bool invertBrake; // brake pin passed as negative means pin is inverted bool invertPower; // power pin passed as negative means pin is inverted + bool invertFault; // fault pin passed as negative means pin is inverted // Raw to milliamp conversion factors avoiding float data types. // Milliamps=rawADCreading * sensefactorInternal / senseScale