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

Bugfix: Preserve direction all times when switching between DCC and DC

This commit is contained in:
Harald Barth 2022-08-23 07:56:56 +02:00
parent 827e4fef86
commit 86215b28ae
5 changed files with 33 additions and 18 deletions

View File

@ -1 +1 @@
#define GITHUB_SHA "PORTX-HAL-20220817-1" #define GITHUB_SHA "PORTX-HAL-20220823"

View File

@ -239,20 +239,29 @@ void MotorDriver::setDCSignal(byte speedcode) {
if (invertBrake) if (invertBrake)
brake=255-brake; brake=255-brake;
analogWrite(brakePin,brake); analogWrite(brakePin,brake);
//DIAG(F("DCSignal %d"), speedcode);
if (HAVE_PORTA(fastSignalPin.shadowinout == &PORTA)) { if (HAVE_PORTA(fastSignalPin.shadowinout == &PORTA)) {
noInterrupts();
HAVE_PORTA(shadowPORTA=PORTA); HAVE_PORTA(shadowPORTA=PORTA);
setSignal(tDir); setSignal(tDir);
HAVE_PORTA(PORTA=shadowPORTA); HAVE_PORTA(PORTA=shadowPORTA);
interrupts();
} else if (HAVE_PORTB(fastSignalPin.shadowinout == &PORTB)) { } else if (HAVE_PORTB(fastSignalPin.shadowinout == &PORTB)) {
noInterrupts();
HAVE_PORTB(shadowPORTB=PORTB); HAVE_PORTB(shadowPORTB=PORTB);
setSignal(tDir); setSignal(tDir);
HAVE_PORTB(PORTB=shadowPORTB); HAVE_PORTB(PORTB=shadowPORTB);
interrupts();
} else if (HAVE_PORTC(fastSignalPin.shadowinout == &PORTC)) { } else if (HAVE_PORTC(fastSignalPin.shadowinout == &PORTC)) {
noInterrupts();
HAVE_PORTC(shadowPORTC=PORTC); HAVE_PORTC(shadowPORTC=PORTC);
setSignal(tDir); setSignal(tDir);
HAVE_PORTC(PORTC=shadowPORTC); HAVE_PORTC(PORTC=shadowPORTC);
interrupts();
} else { } else {
noInterrupts();
setSignal(tDir); setSignal(tDir);
interrupts();
} }
} }

View File

