1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-12-23 12:51:24 +01:00

DC power fix

This commit is contained in:
Asbelos 2022-03-31 22:19:13 +01:00
parent f9e36e6693
commit ece342f037
3 changed files with 48 additions and 26 deletions

View File

@ -97,10 +97,6 @@ bool MotorDriver::isPWMCapable() {
void MotorDriver::setPower(POWERMODE mode) {
bool on=mode==POWERMODE::ON;
if (on) {
// toggle brake before turning power on - resets overcurrent error
// on the Pololu board if brake is wired to ^D2.
setBrake(true);
setBrake(false);
IODevice::write(powerPin,HIGH);
}
else IODevice::write(powerPin,LOW);

View File

@ -134,17 +134,8 @@ bool TrackManager::setTrackMode(byte trackToSet, TRACK_MODE mode, int16_t dcAddr
// 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
int16_t speed1=DCC::getThrottleSpeed(dcAddr);
byte speedByte;
if (speed1<0) speedByte=0;
else {
speedByte=speed1;
bool direction=DCC::getThrottleDirection(dcAddr);
if (mode==TRACK_MODE_DCX) direction=!direction;
if (direction) speedByte|=0x80;
}
track[trackToSet]->setDCSignal(speedByte);
applyDCSpeed(trackToSet);
}
else {
// DCC tracks need to have the brake set off or they will not work.
@ -168,6 +159,20 @@ bool TrackManager::setTrackMode(byte trackToSet, TRACK_MODE mode, int16_t dcAddr
return true;
}
void TrackManager::applyDCSpeed(byte t) {
int16_t speed1=DCC::getThrottleSpeed(trackDCAddr[t]);
byte speedByte;
if (speed1<0) speedByte=0;
else {
speedByte=speed1;
bool direction=DCC::getThrottleDirection(trackDCAddr[t]);
if (trackMode[t]==TRACK_MODE_DCX) direction=!direction;
if (direction) speedByte|=0x80;
}
track[t]->setDCSignal(speedByte);
}
bool TrackManager::parseJ(Print *stream, int16_t params, int16_t p[])
{
@ -240,18 +245,38 @@ MotorDriver * TrackManager::getProgDriver() {
FOR_EACH_TRACK(t)
if (trackMode[t]==TRACK_MODE_PROG) return track[t];
return NULL;
}
}
void TrackManager::setPower2(bool setProg,POWERMODE mode) {
if (setProg) {
APPLY_BY_MODE(TRACK_MODE_PROG,setPower(mode))
}
else {
mainPowerGuess=mode;
FOR_EACH_TRACK(t)
if (track[t]
&& trackMode[t]!=TRACK_MODE_OFF
&& trackMode[t]!=TRACK_MODE_PROG
) track[t]->setPower(mode);
if (!setProg) mainPowerGuess=mode;
FOR_EACH_TRACK(t) {
MotorDriver * driver=track[t];
if (!driver) continue;
switch (trackMode[t]) {
case TRACK_MODE_MAIN:
if (setProg) break;
// toggle brake before turning power on - resets overcurrent error
// on the Pololu board if brake is wired to ^D2.
driver->setBrake(true);
driver->setBrake(false); // DCC runs with brake off
driver->setPower(mode);
break;
case TRACK_MODE_DC:
case TRACK_MODE_DCX:
if (setProg) break;
driver->setBrake(true); // DC starts with brake on
applyDCSpeed(t); // speed match DCC throttles
driver->setPower(mode);
break;
case TRACK_MODE_PROG:
if (!setProg) break;
driver->setBrake(true);
driver->setBrake(false);
driver->setPower(mode);
break;
case TRACK_MODE_OFF:
break;
}
}
}

View File

@ -78,6 +78,7 @@ class TrackManager {
static byte lastTrack;
static byte nextCycleTrack;
static POWERMODE mainPowerGuess;
static void applyDCSpeed(byte t);
static MotorDriver* track[MAX_TRACKS];
static TRACK_MODE trackMode[MAX_TRACKS];