incyclist-devices 2.4.4 → 2.4.6-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -32,7 +32,10 @@ export default class SmartTrainerCyclingMode extends PowerBasedCyclingModeBase i
32
32
  protected tsStart: number;
33
33
  protected simPower: number;
34
34
  protected simSlope: number;
35
+ protected maintainPower: number;
35
36
  protected prevData: any;
37
+ protected prevEkin: number;
38
+ protected prevSimPower: number;
36
39
  protected readonly gearRatios: number[];
37
40
  constructor(adapter: IAdapter, props?: any);
38
41
  getBikeInitRequest(): UpdateRequest;
@@ -45,7 +48,7 @@ export default class SmartTrainerCyclingMode extends PowerBasedCyclingModeBase i
45
48
  protected checkSlopeWithSimulatedShifting(request: UpdateRequest, newRequest?: UpdateRequest): void;
46
49
  checkSlope(request: UpdateRequest, newRequest?: UpdateRequest): void;
47
50
  checkCadenceChange(request: UpdateRequest, newRequest?: UpdateRequest): void;
48
- calculateSimulatedPower(request: UpdateRequest): void;
51
+ calculateSimulatedPower(changed?: string): void;
49
52
  checkGearChange(request: UpdateRequest, newRequest?: UpdateRequest): void;
50
53
  protected verifySimPower(): void;
51
54
  protected checkEmptyRequest(newRequest: UpdateRequest): void;
@@ -95,7 +95,7 @@ class SmartTrainerCyclingMode extends power_base_1.default {
95
95
  { key: 'Mixed', display: 'App + Bike' },
96
96
  { key: 'SmartTrainer', display: 'SmartTreiner (beta)' }
97
97
  ];
98
- virtshift = { key: 'virtshift', name: 'Virtual Shifting', description: 'Enable virtual shifting', type: types_1.CyclingModeProperyType.SingleSelect, options, default: 'SmartTrainer' };
98
+ virtshift = { key: 'virtshift', name: 'Virtual Shifting', description: 'Enable virtual shifting', type: types_1.CyclingModeProperyType.SingleSelect, options, default: 'Mixed' };
99
99
  config.properties.push(virtshift);
100
100
  }
101
101
  if (virtshift && !startGear) {
@@ -141,14 +141,14 @@ class SmartTrainerCyclingMode extends power_base_1.default {
141
141
  }
142
142
  }
