Compare commits

...

2 Commits

View File

@@ -122,6 +122,25 @@ unsigned long lastPacketTime = 0;
const unsigned long watchdogTimeout = 5000; // 5 seconds const unsigned long watchdogTimeout = 5000; // 5 seconds
unsigned long lastFadeTime = 0; unsigned long lastFadeTime = 0;
// -------------------------------
// Animation tuning
// -------------------------------
const float FADE_IN_FACTOR = 0.999f; // boot-up 0 → 100%
const float FADE_OUT_FACTOR = 0.999f; // watchdog 100% → 0
const unsigned long FADE_INTERVAL = 1; // ms between fade steps
// -------------------------------
// Connection state machine
// -------------------------------
enum ConnectionState {
STATE_DISCONNECTED, // fade to zero
STATE_CONNECTING, // fade in 0 → 100%
STATE_WAIT_FOR_FIRST_PACKET, // hold at 100%, wait for first UDP
STATE_CONNECTED // normal UDP-driven slew
};
ConnectionState connectionState = STATE_DISCONNECTED;
// ------------------------------- // -------------------------------
// ESPUI controls // ESPUI controls
// ------------------------------- // -------------------------------
@@ -162,8 +181,6 @@ float applyCalibration(uint8_t ch, float logicalDuty) {
// Calibration UI helpers & callbacks // Calibration UI helpers & callbacks
// ------------------------------- // -------------------------------
void refreshCalibrationUI() { void refreshCalibrationUI() {
String label = "CH" + String(selectedCalChannel) + " (" + channelLabels[selectedCalChannel] + ")";
for (int i = 0; i < 5; i++) { for (int i = 0; i < 5; i++) {
ESPUI.updateControlValue(calInputs[i], String(calibratedPoints[selectedCalChannel][i], 2)); ESPUI.updateControlValue(calInputs[i], String(calibratedPoints[selectedCalChannel][i], 2));
} }
@@ -183,7 +200,6 @@ void calChannelCallback(Control *sender, int type) {
refreshCalibrationUI(); refreshCalibrationUI();
} }
void calPointCallback(Control *sender, int type) { void calPointCallback(Control *sender, int type) {
int index = sender->id - calInputs[0]; int index = sender->id - calInputs[0];
if (index >= 0 && index < 5) { if (index >= 0 && index < 5) {
@@ -507,7 +523,7 @@ void setup() {
); );
// Start ESPUI // Start ESPUI
ESPUI.sliderContinuous = true; // < enables live slider updates ESPUI.sliderContinuous = true; // enables live slider updates
ESPUI.begin("Analog Monitor UI"); ESPUI.begin("Analog Monitor UI");
} }
@@ -535,9 +551,61 @@ void loop() {
if (idx == NUM_CHANNELS) { if (idx == NUM_CHANNELS) {
// Start a new slew toward the target if (connectionState == STATE_DISCONNECTED) {
// First valid packet after being disconnected → start CONNECTING
Serial.println("STATE CHANGE: DISCONNECTED → CONNECTING (UDP connection established)");
connectionState = STATE_CONNECTING;
// Initialize fade-in: start from 0 on all non-override channels
for (int ch = 0; ch < NUM_CHANNELS; ch++) { for (int ch = 0; ch < NUM_CHANNELS; ch++) {
if (!overrideActive[ch]) { // < NEW if (!overrideActive[ch]) {
currentDuty[ch] = 0.0f;
}
}
lastPacketTime = millis(); // prevent watchdog during fade-in
// Ignore this packet's values during fade-in (Option B)
}
else if (connectionState == STATE_CONNECTING) {
// Ignore UDP values during fade-in, just keep watchdog alive
lastPacketTime = millis();
}
else if (connectionState == STATE_WAIT_FOR_FIRST_PACKET) {
// First real packet after fade-in completes
Serial.println("STATE CHANGE: WAIT_FOR_FIRST_PACKET → CONNECTED (first UDP packet received)");
for (int ch = 0; ch < NUM_CHANNELS; ch++) {
if (!overrideActive[ch]) {
targetDuty[ch] = values[ch];
slewStartDuty[ch] = currentDuty[ch]; // currently ~100%
}
}
slewStartTime = millis();
lastPacketTime = millis();
connectionState = STATE_CONNECTED;
// Debug output
Serial.println("Received UDP packet (first after fade-in):");
for (int i = 0; i < NUM_CHANNELS; i++) {
Serial.print(" CH");
Serial.print(i);
Serial.print(" (");
Serial.print(channelLabels[i]);
Serial.print("): ");
Serial.print(values[i], 2);
if (channelUnits[i][0] != '\0') {
Serial.print(" ");
Serial.print(channelUnits[i]);
}
Serial.println();
}
Serial.println();
}
else if (connectionState == STATE_CONNECTED) {
// Normal UDP-driven update
for (int ch = 0; ch < NUM_CHANNELS; ch++) {
if (!overrideActive[ch]) {
targetDuty[ch] = values[ch]; targetDuty[ch] = values[ch];
slewStartDuty[ch] = currentDuty[ch]; slewStartDuty[ch] = currentDuty[ch];
} }
@@ -545,7 +613,7 @@ void loop() {
slewStartTime = millis(); slewStartTime = millis();
lastPacketTime = millis(); lastPacketTime = millis();
// -------- Improved Debug Output -------- // Debug output
Serial.println("Received UDP packet:"); Serial.println("Received UDP packet:");
for (int i = 0; i < NUM_CHANNELS; i++) { for (int i = 0; i < NUM_CHANNELS; i++) {
Serial.print(" CH"); Serial.print(" CH");
@@ -563,16 +631,26 @@ void loop() {
Serial.println(); Serial.println();
} }
} }
}
// -------- Slew-rate limiting -------- // -------- Connection state machine --------
if (millis() - lastPacketTime <= watchdogTimeout) {
unsigned long now = millis(); unsigned long now = millis();
switch (connectionState) {
case STATE_CONNECTED: {
// Check for lost connection
if (now - lastPacketTime > watchdogTimeout) {
Serial.println("STATE CHANGE: CONNECTED → DISCONNECTED (UDP connection lost)");
connectionState = STATE_DISCONNECTED;
break;
}
// Normal slew-rate limiting
float progress = (float)(now - slewStartTime) / (float)slewDuration; float progress = (float)(now - slewStartTime) / (float)slewDuration;
if (progress > 1.0f) progress = 1.0f; if (progress > 1.0f) progress = 1.0f;
for (int ch = 0; ch < NUM_CHANNELS; ch++) { for (int ch = 0; ch < NUM_CHANNELS; ch++) {
if (!overrideActive[ch]) { if (!overrideActive[ch]) {
float newDuty = slewStartDuty[ch] + (targetDuty[ch] - slewStartDuty[ch]) * progress; float newDuty = slewStartDuty[ch] + (targetDuty[ch] - slewStartDuty[ch]) * progress;
currentDuty[ch] = newDuty; currentDuty[ch] = newDuty;
@@ -582,22 +660,63 @@ void loop() {
ledcWrite(pwmPins[ch], duty); ledcWrite(pwmPins[ch], duty);
} }
} }
} break;
case STATE_CONNECTING: {
// If we lose packets even while connecting, fall back to DISCONNECTED
if (now - lastPacketTime > watchdogTimeout) {
Serial.println("STATE CHANGE: CONNECTING → DISCONNECTED (no packets during fade-in)");
connectionState = STATE_DISCONNECTED;
break;
} }
// -------- Watchdog fade-to-zero -------- // Fade-in animation: 0 → 100% on all non-override channels
if (millis() - lastPacketTime > watchdogTimeout) { if (now - lastFadeTime >= FADE_INTERVAL) {
lastFadeTime = now;
const unsigned long fadeInterval = 1; bool allReached = true;
if (millis() - lastFadeTime >= fadeInterval) { for (int ch = 0; ch < NUM_CHANNELS; ch++) {
lastFadeTime = millis(); if (!overrideActive[ch]) {
float cd = currentDuty[ch];
// Exponential approach to 100%
cd = cd * FADE_IN_FACTOR + 100.0f * (1.0f - FADE_IN_FACTOR);
currentDuty[ch] = cd;
float calibratedDuty = applyCalibration(ch, cd);
int duty = (int)((calibratedDuty / 100.0f) * ((1 << pwmResolution) - 1));
ledcWrite(pwmPins[ch], duty);
// Check if we're close enough to 100%
if (cd < 99.0f) {
allReached = false;
}
}
}
if (allReached) {
Serial.println("STATE CHANGE: CONNECTING → STATE_WAIT_FOR_FIRST_PACKET (fade-in complete)");
connectionState = STATE_WAIT_FOR_FIRST_PACKET;
}
}
} break;
case STATE_WAIT_FOR_FIRST_PACKET: {
// Hold at ~100%, do nothing until first UDP packet arrives
// (handled in UDP parsing above)
} break;
case STATE_DISCONNECTED: {
// Watchdog fade-to-zero (always active in this state)
if (now - lastFadeTime >= FADE_INTERVAL) {
lastFadeTime = now;
for (int ch = 0; ch < NUM_CHANNELS; ch++) { for (int ch = 0; ch < NUM_CHANNELS; ch++) {
if (currentDuty[ch] > 0.0f) { if (currentDuty[ch] > 0.0f) {
const float fadeFactor = 0.999f; currentDuty[ch] *= FADE_OUT_FACTOR;
currentDuty[ch] *= fadeFactor;
if (currentDuty[ch] < 0.01f) if (currentDuty[ch] < 0.01f)
currentDuty[ch] = 0.0f; currentDuty[ch] = 0.0f;
@@ -609,5 +728,6 @@ void loop() {
} }
} }
} }
} break;
} }
} }