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

Current check cleanup

This commit is contained in:
Asbelos 2021-01-28 17:18:38 +00:00
parent 7df07b03e4
commit 1b19b61ebd
2 changed files with 10 additions and 21 deletions

View File

@ -30,11 +30,12 @@ DCCWaveform DCCWaveform::progTrack(PREAMBLE_BITS_PROG, false);
bool DCCWaveform::progTrackSyncMain=false; bool DCCWaveform::progTrackSyncMain=false;
bool DCCWaveform::progTrackBoosted=false; bool DCCWaveform::progTrackBoosted=false;
int DCCWaveform::progTripValue=0;
void DCCWaveform::begin(MotorDriver * mainDriver, MotorDriver * progDriver) { void DCCWaveform::begin(MotorDriver * mainDriver, MotorDriver * progDriver) {
mainTrack.motorDriver=mainDriver; mainTrack.motorDriver=mainDriver;
progTrack.motorDriver=progDriver; progTrack.motorDriver=progDriver;
progTripValue = progDriver->mA2raw(TRIP_CURRENT_PROG); // need only calculate once hence static
mainTrack.setPowerMode(POWERMODE::OFF); mainTrack.setPowerMode(POWERMODE::OFF);
progTrack.setPowerMode(POWERMODE::OFF); progTrack.setPowerMode(POWERMODE::OFF);
DCCTimer::begin(DCCWaveform::interruptHandler); DCCTimer::begin(DCCWaveform::interruptHandler);
@ -61,12 +62,6 @@ void DCCWaveform::interruptHandler() {
// WAVE_PENDING means we dont yet know what the next bit is // WAVE_PENDING means we dont yet know what the next bit is
// so we dont check cutrrent on this cycle
if (mainTrack.state!=WAVE_PENDING && progTrack.state!=WAVE_PENDING) {
mainTrack.lastCurrent=mainTrack.motorDriver->getCurrentRaw();
progTrack.lastCurrent=progTrack.motorDriver->getCurrentRaw();
}
if (mainTrack.state==WAVE_PENDING) mainTrack.interrupt2(); if (mainTrack.state==WAVE_PENDING) mainTrack.interrupt2();
if (progTrack.state==WAVE_PENDING) progTrack.interrupt2(); if (progTrack.state==WAVE_PENDING) progTrack.interrupt2();
else if (progTrack.ackPending) progTrack.checkAck(); else if (progTrack.ackPending) progTrack.checkAck();
@ -111,9 +106,6 @@ 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->getRawCurrentTripValue(); int tripValue= motorDriver->getRawCurrentTripValue();
@ -126,6 +118,7 @@ void DCCWaveform::checkPowerOverload() {
break; break;
case POWERMODE::ON: case POWERMODE::ON:
// Check current // Check current
lastCurrent=motorDriver->getCurrentRaw();
if (lastCurrent <= tripValue) { if (lastCurrent <= tripValue) {
sampleDelay = POWER_SAMPLE_ON_WAIT; sampleDelay = POWER_SAMPLE_ON_WAIT;
if(power_good_counter<100) if(power_good_counter<100)
@ -241,16 +234,12 @@ void DCCWaveform::schedulePacket(const byte buffer[], byte byteCount, byte repea
sentResetsSincePacket=0; sentResetsSincePacket=0;
} }
int DCCWaveform::getLastCurrent() {
return lastCurrent;
}
// Operations applicable to PROG track ONLY. // Operations applicable to PROG track ONLY.
// (yes I know I could have subclassed the main track but...) // (yes I know I could have subclassed the main track but...)
void DCCWaveform::setAckBaseline() { void DCCWaveform::setAckBaseline() {
if (isMainTrack) return; if (isMainTrack) return;
int baseline = lastCurrent; int baseline=motorDriver->getCurrentRaw();
ackThreshold= baseline + motorDriver->mA2raw(ackLimitmA); ackThreshold= baseline + motorDriver->mA2raw(ackLimitmA);
if (Diag::ACK) DIAG(F("\nACK baseline=%d/%dmA Threshold=%d/%dmA Duration: %dus <= pulse <= %dus"), if (Diag::ACK) DIAG(F("\nACK baseline=%d/%dmA Threshold=%d/%dmA Duration: %dus <= pulse <= %dus"),
baseline,motorDriver->raw2mA(baseline), baseline,motorDriver->raw2mA(baseline),
@ -278,17 +267,17 @@ byte DCCWaveform::getAck() {
void DCCWaveform::checkAck() { void DCCWaveform::checkAck() {
// This function operates in interrupt() time so must be fast and can't DIAG // This function operates in interrupt() time so must be fast and can't DIAG
if (sentResetsSincePacket > 6) { //ACK timeout if (sentResetsSincePacket > 6) { //ACK timeout
ackCheckDuration=millis()-ackCheckStart; ackCheckDuration=millis()-ackCheckStart;
ackPending = false; ackPending = false;
return; return;
} }
if (lastCurrent > ackMaxCurrent) ackMaxCurrent=lastCurrent; int current=motorDriver->getCurrentRaw();
if (current > ackMaxCurrent) ackMaxCurrent=current;
// An ACK is a pulse lasting between minAckPulseDuration and maxAckPulseDuration uSecs (refer @haba) // An ACK is a pulse lasting between minAckPulseDuration and maxAckPulseDuration uSecs (refer @haba)
if (lastCurrent>ackThreshold) { if (current>ackThreshold) {
if (ackPulseStart==0) ackPulseStart=micros(); // leading edge of pulse detected if (ackPulseStart==0) ackPulseStart=micros(); // leading edge of pulse detected
return; return;
} }

View File

@ -57,7 +57,6 @@ class DCCWaveform {
void setPowerMode(POWERMODE); void setPowerMode(POWERMODE);
POWERMODE getPowerMode(); POWERMODE getPowerMode();
void checkPowerOverload(); void checkPowerOverload();
int getLastCurrent();
inline int get1024Current() { inline int get1024Current() {
if (powerMode == POWERMODE::ON) if (powerMode == POWERMODE::ON)
return (int)(lastCurrent*(long int)1024/motorDriver->getRawCurrentTripValue()); return (int)(lastCurrent*(long int)1024/motorDriver->getRawCurrentTripValue());
@ -131,7 +130,8 @@ class DCCWaveform {
byte pendingPacket[MAX_PACKET_SIZE]; byte pendingPacket[MAX_PACKET_SIZE];
byte pendingLength; byte pendingLength;
byte pendingRepeats; byte pendingRepeats;
volatile int lastCurrent; int lastCurrent;
static int progTripValue;
int maxmA; int maxmA;
int tripmA; int tripmA;