143
143
  checkSlopeWithSimulatedShifting(request, newRequest = {}) {
144
- var _a, _b, _c, _d, _e;
144
+ var _a;
145
+ if (this.simPower)
146
+ return;
145
147
  if (this.gear === undefined) {
146
148
  this.checkSlopeNoShiftig(request, newRequest);
147
149
  return;
148
150
  }
149
151
  if (request.slope === undefined) {
150
- console.log('# set simulated power (same slope):', { simPower: this.simPower, gear: this.gear, simSlope: this.simSlope, routeSlope: this.data.slope, cadence: this.data.pedalRpm, power: this.data.power });
151
- this.logger.logEvent({ message: 'set simulated power (same slope)', simPower: this.simPower, gear: this.gear, simSlope: this.simSlope, routeSlope: this.data.slope, cadence: this.data.pedalRpm, power: this.data.power });
152
152
  }
153
153
  else {
154
154
  const prev = (_a = this.data.slope) !== null && _a !== void 0 ? _a : 0;
@@ -161,36 +161,9 @@ class SmartTrainerCyclingMode extends power_base_1.default {
161
161
  if (slopeAdj !== undefined)
162
162
  this.simSlope = this.simSlope * slopeAdj / 100;
163
163
  }
164
- catch (_f) {
165
- }
166
- const virtualSpeed = (0, calculations_1.calculateVirtualSpeed)(this.data.pedalRpm, this.gearRatios[this.gear - 1]);
167
- const m = (_c = (_b = this.adapter) === null || _b === void 0 ? void 0 : _b.getWeight()) !== null && _c !== void 0 ? _c : 85;
168
- const vCurrent = this.data.speed * 1000 / 3600;
169
- const eKinCurrent = m * vCurrent * vCurrent / 2;
170
- const newPower = calculations_1.default.calculatePower(m, virtualSpeed, (_d = this.simSlope) !== null && _d !== void 0 ? _d : 0);
171
- const prevPower = this.data.power;
172
- if (this.data.speed < 10 && this.data.isPedalling && (this.data.slope < 1 || this.data.speed === 0)) {
173
- this.simPower = Math.max(newPower, prevPower);
174
- console.log('# set simulated power', { power: this.simPower, gear: this.gear, simSlope: this.simSlope, routeSlope: this.data.slope, prevPower, newPower });
175
- this.logger.logEvent({ message: 'set simulated power', power: this.simPower, gear: this.gear, simSlope: this.simSlope, routeSlope: this.data.slope, prevPower, newPower });
176
- }
177
- else if (this.data.slope === prev && newPower < prevPower) {
178
- this.simPower = prevPower;
179
- console.log('# set simulated power:', { power: this.simPower, gear: this.gear, simSlope: this.simSlope, routeSlope: this.data.slope, prevPower, newPower });
180
- this.logger.logEvent({ message: 'set simulated power', power: this.simPower, gear: this.gear, simSlope: this.simSlope, routeSlope: this.data.slope, prevPower, newPower });
181
- }
182
- else {
183
- const powerDiff = newPower - prevPower;
184
- const vTarget = virtualSpeed * 1000 / 3600;
185
- const eKinTarget = m * vTarget * vTarget / 2;
186
- const eKinPrev = eKinCurrent;
187
- const delta = eKinTarget - eKinPrev;
188
- const eKinAfter1sec = eKinPrev - powerDiff * 1;
189
- const vAfter1sec = Math.sqrt(2 * eKinAfter1sec / m) * 3600 / 1000;
190
- this.simPower = calculations_1.default.calculatePower(m, vAfter1sec / 3.6, (_e = this.simSlope) !== null && _e !== void 0 ? _e : 0);
191
- console.log('# set simulated power (Ekin):', { power: this.simPower, gear: this.gear, simSlope: this.simSlope, routeSlope: this.data.slope, eKinPrev, eKinTarget, delta, prevPower, newPower });
192
- this.logger.logEvent({ message: 'set simulated power (Ekin)', power: this.simPower, gear: this.gear, simSlope: this.simSlope, routeSlope: this.data.slope, eKinPrev, eKinTarget, delta, prevPower, newPower });
164
+ catch (_b) {
193
165
  }
166
+ this.calculateSimulatedPower('slope');
194
167
  this.verifySimPower();
195
168
  }
196
169
  newRequest.targetPower = this.simPower;
@@ -214,6 +187,7 @@ class SmartTrainerCyclingMode extends power_base_1.default {
214
187
  }
215
188
  }
216
189
  checkCadenceChange(request, newRequest = {}) {
190
+ var _a;
217
191
  const virtshiftMode = this.getVirtualShiftMode();
218
192
  if (virtshiftMode !== 'Simulated') {
219
193
  return;
@@ -223,40 +197,46 @@ class SmartTrainerCyclingMode extends power_base_1.default {
223
197
  return;
224
198
  }
225
199
  if (this.data.pedalRpm !== this.prevData.pedalRpm) {
226
- console.log('# cadence changed ', { cadence: this.data.pedalRpm, prevCadence: this.prevData.pedalRpm });
227
- this.calculateSimulatedPower(request);
228
- delete request.slope;
200
+ this.logger.logEvent({ message: 'cadence changed', cadence: this.data.pedalRpm, prevCadence: this.prevData.pedalRpm });
201
+ request.slope = (_a = request.slope) !== null && _a !== void 0 ? _a : this.data.slope;
229
202
  }
230
203
  }
231
- calculateSimulatedPower(request) {
232
- var _a, _b, _c, _d;
204
+ calculateSimulatedPower(changed) {
205
+ var _a, _b, _c, _d, _e;
206
+ if (this.simPower)
207
+ return;
233
208
  const m = (_b = (_a = this.adapter) === null || _a === void 0 ? void 0 : _a.getWeight()) !== null && _b !== void 0 ? _b : 85;
234
209
  const vCurrent = this.data.speed * 1000 / 3600;
235
210
  const eKinCurrent = m * vCurrent * vCurrent / 2;
236
211
  if (this.data.pedalRpm > 0) {
237
- const virtualSpeed = (0, calculations_1.calculateVirtualSpeed)(this.data.pedalRpm, this.gearRatios[this.gear - 1]);
238
- const newPower = calculations_1.default.calculatePower(m, virtualSpeed, (_c = this.simSlope) !== null && _c !== void 0 ? _c : 0);
212
+ const virtualSpeed = (0, calculations_1.calculateVirtualSpeed)(this.data.pedalRpm, this.gearRatios[this.gear - 1]) * 3.6;
213
+ const v = virtualSpeed / 3.6;
214
+ const newPower = calculations_1.default.calculatePower(m, virtualSpeed / 3.6, (_c = this.simSlope) !== null && _c !== void 0 ? _c : 0);
239
215
  const prevPower = this.data.power;
240
- if (this.data.speed < 10 && this.data.isPedalling && (this.data.slope < 1 || this.data.speed === 0)) {
216
+ const prev = ((_e = (_d = this.prevSimPower) !== null && _d !== void 0 ? _d : prevPower) !== null && _e !== void 0 ? _e : 0);
217
+ const delta = newPower - prev;
218
+ if (!this.prevEkin && this.data.isPedalling) {
241
219
  this.simPower = Math.max(newPower, prevPower);
242
- console.log('# set simulated power (starting)', { power: this.simPower, gear: this.gear, simSlope: this.simSlope, routeSlope: this.data.slope, prevPower, newPower });
243
- this.logger.logEvent({ message: 'set simulated power (starting)', power: this.simPower, gear: this.gear, simSlope: this.simSlope, routeSlope: this.data.slope, prevPower, newPower });
220
+ this.prevEkin = m * v * v / 2;
221
+ this.logger.logEvent({ message: 'set simulated power (starting)', target: this.simPower, gear: this.gear, simSlope: this.simSlope, routeSlope: this.data.slope, prevTarget: this.prevSimPower, actualPower: prevPower, newPower });
222
+ }
223
+ else if (changed === 'gear') {
224
+ this.simPower = prev + delta / 2;
225
+ this.prevEkin = m * v * v / 2;
226
+ this.logger.logEvent({ message: 'set simulated power (gear change)', target: this.simPower, gear: this.gear, simSlope: this.simSlope, routeSlope: this.data.slope, prevTarget: this.prevSimPower, actualPower: prevPower, newPower });
227
+ }
228
+ else if (changed === 'slope' || changed === 'cadence') {
229
+ this.simPower = prev + delta / 4;
230
+ this.logger.logEvent({ message: `set simulated power (${changed} change)`, target: this.simPower, gear: this.gear, simSlope: this.simSlope, routeSlope: this.data.slope, prevTarget: this.prevSimPower, actualPower: prevPower, newPower });
244
231
  }
245
232
  else {
246
- const powerDiff = newPower - prevPower;
247
- const vTarget = virtualSpeed * 1000 / 3600;
248
- const eKinTarget = m * vTarget * vTarget / 2;
249
- const eKinPrev = eKinCurrent;
250
- const delta = eKinTarget - eKinPrev;
251
- const eKinAfter1sec = eKinPrev - powerDiff * 1;
252
- const vAfter1sec = Math.sqrt(2 * eKinAfter1sec / m) * 3600 / 1000;
253
- this.simPower = calculations_1.default.calculatePower(m, vAfter1sec / 3.6, (_d = this.simSlope) !== null && _d !== void 0 ? _d : 0);
254
- console.log('# set simulated power (Ekin):', { power: this.simPower, gear: this.gear, simSlope: this.simSlope, routeSlope: this.data.slope, eKinPrev, eKinTarget, delta, prevPower, newPower });
255
- this.logger.logEvent({ message: 'set simulated power (Ekin)', power: this.simPower, gear: this.gear, simSlope: this.simSlope, routeSlope: this.data.slope, eKinPrev, eKinTarget, delta, prevPower, newPower });
256
233
  }
257
234
  }
258
235
  else {
259
236
  delete this.simPower;
237
+ if (this.data.speed < 5) {
238
+ delete this.prevEkin;
239
+ }
260
240
  }
261
241
  }
262
242
  checkGearChange(request, newRequest = {}) {
@@ -271,12 +251,13 @@ class SmartTrainerCyclingMode extends power_base_1.default {
271
251
  break;
272
252
  case 'Simulated':
273
253
  if (request.gearDelta !== undefined) {
254
+ const oldGear = this.gear;
274
255
  if (this.gear === undefined) {
275
- const initialGear = this.getSetting('startGear');
276
- this.gear = initialGear + request.gearDelta;
256
+ const initialGear = Number(this.getSetting('startGear'));
257
+ this.gear = initialGear + Number(request.gearDelta);
277
258
  }
278
259
  else {
279
- this.gear += request.gearDelta;
260
+ this.gear += Number(request.gearDelta);
280
261
  }
281
262
  if (this.gear < 1) {
282
263
  this.gear = 1;
@@ -285,9 +266,12 @@ class SmartTrainerCyclingMode extends power_base_1.default {
285
266
  this.gear = this.gearRatios.length;
286
267
  }
287
268
  delete request.gearDelta;
288
- this.calculateSimulatedPower(request);
269
+ console.log('# gear changed');
270
+ this.logger.logEvent({ message: 'gear changed', gear: this.gear, oldGear });
271
+ this.calculateSimulatedPower('gear');
289
272
  if (this.simPower !== undefined) {
290
273
  this.adapter.sendUpdate({ targetPower: this.simPower }).then(() => { });
274
+ this.prevSimPower = this.simPower;
291
275
  }
292
276
  }
293
277
  break;
@@ -310,7 +294,7 @@ class SmartTrainerCyclingMode extends power_base_1.default {
310
294
  }
311
295
  else if (request.gearDelta !== undefined) {
312
296
  if (this.gear === undefined) {
313
- const initialGear = this.getSetting('startGear');
297
+ const initialGear = Number(this.getSetting('startGear'));
314
298
  this.gear = initialGear + request.gearDelta;
315
299
  }
316
300
  else {
@@ -327,7 +311,7 @@ class SmartTrainerCyclingMode extends power_base_1.default {
327
311
  }
328
312
  else {
329
313
  if (this.gear === undefined) {
330
- const initialGear = this.getSetting('startGear');
314
+ const initialGear = Number(this.getSetting('startGear'));
331
315
  this.gear = initialGear + request.gearDelta;
332
316
  }
333
317
  newRequest.gearRatio = this.gearRatios[this.gear];
@@ -386,6 +370,7 @@ class SmartTrainerCyclingMode extends power_base_1.default {
386
370
  }
387
371
  updateData(bikeData, log) {
388
372
  var _a;
373
+ console.log('# update data', bikeData);
389
374
  const prev = this.prevData = Object.assign({}, this.data);
390
375
  const data = super.updateData(bikeData, log);
391
376
  const mode = this.getVirtualShiftMode();
@@ -398,9 +383,10 @@ class SmartTrainerCyclingMode extends power_base_1.default {
398
383
  this.tsStart = Date.now();
399
384
  }
400
385
  if (this.gear === undefined && this.tsStart && data.power > 0 && (Date.now() - this.tsStart > 3000)) {
401
- this.gear = (_a = this.getSetting('startGear')) !== null && _a !== void 0 ? _a : 0;
386
+ this.gear = (_a = Number(this.getSetting('startGear'))) !== null && _a !== void 0 ? _a : 0;
402
387
  data.gearStr = this.getGearString();
403
388
  }
389
+ console.log('# data updated', data);
404
390
  return data;
405
391
  }
406
392
  getData() {
@@ -416,9 +402,11 @@ class SmartTrainerCyclingMode extends power_base_1.default {
416
402
  return super.updateRequired(request);
417
403
  }
418
404
  sendBikeUpdate(incoming) {
405
+ console.log('# bike update request', incoming);
419
406
  this.logger.logEvent({ message: "processing update request", request: incoming, prev: this.prevRequest, data: this.getData() });
420
407
  let newRequest = {};
421
408
  const request = Object.assign({}, incoming);
409
+ delete this.simPower;
422
410
  try {
423
411
  const req = this.checkForResetOrEmpty(request);
424
412
  if (req) {
@@ -434,24 +422,27 @@ class SmartTrainerCyclingMode extends power_base_1.default {
434
422
  catch (err) {
435
423
  this.logger.logEvent({ message: "error", fn: 'sendBikeUpdate()', error: err.message, stack: err.stack });
436
424
  }
425
+ if (newRequest.targetPower) {
426
+ this.prevSimPower = newRequest.targetPower;
427
+ }
437
428
  return newRequest;
438
429
  }
439
430
  getGearString() {
440
- var _a, _b, _c, _d;
431
+ var _a, _b, _c, _d, _e;
441
432
  const mode = this.getVirtualShiftMode();
442
433
  if (mode === "Disabled")
443
434
  return undefined;
444
435
  if (mode === 'Simulated') {
445
- this.gear = (_b = (_a = this.gear) !== null && _a !== void 0 ? _a : this.getSetting('startGear')) !== null && _b !== void 0 ? _b : 0;
436
+ this.gear = (_a = this.gear) !== null && _a !== void 0 ? _a : Number((_b = this.getSetting('startGear')) !== null && _b !== void 0 ? _b : 0);
446
437
  return this.gear.toString();
447
438
  }
448
439
  if (mode === "SlopeDelta")
449
440
  return this.gearDelta > 0 ? `+${this.gearDelta}` : `${this.gearDelta}`;
450
441
  if (mode === 'Adapter') {
451
- this.gear = (_c = this.gear) !== null && _c !== void 0 ? _c : this.getSetting('startGear');
442
+ this.gear = (_c = this.gear) !== null && _c !== void 0 ? _c : Number((_d = this.getSetting('startGear')) !== null && _d !== void 0 ? _d : 1);
452
443
  return this.gear.toString();
453
444
  }
454
- return (_d = this.gear) === null || _d === void 0 ? void 0 : _d.toString();
445
+ return (_e = this.gear) === null || _e === void 0 ? void 0 : _e.toString();
455
446
  }
456
447
  getFeatureToogle() {
457
448
  return (0, features_1.useFeatureToggle)();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "incyclist-devices",
3
- "version": "2.4.4",
3
+ "version": "2.4.6-beta.1",
4
4
  "dependencies": {
5
5
  "@protobuf-ts/runtime": "^2.11.1",
6
6
  "@serialport/bindings-interface": "^1.2.2",