@ -121,8 +121,7 @@ class MotorDriver {
// otherwise the call from interrupt context can undo whatever we do // otherwise the call from interrupt context can undo whatever we do
// from outside interrupt // from outside interrupt
void setBrake( bool on, bool interruptContext=false); void setBrake( bool on, bool interruptContext=false);
__attribute__((always_inline)) inline void setSignal( bool high, bool interruptContext=false) { __attribute__((always_inline)) inline void setSignal( bool high) {
if (!interruptContext) {noInterrupts();}
if (trackPWM) { if (trackPWM) {
DCCTimer::setPWM(signalPin,high); DCCTimer::setPWM(signalPin,high);
} }
@ -136,7 +135,6 @@ class MotorDriver {
if (dualSignal) setHIGH(fastSignalPin2); if (dualSignal) setHIGH(fastSignalPin2);
} }
} }
if (!interruptContext) {interrupts();}
}; };
inline void enableSignal(bool on) { inline void enableSignal(bool on) {
if (on) if (on)

View File

@ -95,7 +95,7 @@ void TrackManager::setDCCSignal( bool on) {
HAVE_PORTA(shadowPORTA=PORTA); HAVE_PORTA(shadowPORTA=PORTA);
HAVE_PORTB(shadowPORTB=PORTB); HAVE_PORTB(shadowPORTB=PORTB);
HAVE_PORTC(shadowPORTC=PORTC); HAVE_PORTC(shadowPORTC=PORTC);
APPLY_BY_MODE(TRACK_MODE_MAIN,setSignal(on, true)); APPLY_BY_MODE(TRACK_MODE_MAIN,setSignal(on));
HAVE_PORTA(PORTA=shadowPORTA); HAVE_PORTA(PORTA=shadowPORTA);
HAVE_PORTB(PORTB=shadowPORTB); HAVE_PORTB(PORTB=shadowPORTB);
HAVE_PORTC(PORTC=shadowPORTC); HAVE_PORTC(PORTC=shadowPORTC);
@ -113,7 +113,7 @@ void TrackManager::setPROGSignal( bool on) {
HAVE_PORTA(shadowPORTA=PORTA); HAVE_PORTA(shadowPORTA=PORTA);
HAVE_PORTB(shadowPORTB=PORTB); HAVE_PORTB(shadowPORTB=PORTB);
HAVE_PORTC(shadowPORTC=PORTC); HAVE_PORTC(shadowPORTC=PORTC);
APPLY_BY_MODE(TRACK_MODE_PROG,setSignal(on, true)); APPLY_BY_MODE(TRACK_MODE_PROG,setSignal(on));
HAVE_PORTA(PORTA=shadowPORTA); HAVE_PORTA(PORTA=shadowPORTA);
HAVE_PORTB(PORTB=shadowPORTB); HAVE_PORTB(PORTB=shadowPORTB);
HAVE_PORTC(PORTC=shadowPORTC); HAVE_PORTC(PORTC=shadowPORTC);
@ -170,16 +170,10 @@ bool TrackManager::setTrackMode(byte trackToSet, TRACK_MODE mode, int16_t dcAddr
// When a track is switched, we must clear any side effects of its previous // When a track is switched, we must clear any side effects of its previous
// state, otherwise trains run away or just dont move. // state, otherwise trains run away or just dont move.
if (mode==TRACK_MODE_DC || mode==TRACK_MODE_DCX) {
// DC tracks need to be given speed of the throttle for that cab address
// otherwise will not match other tracks on same cab.
// This also needs to allow for inverted DCX
applyDCSpeed(trackToSet);
} // This can be done BEFORE the PWM-Timer evaluation (methinks)
else { if (!(mode==TRACK_MODE_DC || mode==TRACK_MODE_DCX)) {
// DCC tracks need to have set the PWM to zero or they will not work. // DCC tracks need to have set the PWM to zero or they will not work.
// 128 is speed=0 and dir=0 and then loosen brake.
track[trackToSet]->detachDCSignal(); track[trackToSet]->detachDCSignal();
track[trackToSet]->setBrake(false); track[trackToSet]->setBrake(false);
} }
@ -221,9 +215,18 @@ bool TrackManager::setTrackMode(byte trackToSet, TRACK_MODE mode, int16_t dcAddr
DCCTimer::clearPWM(); // has to be AFTER trackPWM changes because if trackPWM==true this is undone for that track DCCTimer::clearPWM(); // has to be AFTER trackPWM changes because if trackPWM==true this is undone for that track
} }
#else #else
// For ESP32 we just reinitialize the DCC Waveform
DCCWaveform::begin(); DCCWaveform::begin();
#endif #endif
// This block must be AFTER the PWM-Timer modifications
if (mode==TRACK_MODE_DC || mode==TRACK_MODE_DCX) {
// DC tracks need to be given speed of the throttle for that cab address
// otherwise will not match other tracks on same cab.
// This also needs to allow for inverted DCX
applyDCSpeed(trackToSet);
}
// Normal running tracks are set to the global power state // Normal running tracks are set to the global power state
track[trackToSet]->setPower( track[trackToSet]->setPower(
(mode==TRACK_MODE_MAIN || mode==TRACK_MODE_DC || mode==TRACK_MODE_DCX || mode==TRACK_MODE_EXT) ? (mode==TRACK_MODE_MAIN || mode==TRACK_MODE_DC || mode==TRACK_MODE_DCX || mode==TRACK_MODE_EXT) ?

View File

@ -4,7 +4,12 @@
#include "StringFormatter.h" #include "StringFormatter.h"
#define VERSION "4.2.2 rc1" #define VERSION "4.2.3 rc1"
// 4.2.3 Bugfix direction when togging between MAIN and DC
// Bugfix return fail when F/f argument out of range
// More error checking for out of bounds motor driver current trip limit
// 4.2.2 ESP32 beta
// JOIN/UMJOIN on ESP32
// 4.2.1 ESP32 alpha // 4.2.1 ESP32 alpha
// Ready for alpha test on ESP32. Track switching with <=> untested // Ready for alpha test on ESP32. Track switching with <=> untested
// Send DCC signal on MAIN // Send DCC signal on MAIN