From 136e9934185683f71e9c108b537f6c312344264e Mon Sep 17 00:00:00 2001 From: Mike S Date: Sat, 27 Mar 2021 07:17:47 -0400 Subject: [PATCH] Changed to Continuous analogReads for Teensy --- DCCWaveform.cpp | 17 +++-------------- DCCWaveform.h | 1 + MotorDriver.cpp | 39 +++++++++++++++++++++++++++++++++++++-- MotorDriver.h | 3 +-- WiThrottle.cpp | 2 +- WiThrottle.h | 2 +- 6 files changed, 44 insertions(+), 20 deletions(-) diff --git a/DCCWaveform.cpp b/DCCWaveform.cpp index 125bc93..68549c9 100644 --- a/DCCWaveform.cpp +++ b/DCCWaveform.cpp @@ -24,12 +24,10 @@ #include "DCCTimer.h" #include "DIAG.h" #include "freeMemory.h" - DCCWaveform DCCWaveform::mainTrack(PREAMBLE_BITS_MAIN, true); DCCWaveform DCCWaveform::progTrack(PREAMBLE_BITS_PROG, false); - bool DCCWaveform::progTrackSyncMain=false; bool DCCWaveform::progTrackBoosted=false; int DCCWaveform::progTripValue=0; @@ -52,19 +50,10 @@ void DCCWaveform::begin(MotorDriver * mainDriver, MotorDriver * progDriver) { DCCTimer::begin(DCCWaveform::interruptHandler); } -#if defined(TEENSYDUINO) -void DCCWaveform::loop(bool ackManagerActive) { - noInterrupts(); - mainTrack.checkPowerOverload(false); - progTrack.checkPowerOverload(ackManagerActive); - interrupts(); -} -#else void DCCWaveform::loop(bool ackManagerActive) { mainTrack.checkPowerOverload(false); progTrack.checkPowerOverload(ackManagerActive); } -#endif void DCCWaveform::interruptHandler() { // call the timer edge sensitive actions for progtrack and maintrack @@ -138,7 +127,7 @@ void DCCWaveform::checkPowerOverload(bool ackManagerActive) { break; case POWERMODE::ON: // Check current - lastCurrent=motorDriver->getCurrentRaw(); + lastCurrent=motorDriver->getCurrentRaw(isMainTrack); if (lastCurrent < 0) { // We have a fault pin condition to take care of lastCurrent = -lastCurrent; @@ -286,7 +275,7 @@ void DCCWaveform::schedulePacket(const byte buffer[], byte byteCount, byte repea void DCCWaveform::setAckBaseline() { if (isMainTrack) return; - int baseline=motorDriver->getCurrentRaw(); + int baseline=motorDriver->getCurrentRaw(isMainTrack); ackThreshold= baseline + motorDriver->mA2raw(ackLimitmA); if (Diag::ACK) DIAG(F("ACK baseline=%d/%dmA Threshold=%d/%dmA Duration between %dus and %dus"), baseline,motorDriver->raw2mA(baseline), @@ -320,7 +309,7 @@ void DCCWaveform::checkAck() { return; } - int current=motorDriver->getCurrentRaw(); + int current=motorDriver->getCurrentRaw(isMainTrack); if (current > ackMaxCurrent) ackMaxCurrent=current; // An ACK is a pulse lasting between minAckPulseDuration and maxAckPulseDuration uSecs (refer @haba) diff --git a/DCCWaveform.h b/DCCWaveform.h index f3f26c7..211281b 100644 --- a/DCCWaveform.h +++ b/DCCWaveform.h @@ -19,6 +19,7 @@ */ #ifndef DCCWaveform_h #define DCCWaveform_h + #include "MotorDriver.h" // Wait times for power management. Unit: milliseconds diff --git a/MotorDriver.cpp b/MotorDriver.cpp index c50615a..7f44cd2 100644 --- a/MotorDriver.cpp +++ b/MotorDriver.cpp @@ -20,6 +20,14 @@ #include "MotorDriver.h" #include "DCCTimer.h" #include "DIAG.h" +#if defined(TEENSYDUINO) +#include +#include +ADC *adc = new ADC(); // adc object +#if defined(ARDUINO_TEENSY35) || defined(ARDUINO_TEENSY36) || defined(ARDUINO_TEENSY32) +ADC *adc1 = new ADC(); // adc object +#endif +#endif #define setHIGH(fastpin) *fastpin.inout |= fastpin.maskHIGH #define setLOW(fastpin) *fastpin.inout &= fastpin.maskLOW @@ -63,6 +71,20 @@ MotorDriver::MotorDriver(byte power_pin, byte signal_pin, byte signal_pin2, int8 senseOffset=analogRead(currentPin); // value of sensor at zero current } +#if defined(TEENSYDUINO) + if(currentPin != current_pin && currentPin!=UNUSED_PIN){ + adc->adc0->setReference(ADC_REFERENCE::REF_3V3); + adc->adc0->startContinuous(currentPin); + } else if(currentPin!=UNUSED_PIN){ +#if defined(ARDUINO_TEENSY35) || defined(ARDUINO_TEENSY36) || defined(ARDUINO_TEENSY32) + adc1->adc0->setReference(ADC_REFERENCE::REF_3V3); + adc1->adc0->startContinuous(currentPin); +#else + adc->adc1->setReference(ADC_REFERENCE::REF_3V3); + adc->adc1->startContinuous(currentPin); +#endif + } +#endif faultPin=fault_pin; if (faultPin != UNUSED_PIN) { getFastPin(F("FAULT"),faultPin, 1 /*input*/, fastFaultPin); @@ -137,10 +159,23 @@ bool MotorDriver::canMeasureCurrent() { * senseOffset handles the case where a shield returns values above or below * a central value depending on direction. */ -int MotorDriver::getCurrentRaw() { +int MotorDriver::getCurrentRaw(bool isMain) { if (currentPin==UNUSED_PIN) return 0; - int current = analogRead(currentPin)-senseOffset; + int current; +#if defined(TEENSYDUINO) + if(isMain) { + current = (uint16_t)adc->adc0->analogReadContinuous(); + } else { + #if defined(ARDUINO_TEENSY35) || defined(ARDUINO_TEENSY36) || defined(ARDUINO_TEENSY32) + current = (uint16_t)adc1->adc0->analogReadContinuous(); + #else + current = (uint16_t)adc->adc1->analogReadContinuous(); + #endif + } +#else + current = analogRead(currentPin)-senseOffset; +#endif if (current<0) current=0-current; if ((faultPin != UNUSED_PIN) && isLOW(fastFaultPin) && isHIGH(fastPowerPin)) diff --git a/MotorDriver.h b/MotorDriver.h index f659918..f7d8580 100644 --- a/MotorDriver.h +++ b/MotorDriver.h @@ -47,7 +47,7 @@ class MotorDriver { virtual void setPower( bool on); virtual void setSignal( bool high); virtual void setBrake( bool on); - virtual int getCurrentRaw(); + virtual int getCurrentRaw(bool isMain); virtual unsigned int raw2mA( int raw); virtual int mA2raw( unsigned int mA); inline int getRawCurrentTripValue() { @@ -60,7 +60,6 @@ class MotorDriver { inline byte getFaultPin() { return faultPin; } - private: void getFastPin(const FSH* type,int pin, bool input, FASTPIN & result); void getFastPin(const FSH* type,int pin, FASTPIN & result) { diff --git a/WiThrottle.cpp b/WiThrottle.cpp index ba9198d..f3664a8 100644 --- a/WiThrottle.cpp +++ b/WiThrottle.cpp @@ -388,7 +388,7 @@ WiThrottle * WiThrottle::stashInstance; byte WiThrottle::stashClient; char WiThrottle::stashThrottleChar; -void WiThrottle::getLocoCallback(int locoid) { +void WiThrottle::getLocoCallback(int16_t locoid) { stashStream->mark(stashClient); if (locoid<0) StringFormatter::send(stashStream,F("HMNo loco found on prog track\n")); else { diff --git a/WiThrottle.h b/WiThrottle.h index b065d4f..0f9b573 100644 --- a/WiThrottle.h +++ b/WiThrottle.h @@ -67,7 +67,7 @@ class WiThrottle { static WiThrottle * stashInstance; static byte stashClient; static char stashThrottleChar; - static void getLocoCallback(int locoid); + static void getLocoCallback(int16_t locoid); }; #endif