1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-11-27 10:06:13 +01:00

add broadcastCurrent

This commit is contained in:
Hans R. Tanner 2022-10-05 19:52:52 -04:00
parent d286b3cd00
commit a95a4dded2
6 changed files with 44 additions and 38 deletions

View File

@ -118,6 +118,12 @@ void CommandDistributor::broadcastLoco(byte slot) {
#endif #endif
} }
void CommandDistributor::broadcastCurrent(uint8_t track, uint16_t value)
{
StringFormatter::send(broadcastBufferWriter,F("<a %d %d>\n"), track, value);
broadcast(false);
}
void CommandDistributor::broadcastPower() { void CommandDistributor::broadcastPower() {
bool main=DCCWaveform::mainTrack.getPowerMode()==POWERMODE::ON; bool main=DCCWaveform::mainTrack.getPowerMode()==POWERMODE::ON;
bool prog=DCCWaveform::progTrack.getPowerMode()==POWERMODE::ON; bool prog=DCCWaveform::progTrack.getPowerMode()==POWERMODE::ON;

View File

@ -32,6 +32,7 @@ public :
static void broadcastSensor(int16_t id, bool value); static void broadcastSensor(int16_t id, bool value);
static void broadcastTurnout(int16_t id, bool isClosed); static void broadcastTurnout(int16_t id, bool isClosed);
static void broadcastPower(); static void broadcastPower();
static void broadcastCurrent(uint8_t track, uint16_t value);
static void broadcastText(const FSH * msg); static void broadcastText(const FSH * msg);
static void forget(byte clientId); static void forget(byte clientId);
private: private:

View File

@ -293,10 +293,10 @@ void DCCEXParser::parseOne(Print *stream, byte *com, RingStream * ringStream)
break; break;
case 'A': //switch current reporting on/off case 'A': //switch current reporting on/off
if (params==2) { // <A MAINSTATUS PROGSTATUS> if ((params==2) && (p[0] <= 1) && (p[1] <= 1)) { // <A MAINSTATUS PROGSTATUS> currently only 0,1 higher values reserved for buffer size for local calculation
{ {
DCCWaveform::mainTrack.setRMSMode(p[0], stream); DCCWaveform::mainTrack.setRMSMode(p[0]);
DCCWaveform::progTrack.setRMSMode(p[1], stream); DCCWaveform::progTrack.setRMSMode(p[1]);
} }
return; return;
} }
@ -496,16 +496,16 @@ void DCCEXParser::parseOne(Print *stream, byte *com, RingStream * ringStream)
case 'c': // SEND METER RESPONSES <c> case 'c': // SEND METER RESPONSES <c>
// <c MeterName value C/V unit min max res warn> // <c MeterName value C/V unit min max res warn>
if (params==0) // if (params==0)
{ {
StringFormatter::send(stream, F("<c CurrentMAIN %d C Milli 0 %d 1 %d>\n"), round(DCCWaveform::mainTrack.getCurrentmA()), StringFormatter::send(stream, F("<c CurrentMAIN %d C Milli 0 %d 1 %d>\n"), round(DCCWaveform::mainTrack.getCurrentmA()),
DCCWaveform::mainTrack.getMaxmA(), DCCWaveform::mainTrack.getTripmA()); DCCWaveform::mainTrack.getMaxmA(), DCCWaveform::mainTrack.getTripmA());
// StringFormatter::send(stream, F("<a %d>\n"), DCCWaveform::mainTrack.get1024Current()); //'a' message deprecated, remove once JMRI 4.22 is available // StringFormatter::send(stream, F("<a %d>\n"), DCCWaveform::mainTrack.get1024Current()); //'a' message deprecated, remove once JMRI 4.22 is available
} }
else // else
if (p[0]== 0) // if (p[0]== 0)
StringFormatter::send(stream, F("<c CurrentMAIN %d %d C Milli 0 %d 1 %d>\n"), round(DCCWaveform::mainTrack.getCurrentRMS()), round(DCCWaveform::progTrack.getCurrentRMS()), // StringFormatter::send(stream, F("<c CurrentMAIN %d %d C Milli 0 %d 1 %d>\n"), round(DCCWaveform::mainTrack.getCurrentRMS()), round(DCCWaveform::progTrack.getCurrentRMS()),
DCCWaveform::mainTrack.getMaxmA(), DCCWaveform::mainTrack.getTripmA()); // DCCWaveform::mainTrack.getMaxmA(), DCCWaveform::mainTrack.getTripmA());
return; return;
case 'Q': // SENSORS <Q> case 'Q': // SENSORS <Q>

View File

@ -24,11 +24,11 @@
#include <Arduino.h> #include <Arduino.h>
#include "StringFormatter.h"
#include "DCCWaveform.h" #include "DCCWaveform.h"
#include "DCCTimer.h" #include "DCCTimer.h"
#include "DIAG.h" #include "DIAG.h"
#include "freeMemory.h" #include "freeMemory.h"
#include "CommandDistributor.h"
DCCWaveform DCCWaveform::mainTrack(PREAMBLE_BITS_MAIN, true); DCCWaveform DCCWaveform::mainTrack(PREAMBLE_BITS_MAIN, true);
DCCWaveform DCCWaveform::progTrack(PREAMBLE_BITS_PROG, false); DCCWaveform DCCWaveform::progTrack(PREAMBLE_BITS_PROG, false);
@ -135,17 +135,17 @@ void DCCWaveform::checkPowerOverload(bool ackManagerActive) {
switch (powerMode) { switch (powerMode) {
case POWERMODE::OFF: case POWERMODE::OFF:
sampleDelay = POWER_SAMPLE_OFF_WAIT; sampleDelay = POWER_SAMPLE_OFF_WAIT;
if (sendCurrentSample || (accuSize > 0)) if (sendCurrentSample) // || (accuSize > 0))
StringFormatter::send(outStream, F("<a %d %d>\n"), isMainTrack ? 0 : 1, 0); CommandDistributor::broadcastCurrent(isMainTrack ? 0 : 1, 0);
break; break;
case POWERMODE::ON: case POWERMODE::ON:
// Check current // Check current
lastCurrent=motorDriver->getCurrentRaw(); lastCurrent=motorDriver->getCurrentRaw();
if (sendCurrentSample) if (sendCurrentSample) // || (accuSize > 0))
StringFormatter::send(outStream, F("<a %d %d>\n"), isMainTrack ? 0 : 1, getCurrentmA()); CommandDistributor::broadcastCurrent(isMainTrack ? 0 : 1, getCurrentmA());
else // else
if (accuSize > 0) // if (accuSize > 0)
currAccu = (currAccu * accuFact) + (sq((float)getCurrentmA())); // currAccu = (currAccu * accuFact) + (sq((float)getCurrentmA()));
if (lastCurrent < 0) { if (lastCurrent < 0) {
// We have a fault pin condition to take care of // We have a fault pin condition to take care of
lastCurrent = -lastCurrent; lastCurrent = -lastCurrent;

View File

@ -25,6 +25,7 @@
#define DCCWaveform_h #define DCCWaveform_h
#include "MotorDriver.h" #include "MotorDriver.h"
#include "StringFormatter.h"
// Wait times for power management. Unit: milliseconds // Wait times for power management. Unit: milliseconds
const int POWER_SAMPLE_ON_WAIT = 100; const int POWER_SAMPLE_ON_WAIT = 100;
@ -57,7 +58,6 @@ class DCCWaveform {
static void loop(bool ackManagerActive); static void loop(bool ackManagerActive);
static DCCWaveform mainTrack; static DCCWaveform mainTrack;
static DCCWaveform progTrack; static DCCWaveform progTrack;
void beginTrack(); void beginTrack();
void setPowerMode(POWERMODE); void setPowerMode(POWERMODE);
POWERMODE getPowerMode(); POWERMODE getPowerMode();
@ -72,22 +72,21 @@ class DCCWaveform {
return motorDriver->raw2mA(lastCurrent); return motorDriver->raw2mA(lastCurrent);
return 0; return 0;
} }
inline void setRMSMode(byte newMode, Print* stream) { //0: OFF 1: Broadcast mA Reading >1: Internal calc buffer size inline void setRMSMode(byte newMode) { //0: OFF 1: Broadcast mA Reading >1: Internal calc buffer size
outStream = stream; sendCurrentSample = (newMode == 0x01);
sendCurrentSample = newMode == 1; // accuSize = newMode;
accuSize = newMode; // if (newMode > 1)
if (newMode > 1) // {
{ // accuFact = (float)(newMode - 1) / newMode;
accuFact = (float)(newMode - 1) / newMode; // currAccu = 0;
currAccu = 0; // }
}
}
inline float getCurrentRMS() {
if (accuSize == 0)
return 0;
else
return sqrt(currAccu / accuSize);
} }
// inline float getCurrentRMS() {
// if (accuSize == 0)
// return 0;
// else
// return sqrt(currAccu / accuSize);
// }
inline int getMaxmA() { inline int getMaxmA() {
if (maxmA == 0) { //only calculate this for first request, it doesn't change if (maxmA == 0) { //only calculate this for first request, it doesn't change
maxmA = motorDriver->raw2mA(motorDriver->getRawCurrentTripValue()); //TODO: replace with actual max value or calc maxmA = motorDriver->raw2mA(motorDriver->getRawCurrentTripValue()); //TODO: replace with actual max value or calc
@ -169,11 +168,10 @@ class DCCWaveform {
unsigned long power_sample_overload_wait = POWER_SAMPLE_OVERLOAD_WAIT; unsigned long power_sample_overload_wait = POWER_SAMPLE_OVERLOAD_WAIT;
unsigned int power_good_counter = 0; unsigned int power_good_counter = 0;
bool sendCurrentSample; bool sendCurrentSample = false;
Print* outStream; // volatile double currAccu = 0;
volatile double currAccu = 0; // byte accuSize = 0;
byte accuSize = 0; // float accuFact = 0;
float accuFact = 0;
// ACK management (Prog track only) // ACK management (Prog track only)
volatile bool ackPending; volatile bool ackPending;
volatile bool ackDetected; volatile bool ackDetected;

View File

@ -21,6 +21,8 @@
#include "SerialManager.h" #include "SerialManager.h"
#include "DCCEXParser.h" #include "DCCEXParser.h"
#include "DCCWaveform.h"
SerialManager * SerialManager::first=NULL; SerialManager * SerialManager::first=NULL;
SerialManager::SerialManager(Stream * myserial) { SerialManager::SerialManager(Stream * myserial) {
@ -78,5 +80,4 @@ void SerialManager::loop2() {
if (bufferLength < (COMMAND_BUFFER_SIZE-1)) buffer[bufferLength++] = ch; if (bufferLength < (COMMAND_BUFFER_SIZE-1)) buffer[bufferLength++] = ch;
} }
} }
} }