forked from 360manu/kettlerUSB2BLE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBikeState.js
290 lines (230 loc) · 6.94 KB
/
BikeState.js
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
const EventEmitter = require('events');
var DEBUG = false;
var minGear = 1;
var maxGear = 10;
var gearRatio = [0.2, 0.25, 0.3, 0.35, 0.5, 1.0, 1.6, 2.2, 3.5, 5];
var useRealCalc = true;
var wattIncPerSec = 5;
var wattDecPerSec = 25;
class BikeState extends EventEmitter {
constructor() {
super();
console.log(`[BikeState starting]`);
// init
this.data = null;
this.external = null;
this.mode = 'ERG'; // ERG ou SIM
this.gear = 1;
this.lastRPM_time = 0;
this.lastRPM_cnt = 0;
this.RPM_time = 0;
this.RPM_cnt = 0;
this.lastWHEEL_time = 0;
this.lastWHEEL_cnt = 0;
this.WHEEL_time = 0;
this.WHEEL_cnt = 0;
this.currentPower = 50;
};
// Restart the trainer
restart() {
this.mode = 'ERG'; // ERG ou SIM
this.emit('mode', this.mode);
// update and compute
this.data = null;
};
// Set the bike under the FTMS control
setControl() {};
// Current state
setData(data) {
this.data = data;
this.compute();
return this.data;
};
setGear(gear) {
this.gear = gear;
this.emit('gear', this.gear);
}
// Gear Up
GearUp() {
this.gear++;
if (this.gear > maxGear)
this.gear = maxGear;
this.emit('gear', this.gear);
};
// Gear Down
GearDown() {
this.gear--;
if (this.gear < minGear)
this.gear = minGear;
this.emit('gear', this.gear);
};
/* Puissance a atteindre */
setTargetPower(power) {
this.mode = 'ERG'; // ERG
this.emit('mode', this.mode);
// update and compute
if (this.data == null)
return;
this.data.power = power;
this.emit('simpower', this.data.power);
};
/* Modifie la puissance target */
addPower(increment) {
if (this.data == null)
return;
this.data.power += increment;
// update and compute
this.emit('simpower', this.data.power);
};
/* Mode Simulation : les conditions externes a simuler */
setExternalCondition(windspeed, grade, crr, cw) {
this.mode = 'SIM'; // ERG ou SIM
this.emit('mode', this.mode);
this.external = {
windspeed: windspeed,
grade: grade,
crr: crr,
cw: cw
};
this.emit('windspeed', (windspeed * 3.6).toFixed(1));
this.emit('grade', (grade).toFixed(1));
};
compute_crank_revs_for_CPM() {
this.data.RPMcnt = 0;
this.data.RPMtime = 0;
// pas de data du velo : on ne peut rien faire
if (this.data == null)
return;
//
// Update crank revs for the cycle power measurement char
//
var rpm = this.data.rpm;
this.data.RPMcnt = this.RPM_cnt;
this.data.RPMtime = this.RPM_time;
if(rpm){
if(this.lastRPM_time == 0) {
this.lastRPM_time = Date.now();
}
// Get time difference
var current_time = Date.now();
var t_diff = current_time - this.lastRPM_time;
// Calculate revs made in the meantime
var rpm_speed = (rpm / 60);
var rpm_add = t_diff / 1000 * rpm_speed;
// Full wheel change reached?
var full_change = Math.floor(this.lastRPM_cnt + rpm_add) - Math.floor(this.lastRPM_cnt);
if (full_change >= 1){
this.lastRPM_cnt += rpm_add;
this.lastRPM_time = current_time;
// We can only sent full revs, so calculate time for the partial revs
// and subtract it
var partial_rpm_revs = this.lastRPM_cnt - Math.floor(this.lastRPM_cnt);
var partial_rpm_time = partial_rpm_revs * (1000/rpm_speed);
// Calculate new rev-time
var t = ((current_time - partial_rpm_time)) * 1.024;
var et = t % 65536;
// Assign accumulated crank revs
this.RPM_cnt = Math.floor(this.lastRPM_cnt);
this.RPM_time = et;
this.data.RPMcnt = this.RPM_cnt;
this.data.RPMtime = this.RPM_time;
}
}
};
compute_wheel_revs_for_CPM() {
this.data.WHEELcnt = 0;
this.data.WHEELtime = 0;
// pas de data du velo : on ne peut rien faire
if (this.data == null)
return;
var rpm = this.data.rpm;
this.data.WHEELcnt = this.WHEEL_cnt;
this.data.WHEELtime = this.WHEEL_time;
if (rpm){
if(this.lastWHEEL_time == 0) {
this.lastWHEEL_time = Date.now();
}
var ratio = gearRatio[this.gear - 1];
// Get time difference
var current_time = Date.now();
var t_diff = current_time - this.lastWHEEL_time;
// Calculate revs made in the meantime
var wheel_speed = (rpm / 60) * ratio;
var wheel_add = t_diff / 1000 * wheel_speed;
// Full wheel change reached?
var full_change = Math.floor(this.lastWHEEL_cnt + wheel_add) - Math.floor(this.lastWHEEL_cnt);
if (full_change >= 1){
this.lastWHEEL_cnt += wheel_add;
this.lastWHEEL_time = current_time;
// We can only sent full wheel-revs, so calculate time for the partial revs
// and subtract it
var partial_wheel_revs = this.lastWHEEL_cnt - Math.floor(this.lastWHEEL_cnt);
var partial_wheel_time = partial_wheel_revs * (1000/wheel_speed);
// Calculate new rev-time
var t = ((current_time - partial_wheel_time)) * 2.048;
var et = t % 65536;
// Assign accumulated crank revs
this.WHEEL_cnt = Math.floor(this.lastWHEEL_cnt);
this.WHEEL_time = et;
this.data.WHEELcnt = this.WHEEL_cnt;
this.data.WHEELtime = this.WHEEL_time;
}
}
};
// Do the math
compute() {
this.compute_crank_revs_for_CPM();
this.compute_wheel_revs_for_CPM();
// pas de data du velo : on ne peut rien faire
if (this.data == null)
return;
// rien si en mode ERG
if (this.mode === 'ERG')
return;
// pas de data externe : on ne peut rien faire
if (this.external == null)
return;
var simpower = 0;
if (useRealCalc){
var G = 9.81;
var weight = 80.0; // bike + driver
var circum = 2.1 // Wire circumference
var ratio = gearRatio[this.gear -1];
var losses = 0.05;
var speed = this.data.rpm * ratio * circum / 60.0 ;
// FG = 9.81 * sin(atan(grad)) * weight
var FG = G * Math.sin(this.external.grade / 180.0 * Math.PI) * weight;
// FR = 9.81 * cos(atan(grad)) * weight * CRR
var FR = G * Math.cos(this.external.grade / 180.0 * Math.PI) * weight * this.external.crr;
// FA = 0.5 * CW * 1,225 * speed
var FA = 0.5 * this.external.cw * 1.225 * speed;
var P = (FG + FR + FA) * speed / ( 1 - losses);
P = Math.max(50.0, P);
P = P.toFixed(1);
simpower = P;
}else{
simpower = 170 * (1 + 1.15 * (this.data.rpm - 80.0) / 80.0) * (1.0 + 3 * this.external.grade / 100.0);
simpower = Math.max(0.0, simpower * (1.0 + 0.1 * (this.gear - 5)));
simpower = simpower.toFixed(1);
}
this.emit('targetpower', simpower);
// Set power changes slowly
if (this.currentPower <= simpower) {
this.currentPower += wattIncPerSec;
simpower = Math.min(simpower, this.currentPower);
}else{
this.currentPower -= wattDecPerSec;
simpower = Math.max(simpower, this.currentPower);
}
if (DEBUG) {
console.log('[BikeState.js] - SIM rpm: ', this.data.rpm);
console.log('[BikeState.js] - SIM pente: ', this.external.grade);
console.log('[BikeState.js] - SIM gear : ', this.gear);
console.log('[BikeState.js] - SIM current power: ', this.currentPower);
console.log('[BikeState.js] - SIM calculated power: ', simpower);
}
this.currentPower = simpower;
this.emit('simpower', simpower);
};
};
module.exports = BikeState