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.
This commit is contained in:
Erik Brakkee 2026-06-07 23:24:19 +02:00
parent b8a9abcd2a
commit 7ee776ba1a

View File

@ -61,6 +61,7 @@ func ExposeBluetooth(events <-chan Event) {
lastUpdateTime := event.Now lastUpdateTime := event.Now
var lastCompletedRevs float64 = 0.0 var lastCompletedRevs float64 = 0.0
var lastRevTicks float64 = 0.0 // time in 1/1024s of last completed revolution var lastRevTicks float64 = 0.0 // time in 1/1024s of last completed revolution
prevRpm := float64(event.Cadence)
for event := range events { for event := range events {
now := event.Now now := event.Now
@ -69,6 +70,7 @@ func ExposeBluetooth(events <-chan Event) {
watts := event.Power watts := event.Power
rpm := float64(event.Cadence) rpm := float64(event.Cadence)
avgRpm := (rpm + prevRpm) / 2
totalRevs += rpm / 60.0 * deltaT totalRevs += rpm / 60.0 * deltaT
elapsedTicks += deltaT * 1024.0 elapsedTicks += deltaT * 1024.0
@ -81,7 +83,7 @@ func ExposeBluetooth(events <-chan Event) {
// time at which the last completed revolution occurred // time at which the last completed revolution occurred
// = current time - fractional part of revolution * time per revolution // = current time - fractional part of revolution * time per revolution
fracRev := totalRevs - completedRevs fracRev := totalRevs - completedRevs
ticksPerRev := 1024.0 * 60.0 / rpm ticksPerRev := 1024.0 * 60.0 / avgRpm
lastRevTicks = elapsedTicks - fracRev*ticksPerRev lastRevTicks = elapsedTicks - fracRev*ticksPerRev
lastCompletedRevs = completedRevs lastCompletedRevs = completedRevs
@ -90,11 +92,12 @@ func ExposeBluetooth(events <-chan Event) {
// No change we need to emit the same data ss before. // No change we need to emit the same data ss before.
} }
prevRpm = rpm
payload := make([]byte, 8) payload := make([]byte, 8)
binary.LittleEndian.PutUint16(payload[0:], bleSignalDetectedFlag) binary.LittleEndian.PutUint16(payload[0:], bleSignalDetectedFlag)
binary.LittleEndian.PutUint16(payload[2:], watts) binary.LittleEndian.PutUint16(payload[2:], watts)
binary.LittleEndian.PutUint16(payload[4:], uint16(lastCompletedRevs)) 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 { if _, err := powerChar.Write(payload); err != nil {
logPrintf("bt: failed to write power characteristic: %v\n", err) logPrintf("bt: failed to write power characteristic: %v\n", err)