Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rtl button #28

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +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").
/// @param1 Unused
/// @param2 Unused
/// @param3 Unused
/// @param4 Unused
/// @param5 Unused
/// @param6 Unused
/// @param7 Unused
///
/// @return A MavlinkFrame representing the reutrn to launch command.

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;
}
26 changes: 26 additions & 0 deletions flutter_app/lib/modules/return_to_launch.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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;

// Requires both the constructor (Mission Item) and comm
ReturnToLaunch({required this.comm});

// Skips the queues and forces the drone to return
void returnNoQueue(int systemID, int componentID) async {
if (comm.connectionType == MavlinkCommunicationType.tcp) {
await comm.tcpSocketInitializationFlag.future;
}

var frame = returnToLaunch(comm.sequence, systemID, componentID);

comm.sequence++;
comm.write(frame);

log('[$moduleName] Returning to Launch');
}
}
26 changes: 26 additions & 0 deletions flutter_app/lib/widgets/return_to_launch_widget.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'package:flutter/material.dart';
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,
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(systemID, componentID);
},
child: const Text("Return To Launch"));
}
}
33 changes: 33 additions & 0 deletions flutter_app/test/unit/return_to_launch_constructor_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import 'package:dart_mavlink/dialects/common.dart';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm!

import 'package:dart_mavlink/mavlink.dart';
import 'package:imacs/command_constructors/return_to_launch_constructor.dart';
import 'package:test/test.dart';

void main() {
/// dialect: Selected MAVLink dialect
/// sequence: The sequence number for the MAVLink frame.
/// Each component counts up its send sequence.
/// Allows to detect packet loss.
/// systemId: The MAVLink system ID of the vehicle (normally "1").
/// componentId: The MAVLink component ID (normally "0").
var dialect = MavlinkDialectCommon();
const sequence = 0;
const systemID = 1;
const componentID = 0;

test("Return To Launch", () {
const createReturnToLaunchCommandNumber = mavCmdNavReturnToLaunch;

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));
}
});

var returnToLaunchCommand = returnToLaunch(sequence, systemID, componentID);

parser.parse(returnToLaunchCommand.serialize().buffer.asUint8List());
});
}
50 changes: 50 additions & 0 deletions flutter_app/test/widget/return_to_launch_widget_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:imacs/modules/mavlink_communication.dart';
import 'package:imacs/modules/return_to_launch.dart';
import 'package:imacs/widgets/return_to_launch_widget.dart';

void main() {
group("Return to Launch Widget", () {
testWidgets("Return to Launch Button Generate",
(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))));

// Waits for all frames and animations to settle
await tester.pumpAndSettle();

expect(find.byType(ElevatedButton), findsOneWidget);
expect(find.text("Return To Launch"), findsOneWidget);
});

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There isn't really a perfect way to check this - UI is weird :'). I'm guess you copied this over from the test for set drone more, this works over there since when the drone mode is changed, the text displayed also changes (in the widget not the log). Low key this test isn't that important since the actual widget part is subject to change. If you still want to work on this try adding a way for you module to communicate with the rest of the program that the command has been sent.

});
});
}
Loading