1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-12-23 12:51:24 +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::setPROGSignal(sigProg);
TrackManager::sampleCurrent();
// Move on in the state engine
mainTrack.state=stateTransform[mainTrack.state];
progTrack.state=stateTransform[progTrack.state];
TrackManager::sampleCurrent();
// WAVE_PENDING means we dont yet know what the next bit is
if (mainTrack.state==WAVE_PENDING) mainTrack.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();
//current = analogRead(currentPin)-senseOffset;
current = sampleCurrent-senseOffset;
DIAG(F("%d %d"), current, sampleCurrentTimestamp);
//DIAG(F("%d %d"), current, sampleCurrentTimestamp);
if ((millis() - sampleCurrentTimestamp) > 3)
DIAG(F("Current sample old %d"), millis() - sampleCurrentTimestamp);
//interrupts();

View File

@ -59,32 +59,39 @@ byte TrackManager::tempProgTrack=MAX_TRACKS+1;
*/
void TrackManager::sampleCurrent() {
static byte tr = 0;
byte trAtStart = tr;
static bool waiting = false;
byte count = MAX_TRACKS-1;
if (waiting) {
if (! track[tr]->sampleCurrentFromHW()) {
return; // no result, continue to wait
}
// found value, advance at least one track
// for scope debug track[1]->setBrake(0);
waiting = false;
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) {
// 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 )) {
track[tr]->startCurrentFromHW();
// for scope debug track[1]->setBrake(1);
waiting = true;
break;
}
tr++;
if (tr >= MAX_TRACKS) tr = 0;
count--;
}
if (count == 0) {
DIAG(F("WRONG"));
if (tr > lastTrack) tr = 0;
if (tr == trAtStart) // we are through and nothing found to do
return;
}
}
}