-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwaterrower.py
186 lines (151 loc) · 5.8 KB
/
waterrower.py
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env python3
import serial
import time
from datetime import datetime
import re
import signal
import sys
import os
from argparse import ArgumentParser
class Waterrower:
def __init__(self, serport, logfile ):
self.serialport = serport
if None != logfile:
self.logfile = open( logfile, "w")
else:
self.logfile = None
self.serialport.write(b'USB\r\n')
self.heartrate = 0
self.totalspeed = 0
self.averagespeed = 0
self.strokecount = 0
self.distance = 0
self.display_hours = 0
self.display_minutes = 0
self.display_seconds = 0
self.display_tenthseconds = 0
self.calories_watts = 0
self.calories_total = 0
self.calories_up = 0
self.stroke_state = 0
def __del__(self):
#self.serialport.write(b'EXIT\r\n')
pass
def parseStrokeCount(self, msg):
self.strokecount = int(msg, 16)
def parseTotalSpeed(self, msg):
self.totalspeed = int(msg, 16)
def parseAverageSpeed(self, msg):
self.averagespeed = int(msg,16)
def parseDistance(self, msg):
self.distance = int(msg,16)
def parseHeartRate(self, msg):
self.heartrate = int(msg,16)
def parseDisplayHour(self, msg):
self.display_hours = int(msg)
def parseDisplayMinutes(self, msg):
self.display_minutes = int(msg)
def parseDisplaySeconds(self, msg):
self.display_seconds = int(msg)
def parseDisplayTenthSeconds(self, msg):
self.display_tenthseconds = int(msg)
def parseCaloriesWatts(self, msg):
self.calories_watts = int(msg, 16)
def parseCaloriesTotal(self, msg):
self.calories_total = int(msg, 16)
def parseCaloriesUp(self, msg):
self.calories_up = int(msg,16)
def plot(self):
msg = ("%s | Tm: %02d:%02d:%02d.%d | StrkSt: %d | HR: %s [bpm] Dist: %s [m] Strk: %s Avg: %s [m/s] Tot: %s | CalW: %s TotCal: %s CalUp: %s" %
(datetime.today().isoformat(' '),
self.display_hours,
self.display_minutes,
self.display_seconds,
self.display_tenthseconds,
self.stroke_state,
self.heartrate,
self.distance,
self.strokecount,
self.averagespeed,
self.totalspeed,
self.calories_watts,
self.calories_total,
self.calories_up))
print(msg)
if self.logfile != None:
self.logfile.write("%s\r\n" % msg)
def parse(self, lines):
for line in lines:
line = line.replace(b'\r\n', b'')
if re.search(b'^IDD140', line):
self.parseStrokeCount(line[6:])
elif re.search(b'^IDD148', line):
self.parseTotalSpeed(line[6:])
elif re.search(b'^IDD14A', line):
self.parseAverageSpeed(line[6:])
elif re.search(b'^IDD057', line):
self.parseDistance(line[6:])
elif re.search(b'^IDS1A', line):
self.parseHeartRate(line[5:])
elif re.search(b'^IDS1E3', line):
self.parseDisplayHour( line[6:])
elif re.search(b'^IDS1E2', line):
self.parseDisplayMinutes( line[6:])
elif re.search(b'^IDS1E1', line):
self.parseDisplaySeconds( line[6:])
elif re.search(b'^IDS1E0', line):
self.parseDisplayTenthSeconds( line[6:] )
elif re.search(b'^IDD088', line):
self.parseCaloriesWatts( line[6:] )
elif re.search(b'^IDD08A', line):
self.parseCaloriesTotal( line[6:] )
elif re.search(b'^IDS08C', line):
self.parseCaloriesUp( line[6:] )
elif re.search(b'^SS', line):
self.stroke_state = 1
elif re.search(b'^SE', line):
self.stroke_state = 0
def requestStrokeCount(self):
self.serialport.write(b'IRD140\r\n')
def requestTotalSpeed(self):
self.serialport.write(b'IRD148\r\n')
def requestAverageSpeed(self):
self.serialport.write(b'IRD14A\r\n')
def requestDistance(self):
self.serialport.write(b'IRD057\r\n')
def requestHeartRate(self):
self.serialport.write(b'IRS1A0\r\n')
def requestTime(self):
self.serialport.write(b'IRS1E3\r\n')
self.serialport.write(b'IRS1E2\r\n')
self.serialport.write(b'IRS1E1\r\n')
self.serialport.write(b'IRS1E0\r\n')
def requestCaloriesWatts(self):
self.serialport.write(b'IRD088\r\n')
def requestCaloriesTotal(self):
self.serialport.write(b'IRD08A\r\n')
self.serialport.write(b'IRS08C\r\n')
def signal_handler(signal, frame):
sys.exit(0)
signal.signal( signal.SIGINT, signal_handler)
parser = ArgumentParser(description='Logs Waterrower rowing data')
parser.add_argument('serialport', metavar='serialport', help='the serialport for communication')
parser.add_argument('-f', '--logfile', help='logs the output to this file' )
args = parser.parse_args()
if not os.path.exists(args.serialport):
print('Invalid serial port given!')
sys.exit(0)
with serial.Serial( args.serialport, 19200, timeout=0.01 ) as ser:
waterrower = Waterrower(ser, args.logfile )
while True:
waterrower.requestStrokeCount()
waterrower.requestTotalSpeed()
waterrower.requestAverageSpeed()
waterrower.requestDistance()
waterrower.requestHeartRate()
waterrower.requestTime()
waterrower.requestCaloriesWatts()
waterrower.requestCaloriesTotal()
waterrower.parse( ser.readlines() )
waterrower.plot()
time.sleep(0.03)