Files
abawo-bt-app/lib/model/simulation.dart
2026-11-24 20:02:05 +01:00

184 lines
5.9 KiB
Dart

import 'dart:convert';
import 'dart:math' as math;
import 'dart:typed_data';
enum SimulationPolicy {
auto('Auto'),
force('Force emulation'),
native('Native only');
const SimulationPolicy(this.label);
final String label;
}
class SimulationConfig {
const SimulationConfig({
this.policy = SimulationPolicy.auto,
this.modelId = 1,
this.riderWeight = 75,
this.bikeWeight = 10,
this.gradeMultiplier = 1,
this.rollingMultiplier = 1,
this.windMultiplier = 1,
});
final SimulationPolicy policy;
final int modelId;
final double riderWeight;
final double bikeWeight;
final double gradeMultiplier;
final double rollingMultiplier;
final double windMultiplier;
SimulationConfig copyWith(
{SimulationPolicy? policy,
int? modelId,
double? riderWeight,
double? bikeWeight,
double? gradeMultiplier,
double? rollingMultiplier,
double? windMultiplier}) =>
SimulationConfig(
policy: policy ?? this.policy,
modelId: modelId ?? this.modelId,
riderWeight: riderWeight ?? this.riderWeight,
bikeWeight: bikeWeight ?? this.bikeWeight,
gradeMultiplier: gradeMultiplier ?? this.gradeMultiplier,
rollingMultiplier: rollingMultiplier ?? this.rollingMultiplier,
windMultiplier: windMultiplier ?? this.windMultiplier);
List<int> encode() {
final values = [
riderWeight,
bikeWeight,
gradeMultiplier,
rollingMultiplier,
windMultiplier
];
final limits = [300.0, 100.0, 3.0, 3.0, 3.0];
for (var i = 0; i < values.length; i++) {
if (!values[i].isFinite ||
values[i] < (i == 0 ? 1 : 0) ||
values[i] > limits[i]) {
throw const FormatException('Simulation setting is out of range.');
}
}
if (modelId < 1 || modelId > 255) {
throw const FormatException('Invalid simulation model.');
}
final data = ByteData(14);
data.setUint8(0, 1);
data.setUint8(1, policy.index);
data.setUint8(2, modelId);
for (var i = 0; i < values.length; i++) {
data.setUint16(
4 + i * 2, (values[i] * (i < 2 ? 100 : 1000)).round(), Endian.little);
}
return data.buffer.asUint8List();
}
static SimulationConfig decode(List<int> bytes) {
if (bytes.any((byte) => byte < 0 || byte > 255) ||
bytes.length != 14 ||
bytes[0] != 1 ||
bytes[1] > 2 ||
bytes[2] == 0 ||
bytes[3] != 0) {
throw const FormatException('Unsupported simulation configuration.');
}
final data = ByteData.sublistView(Uint8List.fromList(bytes));
final config = SimulationConfig(
policy: SimulationPolicy.values[bytes[1]],
modelId: bytes[2],
riderWeight: data.getUint16(4, Endian.little) / 100,
bikeWeight: data.getUint16(6, Endian.little) / 100,
gradeMultiplier: data.getUint16(8, Endian.little) / 1000,
rollingMultiplier: data.getUint16(10, Endian.little) / 1000,
windMultiplier: data.getUint16(12, Endian.little) / 1000);
config.encode();
return config;
}
/// Mirrors biketrainer-sim-mode::model at ratio 1, default Crr/Cw,
/// no headwind or drafting. This is not native trainer power prediction.
double previewWatts(double speedKmh, double gradePercent) {
if (modelId != 1 && modelId != 2) {
throw const FormatException('Preview unavailable for this model.');
}
final v = speedKmh / 3.6;
if (!v.isFinite || v <= 0) return 0;
final mass = riderWeight + bikeWeight;
final force = mass *
(gradePercent / 100 * 9.81 * gradeMultiplier +
0.004 * 9.8 * rollingMultiplier) +
(modelId == 2 ? 0 : 0.5 * 0.51 * windMultiplier * v * v);
return math.max(0, force) * v;
}
}
Map<int, String> decodeSimulationModels(List<int> bytes) {
final result = <int, String>{};
for (final entry in utf8.decode(bytes).split(';')) {
final parts = entry.split(':');
final id = parts.length == 2 ? int.tryParse(parts[0]) : null;
if (id == null ||
id < 1 ||
id > 255 ||
parts[1].isEmpty ||
result.containsKey(id)) {
throw const FormatException('Invalid simulation model catalog.');
}
result[id] = parts[1];
}
return result;
}
enum SimulationMode { inactive, native, emulated, fault }
class SimulationStatus {
const SimulationStatus(this.mode, this.speedFresh, this.error, this.watts);
final SimulationMode mode;
final bool speedFresh;
final int error;
final int? watts;
static SimulationStatus decode(List<int> bytes) {
if (bytes.any((byte) => byte < 0 || byte > 255) ||
bytes.length != 6 ||
bytes[0] != 1 ||
bytes[1] > 3) {
throw const FormatException('Unsupported simulation status.');
}
final watts = bytes[4] | bytes[5] << 8;
return SimulationStatus(SimulationMode.values[bytes[1]], bytes[2] & 1 != 0,
bytes[3], watts == 0xffff ? null : watts);
}
String get label => switch (mode) {
SimulationMode.inactive => 'Inactive',
SimulationMode.native => 'Native simulation',
SimulationMode.emulated => 'Emulated simulation',
SimulationMode.fault => 'Simulation fault',
};
String? get details {
if (mode == SimulationMode.fault) {
return switch (error) {
2 =>
'The trainer does not support the requested control. Check the simulation mode.',
3 => 'The trainer rejected the target value. Check your settings.',
4 =>
'The trainer could not apply the target power. Reconnect and try again.',
5 => 'Trainer control was denied. Disconnect other apps and try again.',
_ =>
'Simulation stopped after a trainer error ($error). Reconnect and try again.',
};
}
if (mode != SimulationMode.emulated) return null;
if (!speedFresh) return 'Waiting for trainer speed';
return watts == null
? 'Waiting for target power'
: 'Target power: $watts W';
}
}