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

improve current sense sampling on AVR

This commit is contained in:
Harald Barth 2022-10-02 13:40:46 +02:00
parent 24a7475482
commit 24e5e648b8
4 changed files with 20 additions and 12 deletions

View File

@ -81,11 +81,12 @@ void DCCWaveform::interruptHandler() {
TrackManager::setDCCSignal(sigMain); TrackManager::setDCCSignal(sigMain);
TrackManager::setPROGSignal(sigProg); TrackManager::setPROGSignal(sigProg);
TrackManager::sampleCurrent();
// Move on in the state engine // Move on in the state engine
mainTrack.state=stateTransform[mainTrack.state]; mainTrack.state=stateTransform[mainTrack.state];
progTrack.state=stateTransform[progTrack.state]; progTrack.state=stateTransform[progTrack.state];
TrackManager::sampleCurrent();
// WAVE_PENDING means we dont yet know what the next bit is // WAVE_PENDING means we dont yet know what the next bit is
if (mainTrack.state==WAVE_PENDING) mainTrack.interrupt2(); if (mainTrack.state==WAVE_PENDING) mainTrack.interrupt2();
if (progTrack.state==WAVE_PENDING) progTrack.interrupt2(); if (progTrack.state==WAVE_PENDING) progTrack.interrupt2();

View File

@ -1 +1 @@
#define GITHUB_SHA "PORTX-HAL-202210020044" #define GITHUB_SHA "PORTX-HAL-202210021139Z"

View File

@ -226,7 +226,7 @@ int MotorDriver::getCurrentRaw() {
// noInterrupts(); // noInterrupts();
//current = analogRead(currentPin)-senseOffset; //current = analogRead(currentPin)-senseOffset;
current = sampleCurrent-senseOffset; current = sampleCurrent-senseOffset;
DIAG(F("%d %d"), current, sampleCurrentTimestamp); //DIAG(F("%d %d"), current, sampleCurrentTimestamp);
if ((millis() - sampleCurrentTimestamp) > 3) if ((millis() - sampleCurrentTimestamp) > 3)
DIAG(F("Current sample old %d"), millis() - sampleCurrentTimestamp); DIAG(F("Current sample old %d"), millis() - sampleCurrentTimestamp);
//interrupts(); //interrupts();

View File

@ -59,32 +59,39 @@ byte TrackManager::tempProgTrack=MAX_TRACKS+1;
*/ */
void TrackManager::sampleCurrent() { void TrackManager::sampleCurrent() {
static byte tr = 0; static byte tr = 0;
byte trAtStart = tr;
static bool waiting = false; static bool waiting = false;
byte count = MAX_TRACKS-1;
if (waiting) { if (waiting) {
if (! track[tr]->sampleCurrentFromHW()) { if (! track[tr]->sampleCurrentFromHW()) {
return; // no result, continue to wait return; // no result, continue to wait
} }
// found value, advance at least one track // found value, advance at least one track
// for scope debug track[1]->setBrake(0);
waiting = false; waiting = false;
tr++; tr++;
if (tr >= MAX_TRACKS) tr = 0; if (tr > lastTrack) tr = 0;
if (lastTrack < 2 || trackMode[tr] & TRACK_MODE_PROG) {
return; // We could continue but for prog track we
// rather do it in next interrupt beacuse
// that gives us well defined sampling point.
// For other tracks we care less unless we
// have only few (max 2) tracks.
}
} }
if (!waiting) { if (!waiting) {
// look for a valid track to sample or until we are around // look for a valid track to sample or until we are around
while (count) { while (true) {
if (trackMode[tr] & ( TRACK_MODE_MAIN|TRACK_MODE_PROG|TRACK_MODE_DC|TRACK_MODE_DCX|TRACK_MODE_EXT )) { if (trackMode[tr] & ( TRACK_MODE_MAIN|TRACK_MODE_PROG|TRACK_MODE_DC|TRACK_MODE_DCX|TRACK_MODE_EXT )) {
track[tr]->startCurrentFromHW(); track[tr]->startCurrentFromHW();
// for scope debug track[1]->setBrake(1);
waiting = true; waiting = true;
break; break;
} }
tr++; tr++;
if (tr >= MAX_TRACKS) tr = 0; if (tr > lastTrack) tr = 0;
count--; if (tr == trAtStart) // we are through and nothing found to do
} return;
if (count == 0) {
DIAG(F("WRONG"));
} }
} }
} }