2022-02-22 02:27:27 +01:00
|
|
|
/*
|
|
|
|
* © 2022 Chris Harlow
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
2022-03-18 21:03:19 +01:00
|
|
|
* This file is part of DCC++EX
|
2022-02-22 02:27:27 +01:00
|
|
|
*
|
|
|
|
* This is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* It is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with CommandStation. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#include "TrackManager.h"
|
|
|
|
#include "FSH.h"
|
2022-02-23 16:44:34 +01:00
|
|
|
#include "DCCWaveform.h"
|
2022-02-28 11:38:26 +01:00
|
|
|
#include "DCC.h"
|
2022-02-22 02:27:27 +01:00
|
|
|
#include "MotorDriver.h"
|
|
|
|
#include "DIAG.h"
|
|
|
|
// Virtualised Motor shield multi-track hardware Interface
|
2022-02-28 10:32:26 +01:00
|
|
|
#define FOR_EACH_TRACK(t) for (byte t=0;t<=lastTrack;t++)
|
|
|
|
|
|
|
|
#define APPLY_BY_MODE(findmode,function) \
|
|
|
|
FOR_EACH_TRACK(t) \
|
2022-02-22 02:27:27 +01:00
|
|
|
if (trackMode[t]==findmode) \
|
|
|
|
track[t]->function;
|
|
|
|
|
|
|
|
const int16_t HASH_KEYWORD_PROG = -29718;
|
|
|
|
const int16_t HASH_KEYWORD_MAIN = 11339;
|
2022-02-24 12:58:40 +01:00
|
|
|
const int16_t HASH_KEYWORD_OFF = 22479;
|
|
|
|
const int16_t HASH_KEYWORD_DC = 2183;
|
2022-03-19 17:26:29 +01:00
|
|
|
const int16_t HASH_KEYWORD_DCX = 6463; // DC reversed polarity
|
2022-03-18 17:41:52 +01:00
|
|
|
const int16_t HASH_KEYWORD_A = 65; // parser makes single chars the ascii.
|
2022-02-22 02:27:27 +01:00
|
|
|
|
|
|
|
MotorDriver * TrackManager::track[MAX_TRACKS];
|
2022-03-19 17:26:29 +01:00
|
|
|
TRACK_MODE TrackManager::trackMode[MAX_TRACKS];
|
|
|
|
int16_t TrackManager::trackDCAddr[MAX_TRACKS];
|
|
|
|
|
2022-02-28 10:32:26 +01:00
|
|
|
POWERMODE TrackManager::mainPowerGuess=POWERMODE::OFF;
|
|
|
|
byte TrackManager::lastTrack=0;
|
2022-03-19 12:22:31 +01:00
|
|
|
bool TrackManager::progTrackSyncMain=false;
|
|
|
|
bool TrackManager::progTrackBoosted=false;
|
|
|
|
int16_t TrackManager::joinRelay=UNUSED_PIN;
|
|
|
|
|
2022-02-23 16:44:34 +01:00
|
|
|
|
2022-02-28 10:32:26 +01:00
|
|
|
// The setup call is done this way so that the tracks can be in a list
|
|
|
|
// from the config... the tracks default to NULL in the declaration
|
2022-02-22 02:27:27 +01:00
|
|
|
void TrackManager::Setup(const FSH * shieldname,
|
|
|
|
MotorDriver * track0, MotorDriver * track1, MotorDriver * track2,
|
|
|
|
MotorDriver * track3, MotorDriver * track4, MotorDriver * track5,
|
2022-03-01 13:52:25 +01:00
|
|
|
MotorDriver * track6, MotorDriver * track7 ) {
|
2022-02-28 10:32:26 +01:00
|
|
|
addTrack(0,track0);
|
|
|
|
addTrack(1,track1);
|
|
|
|
addTrack(2,track2);
|
|
|
|
addTrack(3,track3);
|
|
|
|
addTrack(4,track4);
|
|
|
|
addTrack(5,track5);
|
|
|
|
addTrack(6,track6);
|
|
|
|
addTrack(7,track7);
|
|
|
|
|
2022-03-19 17:26:29 +01:00
|
|
|
// Default the first 2 tracks (which may be null) and perform HA waveform check.
|
2022-02-23 16:44:34 +01:00
|
|
|
setTrackMode(0,TRACK_MODE_MAIN);
|
|
|
|
setTrackMode(1,TRACK_MODE_PROG);
|
2022-02-28 10:32:26 +01:00
|
|
|
|
|
|
|
// TODO Fault pin config for odd motor boards (example pololu)
|
2022-02-23 16:44:34 +01:00
|
|
|
// MotorDriver::commonFaultPin = ((mainDriver->getFaultPin() == progDriver->getFaultPin())
|
|
|
|
// && (mainDriver->getFaultPin() != UNUSED_PIN));
|
|
|
|
DIAG(F("Signal pin config: %S accuracy waveform"),
|
|
|
|
MotorDriver::usePWM ? F("high") : F("normal") );
|
2022-02-28 11:38:26 +01:00
|
|
|
DCC::begin(shieldname);
|
2022-02-22 02:27:27 +01:00
|
|
|
}
|
2022-02-28 10:32:26 +01:00
|
|
|
|
|
|
|
void TrackManager::addTrack(byte t, MotorDriver* driver) {
|
|
|
|
track[t]=driver;
|
|
|
|
trackMode[t]=TRACK_MODE_OFF;
|
2022-03-23 18:06:15 +01:00
|
|
|
if (driver) {
|
|
|
|
lastTrack=t;
|
|
|
|
track[t]->setPower(POWERMODE::OFF);
|
|
|
|
}
|
2022-02-28 10:32:26 +01:00
|
|
|
}
|
|
|
|
|
2022-02-22 02:27:27 +01:00
|
|
|
void TrackManager::setDCCSignal( bool on) {
|
2022-02-28 10:32:26 +01:00
|
|
|
APPLY_BY_MODE(TRACK_MODE_MAIN,setSignal(on));
|
2022-02-22 02:27:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void TrackManager::setCutout( bool on) {
|
|
|
|
(void) on;
|
2022-02-28 10:32:26 +01:00
|
|
|
// TODO APPLY_BY_MODE(TRACK_MODE_MAIN,setCutout(on));
|
2022-02-22 02:27:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void TrackManager::setPROGSignal( bool on) {
|
2022-02-28 10:32:26 +01:00
|
|
|
APPLY_BY_MODE(TRACK_MODE_PROG,setSignal(on));
|
2022-02-22 02:27:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void TrackManager::setDCSignal(int16_t cab, byte speedbyte) {
|
2022-03-19 17:26:29 +01:00
|
|
|
FOR_EACH_TRACK(t) {
|
|
|
|
if (trackDCAddr[t]!=cab) continue;
|
|
|
|
if (trackMode[t]==TRACK_MODE_DC) track[t]->setDCSignal(speedbyte);
|
|
|
|
else if (trackMode[t]==TRACK_MODE_DCX) track[t]->setDCSignal(speedbyte ^ 128);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-22 02:27:27 +01:00
|
|
|
|
2022-03-19 17:26:29 +01:00
|
|
|
bool TrackManager::setTrackMode(byte trackToSet, TRACK_MODE mode, int16_t dcAddr) {
|
2022-02-28 10:32:26 +01:00
|
|
|
if (trackToSet>lastTrack || track[trackToSet]==NULL) return false;
|
2022-03-19 17:26:29 +01:00
|
|
|
if (mode==TRACK_MODE_PROG) {
|
2022-02-23 16:44:34 +01:00
|
|
|
// only allow 1 track to be prog
|
2022-02-28 10:32:26 +01:00
|
|
|
FOR_EACH_TRACK(t)
|
2022-03-23 18:06:15 +01:00
|
|
|
if (trackMode[t]==TRACK_MODE_PROG) {
|
|
|
|
track[t]->setPower(POWERMODE::OFF);
|
|
|
|
trackMode[t]=TRACK_MODE_OFF;
|
|
|
|
}
|
2022-02-23 16:44:34 +01:00
|
|
|
}
|
2022-03-19 17:26:29 +01:00
|
|
|
trackMode[trackToSet]=mode;
|
|
|
|
trackDCAddr[trackToSet]=dcAddr;
|
|
|
|
|
2022-02-22 02:27:27 +01:00
|
|
|
// re-evaluate HighAccuracy mode
|
2022-02-28 10:32:26 +01:00
|
|
|
// We can only do this is all main and prog tracks agree
|
2022-02-22 02:27:27 +01:00
|
|
|
bool canDo=true;
|
2022-02-28 10:32:26 +01:00
|
|
|
FOR_EACH_TRACK(t)
|
2022-02-23 16:44:34 +01:00
|
|
|
if (trackMode[t]==TRACK_MODE_MAIN ||trackMode[t]==TRACK_MODE_PROG)
|
2022-02-22 02:27:27 +01:00
|
|
|
canDo &= track[t]->isPWMCapable();
|
|
|
|
MotorDriver::usePWM=canDo;
|
2022-03-23 18:06:15 +01:00
|
|
|
|
|
|
|
// Normal running tracks are set to the global power state
|
|
|
|
track[trackToSet]->setPower(
|
|
|
|
(mode==TRACK_MODE_MAIN || mode==TRACK_MODE_DC || mode==TRACK_MODE_DCX) ?
|
|
|
|
mainPowerGuess : POWERMODE::OFF);
|
|
|
|
|
2022-02-22 02:27:27 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TrackManager::parseJ(Print *stream, int16_t params, int16_t p[])
|
|
|
|
{
|
|
|
|
|
2022-03-18 17:41:52 +01:00
|
|
|
if (params==0) { // <=> List track assignments
|
2022-02-28 10:32:26 +01:00
|
|
|
FOR_EACH_TRACK(t)
|
|
|
|
if (track[t]!=NULL) {
|
2022-03-18 17:41:52 +01:00
|
|
|
StringFormatter::send(stream,F("<= %c "),'A'+t);
|
2022-02-28 10:32:26 +01:00
|
|
|
switch(trackMode[t]) {
|
|
|
|
case TRACK_MODE_MAIN:
|
|
|
|
StringFormatter::send(stream,F("MAIN"));
|
|
|
|
break;
|
|
|
|
case TRACK_MODE_PROG:
|
|
|
|
StringFormatter::send(stream,F("PROG"));
|
|
|
|
break;
|
|
|
|
case TRACK_MODE_OFF:
|
|
|
|
StringFormatter::send(stream,F("OFF"));
|
|
|
|
break;
|
2022-03-19 17:26:29 +01:00
|
|
|
case TRACK_MODE_DC:
|
|
|
|
StringFormatter::send(stream,F("DC %d"),trackDCAddr[t]);
|
|
|
|
break;
|
|
|
|
case TRACK_MODE_DCX:
|
|
|
|
StringFormatter::send(stream,F("DCX %d"),trackDCAddr[t]);
|
|
|
|
break;
|
2022-02-28 10:32:26 +01:00
|
|
|
default:
|
2022-03-19 17:26:29 +01:00
|
|
|
break; // unknown, dont care
|
2022-02-28 10:32:26 +01:00
|
|
|
}
|
|
|
|
StringFormatter::send(stream,F(">\n"));
|
|
|
|
}
|
2022-02-22 02:27:27 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-03-18 17:41:52 +01:00
|
|
|
p[0]-=HASH_KEYWORD_A; // convert A... to 0....
|
|
|
|
|
|
|
|
if (params>1 && (p[0]<0 || p[0]>=MAX_TRACKS))
|
2022-02-22 02:27:27 +01:00
|
|
|
return false;
|
|
|
|
|
2022-03-18 17:41:52 +01:00
|
|
|
if (params==2 && p[1]==HASH_KEYWORD_MAIN) // <= id MAIN>
|
|
|
|
return setTrackMode(p[0],TRACK_MODE_MAIN);
|
2022-02-22 02:27:27 +01:00
|
|
|
|
2022-03-18 17:41:52 +01:00
|
|
|
if (params==2 && p[1]==HASH_KEYWORD_PROG) // <= id PROG>
|
|
|
|
return setTrackMode(p[0],TRACK_MODE_PROG);
|
2022-02-22 02:27:27 +01:00
|
|
|
|
2022-03-18 17:41:52 +01:00
|
|
|
if (params==2 && p[1]==HASH_KEYWORD_OFF) // <= id OFF>
|
|
|
|
return setTrackMode(p[0],TRACK_MODE_OFF);
|
2022-02-22 02:27:27 +01:00
|
|
|
|
2022-03-18 21:03:19 +01:00
|
|
|
if (params==3 && p[1]==HASH_KEYWORD_DC && p[2]>0) // <= id DC cab>
|
2022-03-19 17:26:29 +01:00
|
|
|
return setTrackMode(p[0],TRACK_MODE_DC,p[2]);
|
|
|
|
|
|
|
|
if (params==3 && p[1]==HASH_KEYWORD_DCX && p[2]>0) // <= id DCX cab>
|
|
|
|
return setTrackMode(p[0],TRACK_MODE_DCX,p[2]);
|
2022-02-22 02:27:27 +01:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-02-23 16:44:34 +01:00
|
|
|
byte TrackManager::nextCycleTrack=MAX_TRACKS;
|
|
|
|
|
2022-03-19 12:22:31 +01:00
|
|
|
void TrackManager::loop() {
|
|
|
|
DCCWaveform::loop();
|
|
|
|
DCCACK::loop();
|
|
|
|
bool dontLimitProg=DCCACK::isActive() || progTrackSyncMain || progTrackBoosted;
|
2022-02-23 16:44:34 +01:00
|
|
|
nextCycleTrack++;
|
2022-02-28 10:32:26 +01:00
|
|
|
if (nextCycleTrack>lastTrack) nextCycleTrack=0;
|
2022-02-23 16:44:34 +01:00
|
|
|
if (track[nextCycleTrack]==NULL) return;
|
|
|
|
MotorDriver * motorDriver=track[nextCycleTrack];
|
|
|
|
bool useProgLimit=dontLimitProg? false: trackMode[nextCycleTrack]==TRACK_MODE_PROG;
|
|
|
|
motorDriver->checkPowerOverload(useProgLimit, nextCycleTrack);
|
|
|
|
}
|
2022-02-22 02:27:27 +01:00
|
|
|
|
2022-02-23 16:44:34 +01:00
|
|
|
MotorDriver * TrackManager::getProgDriver() {
|
2022-02-28 10:32:26 +01:00
|
|
|
FOR_EACH_TRACK(t)
|
2022-02-23 16:44:34 +01:00
|
|
|
if (trackMode[t]==TRACK_MODE_PROG) return track[t];
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
void TrackManager::setPower2(bool setProg,POWERMODE mode) {
|
|
|
|
if (setProg) {
|
2022-02-28 10:32:26 +01:00
|
|
|
APPLY_BY_MODE(TRACK_MODE_PROG,setPower(mode))
|
2022-02-23 16:44:34 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
mainPowerGuess=mode;
|
2022-02-28 10:32:26 +01:00
|
|
|
FOR_EACH_TRACK(t)
|
2022-02-23 16:44:34 +01:00
|
|
|
if (track[t]
|
|
|
|
&& trackMode[t]!=TRACK_MODE_OFF
|
|
|
|
&& trackMode[t]!=TRACK_MODE_PROG
|
|
|
|
) track[t]->setPower(mode);
|
|
|
|
}
|
|
|
|
}
|
2022-02-28 10:32:26 +01:00
|
|
|
|
|
|
|
POWERMODE TrackManager::getProgPower() {
|
|
|
|
FOR_EACH_TRACK(t)
|
|
|
|
if (trackMode[t]==TRACK_MODE_PROG)
|
2022-02-23 16:44:34 +01:00
|
|
|
return track[t]->getPower();
|
2022-02-28 10:32:26 +01:00
|
|
|
return POWERMODE::OFF;
|
2022-02-23 16:44:34 +01:00
|
|
|
}
|
|
|
|
|
2022-03-19 12:22:31 +01:00
|
|
|
void TrackManager::setJoinRelayPin(byte joinRelayPin) {
|
|
|
|
joinRelay=joinRelayPin;
|
|
|
|
if (joinRelay!=UNUSED_PIN) {
|
|
|
|
pinMode(joinRelay,OUTPUT);
|
|
|
|
digitalWrite(joinRelay,LOW); // LOW is relay disengaged
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TrackManager::setJoin(bool joined) {
|
|
|
|
progTrackSyncMain=joined;
|
|
|
|
if (joinRelay!=UNUSED_PIN) digitalWrite(joinRelay,joined?HIGH:LOW);
|
|
|
|
}
|