Show the drone's locked home point on the fly-mode map

Adds MSP_WP (118) support to query the flightcontroller's stored home
point (WP#0 is a special case for this in iNAV's getWaypoint(), per
navigation.c) - the FC reports (0,0,0) rather than an error before one
is set, so parseMspHomePoint() treats that pair as "unset". Rendered
with a small pentagon house icon (design/homepoint-pentagon.svg,
ported to a CustomPainter like the other map icons).

Deliberately not polled continuously: a new home_point_provider.dart
refreshes it only at the three moments the FC's home point can
actually change - connect/reconnect, GPS fix acquired, and arming
(iNAV's default reset_home_type=FIRST_ARM only freezes it at the first
arm; before that it continuously follows the aircraft while disarmed).

Mock's implementation offsets the point 25m from the anchor so it
doesn't sit exactly under the drone marker during UI testing.
This commit is contained in:
Constantin Leue
2026-08-09 21:10:00 +02:00
parent c9f16b13f8
commit 5fd5d0117a
12 changed files with 594 additions and 0 deletions
@@ -30,6 +30,14 @@ class _FakeFlightController {
bool wpGetInfoValid = false;
int wpGetInfoCount = 0;
/// null simuliert "FC hat noch keinen Homepoint gesetzt" (`!STATE(
/// GPS_FIX_HOME)`, meldet dann (0,0,0) statt eines Fehlers).
({int lat, int lon, int altCm})? homePoint = (
lat: 525000000,
lon: 134000000,
altCm: 6000,
);
void _tick(Timer _) {
while (_answered < transport.sentData.length) {
final request =
@@ -76,6 +84,17 @@ class _FakeFlightController {
case MspCommands.wpGetInfo:
payload = Uint8List.fromList(
[0, 15, wpGetInfoValid ? 1 : 0, wpGetInfoCount]);
case MspCommands.getWp:
final wpNo = request.payload[0];
final point = homePoint;
final d = ByteData(21);
d.setUint8(0, wpNo);
if (wpNo == 0 && point != null) {
d.setInt32(2, point.lat, Endian.little);
d.setInt32(6, point.lon, Endian.little);
d.setInt32(10, point.altCm, Endian.little);
}
payload = d.buffer.asUint8List();
default:
payload = Uint8List(0);
}
@@ -170,6 +189,43 @@ void main() {
responder.cancel();
}, timeout: const Timeout(Duration(seconds: 5)));
group('readHomePoint', () {
test('fragt MSP_WP mit wp_no=0 ab und dekodiert den Homepoint', () async {
final transport = LoopbackTransport();
final responder = _startFakeFlightController(transport);
final link = MspFlightControllerLink(transport: transport);
await link.connect();
final home = await link.readHomePoint();
expect(home, isNotNull);
expect(home!.lat, closeTo(52.5, 1e-6));
expect(home.lon, closeTo(13.4, 1e-6));
expect(home.altitudeM, closeTo(60.0, 1e-9));
await link.disconnect();
responder.cancel();
}, timeout: const Timeout(Duration(seconds: 5)));
test('liefert null, wenn der FC noch keinen Homepoint gesetzt hat '
'(meldet (0,0,0) statt eines Fehlers)', () async {
final transport = LoopbackTransport();
final responder = _startFakeFlightController(transport)..homePoint = null;
final link = MspFlightControllerLink(transport: transport);
await link.connect();
expect(await link.readHomePoint(), isNull);
await link.disconnect();
responder.cancel();
}, timeout: const Timeout(Duration(seconds: 5)));
test('readHomePoint() vor connect() wirft StateError', () {
final link = MspFlightControllerLink(transport: LoopbackTransport());
expect(() => link.readHomePoint(), throwsStateError);
});
});
test('subscribeTelemetry() vor connect() wirft StateError', () {
final link = MspFlightControllerLink(transport: LoopbackTransport());
expect(() => link.subscribeTelemetry(), throwsStateError);
@@ -152,4 +152,68 @@ void main() {
expect(info.isValid, isFalse);
});
});
group('encodeMspGetWaypointRequest', () {
test('kodiert wp_no als einzelnes Byte', () {
expect(encodeMspGetWaypointRequest(0), Uint8List.fromList([0]));
expect(encodeMspGetWaypointRequest(255), Uint8List.fromList([255]));
});
test('mspWpNumberHome ist 0 (WP#0 = Homepoint laut getWaypoint())', () {
expect(mspWpNumberHome, 0);
});
});
group('parseMspHomePoint', () {
/// Baut die 21-Byte MSP_WP-Antwort wie der echte FC (`fc_msp.c`,
/// `mspFcWaypointOutCommand()`) - dient nur der Testverifikation.
Uint8List encodeFakeWpResponse({
int wpNo = 0,
int action = 0,
required int lat,
required int lon,
required int altCm,
}) {
final d = ByteData(21);
d.setUint8(0, wpNo);
d.setUint8(1, action);
d.setInt32(2, lat, Endian.little);
d.setInt32(6, lon, Endian.little);
d.setInt32(10, altCm, Endian.little);
return d.buffer.asUint8List();
}
test('dekodiert lat/lon (1e-7 deg) und Hoehe (cm -> m)', () {
final home = parseMspHomePoint(encodeFakeWpResponse(
lat: 525000000,
lon: 134000000,
altCm: 6050,
));
expect(home, isNotNull);
expect(home!.lat, closeTo(52.5, 1e-9));
expect(home.lon, closeTo(13.4, 1e-9));
expect(home.altitudeM, closeTo(60.5, 1e-9));
});
test('liefert null bei (0,0) - FCs eigener "kein Homepoint gesetzt"-Wert '
'(STATE(GPS_FIX_HOME) noch nicht erreicht)', () {
final home = parseMspHomePoint(encodeFakeWpResponse(
lat: 0,
lon: 0,
altCm: 0,
));
expect(home, isNull);
});
test('lat == 0 alleine (echter Punkt auf dem Aequator) zaehlt NICHT als '
'unset - nur (0,0) gemeinsam ist der Sonderfall', () {
final home = parseMspHomePoint(encodeFakeWpResponse(
lat: 0,
lon: 134000000,
altCm: 0,
));
expect(home, isNotNull);
expect(home!.lon, closeTo(13.4, 1e-9));
});
});
}