incyclist-devices 2.4.4 → 2.4.5

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,9 @@ 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;
36
38
  protected readonly gearRatios: number[];
37
39
  constructor(adapter: IAdapter, props?: any);
38
40
  getBikeInitRequest(): UpdateRequest;
@@ -45,7 +47,7 @@ export default class SmartTrainerCyclingMode extends PowerBasedCyclingModeBase i
45
47
  protected checkSlopeWithSimulatedShifting(request: UpdateRequest, newRequest?: UpdateRequest): void;
46
48
  checkSlope(request: UpdateRequest, newRequest?: UpdateRequest): void;
47
49
  checkCadenceChange(request: UpdateRequest, newRequest?: UpdateRequest): void;
48
- calculateSimulatedPower(request: UpdateRequest): void;
50
+ calculateSimulatedPower(changed?: string): void;
49
51
  checkGearChange(request: UpdateRequest, newRequest?: UpdateRequest): void;
50
52
  protected verifySimPower(): void;
51
53
  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,7 +141,9 @@ 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;
@@ -161,36 +163,9 @@ class SmartTrainerCyclingMode extends power_base_1.default {
161
163
  if (slopeAdj !== undefined)
162
164
  this.simSlope = this.simSlope * slopeAdj / 100;
163
165
  }
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 });
166
+ catch (_b) {
193
167
  }
168
+ this.calculateSimulatedPower('slope');
194
169
  this.verifySimPower();
195
170
  }
196
171
  newRequest.targetPower = this.simPower;
@@ -224,39 +199,53 @@ class SmartTrainerCyclingMode extends power_base_1.default {
224
199
  }
225
200
  if (this.data.pedalRpm !== this.prevData.pedalRpm) {
226
201
  console.log('# cadence changed ', { cadence: this.data.pedalRpm, prevCadence: this.prevData.pedalRpm });
227
- this.calculateSimulatedPower(request);
202
+ this.logger.logEvent({ message: 'cadence changed', cadence: this.data.pedalRpm, prevCadence: this.prevData.pedalRpm });
203
+ this.calculateSimulatedPower('cadence');
228
204
  delete request.slope;
229
205
  }
230
206
  }
231
- calculateSimulatedPower(request) {
232
- var _a, _b, _c, _d;
207
+ calculateSimulatedPower(changed) {
208
+ var _a, _b, _c, _d, _e;
209
+ if (this.simPower)
210
+ return;
233
211
  const m = (_b = (_a = this.adapter) === null || _a === void 0 ? void 0 : _a.getWeight()) !== null && _b !== void 0 ? _b : 85;
234
212
  const vCurrent = this.data.speed * 1000 / 3600;
235
213
  const eKinCurrent = m * vCurrent * vCurrent / 2;
236
214
  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);
215
+ const virtualSpeed = (0, calculations_1.calculateVirtualSpeed)(this.data.pedalRpm, this.gearRatios[this.gear - 1]) * 3.6;
216
+ const v = virtualSpeed / 3.6;
217
+ const newPower = calculations_1.default.calculatePower(m, virtualSpeed / 3.6, (_c = this.simSlope) !== null && _c !== void 0 ? _c : 0);
239
218
  const prevPower = this.data.power;
240
- if (this.data.speed < 10 && this.data.isPedalling && (this.data.slope < 1 || this.data.speed === 0)) {
219
+ if (!this.prevEkin && this.data.isPedalling) {
241
220
  this.simPower = Math.max(newPower, prevPower);
221
+ this.prevEkin = m * v * v / 2;
242
222
  console.log('# set simulated power (starting)', { power: this.simPower, gear: this.gear, simSlope: this.simSlope, routeSlope: this.data.slope, prevPower, newPower });
243
223
  this.logger.logEvent({ message: 'set simulated power (starting)', power: this.simPower, gear: this.gear, simSlope: this.simSlope, routeSlope: this.data.slope, prevPower, newPower });
244
224
  }
225
+ else if (changed === 'gear') {
226
+ this.simPower = newPower;
227
+ this.prevEkin = m * v * v / 2;
228
+ console.log('# set simulated power (gear change)', { power: this.simPower, gear: this.gear, simSlope: this.simSlope, routeSlope: this.data.slope, prevPower, newPower });
229
+ this.logger.logEvent({ message: 'set simulated power (gear change)', power: this.simPower, gear: this.gear, simSlope: this.simSlope, routeSlope: this.data.slope, prevPower, newPower });
230
+ }
245
231
  else {
246
232
  const powerDiff = newPower - prevPower;
247
233
  const vTarget = virtualSpeed * 1000 / 3600;
248
234
  const eKinTarget = m * vTarget * vTarget / 2;
249
- const eKinPrev = eKinCurrent;
235
+ const eKinPrev = (_d = this.prevEkin) !== null && _d !== void 0 ? _d : 0;
250
236
  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);
237
+ const eKinAfter1sec = eKinPrev + powerDiff * 1;
238
+ const vAfter1sec = Math.sqrt(eKinAfter1sec / m * 2);
239
+ this.simPower = calculations_1.default.calculatePower(m, vAfter1sec, (_e = this.simSlope) !== null && _e !== void 0 ? _e : 0);
254
240
  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
241
  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
242
  }
257
243
  }
258
244
  else {
259
245
  delete this.simPower;
246
+ if (this.data.speed < 5) {
247
+ delete this.prevEkin;
248
+ }
260
249
  }
261
250
  }
