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

Swerve angle offset feature #59

Merged
merged 17 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from 12 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
39 changes: 36 additions & 3 deletions lib/widgets/nt_widgets/multi-topic/yagsl_swerve_drive.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import 'package:dot_cast/dot_cast.dart';
import 'package:provider/provider.dart';
import 'package:vector_math/vector_math_64.dart' show radians;

import 'package:elastic_dashboard/services/text_formatter_builder.dart';
import 'package:elastic_dashboard/widgets/dialog_widgets/dialog_text_input.dart';
import 'package:elastic_dashboard/widgets/dialog_widgets/dialog_toggle_switch.dart';
import 'package:elastic_dashboard/widgets/nt_widgets/nt_widget.dart';

Expand All @@ -23,31 +25,42 @@ class YAGSLSwerveDriveModel extends NTWidgetModel {

bool _showRobotRotation = true;
bool _showDesiredStates = true;
double _angleOffset =
0; // Modifiable angle offset to allow all kinds of swerve libraries setups

get showRobotRotation => _showRobotRotation;
bool get showRobotRotation => _showRobotRotation;

set showRobotRotation(value) {
_showRobotRotation = value;
refresh();
}

get showDesiredStates => _showDesiredStates;
bool get showDesiredStates => _showDesiredStates;

set showDesiredStates(value) {
_showDesiredStates = value;
refresh();
}

double get angleOffset => _angleOffset;

set angleOffset(value) {
_angleOffset = value;
refresh();
}

YAGSLSwerveDriveModel({
required super.ntConnection,
required super.preferences,
required super.topic,
bool showRobotRotation = true,
bool showDesiredStates = true,
double angleOffset = 0.0,
super.dataType,
super.period,
}) : _showDesiredStates = showDesiredStates,
_showRobotRotation = showRobotRotation,
_angleOffset = angleOffset,
super();

YAGSLSwerveDriveModel.fromJson({
Expand All @@ -57,6 +70,7 @@ class YAGSLSwerveDriveModel extends NTWidgetModel {
}) : super.fromJson(jsonData: jsonData) {
_showRobotRotation = tryCast(jsonData['show_robot_rotation']) ?? true;
_showDesiredStates = tryCast(jsonData['show_desired_states']) ?? true;
_angleOffset = tryCast(jsonData['angle_offset']) ?? 0.0;
}

@override
Expand All @@ -65,11 +79,15 @@ class YAGSLSwerveDriveModel extends NTWidgetModel {
...super.toJson(),
'show_robot_rotation': _showRobotRotation,
'show_desired_states': _showDesiredStates,
'angle_offset': _angleOffset,
};
}

@override
List<Widget> getEditProperties(BuildContext context) {
String angleUnit =
tryCast(ntConnection.getLastAnnouncedValue(rotationUnitTopic)) ??
"radians";
DanPeled marked this conversation as resolved.
Show resolved Hide resolved
return [
Row(
children: [
Expand All @@ -93,6 +111,21 @@ class YAGSLSwerveDriveModel extends NTWidgetModel {
),
],
),
const SizedBox(height: 5),
Row(
children: [
Flexible(
child: DialogTextInput(
initialText: _angleOffset.toString(),
label: 'Angle Offset ($angleUnit)',
onSubmit: (String value) async {
angleOffset = double.parse(value);
DanPeled marked this conversation as resolved.
Show resolved Hide resolved
},
formatter: TextFormatterBuilder.decimalTextFormatter(),
),
),
],
),
];
}

Expand Down Expand Up @@ -181,7 +214,7 @@ class YAGSLSwerveDrive extends NTWidget {

double robotAngle = tryCast(model.ntConnection
.getLastAnnouncedValue(model.robotRotationTopic)) ??
0.0;
0.0 + model.angleOffset;

if (rotationUnit == 'degrees') {
robotAngle = radians(robotAngle);
Expand Down
16 changes: 9 additions & 7 deletions test/widgets/nt_widgets/multi-topic/yagsl_swerve_drive_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ void main() {
'period': 0.100,
'show_robot_rotation': true,
'show_desired_states': true,
'angle_offset': 90.0,
};

late SharedPreferences preferences;
Expand Down Expand Up @@ -47,17 +48,18 @@ void main() {

expect(yagslSwerveModel.showRobotRotation, isTrue);
expect(yagslSwerveModel.showDesiredStates, isTrue);
expect(yagslSwerveModel.angleOffset, 90.0);
});

test('YAGSL swerve drive to json', () {
YAGSLSwerveDriveModel yagslSwerveModel = YAGSLSwerveDriveModel(
ntConnection: ntConnection,
preferences: preferences,
topic: 'Test/YAGSL Swerve Drive',
period: 0.100,
showRobotRotation: true,
showDesiredStates: true,
);
ntConnection: ntConnection,
preferences: preferences,
topic: 'Test/YAGSL Swerve Drive',
period: 0.100,
showRobotRotation: true,
showDesiredStates: true,
angleOffset: 90.0);

expect(yagslSwerveModel.toJson(), yagslSwerveJson);
});
Expand Down
Loading