Skip to content

Commit

Permalink
updates to how calculations work when event counter doesn't change
Browse files Browse the repository at this point in the history
  • Loading branch information
half2me committed Aug 11, 2023
1 parent e1701e4 commit 111609f
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 199 deletions.
8 changes: 3 additions & 5 deletions ant/powerProfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,11 @@ func (m PowerMessage) InstantaneousPower() (num uint16) {
// In conditions where packets are lost, average power accurately calculates power over the interval
// between the received messages.
func (m PowerMessage) AveragePower(prev PowerMessage) float32 {
if prev == nil {
return float32(m.InstantaneousPower())
}
eventCountDiff := m.eventCountDiff(prev)

if prev.EventCount() == m.EventCount() {
if eventCountDiff == 0 {
return float32(m.InstantaneousPower())
}

return float32(m.accumulatedPowerDiff(prev)) / float32(m.eventCountDiff(prev))
return float32(m.accumulatedPowerDiff(prev)) / float32(eventCountDiff)
}
53 changes: 19 additions & 34 deletions ant/speedAndCadenceProfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,67 +36,52 @@ func (m SpeedAndCadenceMessage) CumulativeSpeedRevolutionCount() (num uint16) {
return
}

func (m SpeedAndCadenceMessage) SpeedEventTimeDiff(prev SpeedAndCadenceMessage) uint16 {
if prev == nil {
return 0
}
func (m SpeedAndCadenceMessage) speedEventTimeDiff(prev SpeedAndCadenceMessage) uint16 {
return m.SpeedEventTime() - prev.SpeedEventTime()
}

func (m SpeedAndCadenceMessage) CadenceEventTimeDiff(prev SpeedAndCadenceMessage) uint16 {
if prev == nil {
return 0
}
func (m SpeedAndCadenceMessage) cadenceEventTimeDiff(prev SpeedAndCadenceMessage) uint16 {
return m.CadenceEventTime() - prev.CadenceEventTime()
}

func (m SpeedAndCadenceMessage) speedRevolutionCountDiff(prev SpeedAndCadenceMessage) uint16 {
return m.CumulativeSpeedRevolutionCount() - prev.CumulativeSpeedRevolutionCount()
}

func (m SpeedAndCadenceMessage) CadenceRevolutionCountDiff(prev SpeedAndCadenceMessage) uint16 {
func (m SpeedAndCadenceMessage) cadenceRevolutionCountDiff(prev SpeedAndCadenceMessage) uint16 {
return m.CumulativeCadenceRevolutionCount() - prev.CumulativeCadenceRevolutionCount()
}

// Returns the cadence calculated from the previous message (0 if previous message is nil)
// The second parameter "nochange" indicates that the EventTime counter has not changed since the
// previous message, which means a complete rotation has not yet occurred. You should use this value
// to handle cases where the pedal stops: "coasting" (EventTime counter does not change)
// Cadence Returns the cadence calculated from the previous message
// If the "ok" parameter is false, this indicates that a complete rotation has not yet occurred.
// In this case the cadence has not changed, but it is impossible to calculate from these two messages.
// We can use this to also handle cases where the pedal stops: "coasting" (EventTime counter does not change)
// Cadence: (RPM)
func (m SpeedAndCadenceMessage) Cadence(prev SpeedAndCadenceMessage) (cadence float32, nochange bool) {
if prev == nil {
func (m SpeedAndCadenceMessage) Cadence(prev SpeedAndCadenceMessage) (cadence float32, ok bool) {
eventCountDiff := m.cadenceEventTimeDiff(prev)
if eventCountDiff == 0 {
return 0, false
}

if m.CadenceEventTimeDiff(prev) == 0 {
return 0, true
}

return float32(m.CadenceRevolutionCountDiff(prev)) * 1024 * 60 / float32(m.CadenceEventTimeDiff(prev)), false
return float32(m.cadenceRevolutionCountDiff(prev)) * 1024 * 60 / float32(eventCountDiff), true
}

// Distance travelled since the last message: (m)
// circumference: Circumference of the wheel (m)
func (m SpeedAndCadenceMessage) Distance(prev SpeedAndCadenceMessage, circumference float32) float32 {
if prev == nil {
return 0
}
return float32(m.speedRevolutionCountDiff(prev)) * circumference
}

// Speed: (m/s)
// Speed in (m/s)
// circumference: Circumference of the wheel (m)
// The second parameter "nochange" indicates that the EventTime counter has not changed since the
// previous message, which means a complete rotation has not yet occurred. You should use this value
// to handle cases where the pedal stops: "coasting" (EventTime counter does not change)
func (m SpeedAndCadenceMessage) Speed(prev SpeedAndCadenceMessage, circumference float32) (speed float32, nochange bool) {
if prev == nil {
// If the "ok" parameter is false, this indicates that a complete rotation has not yet occurred.
// In this case the speed has not changed, but it is impossible to calculate from these two messages.
// We can use this to also handle cases where the pedal stops: "coasting" (EventTime counter does not change)
func (m SpeedAndCadenceMessage) Speed(prev SpeedAndCadenceMessage, circumference float32) (speed float32, ok bool) {
eventCountDiff := m.speedEventTimeDiff(prev)
if eventCountDiff == 0 {
return 0, false
}

if m.SpeedEventTimeDiff(prev) == 0 {
return 0, true
}

return m.Distance(prev, circumference) * 1024 / float32(m.SpeedEventTimeDiff(prev)), false
return m.Distance(prev, circumference) * 1024 / float32(eventCountDiff), true
}
143 changes: 0 additions & 143 deletions cmd/server/server.go

This file was deleted.

10 changes: 1 addition & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,4 @@ module github.com/half2me/antgo

go 1.20

require (
github.com/google/gousb v1.1.2
github.com/redis/go-redis/v9 v9.0.3
)

require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
)
require github.com/google/gousb v1.1.2
8 changes: 0 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +1,2 @@
github.com/bsm/ginkgo/v2 v2.7.0 h1:ItPMPH90RbmZJt5GtkcNvIRuGEdwlBItdNVoyzaNQao=
github.com/bsm/gomega v1.26.0 h1:LhQm+AFcgV2M0WyKroMASzAzCAJVpAxQXv4SaI9a69Y=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/google/gousb v1.1.2 h1:1BwarNB3inFTFhPgUEfah4hwOPuDz/49I0uX8XNginU=
github.com/google/gousb v1.1.2/go.mod h1:GGWUkK0gAXDzxhwrzetW592aOmkkqSGcj5KLEgmCVUg=
github.com/redis/go-redis/v9 v9.0.3 h1:+7mmR26M0IvyLxGZUHxu4GiBkJkVDid0Un+j4ScYu4k=
github.com/redis/go-redis/v9 v9.0.3/go.mod h1:WqMKv5vnQbRuZstUwxQI195wHy+t4PuXDOjzMvcuQHk=

0 comments on commit 111609f

Please sign in to comment.