forked from 360manu/kettlerUSB2BLE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BikeState.js
124 lines (105 loc) · 2.62 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
const EventEmitter = require('events');
var DEBUG = true;
var minGear = 1;
var maxGear = 10;
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;
};
// 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;
// update
this.compute();
};
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));
};
// Do the math
compute() {
// rien si en mode ERG
if (this.mode === 'ERG')
return;
// pas de data du velo : on ne peut rien faire
if (this.data == null)
return;
// pas de data externe : on ne peut rien faire
if (this.external == null)
return;
var simpower = 170 * (1 + 1.15 * (this.data.rpm - 80.0) / 80.0) * (1.0 + 3 * this.external.grade / 100.0);
// apply gear
simpower = Math.max(0.0, simpower * (1.0 + 0.1 * (this.gear - 5)));
// store
simpower = simpower.toFixed(1);
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 calculated power: ', simpower);
console.log('[BikeState.js] - SIM heart rate: ', this.data.hr);
}
this.emit('simpower', simpower);
};
};
module.exports = BikeState