Skip to content

Commit

Permalink
Add option to tie global constraints to default constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
mjansen4857 committed Nov 20, 2023
1 parent d562c8d commit ed4bbd1
Show file tree
Hide file tree
Showing 12 changed files with 151 additions and 4 deletions.
1 change: 1 addition & 0 deletions lib/pages/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@ class _HomePageState extends State<HomePage> with TickerProviderStateMixin {
}

void _onProjectSettingsChanged() {
ProjectPage.settingsUpdated = true;
_saveProjectSettingsToFile(_projectDir!);

setState(() {
Expand Down
17 changes: 17 additions & 0 deletions lib/pages/project/project_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class ProjectPage extends StatefulWidget {
final VoidCallback? onFoldersChanged;
final bool simulatePath;

// Stupid workaround to get when settings are updated
static bool settingsUpdated = false;

const ProjectPage({
super.key,
required this.prefs,
Expand Down Expand Up @@ -170,6 +173,20 @@ class _ProjectPageState extends State<ProjectPage> {
);
}

// Stupid workaround but it works
if (ProjectPage.settingsUpdated) {
PathConstraints defaultConstraints = _getDefaultConstraints();

Check warning on line 178 in lib/pages/project/project_page.dart

View check run for this annotation

Codecov / codecov/patch

lib/pages/project/project_page.dart#L178

Added line #L178 was not covered by tests

for (PathPlannerPath path in _paths) {
if (path.useDefaultConstraints) {
path.globalConstraints = defaultConstraints.clone();
path.generateAndSavePath();

Check warning on line 183 in lib/pages/project/project_page.dart

View check run for this annotation

Codecov / codecov/patch

lib/pages/project/project_page.dart#L180-L183

Added lines #L180 - L183 were not covered by tests
}
}

ProjectPage.settingsUpdated = false;
}

return Stack(
children: [
Container(
Expand Down
8 changes: 7 additions & 1 deletion lib/path/pathplanner_path.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class PathPlannerPath {
bool reversed;
PreviewStartingState? previewStartingState;
String? folder;
bool useDefaultConstraints;

FileSystem fs;
String pathDir;
Expand Down Expand Up @@ -60,7 +61,8 @@ class PathPlannerPath {
rotationTargets = [],
eventMarkers = [],
reversed = false,
previewStartingState = null {
previewStartingState = null,
useDefaultConstraints = false {
waypoints.addAll([
Waypoint(
anchor: const Point(2.0, 7.0),
Expand Down Expand Up @@ -93,6 +95,7 @@ class PathPlannerPath {
required this.reversed,
required this.folder,
required this.previewStartingState,
required this.useDefaultConstraints,
}) : pathPoints = [] {
generatePathPoints();
}
Expand Down Expand Up @@ -127,6 +130,7 @@ class PathPlannerPath {
previewStartingState: json['previewStartingState'] == null
? null
: PreviewStartingState.fromJson(json['previewStartingState']),
useDefaultConstraints: json['useDefaultConstraints'] ?? false,
);

void generateAndSavePath() {
Expand Down Expand Up @@ -214,6 +218,7 @@ class PathPlannerPath {
'reversed': reversed,
'folder': folder,
'previewStartingState': previewStartingState?.toJson(),
'useDefaultConstraints': useDefaultConstraints,
};
}

Expand Down Expand Up @@ -461,6 +466,7 @@ class PathPlannerPath {
reversed: reversed,
folder: folder,
previewStartingState: previewStartingState?.clone(),
useDefaultConstraints: useDefaultConstraints,
);
}

Expand Down
4 changes: 4 additions & 0 deletions lib/services/simulator/trajectory_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ class TrajectoryGenerator {
reversed: path.reversed,
folder: null,
previewStartingState: null,
useDefaultConstraints: path.useDefaultConstraints,
);
} else if ((closestPointIdx == 0 && robotNextControl == null) ||
((closestDist -
Expand Down Expand Up @@ -185,6 +186,7 @@ class TrajectoryGenerator {
reversed: path.reversed,
folder: null,
previewStartingState: null,
useDefaultConstraints: path.useDefaultConstraints,
);
}

Expand Down Expand Up @@ -234,6 +236,7 @@ class TrajectoryGenerator {
reversed: path.reversed,
folder: null,
previewStartingState: null,
useDefaultConstraints: path.useDefaultConstraints,

Check warning on line 239 in lib/services/simulator/trajectory_generator.dart

View check run for this annotation

Codecov / codecov/patch

lib/services/simulator/trajectory_generator.dart#L239

Added line #L239 was not covered by tests
);
}

Expand Down Expand Up @@ -361,6 +364,7 @@ class TrajectoryGenerator {
reversed: path.reversed,
folder: null,
previewStartingState: null,
useDefaultConstraints: path.useDefaultConstraints,
);
}

Expand Down
16 changes: 16 additions & 0 deletions lib/widgets/editor/split_path_editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:flutter/services.dart';
import 'package:multi_split_view/multi_split_view.dart';
import 'package:pathplanner/path/constraints_zone.dart';
import 'package:pathplanner/path/event_marker.dart';
import 'package:pathplanner/path/path_constraints.dart';
import 'package:pathplanner/path/pathplanner_path.dart';
import 'package:pathplanner/path/rotation_target.dart';
import 'package:pathplanner/path/waypoint.dart';
Expand Down Expand Up @@ -467,6 +468,7 @@ class _SplitPathEditorState extends State<SplitPathEditor>
waypointsTreeController: _waypointsTreeController,
undoStack: widget.undoStack,
holonomicMode: _holonomicMode,
defaultConstraints: _getDefaultConstraints(),
onPathChanged: () {
setState(() {
widget.path.generateAndSavePath();
Expand Down Expand Up @@ -686,4 +688,18 @@ class _SplitPathEditorState extends State<SplitPathEditor>
double _pixelsToMeters(double pixels) {
return (pixels / PathPainter.scale) / widget.fieldImage.pixelsPerMeter;
}

PathConstraints _getDefaultConstraints() {
return PathConstraints(
maxVelocity: widget.prefs.getDouble(PrefsKeys.defaultMaxVel) ??
Defaults.defaultMaxVel,
maxAcceleration: widget.prefs.getDouble(PrefsKeys.defaultMaxAccel) ??
Defaults.defaultMaxAccel,
maxAngularVelocity: widget.prefs.getDouble(PrefsKeys.defaultMaxAngVel) ??
Defaults.defaultMaxAngVel,
maxAngularAcceleration:
widget.prefs.getDouble(PrefsKeys.defaultMaxAngAccel) ??
Defaults.defaultMaxAngAccel,
);
}
}
41 changes: 41 additions & 0 deletions lib/widgets/editor/tree_widgets/global_constraints_tree.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:pathplanner/path/path_constraints.dart';
import 'package:pathplanner/path/pathplanner_path.dart';
import 'package:pathplanner/widgets/editor/tree_widgets/tree_card_node.dart';
import 'package:pathplanner/widgets/number_text_field.dart';
Expand All @@ -9,13 +10,15 @@ class GlobalConstraintsTree extends StatelessWidget {
final VoidCallback? onPathChanged;
final ChangeStack undoStack;
final bool holonomicMode;
final PathConstraints defaultConstraints;

const GlobalConstraintsTree({
super.key,
required this.path,
this.onPathChanged,
required this.undoStack,
required this.holonomicMode,
required this.defaultConstraints,
});

@override
Expand All @@ -39,6 +42,7 @@ class GlobalConstraintsTree extends StatelessWidget {
initialText:
path.globalConstraints.maxVelocity.toStringAsFixed(2),
label: 'Max Velocity (M/S)',
enabled: !path.useDefaultConstraints,
onSubmitted: (value) {
if (value != null && value > 0) {
_addChange(
Expand All @@ -53,6 +57,7 @@ class GlobalConstraintsTree extends StatelessWidget {
initialText:
path.globalConstraints.maxAcceleration.toStringAsFixed(2),
label: 'Max Acceleration (M/S²)',
enabled: !path.useDefaultConstraints,
onSubmitted: (value) {
if (value != null && value > 0) {
_addChange(
Expand All @@ -76,6 +81,7 @@ class GlobalConstraintsTree extends StatelessWidget {
.toStringAsFixed(2),
label: 'Max Angular Velocity (Deg/S)',
arrowKeyIncrement: 1.0,
enabled: !path.useDefaultConstraints,
onSubmitted: (value) {
if (value != null && value > 0) {
_addChange(() =>
Expand All @@ -91,6 +97,7 @@ class GlobalConstraintsTree extends StatelessWidget {
.toStringAsFixed(2),
label: 'Max Angular Acceleration (Deg/S²)',
arrowKeyIncrement: 1.0,
enabled: !path.useDefaultConstraints,
onSubmitted: (value) {
if (value != null && value > 0) {
_addChange(() => path
Expand All @@ -102,6 +109,40 @@ class GlobalConstraintsTree extends StatelessWidget {
],
),
),
const SizedBox(height: 12),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 6.0),
child: Row(
children: [
Checkbox(
value: path.useDefaultConstraints,
onChanged: (value) {
undoStack.add(Change(
(
path.useDefaultConstraints,
path.globalConstraints.clone()
),
() {
path.useDefaultConstraints = value ?? false;
path.globalConstraints = defaultConstraints.clone();
onPathChanged?.call();
},
(oldValue) {
path.useDefaultConstraints = oldValue.$1;
path.globalConstraints = oldValue.$2.clone();
onPathChanged?.call();
},
));
},
),
const SizedBox(width: 4),
const Text(
'Use Default Constraints',
style: TextStyle(fontSize: 18),
),
],
),
),
],
);
}
Expand Down
4 changes: 4 additions & 0 deletions lib/widgets/editor/tree_widgets/path_tree.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:pathplanner/path/path_constraints.dart';
import 'package:pathplanner/path/pathplanner_path.dart';
import 'package:pathplanner/widgets/editor/tree_widgets/constraint_zones_tree.dart';
import 'package:pathplanner/widgets/editor/tree_widgets/editor_settings_tree.dart';
Expand Down Expand Up @@ -32,6 +33,7 @@ class PathTree extends StatefulWidget {
final ChangeStack undoStack;
final num? pathRuntime;
final bool holonomicMode;
final PathConstraints defaultConstraints;

const PathTree({
super.key,
Expand All @@ -56,6 +58,7 @@ class PathTree extends StatefulWidget {
this.pathRuntime,
this.onPathChangedNoSim,
required this.holonomicMode,
required this.defaultConstraints,
});

@override
Expand Down Expand Up @@ -109,6 +112,7 @@ class _PathTreeState extends State<PathTree> {
onPathChanged: widget.onPathChanged,
undoStack: widget.undoStack,
holonomicMode: widget.holonomicMode,
defaultConstraints: widget.defaultConstraints,
),
GoalEndStateTree(
path: widget.path,
Expand Down
4 changes: 1 addition & 3 deletions lib/widgets/number_text_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ class NumberTextField extends StatelessWidget {

@override
Widget build(BuildContext context) {
ColorScheme colorScheme = Theme.of(context).colorScheme;

return SizedBox(
height: height,
child: CallbackShortcuts(
Expand All @@ -55,7 +53,7 @@ class NumberTextField extends StatelessWidget {
FilteringTextInputFormatter.allow(
RegExp(r'(^(-?)\d*\.?\d*)([+/\*\-](-?)\d*\.?\d*)*')),
],
style: TextStyle(fontSize: 14, color: colorScheme.onSurface),
style: const TextStyle(fontSize: 14),
decoration: InputDecoration(
contentPadding: const EdgeInsets.fromLTRB(8, 4, 8, 4),
labelText: label,
Expand Down
8 changes: 8 additions & 0 deletions test/path/pathplanner_path_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ void main() {
reversed: false,
folder: null,
previewStartingState: null,
useDefaultConstraints: false,
);

expect(path.name, 'test');
Expand Down Expand Up @@ -94,6 +95,7 @@ void main() {
reversed: false,
folder: null,
previewStartingState: PreviewStartingState(rotation: 10, velocity: 1),
useDefaultConstraints: false,
);

Map<String, dynamic> json = path.toJson();
Expand Down Expand Up @@ -128,6 +130,7 @@ void main() {
reversed: false,
folder: null,
previewStartingState: null,
useDefaultConstraints: false,
);
PathPlannerPath cloned = path.duplicate('test');

Expand Down Expand Up @@ -163,6 +166,7 @@ void main() {
reversed: false,
folder: null,
previewStartingState: null,
useDefaultConstraints: false,
);
PathPlannerPath path2 = PathPlannerPath(
name: 'test',
Expand All @@ -186,6 +190,7 @@ void main() {
reversed: false,
folder: null,
previewStartingState: null,
useDefaultConstraints: false,
);
PathPlannerPath path3 = PathPlannerPath(
name: 'test2',
Expand All @@ -209,6 +214,7 @@ void main() {
reversed: false,
folder: null,
previewStartingState: null,
useDefaultConstraints: false,
);

expect(path2, path1);
Expand Down Expand Up @@ -244,6 +250,7 @@ void main() {
reversed: false,
folder: null,
previewStartingState: null,
useDefaultConstraints: false,
);

path.addWaypoint(const Point(6.0, 1.0));
Expand Down Expand Up @@ -293,6 +300,7 @@ void main() {
reversed: false,
folder: null,
previewStartingState: null,
useDefaultConstraints: false,
);

path.insertWaypointAfter(1);
Expand Down
2 changes: 2 additions & 0 deletions test/services/simulator/trajectory_generator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ void main() {
reversed: false,
folder: null,
previewStartingState: null,
useDefaultConstraints: false,
);

Trajectory sim = Trajectory.simulate(test, 0, 0);
Expand Down Expand Up @@ -81,6 +82,7 @@ void main() {
reversed: false,
folder: null,
previewStartingState: null,
useDefaultConstraints: false,
);

// Basic coverage tests, expand in future
Expand Down
Loading

0 comments on commit ed4bbd1

Please sign in to comment.