Skip to content

Commit

Permalink
Added 3-Axis Accelerometer
Browse files Browse the repository at this point in the history
  • Loading branch information
Gold872 committed Nov 26, 2023
1 parent 628f18e commit 5096125
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import 'package:elastic_dashboard/widgets/nt4_widgets/multi-topic/robot_preferen
import 'package:elastic_dashboard/widgets/nt4_widgets/multi-topic/split_button_chooser.dart';
import 'package:elastic_dashboard/widgets/nt4_widgets/multi-topic/subsystem_widget.dart';
import 'package:elastic_dashboard/widgets/nt4_widgets/multi-topic/swerve_drive.dart';
import 'package:elastic_dashboard/widgets/nt4_widgets/multi-topic/three_axis_accelerometer.dart';
import 'package:elastic_dashboard/widgets/nt4_widgets/single_topic/match_time.dart';
import 'package:elastic_dashboard/widgets/nt4_widgets/single_topic/multi_color_view.dart';
import 'package:elastic_dashboard/widgets/nt4_widgets/single_topic/number_bar.dart';
Expand Down Expand Up @@ -254,6 +255,12 @@ class DraggableNT4WidgetContainer extends DraggableWidgetContainer {
key: UniqueKey(),
jsonData: widgetProperties,
);
case '3-Axis Accelerometer':
case '3AxisAccelerometer':
return ThreeAxisAccelerometer.fromJson(
key: UniqueKey(),
jsonData: widgetProperties,
);
case 'Field':
return FieldWidget.fromJson(
key: UniqueKey(),
Expand Down
3 changes: 3 additions & 0 deletions lib/widgets/network_tree/tree_row.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import 'package:elastic_dashboard/widgets/nt4_widgets/multi-topic/combo_box_choo
import 'package:elastic_dashboard/widgets/nt4_widgets/multi-topic/robot_preferences.dart';
import 'package:elastic_dashboard/widgets/nt4_widgets/multi-topic/subsystem_widget.dart';
import 'package:elastic_dashboard/widgets/nt4_widgets/multi-topic/swerve_drive.dart';
import 'package:elastic_dashboard/widgets/nt4_widgets/multi-topic/three_axis_accelerometer.dart';
import 'package:elastic_dashboard/widgets/nt4_widgets/single_topic/boolean_box.dart';
import 'package:elastic_dashboard/widgets/nt4_widgets/nt4_widget.dart';
import 'package:elastic_dashboard/widgets/nt4_widgets/single_topic/text_display.dart';
Expand Down Expand Up @@ -158,6 +159,8 @@ class TreeRow {
switch (type) {
case 'Gyro':
return Gyro(key: UniqueKey(), topic: topic);
case '3AxisAccelerometer':
return ThreeAxisAccelerometer(key: UniqueKey(), topic: topic);
case 'Field2d':
return FieldWidget(key: UniqueKey(), topic: topic);
case 'PowerDistribution':
Expand Down
169 changes: 169 additions & 0 deletions lib/widgets/nt4_widgets/multi-topic/three_axis_accelerometer.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
import 'package:dot_cast/dot_cast.dart';
import 'package:elastic_dashboard/services/globals.dart';
import 'package:elastic_dashboard/services/nt4_connection.dart';
import 'package:elastic_dashboard/widgets/nt4_widgets/nt4_widget.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class ThreeAxisAccelerometer extends StatelessWidget with NT4Widget {
@override
String type = '3-Axis Accelerometer';

late String xTopic;
late String yTopic;
late String zTopic;

ThreeAxisAccelerometer(
{super.key, required topic, period = Globals.defaultPeriod}) {
super.topic = topic;
super.period = period;

init();
}

ThreeAxisAccelerometer.fromJson(
{super.key, required Map<String, dynamic> jsonData}) {
topic = tryCast(jsonData['topic']) ?? '';
period = tryCast(jsonData['period']) ?? Globals.defaultPeriod;

init();
}

@override
void init() {
super.init();

xTopic = '$topic/X';
yTopic = '$topic/Y';
zTopic = '$topic/Z';
}

@override
void resetSubscription() {
super.resetSubscription();

xTopic = '$topic/X';
yTopic = '$topic/Y';
zTopic = '$topic/Z';
}

@override
Widget build(BuildContext context) {
notifier = context.watch<NT4WidgetNotifier?>();

return StreamBuilder(
stream: subscription?.periodicStream(),
builder: (context, snapshot) {
double xAccel =
tryCast(nt4Connection.getLastAnnouncedValue(xTopic)) ?? 0.0;
double yAccel =
tryCast(nt4Connection.getLastAnnouncedValue(yTopic)) ?? 0.0;
double zAccel =
tryCast(nt4Connection.getLastAnnouncedValue(zTopic)) ?? 0.0;

return Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
mainAxisSize: MainAxisSize.max,
children: [
// X Acceleration
Flexible(
flex: 16,
child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text('X'),
const SizedBox(width: 10),
Expanded(
child: Container(
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.surface,
borderRadius: BorderRadius.circular(5.0),
),
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: Text(
'${xAccel.toStringAsFixed(2)} g',
style: TextStyle(
color: Theme.of(context).colorScheme.onSurface,
overflow: TextOverflow.ellipsis,
fontSize: 12.0,
),
textAlign: TextAlign.center,
),
),
),
],
),
),
const Flexible(
child: SizedBox(height: 4.0),
),
// Y Acceleration
Flexible(
flex: 16,
child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text('Y'),
const SizedBox(width: 10),
Expanded(
child: Container(
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.surface,
borderRadius: BorderRadius.circular(5.0),
),
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: Text(
'${yAccel.toStringAsFixed(2)} g',
style: TextStyle(
color: Theme.of(context).colorScheme.onSurface,
overflow: TextOverflow.ellipsis,
fontSize: 12.0,
),
textAlign: TextAlign.center,
),
),
),
],
),
),
const Flexible(
child: SizedBox(height: 4.0),
),
// Z Acceleration
Flexible(
flex: 16,
child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text('Z'),
const SizedBox(width: 10),
Expanded(
child: Container(
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.surface,
borderRadius: BorderRadius.circular(5.0),
),
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: Text(
'${zAccel.toStringAsFixed(2)} g',
style: TextStyle(
color: Theme.of(context).colorScheme.onSurface,
overflow: TextOverflow.ellipsis,
fontSize: 12.0,
),
textAlign: TextAlign.center,
),
),
),
],
),
),
],
);
},
);
}
}

0 comments on commit 5096125

Please sign in to comment.