-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathWorkout.h
289 lines (211 loc) · 6.42 KB
/
Workout.h
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
#ifndef WORKOUT_H
#define WORKOUT_H
#include "WorkoutStats.h"
enum WorkoutType {
DEADHANG,
BODYWEIGHT,
RESET
};
enum WorkoutState {
HANGING,
IDLE
};
class Workout {
public:
Workout() {
fullReset(IDLE);
}
void setBodyWeight(float newWeight){
mBodyWeight = newWeight;
}
float current(){
return mRepStats.last();
}
float sessionMax(){
return mSessionMax;
}
int percentMax() {
if (mSessionMax != 0.0f)
return (int) (mRepStats.last() * 100.0f / mSessionMax);
else return 0;
}
float strengthToWeight(float strength){
return strength / mBodyWeight;
}
void reset(WorkoutState newState) {
mIdleTime = 0;
mHangingTime = 0;
mStartTime = millis();
mState = newState;
}
void fullReset(WorkoutState newState) {
reset(newState);
mAbortedRepsCount = 0;
mRepStats.reset();
mRepPercentMaxStats.reset();
}
/**
* Grab current load from sensors and update hanging stats
*/
void updateStats(){
// grab stats from load cells
if (scaleDown.wait_ready_timeout(100) && scaleUp.wait_ready_timeout(100)) {
float readingL = abs(scaleDown.get_units() / 1000.0f);
float readingR = abs(scaleUp.get_units() / 1000.0f);
mRepStats.addData(readingL + readingR);
if(mRepStats.last() > 1){
#ifdef DEBUG
Serial.print(readingL);
Serial.print(F(","));
Serial.println(readingR);
#endif
ble.print(readingL);
ble.print(F(","));
ble.println(readingR);
}
} else {
Serial.println(F("HX711 not found."));
}
}
/**
* Called after 3 short hangs to switch to the next workout mode
*/
void updateWorkoutType(){
if( mAbortedRepsCount >= MAX_ABORTED_REPS) {
switch (mType) {
case DEADHANG :
mType = BODYWEIGHT;
Serial.println(F("Workout: BODYWEIGHT"));
break;
case BODYWEIGHT:
/* mType = RESET;
Serial.println(F("Workout: RESET"));
break;
case RESET :*/
mType = DEADHANG;
Serial.println(F("Workout: DEADHANG"));
break;
}
fullReset(IDLE);
}
}
void render() {
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 0);
switch (mType) {
case DEADHANG:
switch (mState) {
case IDLE :
display.println(F("DeadHang"));
display.print(mTUT/1000);
display.print(F("s "));
display.print(mTotalNumRep);
display.println(F("r"));
display.print((int)mRepStats.mean());
display.print(F("kg "));
display.print((int)(mRepPercentMaxStats.mean()));
display.println(F("% "));
display.print(strengthToWeight(mRepStats.mean()));
display.println(F("sw"));
break;
case HANGING :
display.println(F("DeadHang"));
display.print(mHangingTime/1000);
display.println(F("s"));
display.print((int)mRepStats.last());
display.print(F("kg "));
display.print(percentMax());
display.println(F("%"));
display.print(strengthToWeight(mRepStats.last()));
display.println(F("sw "));
display.println(F(""));
break;
}
break;
case BODYWEIGHT:
switch (mState) {
case IDLE :
display.println(F("BodyWeight"));
display.print(mBodyWeight);
display.println(F("kg"));
display.println(F("start ..."));
break;
case HANGING :
display.println(F("BodyWeight"));
display.print(mRepStats.last());
display.println(F("kg"));
display.print(mHangingTime/1000);
display.println(F("s"));
break;
}
break;
case RESET:
break;
}
display.display();
}
void loop() {
updateStats();
updateWorkoutType();
render();
if (mState == IDLE) {
mIdleTime += FRAME_RATE_MS;
// If nothing has been done for too long do a full reset
if(mIdleTime >= MAX_IDLE_TIME_MS){
fullReset(IDLE);
}
// Start HANGING rep if there's more thant 2kg applied while IDLE
if(mRepStats.last() > 2){
mRepsCount++;
mTotalNumRep++;
mRepStats.reset();
reset(HANGING);
}
}
if (mState == HANGING){
mHangingTime += FRAME_RATE_MS;
// Store time under tension & percentMaxStats
int pMax = percentMax();
mTUT += FRAME_RATE_MS;
mRepPercentMaxStats.addData(pMax);
// check for new max force
if(mSessionMax < mRepStats.last() ){
mSessionMax = mRepStats.last();
mSessionMaxStrength2Weight = strengthToWeight(mSessionMax);
}
// Go back to IDLE when there's less than 2kg applied when HANGING
if(mRepStats.last() <= 2) {
// under 1" hang we consider that's a click
if(mHangingTime < 1000){
mAbortedRepsCount++;
}
if(mType == BODYWEIGHT){
mBodyWeight = mRepStats.maxVal();
writeFloat(BODYWEIGHT_EEPROM_ADDRESS, mBodyWeight);
Serial.print(F("Write climber data to EEPROM : "));
Serial.println(mBodyWeight);
}
reset(IDLE);
return;
}
}
}
protected:
unsigned short mAbortedRepsCount = 0;
unsigned short mRepsCount = 0;
unsigned short mTotalNumRep = 0;
float mBodyWeight = 70.0f;
float mSessionMax = 20.0f;
float mSessionMaxStrength2Weight = mSessionMax/mBodyWeight;
unsigned int mHangingTime;
unsigned int mIdleTime;
unsigned long mStartTime;
unsigned long mTUT = 0L;
WorkoutType mType = DEADHANG;
WorkoutState mState = IDLE;
WorkoutStats mRepStats;
WorkoutStats mRepPercentMaxStats;
};
#endif