diff --git a/DCCWaveform.cpp b/DCCWaveform.cpp index f66da28..dc4b503 100644 --- a/DCCWaveform.cpp +++ b/DCCWaveform.cpp @@ -123,10 +123,13 @@ void DCCWaveform::setPowerMode(POWERMODE mode) { void DCCWaveform::checkPowerOverload() { + static int progTripValue = motorDriver->mA2raw(TRIP_CURRENT_PROG); // need only calculate once, hence static + if (millis() - lastSampleTaken < sampleDelay) return; lastSampleTaken = millis(); int tripValue= motorDriver->rawCurrentTripValue; - if (!isMainTrack && (ackPending || progTrackSyncMain)) tripValue=ACK_CURRENT_TRIP; + if (!isMainTrack && !ackPending && !progTrackSyncMain) + tripValue=progTripValue; switch (powerMode) { case POWERMODE::OFF: @@ -143,8 +146,8 @@ void DCCWaveform::checkPowerOverload() { if (power_sample_overload_wait>POWER_SAMPLE_OVERLOAD_WAIT) power_sample_overload_wait=POWER_SAMPLE_OVERLOAD_WAIT; } else { setPowerMode(POWERMODE::OVERLOAD); - unsigned int mA=motorDriver->convertToMilliamps(lastCurrent); - unsigned int maxmA=motorDriver->convertToMilliamps(tripValue); + unsigned int mA=motorDriver->raw2mA(lastCurrent); + unsigned int maxmA=motorDriver->raw2mA(tripValue); DIAG(F("\n*** %S TRACK POWER OVERLOAD current=%d max=%d offtime=%l ***\n"), isMainTrack ? F("MAIN") : F("PROG"), mA, maxmA, power_sample_overload_wait); power_good_counter=0; sampleDelay = power_sample_overload_wait; @@ -287,7 +290,7 @@ int DCCWaveform::getLastCurrent() { void DCCWaveform::setAckBaseline() { if (isMainTrack) return; ackThreshold=motorDriver->getCurrentRaw() + (int)(65 / motorDriver->senseFactor); - if (Diag::ACK) DIAG(F("\nACK-BASELINE %d/%dmA"),ackThreshold,motorDriver->convertToMilliamps(ackThreshold)); + if (Diag::ACK) DIAG(F("\nACK-BASELINE %d/%dmA"),ackThreshold,motorDriver->raw2mA(ackThreshold)); } void DCCWaveform::setAckPending() { @@ -303,7 +306,7 @@ void DCCWaveform::setAckPending() { byte DCCWaveform::getAck() { if (ackPending) return (2); // still waiting if (Diag::ACK) DIAG(F("\nACK-%S after %dmS max=%d/%dmA pulse=%duS"),ackDetected?F("OK"):F("FAIL"), ackCheckDuration, - ackMaxCurrent,motorDriver->convertToMilliamps(ackMaxCurrent), ackPulseDuration); + ackMaxCurrent,motorDriver->raw2mA(ackMaxCurrent), ackPulseDuration); if (ackDetected) return (1); // Yes we had an ack return(0); // pending set off but not detected means no ACK. } diff --git a/DCCWaveform.h b/DCCWaveform.h index b1c6f7d..5af7144 100644 --- a/DCCWaveform.h +++ b/DCCWaveform.h @@ -105,7 +105,9 @@ class DCCWaveform { POWERMODE powerMode; unsigned long lastSampleTaken; unsigned int sampleDelay; - static const int ACK_CURRENT_TRIP=1000; // During ACK processing limit can be higher + // Trip current for programming track, 250mA. Change only if you really + // need to be non-NMRA-compliant because of decoders that are not either. + static const int TRIP_CURRENT_PROG=250; unsigned long power_sample_overload_wait = POWER_SAMPLE_OVERLOAD_WAIT; unsigned int power_good_counter = 0; diff --git a/MotorDriver.cpp b/MotorDriver.cpp index 75b5d05..5420488 100644 --- a/MotorDriver.cpp +++ b/MotorDriver.cpp @@ -76,6 +76,9 @@ int MotorDriver::getCurrentRaw() { return analogReadFast(currentPin); } -unsigned int MotorDriver::convertToMilliamps( int raw) { +unsigned int MotorDriver::raw2mA( int raw) { return (unsigned int)(raw * senseFactor); } +int MotorDriver::mA2raw( unsigned int mA) { + return (int)(mA / senseFactor); +} diff --git a/MotorDriver.h b/MotorDriver.h index 8dbee0a..cce07ad 100644 --- a/MotorDriver.h +++ b/MotorDriver.h @@ -27,7 +27,8 @@ class MotorDriver { virtual void setSignal( bool high); virtual void setBrake( bool on); virtual int getCurrentRaw(); - virtual unsigned int convertToMilliamps( int rawValue); + virtual unsigned int raw2mA( int raw); + virtual int mA2raw( unsigned int mA); byte powerPin, signalPin, signalPin2, brakePin,currentPin,faultPin; float senseFactor; diff --git a/MotorDrivers.h b/MotorDrivers.h index b7ed3de..3695e0a 100644 --- a/MotorDrivers.h +++ b/MotorDrivers.h @@ -10,40 +10,37 @@ // This file contains configurations for known/supported motor shields. // A configuration defined by macro here can be used in your sketch. // A custom hardware setup will require your sketch to create MotorDriver instances -// similar to those defined here, WITHOUT editing this file. +// similar to those defined here, WITHOUT editing this file. You can put your +// custom defines in config.h. const byte UNUSED_PIN = 255; -// Trip current for programming track in mA. Change only if you really -// need to be non-NMRA-compliant because of decoders that are not either. -#define TRIP_CURRENT_PROG 250 - // MotorDriver(byte power_pin, byte signal_pin, byte signal_pin2, byte brake_pin, byte current_pin, // float senseFactor, unsigned int tripMilliamps, byte faultPin); // Arduino standard Motor Shield #define STANDARD_MOTOR_SHIELD F("STANDARD_MOTOR_SHIELD"), \ new MotorDriver(3, 12, UNUSED_PIN, UNUSED_PIN, A0, 2.99, 2000, UNUSED_PIN), \ - new MotorDriver(11, 13, UNUSED_PIN, UNUSED_PIN, A1, 2.99, TRIP_CURRENT_PROG, UNUSED_PIN) + new MotorDriver(11, 13, UNUSED_PIN, UNUSED_PIN, A1, 2.99, 2000, UNUSED_PIN) // Pololu Motor Shield #define POLOLU_MOTOR_SHIELD F("POLOLU_MOTOR_SHIELD"), \ - new MotorDriver(4, 7, UNUSED_PIN, 9, A0, 18, 2000, 12), \ - new MotorDriver(2, 8, UNUSED_PIN, 10, A1, 18, TRIP_CURRENT_PROG, UNUSED_PIN) + new MotorDriver(4, 7, UNUSED_PIN, 9, A0, 18, 3000, 12), \ + new MotorDriver(2, 8, UNUSED_PIN, 10, A1, 18, 3000, UNUSED_PIN) // Firebox Mk1 #define FIREBOX_MK1 F("FIREBOX_MK1"), \ new MotorDriver(3, 6, 7, UNUSED_PIN, A5, 9.766, 5500, UNUSED_PIN), \ - new MotorDriver(4, 8, 9, UNUSED_PIN, A1, 5.00, TRIP_CURRENT_PROG, UNUSED_PIN) + new MotorDriver(4, 8, 9, UNUSED_PIN, A1, 5.00, 1000, UNUSED_PIN) // Firebox Mk1S #define FIREBOX_MK1S F("FIREBOX_MK1A"), \ new MotorDriver(24, 21, 22, 25, 23, 9.766, 5500, UNUSED_PIN), \ - new MotorDriver(30, 27, 28, 31, 29, 5.00, TRIP_CURRENT_PROG, UNUSED_PIN) + new MotorDriver(30, 27, 28, 31, 29, 5.00, 1000, UNUSED_PIN) // FunduMoto Motor Shield #define FUNDUMOTO_SHIELD F("FUNDUMOTO_SHIELD"), \ new MotorDriver(10, 12, UNUSED_PIN, 9, A0, 2.99, 2000, UNUSED_PIN), \ - new MotorDriver(11, 13, UNUSED_PIN, UNUSED_PIN, A1, 2.99, TRIP_CURRENT_PROG, UNUSED_PIN) + new MotorDriver(11, 13, UNUSED_PIN, UNUSED_PIN, A1, 2.99, 2000, UNUSED_PIN) #endif