-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCycle.py
More file actions
416 lines (338 loc) · 16 KB
/
Copy pathCycle.py
File metadata and controls
416 lines (338 loc) · 16 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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# PiCycle - Python Exersize Bike Trainer
# Copyright (C) 2019 Jason Birch
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#/****************************************************************************/
#/* PiCycle - Python Exersize Bike Trainer. */
#/* ------------------------------------------------------------------------ */
#/* V1.00 - 2019-02-14 - Jason Birch */
#/* ------------------------------------------------------------------------ */
#/* Cycle class. */
#/****************************************************************************/
import os
import math
import time
import random
import datetime
import pygame
import User
class Cycle:
# /********************/
# /* Class constants. */
#/********************/
STATUS_STOPPED = 0
STATUS_RUNNING = 1
def __init__(self, NewStartPos, MainUser, MainEvent):
# /*******************************/
# /* initialise class variables. */
#/*******************************/
self.WavLap = pygame.mixer.Sound("SOUND/lap.wav")
self.WavLastLap = pygame.mixer.Sound("SOUND/last-lap.wav")
self.State = self.STATUS_STOPPED
self.ThisUser = MainUser
self.ThisEvent = MainEvent
self.StartPos = NewStartPos
self.Position = self.StartPos
self.LastPosition = self.StartPos
self.MinSpeed = 15
self.MaxSpeed = 20
self.Speed = 0
self.SpeedTimer = time.time()
self.WheelDiameterInch = 26.0
self.WheelDiameterCm = 66.0
self.TotalPulseCount = 0.0
self.TotalDistance = 0.0
self.EventDistancePulseCount = 0.0
self.EventDistance = 0.0
self.EventLapDistance = 1.0
self.LastEventDistance = 0.0
self.EventLap = 1
self.EventLapComplete = 0
self.EventLapStartTime = 0
self.EventLapTime = datetime.datetime.now()
self.EventLastLapTime = datetime.datetime.now()
self.LastEventTime = datetime.timedelta(0, 0)
#/********************************/
#/* Load cycle data from a file. */
#/********************************/
def LoadCycle(self):
# /*******************/
# /* Default values. */
#/*******************/
self.WheelDiameterInch = 26.0
self.WheelDiameterCm = 66.0
self.TotalPulseCount = 0.0
if os.path.isfile("CYCLE/CYCLE.CYC"):
File = open("CYCLE/CYCLE.CYC", 'r', 0)
TextLine = "."
while TextLine != "":
TextLine = File.readline()
TextLine = TextLine.replace("\n", "")
if TextLine[:20] == "WHEEL_DIAMETER_INCH=":
self.WheelDiameterInch = float(TextLine[20:])
elif TextLine[:18] == "WHEEL_DIAMETER_CM=":
self.WheelDiameterCm = float(TextLine[18:])
elif TextLine[:18] == "TOTAL_PULSE_COUNT=":
self.TotalPulseCount = float(TextLine[18:])
File.close()
#/******************************/
#/* Save cycle data to a file. */
#/******************************/
def SaveCycle(self):
File = open("CYCLE/CYCLE.CYC", 'w', 0)
File.write("WHEEL_DIAMETER_INCH=" + str(self.WheelDiameterInch) + "\n")
File.write("WHEEL_DIAMETER_CM=" + str(self.WheelDiameterCm) + "\n")
File.write("TOTAL_PULSE_COUNT=" + str(self.TotalPulseCount) + "\n")
File.close()
#/*************************************/
#/* Handle cycle sensor pulse events. */
#/*************************************/
def Pulse(self, Period, NewSpeed):
if self.ThisEvent.GetState() != self.ThisEvent.STATE_EVENT_PAUSED:
# /*******************************************************/
# /* Perform computer player random cycle speed changes. */
#/*******************************************************/
if NewSpeed != -1:
if self.State == self.STATUS_STOPPED:
self.Speed = 0
elif self.State == self.STATUS_RUNNING:
NewPeriod = Period * (((NewSpeed / 60.0 / 60.0) * self.ThisUser.GetUnitScale()) / (math.pi * self.GetWheelDiameter()))
self.TotalPulseCount += NewPeriod
self.EventDistancePulseCount += NewPeriod
self.ThisUser.SetTotalPulseCount(self.ThisUser.GetTotalPulseCount() + NewPeriod)
self.Speed = NewSpeed
else:
# /******************************************************************/
# /* Perform user cycle speed changes based on sensor pulse period. */
#/******************************************************************/
self.TotalPulseCount += Period
self.ThisUser.SetTotalPulseCount(self.ThisUser.GetTotalPulseCount() + Period)
if self.ThisEvent.GetState() == self.ThisEvent.STATE_EVENT_READY:
self.ThisEvent.SetState(self.ThisEvent.STATE_EVENT_FALSE_START)
elif self.ThisEvent.GetState() == self.ThisEvent.STATE_EVENT_GO or self.ThisEvent.GetState() == self.ThisEvent.STATE_EVENT_RUNNING or self.ThisEvent.GetState() == self.ThisEvent.STATE_EVENT_OFF:
if self.ThisEvent.GetDistance() == 0 or self.EventDistance < self.ThisEvent.GetDistance():
self.EventDistancePulseCount += Period
# /******************************************/
# /* Record period between wheel rotations. */
#/******************************************/
SpeedTimerDelta = time.time() - self.SpeedTimer
# /**********************************************/
# /* Calcuate speed from period between pulses. */
#/**********************************************/
if SpeedTimerDelta:
self.Speed = math.pi * self.GetWheelDiameter() / self.ThisUser.GetUnitScale() / (SpeedTimerDelta / 60.0 / 60.0)
else:
self.Speed = 0
self.SpeedTimer = time.time()
#/************************************/
#/* Called every application period. */
#/************************************/
def Period(self, UserCycle = False):
# /********************************************/
# /* Calculate distance from circumference of */
# /* wheel by number of wheel rotations. */
#/********************************************/
self.TotalDistance = math.pi * self.GetWheelDiameter() * self.TotalPulseCount / self.ThisUser.GetUnitScale()
self.EventDistance = math.pi * self.GetWheelDiameter() * self.EventDistancePulseCount / self.ThisUser.GetUnitScale()
LastEventLap = self.EventLap
self.EventLap = 1 + int(self.EventDistance / self.EventLapDistance)
self.EventLapComplete = (self.EventDistance % self.EventLapDistance) * self.EventLapDistance
# /**************************************************/
# /* Handle user crossing the lap line in an event. */
#/**************************************************/
if UserCycle == True and self.ThisEvent.GetState() == self.ThisEvent.STATE_EVENT_RUNNING and LastEventLap != self.EventLap:
if self.EventLap == self.ThisEvent.GetLapCount() + 1:
self.WavLastLap.play()
else:
self.WavLap.play()
self.EventLastLapTime = self.EventLapTime
self.EventLapTime = datetime.datetime.now()
#/******************************************/
#/* Clear the cycle to a known idle state. */
#/******************************************/
def Reset(self):
self.State = self.STATUS_STOPPED
self.EventDistancePulseCount = 0
self.Position = self.StartPos
self.EventLap = 1
self.EventLapComplete = 0
self.EventLapStartTime = 0
self.EventLastLapTime = datetime.datetime.now()
self.EventLapTime = datetime.datetime.now()
self.Speed = 0
self.SpeedTimer = time.time()
#/********************************/
#/* Start the cycle in an event. */
#/********************************/
def Start(self):
self.State = self.STATUS_RUNNING
self.EventLastLapTime = datetime.datetime.now()
self.EventLapTime = datetime.datetime.now()
#/********************************************************/
#/* Get the configured diameter of the cycle wheel. */
#/* Required for acurate distance and speed calculation. */
#/********************************************************/
def GetWheelDiameter(self):
if self.ThisUser.GetUnits() == "M":
return self.WheelDiameterInch
else:
return self.WheelDiameterCm
#/********************************************************/
#/* Set the configured diameter of the cycle wheel. */
#/* Required for acurate distance and speed calculation. */
#/********************************************************/
def SetWheelDiameter(self, NewWheelDiameter):
if self.ThisUser.GetUnits() == "M":
self.WheelDiameterInch = float(NewWheelDiameter)
self.WheelDiameterCm = self.WheelDiameterInch * User.User.INCHES_TO_CM
else:
self.WheelDiameterCm = float(NewWheelDiameter)
self.WheelDiameterInch = self.WheelDiameterCm / User.User.INCHES_TO_CM
#/************************************************************************/
#/* Get accumulated total distance of cycle since application first run. */
#/************************************************************************/
def GetTotalDistance(self):
return self.TotalDistance
#/*******************************************/
#/* Get last event total distance of cycle. */
#/*******************************************/
def GetLastEventDistance(self):
return self.LastEventDistance
#/*******************************************/
#/* Set last event total distance of cycle. */
#/*******************************************/
def SetLastEventDistance(self, NewLastEventDistance):
self.LastEventDistance = NewLastEventDistance
#/***************************************/
#/* Get last event total time of cycle. */
#/***************************************/
def GetLastEventTime(self):
return self.LastEventTime
#/***************************************/
#/* Set last event total time of cycle. */
#/***************************************/
def SetLastEventTime(self, NewLastEventTime):
self.LastEventTime = NewLastEventTime
#/****************************************/
#/* Get user associated with this cycle. */
#/****************************************/
def GetUser(self):
return self.ThisUser
#/**************************************************************/
#/* Get accumulated total distance of user since user created. */
#/**************************************************************/
def GetUserDistance(self):
return math.pi * self.GetWheelDiameter() * self.ThisUser.GetTotalPulseCount() / self.ThisUser.GetUnitScale()
#/************************************************/
#/* Get the total distance for the active event. */
#/************************************************/
def GetEventDistance(self):
return self.EventDistance
#/***************************************************/
#/* Get the distance for a lap of the active event. */
#/***************************************************/
def GetEventLapDistance(self):
return self.EventLapDistance
#/***************************************/
#/* Get the current speed of the cycle. */
#/***************************************/
def GetSpeed(self):
# /************************************************************************/
# /* If a pulse has not been received by the sensor in an excessive time, */
# /* consider that the wheel of the cycle has stopped rotating. */
#/************************************************************************/
if time.time() - self.SpeedTimer > 2:
self.Speed = 0
return self.Speed
#/******************************************************/
#/* Get the average cycle speed for the current event. */
#/******************************************************/
def GetAvgSpeed(self):
return self.GetEventDistance() / (0.00001 + self.ThisEvent.GetElapsedSeconds() / (60.0 * 60.0))
#/***************************************************/
#/* Get the average lap time for the current event. */
#/***************************************************/
def GetAvgLapTime(self):
return self.ThisEvent.GetElapsedSeconds() / self.ThisEvent.GetLapCount()
#/************************************************/
#/* For computer cyclists, alter the cycle speed */
#/* within the range for the current user level. */
#/************************************************/
def VarySpeedInRange(self):
if self.Speed == 0:
self.Speed = self.MinSpeed + random.randrange(self.MaxSpeed - self.MinSpeed)
else:
if random.randrange(50) == 0:
self.Speed += random.randrange(100) / 100.0 - 0.5
if self.Speed < self.MinSpeed:
self.Speed = self.MinSpeed
if self.Speed > self.MaxSpeed:
self.Speed = self.MaxSpeed
#/*****************************************************/
#/* Get the current cycle position out of all cycles. */
#/*****************************************************/
def GetPos(self):
return self.Position
#/*****************************************************/
#/* Set the current cycle position out of all cycles. */
#/*****************************************************/
def SetPos(self, NewPosition):
self.Position = NewPosition
#/********************************************************/
#/* Get the last event cycle position out of all cycles. */
#/********************************************************/
def GetLastPos(self):
return self.LastPosition
#/********************************************************/
#/* Set the last event cycle position out of all cycles. */
#/********************************************************/
def SetLastPos(self, NewPosition):
self.LastPosition = NewPosition
#/*********************************************************/
#/* Set the min and max speed values for computer cycles, */
#/* based on the user level. */
#/*********************************************************/
def SetSpeedRange(self, NewMinSpeed, NewMaxSpeed):
self.MinSpeed = NewMinSpeed
self.MaxSpeed = NewMaxSpeed
if self.MinSpeed <= 0:
self.MinSpeed = 0.5
if self.MaxSpeed <= 0:
self.MaxSpeed = 0.5
#/************************************************/
#/* Get the number of laps for the active event. */
#/************************************************/
def GetEventLap(self):
return self.EventLap
#/**********************************************************/
#/* Get the number of laps completed for the active event. */
#/**********************************************************/
def GetEventLapComplete(self):
return self.EventLapComplete
#/**************************************/
#/* Get the time into the current lap. */
#/**************************************/
def GetLapPeriod(self):
if self.ThisEvent.GetState() == self.ThisEvent.STATE_EVENT_GO or self.ThisEvent.GetState() == self.ThisEvent.STATE_EVENT_RUNNING or self.ThisEvent.GetState() == self.ThisEvent.STATE_EVENT_OFF:
return datetime.datetime.now() - self.EventLapTime
else:
return datetime.timedelta(0)
#/*************************************/
#/* Get the time of the previous lap. */
#/*************************************/
def GetLastLapPeriod(self):
if self.ThisEvent.GetState() == self.ThisEvent.STATE_EVENT_RUNNING or self.ThisEvent.GetState() == self.ThisEvent.STATE_EVENT_FINISH or self.ThisEvent.GetState() == self.ThisEvent.STATE_EVENT_OFF:
return self.EventLapTime - self.EventLastLapTime
else:
return datetime.timedelta(0)