The GO 4-style HUD needs far more live data than the bridge sent, and pushing every SDK callback straight to Dart would rebuild the screen dozens of times a second. This is the telemetry foundation (plan P1). On the Kotlin side, every source now writes into a TelemetryHub that emits one telemetry event per tick: flight-controller state, gimbal and vision at up to 10 Hz, battery, links, RC, storage and camera settings folded in at up to 2 Hz. Each event carries the full latest snapshot, so a consumer that drops packets never loses a slow group. New bridges cover batteries (every pack, cell voltages, warning thresholds), link quality and RC battery, diagnostics, and obstacle sensing per sensor. KeyBridge adds generic getKey/setKey/listenKey over KeyManager for the settings rows to come. The flight controller also gains smart-RTH confirmation and simulator start/stop. On the Dart side, FlightModel now owns one ChangeNotifier per subsystem (aircraft, battery, links, camera, gimbal, vision, alerts, mission), so a HUD widget can listen to just the one it draws. TelemetryReducer routes events into them, only notifies groups that changed, and gives FlightModel one notification per tick. Flat getters keep the existing screens working. Diagnostics become graded, de-duplicated alerts. Settings > About > Telemetry inspector shows every field live and can start the simulator. Battery no longer arrives as its own event, so the API Server reads the percentage from the telemetry snapshot (the old event is still accepted). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
111 lines
3.6 KiB
Dart
111 lines
3.6 KiB
Dart
import 'package:flutter/foundation.dart';
|
|
|
|
import 'parse.dart';
|
|
|
|
/// One physical battery. Volts and amps (the bridge converts from mV / mA).
|
|
@immutable
|
|
class BatteryPack {
|
|
const BatteryPack({
|
|
required this.index,
|
|
this.percent,
|
|
this.voltage,
|
|
this.current,
|
|
this.temperature,
|
|
this.chargeRemainingMah,
|
|
this.fullChargeCapacityMah,
|
|
this.designCapacityMah,
|
|
this.discharges,
|
|
this.lifetimePercent,
|
|
this.isCharging,
|
|
this.cellVoltages = const <double>[],
|
|
this.cellVoltageLevel,
|
|
this.connectionState,
|
|
this.selfHeatingState,
|
|
});
|
|
|
|
factory BatteryPack.fromMap(Map<String, dynamic> m) => BatteryPack(
|
|
index: asInt(m['index']) ?? 0,
|
|
percent: asInt(m['percent']),
|
|
voltage: finite(m['voltage']),
|
|
current: finite(m['current']),
|
|
temperature: finite(m['temperature']),
|
|
chargeRemainingMah: asInt(m['chargeRemainingMah']),
|
|
fullChargeCapacityMah: asInt(m['fullChargeCapacityMah']),
|
|
designCapacityMah: asInt(m['designCapacityMah']),
|
|
discharges: asInt(m['discharges']),
|
|
lifetimePercent: asInt(m['lifetimePercent']),
|
|
isCharging: asBool(m['isCharging']),
|
|
cellVoltages: <double>[
|
|
if (m['cellVoltages'] is List)
|
|
for (final Object? v in m['cellVoltages'] as List) if (v is num) v.toDouble(),
|
|
],
|
|
cellVoltageLevel: asString(m['cellVoltageLevel']),
|
|
connectionState: asString(m['connectionState']),
|
|
selfHeatingState: asString(m['selfHeatingState']),
|
|
);
|
|
|
|
final int index;
|
|
final int? percent;
|
|
final double? voltage; // V
|
|
final double? current; // A, negative while discharging
|
|
final double? temperature; // °C
|
|
final int? chargeRemainingMah;
|
|
final int? fullChargeCapacityMah;
|
|
final int? designCapacityMah;
|
|
final int? discharges;
|
|
final int? lifetimePercent;
|
|
final bool? isCharging;
|
|
|
|
/// Per-cell volts; empty until the `CELL_VOLTAGES` key answers (never, on
|
|
/// non-smart batteries).
|
|
final List<double> cellVoltages;
|
|
final String? cellVoltageLevel; // LEVEL_0..LEVEL_3
|
|
final String? connectionState; // NORMAL / INVALID / EXCEPTION
|
|
final String? selfHeatingState;
|
|
|
|
/// Spread between the strongest and weakest cell — the imbalance GO 4 warns on.
|
|
double? get cellDeviation {
|
|
if (cellVoltages.length < 2) return null;
|
|
final List<double> s = List<double>.of(cellVoltages)..sort();
|
|
return s.last - s.first;
|
|
}
|
|
}
|
|
|
|
/// Every battery the aircraft carries plus the flight controller's warning
|
|
/// thresholds. Fed from the `battery` group of the telemetry tick (≤2 Hz).
|
|
class BatteryState extends ChangeNotifier {
|
|
/// The weakest pack's charge — the figure that ends the flight.
|
|
int? percent;
|
|
double? voltage; // V
|
|
double? temperature; // °C
|
|
|
|
/// Warning thresholds in %, as set on the flight controller.
|
|
int? lowWarningThreshold;
|
|
int? seriousLowWarningThreshold;
|
|
|
|
List<BatteryPack> packs = const <BatteryPack>[];
|
|
|
|
DateTime? updatedAt;
|
|
|
|
bool get isMultiPack => packs.length > 1;
|
|
|
|
void apply(Map<String, dynamic> b) {
|
|
percent = asInt(b['percent']);
|
|
voltage = finite(b['voltage']);
|
|
temperature = finite(b['temperature']);
|
|
lowWarningThreshold = asInt(b['lowWarningThreshold']);
|
|
seriousLowWarningThreshold = asInt(b['seriousLowWarningThreshold']);
|
|
packs = asMapList(b['packs']).map(BatteryPack.fromMap).toList(growable: false);
|
|
updatedAt = DateTime.now();
|
|
notifyListeners();
|
|
}
|
|
|
|
void clear() {
|
|
percent = lowWarningThreshold = seriousLowWarningThreshold = null;
|
|
voltage = temperature = null;
|
|
packs = const <BatteryPack>[];
|
|
updatedAt = null;
|
|
notifyListeners();
|
|
}
|
|
}
|