Skip to content

Commit

Permalink
Configure navgrid node and field size through GUI (#478)
Browse files Browse the repository at this point in the history
* edit navgrid attributes

* update default navgrid to use 0.3m node size

* update navgrid page tests
  • Loading branch information
mjansen4857 authored Nov 18, 2023
1 parent dacc8de commit f598fe1
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 8 deletions.
113 changes: 113 additions & 0 deletions lib/pages/nav_grid_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import 'dart:math';

import 'package:file/file.dart';
import 'package:flutter/material.dart';
import 'package:function_tree/function_tree.dart';
import 'package:path/path.dart';
import 'package:pathplanner/pathfinding/nav_grid.dart';
import 'package:pathplanner/widgets/field_image.dart';
import 'package:pathplanner/util/path_painter_util.dart';
import 'package:pathplanner/widgets/number_text_field.dart';

class NavGridPage extends StatefulWidget {
final Directory deployDirectory;
Expand Down Expand Up @@ -130,10 +132,121 @@ class _NavGridPageState extends State<NavGridPage> {
),
),
),
Align(
alignment: Alignment.bottomRight,
child: Padding(
padding: const EdgeInsets.all(16.0),
child: FloatingActionButton.extended(
label: const Text('Edit Grid'),
icon: const Icon(Icons.edit),
onPressed: _showEditDialog,
),
),
),
],
);
}

void _showEditDialog() {
TextEditingController nodeSizeController = TextEditingController();
TextEditingController fieldLengthController = TextEditingController();
TextEditingController fieldWidthController = TextEditingController();

showDialog(
context: this.context,
builder: (context) {
return AlertDialog(
title: const Text('Edit Grid'),
content: SizedBox(
width: 350,
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Expanded(
child: NumberTextField(
initialText: _grid.nodeSizeMeters.toStringAsFixed(2),
label: 'Node Size (M)',
arrowKeyIncrement: 0.05,
controller: nodeSizeController,
)),
],
),
const Text(
'Larger node size = more performance, but less accuracy'),
const SizedBox(height: 16),
Row(
children: [
Expanded(
child: NumberTextField(
initialText: _grid.fieldSize.width.toStringAsFixed(2),
label: 'Field Length (M)',
arrowKeyIncrement: 0.01,
controller: fieldLengthController,
)),
const SizedBox(width: 8),
Expanded(
child: NumberTextField(
initialText: _grid.fieldSize.height.toStringAsFixed(2),
label: 'Field Width (M)',
arrowKeyIncrement: 0.01,
controller: fieldWidthController,
)),
],
),
const SizedBox(height: 32),
const Text(
'Note: Changing these attributes will clear the navgrid. This cannot be undone.'),
],
),
),
actions: [
TextButton(
onPressed: () async {
String fileContent = await DefaultAssetBundle.of(this.context)
.loadString('resources/default_navgrid.json');

setState(() {
_grid = NavGrid.fromJson(jsonDecode(fileContent));
});
_saveNavGrid();

if (mounted) {
Navigator.of(context).pop();
}
},
child: const Text('Restore Default'),
),
TextButton(
onPressed: () {
if (nodeSizeController.text.isNotEmpty &&
fieldLengthController.text.isNotEmpty &&
fieldWidthController.text.isNotEmpty) {
num nodeSize = nodeSizeController.text.interpret();
num fieldLength = fieldLengthController.text.interpret();
num fieldWidth = fieldWidthController.text.interpret();

setState(() {
_grid = NavGrid.blankGrid(
nodeSizeMeters: nodeSize,
fieldSize:
Size(fieldLength.toDouble(), fieldWidth.toDouble()),
);
});
_saveNavGrid();
Navigator.of(context).pop();
}
},
child: const Text('Confirm'),
),
],
);
},
);
}

double _xPixelsToMeters(double pixels) {
return ((pixels - 48) / _NavigationPainter.scale) /
widget.fieldImage.pixelsPerMeter;
Expand Down
10 changes: 10 additions & 0 deletions lib/pathfinding/nav_grid.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ class NavGrid {
required this.grid,
});

NavGrid.blankGrid({
required this.nodeSizeMeters,
required this.fieldSize,
}) : grid = [] {
int rows = (fieldSize.height / nodeSizeMeters).ceil();
int cols = (fieldSize.width / nodeSizeMeters).ceil();

grid = List.generate(rows, (index) => List.filled(cols, false));
}

NavGrid.fromJson(Map<String, dynamic> json)
: fieldSize = _sizeFromJson(json['field_size']),
nodeSizeMeters = json['nodeSizeMeters'] ?? 0.2,
Expand Down
10 changes: 3 additions & 7 deletions lib/widgets/number_text_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ class NumberTextField extends StatelessWidget {
this.onSubmitted,
this.enabled = true,
this.arrowKeyIncrement = 0.01,
TextEditingController? controller,
}) {
_controller = _getController(initialText);
_controller = controller ?? TextEditingController();
_controller.text = initialText;
}

@override
Expand Down Expand Up @@ -114,10 +116,4 @@ class NumberTextField extends StatelessWidget {
}
}
}

TextEditingController _getController(String text) {
return TextEditingController(text: text)
..selection =
TextSelection.fromPosition(TextPosition(offset: text.length));
}
}
Loading

0 comments on commit f598fe1

Please sign in to comment.