1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-11-27 01:56:14 +01:00

Merge branch 'progcurlimit'

This commit is contained in:
Harald Barth 2020-09-27 12:22:31 +02:00
commit 87fcfa0823
5 changed files with 25 additions and 19 deletions

View File

@ -123,10 +123,13 @@ void DCCWaveform::setPowerMode(POWERMODE mode) {
void DCCWaveform::checkPowerOverload() { void DCCWaveform::checkPowerOverload() {
static int progTripValue = motorDriver->mA2raw(TRIP_CURRENT_PROG); // need only calculate once, hence static
if (millis() - lastSampleTaken < sampleDelay) return; if (millis() - lastSampleTaken < sampleDelay) return;
lastSampleTaken = millis(); lastSampleTaken = millis();
int tripValue= motorDriver->rawCurrentTripValue; int tripValue= motorDriver->rawCurrentTripValue;
if (!isMainTrack && (ackPending || progTrackSyncMain)) tripValue=ACK_CURRENT_TRIP; if (!isMainTrack && !ackPending && !progTrackSyncMain)
tripValue=progTripValue;
switch (powerMode) { switch (powerMode) {
case POWERMODE::OFF: 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; if (power_sample_overload_wait>POWER_SAMPLE_OVERLOAD_WAIT) power_sample_overload_wait=POWER_SAMPLE_OVERLOAD_WAIT;
} else { } else {
setPowerMode(POWERMODE::OVERLOAD); setPowerMode(POWERMODE::OVERLOAD);
unsigned int mA=motorDriver->convertToMilliamps(lastCurrent); unsigned int mA=motorDriver->raw2mA(lastCurrent);
unsigned int maxmA=motorDriver->convertToMilliamps(tripValue); 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); 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; power_good_counter=0;
sampleDelay = power_sample_overload_wait; sampleDelay = power_sample_overload_wait;
@ -287,7 +290,7 @@ int DCCWaveform::getLastCurrent() {
void DCCWaveform::setAckBaseline() { void DCCWaveform::setAckBaseline() {
if (isMainTrack) return; if (isMainTrack) return;
ackThreshold=motorDriver->getCurrentRaw() + (int)(65 / motorDriver->senseFactor); 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() { void DCCWaveform::setAckPending() {
@ -303,7 +306,7 @@ void DCCWaveform::setAckPending() {
byte DCCWaveform::getAck() { byte DCCWaveform::getAck() {
if (ackPending) return (2); // still waiting 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, 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 if (ackDetected) return (1); // Yes we had an ack
return(0); // pending set off but not detected means no ACK. return(0); // pending set off but not detected means no ACK.
} }

View File

@ -105,7 +105,9 @@ class DCCWaveform {
POWERMODE powerMode; POWERMODE powerMode;
unsigned long lastSampleTaken; unsigned long lastSampleTaken;
unsigned int sampleDelay; 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 long power_sample_overload_wait = POWER_SAMPLE_OVERLOAD_WAIT;
unsigned int power_good_counter = 0; unsigned int power_good_counter = 0;

View File

@ -76,6 +76,9 @@ int MotorDriver::getCurrentRaw() {
return analogReadFast(currentPin); return analogReadFast(currentPin);
} }
unsigned int MotorDriver::convertToMilliamps( int raw) { unsigned int MotorDriver::raw2mA( int raw) {
return (unsigned int)(raw * senseFactor); return (unsigned int)(raw * senseFactor);
} }
int MotorDriver::mA2raw( unsigned int mA) {
return (int)(mA / senseFactor);
}

View File

@ -27,7 +27,8 @@ class MotorDriver {
virtual void setSignal( bool high); virtual void setSignal( bool high);
virtual void setBrake( bool on); virtual void setBrake( bool on);
virtual int getCurrentRaw(); 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; byte powerPin, signalPin, signalPin2, brakePin,currentPin,faultPin;
float senseFactor; float senseFactor;

View File

@ -10,40 +10,37 @@
// This file contains configurations for known/supported motor shields. // This file contains configurations for known/supported motor shields.
// A configuration defined by macro here can be used in your sketch. // A configuration defined by macro here can be used in your sketch.
// A custom hardware setup will require your sketch to create MotorDriver instances // 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; 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, // MotorDriver(byte power_pin, byte signal_pin, byte signal_pin2, byte brake_pin, byte current_pin,
// float senseFactor, unsigned int tripMilliamps, byte faultPin); // float senseFactor, unsigned int tripMilliamps, byte faultPin);
// Arduino standard Motor Shield // Arduino standard Motor Shield
#define STANDARD_MOTOR_SHIELD F("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(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 // Pololu Motor Shield
#define POLOLU_MOTOR_SHIELD F("POLOLU_MOTOR_SHIELD"), \ #define POLOLU_MOTOR_SHIELD F("POLOLU_MOTOR_SHIELD"), \
new MotorDriver(4, 7, UNUSED_PIN, 9, A0, 18, 2000, 12), \ new MotorDriver(4, 7, UNUSED_PIN, 9, A0, 18, 3000, 12), \
new MotorDriver(2, 8, UNUSED_PIN, 10, A1, 18, TRIP_CURRENT_PROG, UNUSED_PIN) new MotorDriver(2, 8, UNUSED_PIN, 10, A1, 18, 3000, UNUSED_PIN)
// Firebox Mk1 // Firebox Mk1
#define FIREBOX_MK1 F("FIREBOX_MK1"), \ #define FIREBOX_MK1 F("FIREBOX_MK1"), \
new MotorDriver(3, 6, 7, UNUSED_PIN, A5, 9.766, 5500, UNUSED_PIN), \ 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 // Firebox Mk1S
#define FIREBOX_MK1S F("FIREBOX_MK1A"), \ #define FIREBOX_MK1S F("FIREBOX_MK1A"), \
new MotorDriver(24, 21, 22, 25, 23, 9.766, 5500, UNUSED_PIN), \ 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 // FunduMoto Motor Shield
#define FUNDUMOTO_SHIELD F("FUNDUMOTO_SHIELD"), \ #define FUNDUMOTO_SHIELD F("FUNDUMOTO_SHIELD"), \
new MotorDriver(10, 12, UNUSED_PIN, 9, A0, 2.99, 2000, UNUSED_PIN), \ 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 #endif