From 2bf21404e0343c0d01475fa93053b41c409bbb95 Mon Sep 17 00:00:00 2001 From: andrewwan Date: Fri, 1 Nov 2024 00:56:28 -0400 Subject: [PATCH] Updated RTL --- .../return_to_launch_constructor.dart | 52 ++++++++----------- flutter_app/lib/modules/return_to_launch.dart | 12 ++--- .../lib/widgets/return_to_launch_widget.dart | 10 +++- .../return_to_launch_constructor_test.dart | 9 +--- .../widget/return_to_launch_widget_test.dart | 29 +++++++++-- 5 files changed, 60 insertions(+), 52 deletions(-) diff --git a/flutter_app/lib/command_constructors/return_to_launch_constructor.dart b/flutter_app/lib/command_constructors/return_to_launch_constructor.dart index 850e1f7..81c74eb 100644 --- a/flutter_app/lib/command_constructors/return_to_launch_constructor.dart +++ b/flutter_app/lib/command_constructors/return_to_launch_constructor.dart @@ -1,43 +1,37 @@ import 'package:dart_mavlink/dialects/common.dart'; +import 'package:dart_mavlink/mavlink.dart'; /// Constructs a MissionItem command to return to launch using MAV_CMD_NAV_RETURN_TO_LAUNCH (20). /// /// @sequence The sequence number for the MAVLink frame. /// @systemId The MAVLink system ID of the vehicle (normally "1"). /// @componentId The MAVLink component ID (normally "0"). -/// @latitude The latitude of the launchpad (Defaults to where it was armed). -/// @longitude The longitude of the launchpad (Defaults to where it was armed). -/// @altitude The altitude of the waypoint (Defaults to 15m) /// @param1 Unused /// @param2 Unused /// @param3 Unused /// @param4 Unused +/// @param5 Unused +/// @param6 Unused +/// @param7 Unused /// -/// @return A MissionItem representing the reutrn to launch command. +/// @return A MavlinkFrame representing the reutrn to launch command. -MissionItem returnToLaunch(int sequence, int systemID, int componentID, - {double latitude = 0.0, - double longitude = 0.0, - double altitude = 15.0, - double param1 = 0, - double param2 = 0, - double param3 = 0, - double param4 = 0}) { - var missionItem = MissionItem( - targetSystem: systemID, - targetComponent: componentID, - seq: sequence, - frame: mavFrameGlobalRelativeAlt, - command: mavCmdNavReturnToLaunch, - current: 1, - autocontinue: 1, - param1: param1, - param2: param2, - param3: param3, - param4: param4, - x: latitude, - y: longitude, - z: altitude, - missionType: 0); - return missionItem; +MavlinkFrame returnToLaunch(int sequence, int systemID, int componentID) { + var commandLong = CommandLong( + targetSystem: systemID, + targetComponent: componentID, + command: mavCmdNavReturnToLaunch, + confirmation: 0, + param1: 0, + param2: 0, + param3: 0, + param4: 0, + param5: 0, + param6: 0, + param7: 0, + ); + + var frm = MavlinkFrame.v2(sequence, systemID, componentID, commandLong); + + return frm; } diff --git a/flutter_app/lib/modules/return_to_launch.dart b/flutter_app/lib/modules/return_to_launch.dart index 214b05e..6f09f73 100644 --- a/flutter_app/lib/modules/return_to_launch.dart +++ b/flutter_app/lib/modules/return_to_launch.dart @@ -1,5 +1,6 @@ import 'package:dart_mavlink/dialects/common.dart'; import 'package:dart_mavlink/mavlink.dart'; +import 'package:imacs/command_constructors/return_to_launch_constructor.dart'; import 'package:imacs/modules/mavlink_communication.dart'; import 'dart:developer'; @@ -7,22 +8,17 @@ const String moduleName = "Return To Launch"; class ReturnToLaunch { final MavlinkCommunication comm; - final MissionItem returnToLaunchConstructor; // Requires both the constructor (Mission Item) and comm - ReturnToLaunch({required this.comm, required this.returnToLaunchConstructor}); + ReturnToLaunch({required this.comm}); // Skips the queues and forces the drone to return - void returnNoQueue() async { + void returnNoQueue(int systemID, int componentID) async { if (comm.connectionType == MavlinkCommunicationType.tcp) { await comm.tcpSocketInitializationFlag.future; } - var frame = MavlinkFrame.v2( - returnToLaunchConstructor.seq, - returnToLaunchConstructor.targetSystem, - returnToLaunchConstructor.targetComponent, - returnToLaunchConstructor); + var frame = returnToLaunch(comm.sequence, systemID, componentID); comm.sequence++; comm.write(frame); diff --git a/flutter_app/lib/widgets/return_to_launch_widget.dart b/flutter_app/lib/widgets/return_to_launch_widget.dart index 0512245..05e3415 100644 --- a/flutter_app/lib/widgets/return_to_launch_widget.dart +++ b/flutter_app/lib/widgets/return_to_launch_widget.dart @@ -3,9 +3,15 @@ import 'package:imacs/modules/return_to_launch.dart'; class ReturnToLaunchButton extends StatelessWidget { final ReturnToLaunch returnToLaunchCommand; + final int systemID; + final int componentID; // Needs the ReturnToLaunch object from the return_to_launch dart - const ReturnToLaunchButton({Key? key, required this.returnToLaunchCommand}) + const ReturnToLaunchButton( + {Key? key, + required this.returnToLaunchCommand, + required this.systemID, + required this.componentID}) : super(key: key); // Returns a button that tells the drone to return to launch @@ -13,7 +19,7 @@ class ReturnToLaunchButton extends StatelessWidget { Widget build(BuildContext context) { return ElevatedButton( onPressed: () { - returnToLaunchCommand.returnNoQueue(); + returnToLaunchCommand.returnNoQueue(systemID, componentID); }, child: const Text("Return To Launch")); } diff --git a/flutter_app/test/unit/return_to_launch_constructor_test.dart b/flutter_app/test/unit/return_to_launch_constructor_test.dart index 03584a2..d425298 100644 --- a/flutter_app/test/unit/return_to_launch_constructor_test.dart +++ b/flutter_app/test/unit/return_to_launch_constructor_test.dart @@ -17,23 +17,16 @@ void main() { test("Return To Launch", () { const createReturnToLaunchCommandNumber = mavCmdNavReturnToLaunch; - const latitude = 47.938; - const longitude = 8.545; - const altitude = 15.0; var parser = MavlinkParser(dialect); parser.stream.listen((MavlinkFrame frm) { if (frm.message is MissionItem) { var mi = frm.message as MissionItem; expect(mi.command, equals(createReturnToLaunchCommandNumber)); - expect(mi.x, equals(latitude)); - expect(mi.y, equals(longitude)); - expect(mi.z, equals(altitude)); } }); - var returnToLaunchCommand = returnToLaunch(sequence, systemID, componentID, - latitude: latitude, longitude: longitude, altitude: altitude); + var returnToLaunchCommand = returnToLaunch(sequence, systemID, componentID); parser.parse(returnToLaunchCommand.serialize().buffer.asUint8List()); }); diff --git a/flutter_app/test/widget/return_to_launch_widget_test.dart b/flutter_app/test/widget/return_to_launch_widget_test.dart index 6035fb3..bbb1955 100644 --- a/flutter_app/test/widget/return_to_launch_widget_test.dart +++ b/flutter_app/test/widget/return_to_launch_widget_test.dart @@ -11,14 +11,15 @@ void main() { (WidgetTester tester) async { final mavlinkCommunication = MavlinkCommunication( MavlinkCommunicationType.tcp, '127.0.0.1', 14550); - final ReturnToLaunch command = ReturnToLaunch( - comm: mavlinkCommunication, - returnToLaunchConstructor: returnToLaunch(0, 1, 0)); + final ReturnToLaunch command = ReturnToLaunch(comm: mavlinkCommunication); // Tests to see if the button renders await tester.pumpWidget(MaterialApp( home: Scaffold( - body: ReturnToLaunchButton(returnToLaunchCommand: command)))); + body: ReturnToLaunchButton( + returnToLaunchCommand: command, + systemID: 1, + componentID: 0)))); // Waits for all frames and animations to settle await tester.pumpAndSettle(); @@ -27,6 +28,24 @@ void main() { expect(find.text("Return To Launch"), findsOneWidget); }); - testWidgets("Button sends MavLink command", (WidgetTester tester) async {}); + testWidgets("Button sends MavLink command", (WidgetTester tester) async { + final mavlinkCommunication = MavlinkCommunication( + MavlinkCommunicationType.tcp, '127.0.0.1', 14550); + final ReturnToLaunch command = ReturnToLaunch(comm: mavlinkCommunication); + + // Tests to see if the button renders + await tester.pumpWidget(MaterialApp( + home: Scaffold( + body: ReturnToLaunchButton( + returnToLaunchCommand: command, + systemID: 1, + componentID: 0)))); + + await tester.pumpAndSettle(); + await tester.tap(find.byType(ReturnToLaunchButton)); + await tester.pumpAndSettle(); + + expect(find.text("Return to Launch Command Sent"), findsOneWidget); + }); }); }