From 94820417992b37c7b10e4c2d53c27eeba5edb53e Mon Sep 17 00:00:00 2001 From: Harald Barth Date: Sun, 12 Feb 2023 23:31:13 +0100 Subject: [PATCH] DC via power pin 1st try --- GITHUB_SHA.h | 2 +- MotorDriver.cpp | 27 ++++++++++++++++----------- MotorDriver.h | 4 +++- TrackManager.cpp | 4 ++-- 4 files changed, 22 insertions(+), 15 deletions(-) diff --git a/GITHUB_SHA.h b/GITHUB_SHA.h index 98ac27a..71f19e4 100644 --- a/GITHUB_SHA.h +++ b/GITHUB_SHA.h @@ -1 +1 @@ -#define GITHUB_SHA "devel-202302121935Z" +#define GITHUB_SHA "devel-202302122239Z" diff --git a/MotorDriver.cpp b/MotorDriver.cpp index c41420a..9fc2a9f 100644 --- a/MotorDriver.cpp +++ b/MotorDriver.cpp @@ -146,12 +146,16 @@ void MotorDriver::setPower(POWERMODE mode) { if (on) { noInterrupts(); IODevice::write(powerPin,invertPower ? LOW : HIGH); + if (DCinuse) + setDCSignal(curSpeedCode); interrupts(); if (isProgTrack) DCCWaveform::progTrack.clearResets(); } else { noInterrupts(); + if (DCinuse) + detachDCSignal(); IODevice::write(powerPin,invertPower ? HIGH : LOW); interrupts(); } @@ -245,8 +249,9 @@ uint16_t taurustones[28] = { 165, 175, 196, 220, 220, 196, 175, 165 }; #endif void MotorDriver::setDCSignal(byte speedcode) { - if (brakePin == UNUSED_PIN) - return; + curSpeedCode = speedcode; + DCinuse = true; + #if defined(ARDUINO_AVR_UNO) TCCR2B = (TCCR2B & B11111000) | B00000110; // set divisor on timer 2 to result in (approx) 122.55Hz #endif @@ -257,7 +262,7 @@ void MotorDriver::setDCSignal(byte speedcode) { // spedcoode is a dcc speed & direction byte tSpeed=speedcode & 0x7F; // DCC Speed with 0,1 stop and speed steps 2 to 127 byte tDir=speedcode & 0x80; - byte brake; + byte pwmratio; #if defined(ARDUINO_ARCH_ESP32) { int f = 131; @@ -266,18 +271,18 @@ void MotorDriver::setDCSignal(byte speedcode) { f = taurustones[ (tSpeed-2)/2 ] ; } } - DCCEXanalogWriteFrequency(brakePin, f); // set DC PWM frequency to 100Hz XXX May move to setup + DCCEXanalogWriteFrequency(powerPin, f); // set DC PWM frequency to 100Hz XXX May move to setup } #endif - if (tSpeed <= 1) brake = 255; - else if (tSpeed >= 127) brake = 0; - else brake = 2 * (128-tSpeed); - if (invertBrake) - brake=255-brake; + if (tSpeed <= 1) pwmratio = 0; + else if (tSpeed >= 127) pwmratio = 255; + else pwmratio = 2 * tSpeed; + if (invertPower) + pwmratio =255-pwmratio; #if defined(ARDUINO_ARCH_ESP32) - DCCEXanalogWrite(brakePin,brake); + DCCEXanalogWrite(powerPin,pwmratio); #else - analogWrite(brakePin,brake); + analogWrite(powerPin,pwmratio); #endif //DIAG(F("DCSignal %d"), speedcode); if (HAVE_PORTA(fastSignalPin.shadowinout == &PORTA)) { diff --git a/MotorDriver.h b/MotorDriver.h index 51f7654..e4fbb21 100644 --- a/MotorDriver.h +++ b/MotorDriver.h @@ -152,6 +152,7 @@ class MotorDriver { #else setDCSignal(128); #endif + DCinuse = false; }; int getCurrentRaw(bool fromISR=false); unsigned int raw2mA( int raw); @@ -232,6 +233,7 @@ class MotorDriver { static const int TRIP_CURRENT_PROG=250; unsigned long power_sample_overload_wait = POWER_SAMPLE_OVERLOAD_WAIT; unsigned int power_good_counter = 0; - + bool DCinuse = false; + byte curSpeedCode = 0; }; #endif diff --git a/TrackManager.cpp b/TrackManager.cpp index cbb672a..83f8152 100644 --- a/TrackManager.cpp +++ b/TrackManager.cpp @@ -218,7 +218,7 @@ bool TrackManager::setTrackMode(byte trackToSet, TRACK_MODE mode, int16_t dcAddr if (!(mode==TRACK_MODE_DC || mode==TRACK_MODE_DCX)) { // DCC tracks need to have set the PWM to zero or they will not work. track[trackToSet]->detachDCSignal(); - track[trackToSet]->setBrake(false); + //track[trackToSet]->setBrake(false); } // EXT is a special case where the signal pin is @@ -396,7 +396,7 @@ void TrackManager::setPower2(bool setProg,POWERMODE mode) { case TRACK_MODE_DC: case TRACK_MODE_DCX: if (setProg) break; - driver->setBrake(true); // DC starts with brake on + //driver->setBrake(true); // DC starts with brake on applyDCSpeed(t); // speed match DCC throttles driver->setPower(mode); break;