From 7ee776ba1a87ef20030c40ce0ade415b3f007ee7 Mon Sep 17 00:00:00 2001 From: Erik Brakkee Date: Sun, 7 Jun 2026 23:24:19 +0200 Subject: [PATCH] slightly more accurate computation of the time of the last full revolution by using the average RPM over the last interval instead of using the last RPM. --- cmd/bridge/bluetooth.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/cmd/bridge/bluetooth.go b/cmd/bridge/bluetooth.go index 383791e..9980f5e 100644 --- a/cmd/bridge/bluetooth.go +++ b/cmd/bridge/bluetooth.go @@ -61,6 +61,7 @@ func ExposeBluetooth(events <-chan Event) { lastUpdateTime := event.Now var lastCompletedRevs float64 = 0.0 var lastRevTicks float64 = 0.0 // time in 1/1024s of last completed revolution + prevRpm := float64(event.Cadence) for event := range events { now := event.Now @@ -69,6 +70,7 @@ func ExposeBluetooth(events <-chan Event) { watts := event.Power rpm := float64(event.Cadence) + avgRpm := (rpm + prevRpm) / 2 totalRevs += rpm / 60.0 * deltaT elapsedTicks += deltaT * 1024.0 @@ -81,7 +83,7 @@ func ExposeBluetooth(events <-chan Event) { // time at which the last completed revolution occurred // = current time - fractional part of revolution * time per revolution fracRev := totalRevs - completedRevs - ticksPerRev := 1024.0 * 60.0 / rpm + ticksPerRev := 1024.0 * 60.0 / avgRpm lastRevTicks = elapsedTicks - fracRev*ticksPerRev lastCompletedRevs = completedRevs @@ -90,11 +92,12 @@ func ExposeBluetooth(events <-chan Event) { // No change we need to emit the same data ss before. } + prevRpm = rpm payload := make([]byte, 8) binary.LittleEndian.PutUint16(payload[0:], bleSignalDetectedFlag) binary.LittleEndian.PutUint16(payload[2:], watts) binary.LittleEndian.PutUint16(payload[4:], uint16(lastCompletedRevs)) - binary.LittleEndian.PutUint16(payload[6:], uint16(lastRevTicks)) + binary.Little\Endian.PutUint16(payload[6:], uint16(lastRevTicks)) if _, err := powerChar.Write(payload); err != nil { logPrintf("bt: failed to write power characteristic: %v\n", err)