Skip to content

Commit

Permalink
added rpm graphs
Browse files Browse the repository at this point in the history
  • Loading branch information
aidanahram committed Apr 2, 2024
1 parent 7ecdcb7 commit be21bcd
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 21 deletions.
43 changes: 37 additions & 6 deletions lib/src/models/view/electrical.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import "package:rover_dashboard/models.dart";
class ElectricalModel with ChangeNotifier {
/// The [Metrics] model for electrical data.
ElectricalMetrics get metrics => models.rover.metrics.electrical;

/// The [Metrics] model for drive data used for graphs.
DriveMetrics get driveMetrics => models.rover.metrics.drive;

/// The timestamp of the first or earliest reading. All other timestamps are based on this.
Timestamp? firstTimestamp;
Expand All @@ -18,6 +21,12 @@ class ElectricalModel with ChangeNotifier {
/// Current readings over time.
final DoubleLinkedQueue<SensorReading> currentReadings = DoubleLinkedQueue<SensorReading>();

/// Left speed readings over time.
final DoubleLinkedQueue<SensorReading> leftSpeeds = DoubleLinkedQueue<SensorReading>();

/// Right speed readings over time.
final DoubleLinkedQueue<SensorReading> rightSpeeds = DoubleLinkedQueue<SensorReading>();

/// Whether to listen for new data from the rover. This is false after loading a file.
bool isListening = true;

Expand All @@ -27,18 +36,21 @@ class ElectricalModel with ChangeNotifier {
/// Listens to all the [ScienceTestBuilder]s in the UI.
ElectricalModel() {
metrics.addListener(updateData);
driveMetrics.addListener(updateData);
}

@override
void dispose() {
metrics.removeListener(updateData);
driveMetrics.removeListener(updateData);
super.dispose();
}

/// Updates the graph using [addMessage] with the latest data from [metrics].
void updateData() {
if (!isListening) return;
addMessage(metrics.data.wrap());
addMessage(driveMetrics.data.wrap());
notifyListeners();
}

Expand All @@ -50,24 +62,43 @@ class ElectricalModel with ChangeNotifier {

/// Adds a [WrappedMessage] containing a [ElectricalData] to the UI.
void addMessage(WrappedMessage wrapper) {
if (wrapper.name != ElectricalData().messageName) throw ArgumentError("Incorrect log type: ${wrapper.name}");
if (!wrapper.hasTimestamp()) { throw ArgumentError("Data is missing a timestamp"); }
firstTimestamp ??= wrapper.timestamp;
final data = wrapper.decode(ElectricalData.fromBuffer);
final time = wrapper.timestamp - firstTimestamp!;
if(data.hasBatteryCurrent()) currentReadings.pushWithLimit(SensorReading(time: time, value: data.batteryCurrent), 30);
if(data.hasBatteryVoltage()) voltageReadings.pushWithLimit(SensorReading(time: time, value: data.batteryVoltage), 30);
}
switch (wrapper.name){
case "ElectricalData":
final data = wrapper.decode(ElectricalData.fromBuffer);
final time = wrapper.timestamp - firstTimestamp!;
if(data.hasBatteryCurrent()) currentReadings.pushWithLimit(SensorReading(time: time, value: data.batteryCurrent), 30);
if(data.hasBatteryVoltage()) voltageReadings.pushWithLimit(SensorReading(time: time, value: data.batteryVoltage), 30);
case "DriveData":
print("got drive data");
final data = wrapper.decode(DriveData.fromBuffer);
final time = wrapper.timestamp - firstTimestamp!;
/*print("has right ${data.hasRight()}");
print("has left ${data.hasLeft()}");
print("has throttle ${data.hasThrottle()}");
//if(!data.hasThrottle()) break;
*/
print(data.left);
if(data.hasLeft()) leftSpeeds.pushWithLimit(SensorReading(time: time, value: data.throttle * data.left), 30);
if(data.hasRight()) rightSpeeds.pushWithLimit(SensorReading(time: time, value: data.throttle * data.right), 30);
default:
throw ArgumentError("Incorrect log type: ${wrapper.name}");
}
}

/// Clears all the readings from all the samples.
void clear() {
isListening = true;
voltageReadings.clear();
currentReadings.clear();
leftSpeeds.clear();
rightSpeeds.clear();
firstTimestamp = null;
notifyListeners();
}

/// Changes the axis that the UI displays the graphsy
void changeDirection(){
axis = !axis;
notifyListeners();
Expand Down
48 changes: 33 additions & 15 deletions lib/src/pages/electrical.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import "package:rover_dashboard/pages.dart";
import "package:rover_dashboard/widgets.dart";

class _LineChart extends StatelessWidget {
final Iterable<SensorReading> readings;
final List<Iterable<SensorReading>> readings;
final List<Color> colors;
final String bottomUnitName;
final String sideUnitName;
final String title;
Expand All @@ -18,6 +19,7 @@ class _LineChart extends StatelessWidget {

const _LineChart({
required this.readings,
required this.colors,
required this.bottomUnitName,
required this.sideUnitName,
required this.title,
Expand All @@ -31,13 +33,14 @@ class _LineChart extends StatelessWidget {
Widget build(BuildContext context) => LineChart(
LineChartData(
lineBarsData: [
LineChartBarData(
color: Colors.blue,
spots: [
for (final reading in readings)
if(reading.time > 0) FlSpot(reading.time, reading.value),
],
),
for(int i = 0; i < readings.length; i++)
LineChartBarData(
color: colors[i],
spots: [
for (final reading in readings[i])
if(reading.time > 0) FlSpot(reading.time, reading.value),
],
),
],
titlesData: FlTitlesData(
topTitles: AxisTitles(
Expand All @@ -54,8 +57,11 @@ class _LineChart extends StatelessWidget {
),
),
leftTitles: AxisTitles(
axisNameWidget: Text(sideUnitName),
sideTitles: const SideTitles(reservedSize: 25),
axisNameWidget: Text(
sideUnitName,
style: const TextStyle(color: Colors.blue, fontWeight: FontWeight.bold),
),
sideTitles: const SideTitles(showTitles: true, reservedSize: 25),
),
bottomTitles: AxisTitles(
axisNameWidget: Text(bottomUnitName),
Expand All @@ -76,10 +82,12 @@ class _LineChart extends StatelessWidget {
touchTooltipData: LineTouchTooltipData(
fitInsideVertically: true,
fitInsideHorizontally: true,
getTooltipItems:(touchedSpots) => [LineTooltipItem("${touchedSpots.first.y.toStringAsFixed(2)} $sideUnitName", const TextStyle(color: Colors.white))],
getTooltipItems:(touchedSpots) => [
for(final spot in touchedSpots)
LineTooltipItem("${spot.y.toStringAsFixed(2)} $sideUnitName", const TextStyle(color: Colors.white))
],
),
),

),
duration: const Duration(milliseconds: 10),
);
Expand All @@ -94,17 +102,26 @@ class ElectricalPage extends ReactiveWidget<ElectricalModel> {
Widget build(BuildContext context, ElectricalModel model) {
final graphs = <Widget>[
_LineChart(
readings: model.voltageReadings,
readings: [model.voltageReadings],
colors: const [Colors.blue],
bottomUnitName: "Time",
sideUnitName: "V",
title: "Voltage Graph",
),
_LineChart(
readings: model.currentReadings,
readings: [model.currentReadings],
colors: const [Colors.blue],
bottomUnitName: "Time",
sideUnitName: "A",
title: "Curent Graph",
title: "Current Graph",
),
_LineChart(
readings: [model.rightSpeeds, model.leftSpeeds],
colors: const [Colors.red, Colors.black],
bottomUnitName: "Time",
sideUnitName: "RPM",
title: "Speeds",
)
];


Expand Down Expand Up @@ -136,6 +153,7 @@ class ElectricalPage extends ReactiveWidget<ElectricalModel> {
else
for(final graph in graphs)
Expanded(child: graph,),
const SizedBox(height: 8),
],
);
}
Expand Down

0 comments on commit be21bcd

Please sign in to comment.