-
-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathantSCS.py
More file actions
86 lines (76 loc) · 4.46 KB
/
antSCS.py
File metadata and controls
86 lines (76 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#-------------------------------------------------------------------------------
# Version info
#-------------------------------------------------------------------------------
__version__ = "2020-12-27"
# 2020-12-27 Rollover more according specification
# 2020-06-16 Modified: device-by-zero due to zero Cadence/SpeedKmh
# 2020-06-09 First version, based upon antHRM.py
#-------------------------------------------------------------------------------
import time
import antDongle as ant
import logfile
def Initialize():
global PedalEchoPreviousCount, CadenceEventTime, CadenceEventCount, SpeedEventTime, SpeedEventCount
PedalEchoPreviousCount = 0 # There is no previous
CadenceEventTime = 0 # Initiate the even variables
CadenceEventCount = 0
SpeedEventTime = 0
SpeedEventCount = 0
def BroadcastMessage (_PedalEchoTime, PedalEchoCount, SpeedKmh, Cadence):
global PedalEchoPreviousCount, CadenceEventTime, CadenceEventCount, SpeedEventTime, SpeedEventCount
#-------------------------------------------------------------------------
# If pedal passed the magnet, calculate new values
# Otherwise repeat previous message
# Avoid devide-by-zero!
#-------------------------------------------------------------------------
if PedalEchoCount != PedalEchoPreviousCount and Cadence > 0 and SpeedKmh > 0:
#---------------------------------------------------------------------
# Cadence variables
# Based upon the number of pedal-cycles that are done and the given
# cadence, calculate the elapsed time.
# _PedalEchoTime is not used, because that give rounding errors and
# an instable reading.
#---------------------------------------------------------------------
PedalCycles = PedalEchoCount - PedalEchoPreviousCount
ElapsedTime = PedalCycles / Cadence * 60 # count / count/min * seconds/min = seconds
CadenceEventTime += ElapsedTime * 1024 # 1/1024 seconds
CadenceEventCount += PedalCycles
#---------------------------------------------------------------------
# Speed variables
# First calculate how many wheel-cycles can be done
# Then (based upon rounded #of cycles) calculate the elapsed time
#---------------------------------------------------------------------
Circumference = 2.096 # Note: SimulANT has 2.070 as default
WheelCadence = SpeedKmh / 3.6 / Circumference # km/hr / kseconds/hr / meters = cycles/s
WheelCycles = round(ElapsedTime * WheelCadence, 0) # seconds * /s = cycles
ElapsedTime = WheelCycles / SpeedKmh * 3.6 * Circumference
SpeedEventTime += ElapsedTime * 1024
SpeedEventCount += WheelCycles
#-------------------------------------------------------------------------
# Rollover after 0xffff
#-------------------------------------------------------------------------
CadenceEventTime = int(CadenceEventTime) & 0xffff # roll-over at 65535 = 64 seconds
CadenceEventCount = int(CadenceEventCount) & 0xffff # roll-over at 65535
SpeedEventTime = int(SpeedEventTime) & 0xffff # roll-over at 65535 = 64 seconds
SpeedEventCount = int(SpeedEventCount) & 0xffff # roll-over at 65535
#-------------------------------------------------------------------------
# Prepare for next event
#-------------------------------------------------------------------------
PedalEchoPreviousCount = PedalEchoCount
#-------------------------------------------------------------------------
# Compose message
#-------------------------------------------------------------------------
info = ant.msgPage_SCS (ant.channel_SCS, CadenceEventTime, CadenceEventCount, SpeedEventTime, SpeedEventCount)
scsdata = ant.ComposeMessage (ant.msgID_BroadcastData, info)
#-------------------------------------------------------------------------
# Return message to be sent
#-------------------------------------------------------------------------
return scsdata
#-------------------------------------------------------------------------------
# Main program for module test
#-------------------------------------------------------------------------------
if __name__ == "__main__":
Initialize()
time.sleep(1)
scsdata = BroadcastMessage (0, 1, 45.6, 123)
print (logfile.HexSpace(scsdata))