Skip to content

Commit

Permalink
Updated RTL
Browse files Browse the repository at this point in the history
  • Loading branch information
Person8790 committed Nov 1, 2024
1 parent 35d9b1b commit 2bf2140
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 52 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
12 changes: 4 additions & 8 deletions flutter_app/lib/modules/return_to_launch.dart
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
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';

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);
Expand Down
10 changes: 8 additions & 2 deletions flutter_app/lib/widgets/return_to_launch_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,23 @@ 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
@override
Widget build(BuildContext context) {
return ElevatedButton(
onPressed: () {
returnToLaunchCommand.returnNoQueue();
returnToLaunchCommand.returnNoQueue(systemID, componentID);
},
child: const Text("Return To Launch"));
}
Expand Down
9 changes: 1 addition & 8 deletions flutter_app/test/unit/return_to_launch_constructor_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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());
});
Expand Down
29 changes: 24 additions & 5 deletions flutter_app/test/widget/return_to_launch_widget_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
});
});
}

0 comments on commit 2bf2140

Please sign in to comment.