From 17c004aecf558baa980bf9bb620cd7e456cf7969 Mon Sep 17 00:00:00 2001 From: Colin Murdoch Date: Mon, 25 Sep 2023 14:32:54 +0100 Subject: [PATCH] Code corrections code corrections --- DCCEXParser.cpp | 37 ++++++++++++++++--------- EXRAIL2.cpp | 6 ++--- TrackManager.cpp | 70 +++++++++++++++++++++++++++++++++++------------- TrackManager.h | 2 +- 4 files changed, 81 insertions(+), 34 deletions(-) diff --git a/DCCEXParser.cpp b/DCCEXParser.cpp index a8c257b..a68b398 100644 --- a/DCCEXParser.cpp +++ b/DCCEXParser.cpp @@ -556,6 +556,7 @@ void DCCEXParser::parseOne(Print *stream, byte *com, RingStream * ringStream) bool prog=false; bool join=false; bool singletrack=false; + byte t=0; if (params > 1) break; if (params==0) { // All main=true; @@ -576,8 +577,8 @@ void DCCEXParser::parseOne(Print *stream, byte *com, RingStream * ringStream) } #endif else if (p[0] >= 'A' && p[0] <= 'H') { // <1 A-H> - byte t = (p[0] - 'A'); - DIAG(F("Processing track - %d "), t); + t = (p[0] - 'A'); + //DIAG(F("Processing track - %d "), t); if (TrackManager::isProg(t)) { main = false; prog = true; @@ -588,7 +589,7 @@ void DCCEXParser::parseOne(Print *stream, byte *com, RingStream * ringStream) prog=false; } singletrack=true; - if (main) TrackManager::setTrackPower(POWERMODE::ON, t); + } else break; // will reply } @@ -597,6 +598,12 @@ void DCCEXParser::parseOne(Print *stream, byte *com, RingStream * ringStream) if (main) TrackManager::setMainPower(POWERMODE::ON); if (prog) TrackManager::setProgPower(POWERMODE::ON); } + else { + if (main) TrackManager::setTrackPower(false, POWERMODE::ON, t); + if (prog) { + TrackManager::setTrackPower(true, POWERMODE::ON, t); + } + } CommandDistributor::broadcastPower(); return; } @@ -606,6 +613,7 @@ void DCCEXParser::parseOne(Print *stream, byte *com, RingStream * ringStream) bool main=false; bool prog=false; bool singletrack=false; + byte t=0; if (params > 1) break; if (params==0) { // All main=true; @@ -621,8 +629,8 @@ void DCCEXParser::parseOne(Print *stream, byte *com, RingStream * ringStream) } #endif <= else if (p[0] >= 'A' && p[0] <= 'H') { // <1 A-H> - byte t = (p[0] - 'A'); - DIAG(F("Processing track - %d "), t); + t = (p[0] - 'A'); + //DIAG(F("Processing track - %d "), t); if (TrackManager::isProg(t)) { main = false; prog = true; @@ -632,24 +640,29 @@ void DCCEXParser::parseOne(Print *stream, byte *com, RingStream * ringStream) main=true; prog=false; } - singletrack=true; - if (main) TrackManager::setTrackPower(POWERMODE::OFF, t); - } - + singletrack=true; + } else break; // will reply } TrackManager::setJoin(false); if (!singletrack) { - if (main) TrackManager::setMainPower(POWERMODE::OFF); + if (main) TrackManager::setMainPower(POWERMODE::OFF); + if (prog) { + TrackManager::progTrackBoosted=false; // Prog track boost mode will not outlive prog track off + TrackManager::setProgPower(POWERMODE::OFF); + } + } + else { + if (main) TrackManager::setTrackPower(false, POWERMODE::OFF, t); if (prog) { TrackManager::progTrackBoosted=false; // Prog track boost mode will not outlive prog track off - TrackManager::setProgPower(POWERMODE::OFF); + TrackManager::setTrackPower(true, POWERMODE::OFF, t); } } CommandDistributor::broadcastPower(); return; - } + } case '!': // ESTOP ALL DCC::setThrottle(0,1,1); // this broadcasts speed 1(estop) and sets all reminders to speed 1. diff --git a/EXRAIL2.cpp b/EXRAIL2.cpp index 07f4768..8fd9532 100644 --- a/EXRAIL2.cpp +++ b/EXRAIL2.cpp @@ -783,13 +783,13 @@ void RMFT2::loop2() { case OPCODE_SET_POWER: // operand is TRACK_POWER , trackid - + //byte thistrack=getOperand(1); switch (operand) { case TRACK_POWER_0: - TrackManager::setTrackPower(POWERMODE::OFF, getOperand(1)); + TrackManager::setTrackPower(TrackManager::isProg(getOperand(1)), POWERMODE::OFF, getOperand(1)); break; case TRACK_POWER_1: - TrackManager::setTrackPower(POWERMODE::ON, getOperand(1)); + TrackManager::setTrackPower(TrackManager::isProg(getOperand(1)), POWERMODE::ON, getOperand(1)); break; } diff --git a/TrackManager.cpp b/TrackManager.cpp index ad7ef6a..d653645 100644 --- a/TrackManager.cpp +++ b/TrackManager.cpp @@ -364,16 +364,16 @@ void TrackManager::streamTrackState(Print* stream, byte t) { break; #endif case TRACK_MODE_NONE: - if (pstate) {format=F("<= %c NONE %ON>\n");} else {format=F("<= %c NONE %OFF>\n");} + if (pstate) {format=F("<= %c NONE ON>\n");} else {format=F("<= %c NONE OFF>\n");} break; case TRACK_MODE_EXT: if (pstate) {format=F("<= %c EXT ON>\n");} else {format=F("<= %c EXT OFF>\n");} break; case TRACK_MODE_DC: - if (pstate) {format=F("<= %c DC ON %d>\n");} else {format=F("<= %c DC OFF %d>\n");} + if (pstate) {format=F("<= %c DC %d ON>\n");} else {format=F("<= %c DC %d OFF>\n");} break; case TRACK_MODE_DCX: - if (pstate) {format=F("<= %c DCX ON %d>\n");} else {format=F("<= %c DCX OFF %d>\n");} + if (pstate) {format=F("<= %c DCX %d ON>\n");} else {format=F("<= %c DCX %d OFF>\n");} break; default: break; // unknown, dont care @@ -416,9 +416,51 @@ std::vectorTrackManager::getMainDrivers() { void TrackManager::setPower2(bool setProg,POWERMODE mode) { if (!setProg) mainPowerGuess=mode; FOR_EACH_TRACK(t) { - MotorDriver * driver=track[t]; - if (!driver) continue; - switch (track[t]->getMode()) { + TrackManager::setTrackPower(setProg, mode, t); + // MotorDriver * driver=track[t]; + // if (!driver) continue; + // switch (track[t]->getMode()) { + // 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. + // // XXX see if we can make this conditional + // 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_EXT: + // driver->setBrake(true); + // driver->setBrake(false); + // driver->setPower(mode); + // break; + // case TRACK_MODE_NONE: + // break; + // } + + } +} + +void TrackManager::setTrackPower(bool setProg, POWERMODE mode, byte thistrack) { + + DIAG(F("SetTrackPower Processing Track %d"), thistrack); + MotorDriver * driver=track[thistrack]; + if (!driver) return; + + switch (track[thistrack]->getMode()) { case TRACK_MODE_MAIN: if (setProg) break; // toggle brake before turning power on - resets overcurrent error @@ -432,7 +474,7 @@ void TrackManager::setPower2(bool setProg,POWERMODE mode) { case TRACK_MODE_DCX: if (setProg) break; driver->setBrake(true); // DC starts with brake on - applyDCSpeed(t); // speed match DCC throttles + applyDCSpeed(thistrack); // speed match DCC throttles driver->setPower(mode); break; case TRACK_MODE_PROG: @@ -449,17 +491,9 @@ void TrackManager::setPower2(bool setProg,POWERMODE mode) { case TRACK_MODE_NONE: break; } - - } -} - -void TrackManager::setTrackPower(POWERMODE mode, byte thistrack) { - - DIAG(F("SetPower Processing Track %d"), thistrack); - MotorDriver * driver=track[thistrack]; - if (!driver) return; - - driver->setPower(mode); + if (mode == POWERMODE::ON) {DIAG(F("Power Track %d ON"), thistrack);} + else {DIAG(F("Power Track %d OFF"), thistrack);} + //driver->setPower(mode); } diff --git a/TrackManager.h b/TrackManager.h index c6eea5c..39f708a 100644 --- a/TrackManager.h +++ b/TrackManager.h @@ -64,7 +64,7 @@ class TrackManager { #ifdef ARDUINO_ARCH_ESP32 static std::vectorgetMainDrivers(); #endif - static void setTrackPower(POWERMODE mode, byte thistrack); + static void setTrackPower(bool setProg, POWERMODE mode, byte thistrack); static void setPower2(bool progTrack,POWERMODE mode); static void setPower(POWERMODE mode) {setMainPower(mode); setProgPower(mode);} static void setMainPower(POWERMODE mode) {setPower2(false,mode);}