262
251
  checkGearChange(request, newRequest = {}) {
@@ -271,12 +260,13 @@ class SmartTrainerCyclingMode extends power_base_1.default {
271
260
  break;
272
261
  case 'Simulated':
273
262
  if (request.gearDelta !== undefined) {
263
+ const oldGear = this.gear;
274
264
  if (this.gear === undefined) {
275
- const initialGear = this.getSetting('startGear');
276
- this.gear = initialGear + request.gearDelta;
265
+ const initialGear = Number(this.getSetting('startGear'));
266
+ this.gear = initialGear + Number(request.gearDelta);
277
267
  }
278
268
  else {
279
- this.gear += request.gearDelta;
269
+ this.gear += Number(request.gearDelta);
280
270
  }
281
271
  if (this.gear < 1) {
282
272
  this.gear = 1;
@@ -285,7 +275,9 @@ class SmartTrainerCyclingMode extends power_base_1.default {
285
275
  this.gear = this.gearRatios.length;
286
276
  }
287
277
  delete request.gearDelta;
288
- this.calculateSimulatedPower(request);
278
+ console.log('# gear changed');
279
+ this.logger.logEvent({ message: 'gear changed', gear: this.gear, oldGear });
280
+ this.calculateSimulatedPower('gear');
289
281
  if (this.simPower !== undefined) {
290
282
  this.adapter.sendUpdate({ targetPower: this.simPower }).then(() => { });
291
283
  }
@@ -310,7 +302,7 @@ class SmartTrainerCyclingMode extends power_base_1.default {
310
302
  }
311
303
  else if (request.gearDelta !== undefined) {
312
304
  if (this.gear === undefined) {
313
- const initialGear = this.getSetting('startGear');
305
+ const initialGear = Number(this.getSetting('startGear'));
314
306
  this.gear = initialGear + request.gearDelta;
315
307
  }
316
308
  else {
@@ -327,7 +319,7 @@ class SmartTrainerCyclingMode extends power_base_1.default {
327
319
  }
328
320
  else {
329
321
  if (this.gear === undefined) {
330
- const initialGear = this.getSetting('startGear');
322
+ const initialGear = Number(this.getSetting('startGear'));
331
323
  this.gear = initialGear + request.gearDelta;
332
324
  }
333
325
  newRequest.gearRatio = this.gearRatios[this.gear];
@@ -386,6 +378,7 @@ class SmartTrainerCyclingMode extends power_base_1.default {
386
378
  }
387
379
  updateData(bikeData, log) {
388
380
  var _a;
381
+ console.log('# update data', bikeData);
389
382
  const prev = this.prevData = Object.assign({}, this.data);
390
383
  const data = super.updateData(bikeData, log);
391
384
  const mode = this.getVirtualShiftMode();
@@ -398,9 +391,10 @@ class SmartTrainerCyclingMode extends power_base_1.default {
398
391
  this.tsStart = Date.now();
399
392
  }
400
393
  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;
394
+ this.gear = (_a = Number(this.getSetting('startGear'))) !== null && _a !== void 0 ? _a : 0;
402
395
  data.gearStr = this.getGearString();
403
396
  }
397
+ console.log('# data updated', data);
404
398
  return data;
405
399
  }
406
400
  getData() {
@@ -416,9 +410,11 @@ class SmartTrainerCyclingMode extends power_base_1.default {
416
410
  return super.updateRequired(request);
417
411
  }
418
412
  sendBikeUpdate(incoming) {
413
+ console.log('# bike update request', incoming);
419
414
  this.logger.logEvent({ message: "processing update request", request: incoming, prev: this.prevRequest, data: this.getData() });
420
415
  let newRequest = {};
421
416
  const request = Object.assign({}, incoming);
417
+ delete this.simPower;
422
418
  try {
423
419
  const req = this.checkForResetOrEmpty(request);
424
420
  if (req) {
@@ -437,21 +433,21 @@ class SmartTrainerCyclingMode extends power_base_1.default {
437
433
  return newRequest;
438
434
  }
439
435
  getGearString() {
440
- var _a, _b, _c, _d;
436
+ var _a, _b, _c, _d, _e;
441
437
  const mode = this.getVirtualShiftMode();
442
438
  if (mode === "Disabled")
443
439
  return undefined;
444
440
  if (mode === 'Simulated') {
445
- this.gear = (_b = (_a = this.gear) !== null && _a !== void 0 ? _a : this.getSetting('startGear')) !== null && _b !== void 0 ? _b : 0;
441
+ this.gear = (_a = this.gear) !== null && _a !== void 0 ? _a : Number((_b = this.getSetting('startGear')) !== null && _b !== void 0 ? _b : 0);
446
442
  return this.gear.toString();
447
443
  }
448
444
  if (mode === "SlopeDelta")
449
445
  return this.gearDelta > 0 ? `+${this.gearDelta}` : `${this.gearDelta}`;
450
446
  if (mode === 'Adapter') {
451
- this.gear = (_c = this.gear) !== null && _c !== void 0 ? _c : this.getSetting('startGear');
447
+ this.gear = (_c = this.gear) !== null && _c !== void 0 ? _c : Number((_d = this.getSetting('startGear')) !== null && _d !== void 0 ? _d : 1);
452
448
  return this.gear.toString();
453
449
  }
454
- return (_d = this.gear) === null || _d === void 0 ? void 0 : _d.toString();
450
+ return (_e = this.gear) === null || _e === void 0 ? void 0 : _e.toString();
455
451
  }
456
452
  getFeatureToogle() {
457
453
  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.5",
4
4
  "dependencies": {
5
5
  "@protobuf-ts/runtime": "^2.11.1",
6
6
  "@serialport/bindings-interface": "^1.2.2",