Tach output: hardware-timer ISR, slew glide, adaptive dither
The Patrol cluster counts pulses in ~150 rpm quanta, so the needle stepped no matter how clean the data was. Three-stage rework of the tach path in gauge_outputs: - Drop LEDC for the tach: Arduino 2.x ledcChangeFrequency() re-inits the timer and truncates the waveform mid-period, corrupting the cluster's counting window on every update. IO7 is now toggled from a 1 MHz hardware-timer ISR with auto-reload - phase-continuous through every frequency change and immune to task scheduling (an esp_timer version wobbled under WiFi bursts). - Slew limiter: update() (5 Hz packets) only sets a target; tick(), called every 20 ms from loop(), glides the output 25%/tick toward it. - Adaptive dither: random +/-1.2 Hz (~45 rpm, half the cluster's count quantum) per tick while the target moves, fading to zero within 1.2 s at steady rpm - emulates the natural pulse jitter that makes factory senders look smooth, without flickering the needle at rest. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -254,6 +254,14 @@ void loop() {
|
||||
was_connected = is_connected;
|
||||
}
|
||||
|
||||
// Tach slew: glide the output frequency between the 5 Hz data points so the
|
||||
// pulse-counting cluster tach sweeps instead of stair-stepping.
|
||||
static uint32_t last_tach_tick = 0;
|
||||
if (millis() - last_tach_tick >= 20) {
|
||||
last_tach_tick = millis();
|
||||
GaugeOutputs::tick();
|
||||
}
|
||||
|
||||
// UI + gauge outputs at the packet rate (5 Hz)
|
||||
if (millis() - last_ui_update > 200) {
|
||||
last_ui_update = millis();
|
||||
|
||||
+121
-27
@@ -3,26 +3,30 @@
|
||||
*
|
||||
* LEDC channel plan (Arduino-ESP32 2.x: timer = channel / 2):
|
||||
* channel 1 -> timer 0 : LovyanGFX backlight (display_init.cpp, 44.1 kHz)
|
||||
* channel 2 -> timer 1 : tachometer (frequency varies with rpm)
|
||||
* channel 4 -> timer 2 : coolant PWM (fixed frequency, duty varies)
|
||||
* The two gauge channels sit on their own timers so changing the tach
|
||||
* frequency never disturbs the backlight or the coolant PWM.
|
||||
*
|
||||
* The tachometer does NOT use LEDC. Arduino 2.x ledcChangeFrequency()
|
||||
* re-initializes the timer, truncating the waveform mid-period; the Patrol
|
||||
* tach counts pulses, so every frequency change corrupted its counting
|
||||
* window and the needle stepped (verified on the vehicle, 2026-09-23).
|
||||
* An esp_timer-task version still wobbled: its callbacks are dispatched from
|
||||
* a task, so WiFi/ESP-NOW bursts on core 0 delay edges by milliseconds and
|
||||
* then bunch them. IO7 is therefore toggled from a HARDWARE timer interrupt
|
||||
* (1 MHz counter, auto-reload): edge timing is immune to task scheduling,
|
||||
* and a new rpm value only changes the length of the NEXT half-period, so
|
||||
* the pulse train is phase-continuous through frequency changes - the same
|
||||
* behaviour as the CANNY PLC output that drove this cluster smoothly.
|
||||
*/
|
||||
|
||||
#include "modules/gauge_outputs.h"
|
||||
#include <Preferences.h>
|
||||
#include <math.h>
|
||||
#include "driver/gpio.h"
|
||||
|
||||
namespace GaugeOutputs {
|
||||
|
||||
#define TACH_CHANNEL 2
|
||||
#define TEMP_CHANNEL 4
|
||||
#define TEMP_RESOLUTION 8 // bits; duty 0..255 (matches the config's 8-bit law)
|
||||
// The tach runs from ~5 Hz to a few hundred Hz. The LEDC divider only reaches
|
||||
// low frequencies with a high duty resolution (f_min ~= 80 MHz / (2^res * 1024)),
|
||||
// so the tach timer uses 14 bits: ~4.8 Hz .. ~4.9 kHz.
|
||||
#define TACH_RESOLUTION 14
|
||||
#define TACH_DUTY (1 << (TACH_RESOLUTION - 1)) // 50 %
|
||||
#define TACH_MIN_HZ 5.0
|
||||
|
||||
static const char* NVS_NAMESPACE = "gaugecfg";
|
||||
@@ -34,6 +38,10 @@ static bool g_initialized = false;
|
||||
// Live output state, for status and change detection
|
||||
static bool g_tach_on = false;
|
||||
static double g_tach_hz = 0.0;
|
||||
// Slew target: update() (5 Hz) sets it, tick() (~50 Hz) glides toward it so the
|
||||
// pulse-counting cluster tach sweeps instead of stair-stepping.
|
||||
static double g_tach_target_hz = 0.0;
|
||||
static bool g_tach_have = false;
|
||||
static bool g_temp_on = false;
|
||||
static uint8_t g_temp_duty = 0;
|
||||
static uint16_t g_temp_hz_setup = 0;
|
||||
@@ -63,28 +71,68 @@ static bool loadConfig() {
|
||||
return ok;
|
||||
}
|
||||
|
||||
// --- phase-continuous tach generator (hardware timer ISR + GPIO toggle) ---
|
||||
static hw_timer_t* g_tach_hw = NULL; // GP timer 3, 1 MHz, auto-reload
|
||||
static volatile bool g_tach_level = false;
|
||||
|
||||
static void IRAM_ATTR tachTimerIsr() {
|
||||
g_tach_level = !g_tach_level;
|
||||
gpio_set_level((gpio_num_t)GAUGE_TACH_PIN, g_tach_level);
|
||||
}
|
||||
|
||||
static void tachOff() {
|
||||
if (g_tach_on) {
|
||||
ledcWrite(TACH_CHANNEL, 0);
|
||||
g_tach_on = false;
|
||||
timerAlarmDisable(g_tach_hw);
|
||||
g_tach_level = false;
|
||||
gpio_set_level((gpio_num_t)GAUGE_TACH_PIN, 0);
|
||||
}
|
||||
g_tach_hz = 0.0;
|
||||
}
|
||||
|
||||
// The cluster counts pulses over a fixed gate window: its reading moves in
|
||||
// ~150 rpm quanta (observed 2026-09-23: alternating +100/+200 needle steps =
|
||||
// a 150 quantum on a dial that rounds to 100). A perfectly stable frequency
|
||||
// parks on one quantum and SNAPS to the next; a real engine's natural pulse
|
||||
// jitter instead straddles the count boundary so the damped needle averages
|
||||
// to the true position. Recreate that: dither the output by a small RANDOM
|
||||
// amount each tick (random, not periodic - a fixed-period dither can beat
|
||||
// against the cluster's gate and visibly wobble the needle at steady rpm;
|
||||
// observed with a 280 ms triangle, 2026-09-23). 1.2 Hz = ~45 rpm.
|
||||
// The dither is only useful while the target MOVES (it smooths quantum
|
||||
// transitions); at a steady target it only flickers the needle. So it fades
|
||||
// out once the target has been still for DITHER_HOLD_MS.
|
||||
#define DITHER_HZ 1.2
|
||||
#define DITHER_HOLD_MS 500 // full dither this long after the last move
|
||||
#define DITHER_FADE_MS 700 // then fade to zero over this long
|
||||
|
||||
static double g_dither_scale = 0.0; // set by tick(), used by tachSet()
|
||||
|
||||
static double ditherNow() {
|
||||
if (g_dither_scale <= 0.0) return 0.0;
|
||||
return ((double)esp_random() / (double)UINT32_MAX * 2.0 - 1.0)
|
||||
* DITHER_HZ * g_dither_scale;
|
||||
}
|
||||
|
||||
static void tachSet(double hz) {
|
||||
// Only touch the timer when the frequency actually moved; every change
|
||||
// restarts the period and would jitter the needle if done each frame.
|
||||
if (!g_tach_on || fabs(hz - g_tach_hz) > 0.25) {
|
||||
double got = ledcChangeFrequency(TACH_CHANNEL, hz, TACH_RESOLUTION);
|
||||
if (got <= 0.0) {
|
||||
tachOff();
|
||||
return;
|
||||
}
|
||||
g_tach_hz = hz;
|
||||
if (hz < TACH_MIN_HZ) {
|
||||
tachOff();
|
||||
return;
|
||||
}
|
||||
g_tach_hz = hz; // base (undithered)
|
||||
double out = hz + ditherNow();
|
||||
if (out < TACH_MIN_HZ) out = TACH_MIN_HZ;
|
||||
// Auto-reload picks the new half-period up at the NEXT edge - the
|
||||
// half-period in flight completes untouched, so the pulse train stays
|
||||
// phase-continuous through every frequency change.
|
||||
uint64_t half_us = (uint64_t)(500000.0 / out);
|
||||
timerAlarmWrite(g_tach_hw, half_us, true);
|
||||
if (!g_tach_on) {
|
||||
ledcWrite(TACH_CHANNEL, TACH_DUTY);
|
||||
g_tach_on = true;
|
||||
g_tach_level = true;
|
||||
gpio_set_level((gpio_num_t)GAUGE_TACH_PIN, 1);
|
||||
timerRestart(g_tach_hw);
|
||||
timerAlarmEnable(g_tach_hw);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -127,18 +175,19 @@ void init() {
|
||||
Serial.printf("[Gauges] Cached config v%u loaded from NVS\n", g_cfg.cfg_version);
|
||||
}
|
||||
|
||||
// Tach: set up at a nominal frequency, output low until data arrives.
|
||||
ledcSetup(TACH_CHANNEL, 100, TACH_RESOLUTION);
|
||||
ledcAttachPin(GAUGE_TACH_PIN, TACH_CHANNEL);
|
||||
ledcWrite(TACH_CHANNEL, 0);
|
||||
// Tach: plain GPIO toggled by a hardware timer ISR, low until data arrives.
|
||||
pinMode(GAUGE_TACH_PIN, OUTPUT);
|
||||
digitalWrite(GAUGE_TACH_PIN, LOW);
|
||||
g_tach_hw = timerBegin(3, 80, true); // 80 MHz / 80 -> 1 MHz ticks
|
||||
timerAttachInterrupt(g_tach_hw, tachTimerIsr, true);
|
||||
|
||||
// Temp: fixed frequency from config, duty 0.
|
||||
tempSetupFrequency(g_cfg.temp_pwm_hz);
|
||||
ledcWrite(TEMP_CHANNEL, 0);
|
||||
|
||||
g_initialized = true;
|
||||
Serial.printf("[Gauges] Outputs ready: tach IO%d (LEDC ch%d), temp IO%d (LEDC ch%d @ %u Hz)\n",
|
||||
GAUGE_TACH_PIN, TACH_CHANNEL, GAUGE_TEMP_PIN, TEMP_CHANNEL, g_cfg.temp_pwm_hz);
|
||||
Serial.printf("[Gauges] Outputs ready: tach IO%d (hw-timer ISR), temp IO%d (LEDC ch%d @ %u Hz)\n",
|
||||
GAUGE_TACH_PIN, GAUGE_TEMP_PIN, TEMP_CHANNEL, g_cfg.temp_pwm_hz);
|
||||
}
|
||||
|
||||
void applyConfig(const link_config_t& cfg, bool persist) {
|
||||
@@ -180,8 +229,15 @@ void update(const automotive_data_t& d, bool link_ok) {
|
||||
hz *= (1.0 + g_cfg.tach_trim_pct / 100.0);
|
||||
if (g_cfg.tach_min_hz && hz < g_cfg.tach_min_hz) hz = g_cfg.tach_min_hz;
|
||||
if (g_cfg.tach_max_hz && hz > g_cfg.tach_max_hz) hz = g_cfg.tach_max_hz;
|
||||
if (hz >= TACH_MIN_HZ) tachSet(hz); else tachOff();
|
||||
if (hz >= TACH_MIN_HZ) {
|
||||
g_tach_target_hz = hz; // tick() glides there
|
||||
g_tach_have = true;
|
||||
} else {
|
||||
g_tach_have = false;
|
||||
tachOff();
|
||||
}
|
||||
} else {
|
||||
g_tach_have = false;
|
||||
tachOff();
|
||||
}
|
||||
}
|
||||
@@ -217,6 +273,44 @@ void update(const automotive_data_t& d, bool link_ok) {
|
||||
}
|
||||
}
|
||||
|
||||
void tick() {
|
||||
if (!g_initialized || !g_tach_have) return;
|
||||
|
||||
// Dither envelope: full while the target moves, fading to zero at rest.
|
||||
static double g_last_target = 0.0;
|
||||
static uint32_t g_last_move_ms = 0;
|
||||
if (fabs(g_tach_target_hz - g_last_target) > 0.5) {
|
||||
g_last_target = g_tach_target_hz;
|
||||
g_last_move_ms = millis();
|
||||
}
|
||||
uint32_t age = millis() - g_last_move_ms;
|
||||
if (age < DITHER_HOLD_MS)
|
||||
g_dither_scale = 1.0;
|
||||
else if (age < DITHER_HOLD_MS + DITHER_FADE_MS)
|
||||
g_dither_scale = 1.0 - (double)(age - DITHER_HOLD_MS) / DITHER_FADE_MS;
|
||||
else
|
||||
g_dither_scale = 0.0;
|
||||
|
||||
double base;
|
||||
if (!g_tach_on || g_tach_hz < TACH_MIN_HZ) {
|
||||
// First set after off (or after applyConfig() invalidated g_tach_hz):
|
||||
// jump straight to the target, no glide from a bogus starting point.
|
||||
base = g_tach_target_hz;
|
||||
} else {
|
||||
double diff = g_tach_target_hz - g_tach_hz;
|
||||
if (fabs(diff) <= 1.0) {
|
||||
// Within 1 Hz (30 rpm at 2 ppr): sit on the target.
|
||||
base = g_tach_target_hz;
|
||||
} else {
|
||||
// Exponential approach: 25% of the remaining distance per ~20 ms
|
||||
// tick (time constant ~80 ms - invisible lag on the needle).
|
||||
base = g_tach_hz + diff * 0.25;
|
||||
}
|
||||
}
|
||||
// Called every tick even at a steady target so the dither keeps cycling.
|
||||
tachSet(base);
|
||||
}
|
||||
|
||||
void printStatus() {
|
||||
Serial.printf("[Gauges] tach: %s %.1f Hz (%s) | temp: %s duty %u/255 @ %u Hz (%s) | cfg v%u\n",
|
||||
g_tach_on ? "ON " : "off", g_tach_hz > 0 ? g_tach_hz : 0.0, g_tach_src,
|
||||
|
||||
Reference in New Issue
Block a user