Skip to content

Commit

Permalink
electrical graphs only update every second now
Browse files Browse the repository at this point in the history
  • Loading branch information
aidanahram committed Apr 7, 2024
1 parent be21bcd commit f995911
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
31 changes: 19 additions & 12 deletions lib/src/models/view/electrical.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import "package:flutter/material.dart";
import "dart:collection";
import "dart:async";

import "package:rover_dashboard/data.dart";
import "package:rover_dashboard/models.dart";
Expand All @@ -11,6 +12,9 @@ class ElectricalModel with ChangeNotifier {

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

/// [WrappedMessage] of incoming drive data
WrappedMessage driveData = WrappedMessage();

/// The timestamp of the first or earliest reading. All other timestamps are based on this.
Timestamp? firstTimestamp;
Expand All @@ -37,6 +41,7 @@ class ElectricalModel with ChangeNotifier {
ElectricalModel() {
metrics.addListener(updateData);
driveMetrics.addListener(updateData);
Timer.periodic(const Duration(seconds: 1), updateDrive);
}

@override
Expand All @@ -54,13 +59,25 @@ class ElectricalModel with ChangeNotifier {
notifyListeners();
}

/// Function called periodically to updata [driveData]
void updateDrive(Timer time) {
if(driveData.hasTimestamp()){
final data = driveData.decode(DriveData.fromBuffer);
final time = driveData.timestamp - firstTimestamp!;
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);
notifyListeners();
driveData = WrappedMessage();
}
}

/// Whether the page is currently loading.
bool isLoading = false;

/// The error, if any, that occurred while loading the data.
String? errorText;

/// Adds a [WrappedMessage] containing a [ElectricalData] to the UI.
/// Adds a [WrappedMessage] containing a [ElectricalData] and or [DriveData] to the UI.
void addMessage(WrappedMessage wrapper) {
if (!wrapper.hasTimestamp()) { throw ArgumentError("Data is missing a timestamp"); }
firstTimestamp ??= wrapper.timestamp;
Expand All @@ -71,17 +88,7 @@ class ElectricalModel with ChangeNotifier {
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);
driveData.mergeFromMessage(wrapper);
default:
throw ArgumentError("Incorrect log type: ${wrapper.name}");
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/pages/electrical.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class _LineChart extends StatelessWidget {
sideUnitName,
style: const TextStyle(color: Colors.blue, fontWeight: FontWeight.bold),
),
sideTitles: const SideTitles(showTitles: true, reservedSize: 25),
sideTitles: const SideTitles(showTitles: true, reservedSize: 35),
),
bottomTitles: AxisTitles(
axisNameWidget: Text(bottomUnitName),
Expand Down

0 comments on commit f995911

Please sign in to comment.