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

Modified Critical Metric from String to MetricLine #139

Merged
merged 11 commits into from
Apr 18, 2024
1 change: 0 additions & 1 deletion lib/data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export "src/data/metrics/arm.dart";
export "src/data/metrics/drive.dart";
export "src/data/metrics/gripper.dart";
export "src/data/metrics/position.dart";
export "src/data/metrics/mars.dart";
export "src/data/metrics/metrics.dart";
export "src/data/metrics/science.dart";

Expand Down
1 change: 0 additions & 1 deletion lib/models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ export "src/models/rover/rover.dart";
// View models
export "src/models/view/map.dart";
export "src/models/view/logs.dart";
export "src/models/view/mars.dart";
export "src/models/view/science.dart";
export "src/models/view/timer.dart";

Expand Down
31 changes: 15 additions & 16 deletions lib/src/data/metrics/arm.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,23 @@ class ArmMetrics extends Metrics<ArmData> {
String get name => "Arm Base";

/// Returns a description of a [MotorData].
List<String> getMotorData(MotorData motor) => [
" Is moving? ${motor.isMoving}",
" Limit? ${motor.isLimitSwitchPressed}",
" Direction: ${motor.direction.humanName}",
" Steps: ${motor.currentStep} --> ${motor.targetStep}",
" Angle: ${motor.angle}",
List<MetricLine> getMotorData(MotorData motor, String part) => [
MetricLine("$part: Is moving? ${motor.isMoving}", severity: motor.isMoving ? Severity.info : null),
MetricLine("$part: Limit? ${motor.isLimitSwitchPressed}", severity: motor.isLimitSwitchPressed ? Severity.warning : null),
MetricLine("$part: Direction: ${motor.direction.humanName}"),
MetricLine("$part: Steps: ${motor.currentStep} --> ${motor.targetStep}"),
MetricLine("$part: Angle: ${motor.angle}"),
];

@override
List<String> get allMetrics => [
"IK: ",
" Current: ${data.currentPosition.prettyPrint}",
" Target: ${data.targetPosition.prettyPrint}",
"------------------------------",
"Swivel: ", ...getMotorData(data.base),
"------------------------------",
"Shoulder: ", ...getMotorData(data.shoulder),
"------------------------------",
"Elbow: ", ...getMotorData(data.elbow),
List<MetricLine> get allMetrics => [
MetricLine("IK: "),
MetricLine(" Current: ${data.currentPosition.prettyPrint}"),
MetricLine(" Target: ${data.targetPosition.prettyPrint}"),
...getMotorData(data.base, "Swivel"),
MetricLine("------------------------------",),
...getMotorData(data.shoulder, "Shoulder"),
MetricLine("------------------------------",),
...getMotorData(data.elbow, "Elbow"),
];
}
35 changes: 30 additions & 5 deletions lib/src/data/metrics/drive.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,37 @@ class DriveMetrics extends Metrics<DriveData> {
@override
String get name => "Drive";

/// The severity based on the throttle speed.
Severity? get throttleSeverity {
if (data.throttle == 0) {
return null;
} else if (data.throttle <= 0.3) {
return Severity.info;
} else if (data.throttle <= 0.75) {
return Severity.warning;
} else {
return Severity.critical;
}
}

/// The severity for the electrical metrics.
Severity? get electricalSeverity {
if (data.batteryVoltage == 0) return null;
if (data.batteryVoltage <= 25) {
return Severity.critical;
} else if (data.batteryVoltage <= 26) {
return Severity.warning;
} else {
return null;
}
}

@override
List<String> get allMetrics => [
"Throttle: ${data.throttle.toStringAsFixed(2)}",
"Left: ${data.left.toStringAsFixed(2)}",
"Right: ${data.right.toStringAsFixed(2)}",
"Battery: ${data.batteryVoltage.toStringAsFixed(2)}V, ${data.batteryCurrent.toStringAsFixed(2)}A, ${data.batteryTemperature.toStringAsFixed(2)}°F",
List<MetricLine> get allMetrics => [
MetricLine("Throttle: ${data.throttle.toStringAsFixed(2)}", severity: throttleSeverity),
MetricLine("Left: ${data.left.toStringAsFixed(2)}"),
MetricLine("Right: ${data.right.toStringAsFixed(2)}"),
MetricLine("Battery: ${data.batteryVoltage.toStringAsFixed(2)}V,${data.batteryCurrent.toStringAsFixed(2)}A, ${data.batteryTemperature.toStringAsFixed(2)}°F", severity: electricalSeverity),
];

@override
Expand Down
24 changes: 12 additions & 12 deletions lib/src/data/metrics/gripper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ class GripperMetrics extends Metrics<GripperData> {
String get name => "Gripper";

/// Returns a human-readable description of a [MotorData].
List<String> getMotorData(MotorData motor) => [
" Is moving? ${motor.isMoving}",
" Limit? ${motor.isLimitSwitchPressed}",
" Direction: ${motor.direction.humanName}",
" Steps: ${motor.currentStep} --> ${motor.targetStep}",
" Angle: ${motor.angle}",
List<MetricLine> getMotorData(MotorData motor, String functionality) => [
MetricLine("$functionality Is moving? ${motor.isMoving}", severity: motor.isMoving ? Severity.info : null),
MetricLine("$functionality Limit? ${motor.isLimitSwitchPressed}", severity: motor.isLimitSwitchPressed ? Severity.warning : null),
MetricLine("$functionality Direction: ${motor.direction.humanName}"),
MetricLine("$functionality Steps: ${motor.currentStep} --> ${motor.targetStep}"),
MetricLine("$functionality Angle: ${motor.angle}"),
];

@override
List<String> get allMetrics => [
"Lift: ", ...getMotorData(data.lift),
"------------------------------",
"Rotate: ", ...getMotorData(data.rotate),
"------------------------------",
"Pinch: ", ...getMotorData(data.pinch),
List<MetricLine> get allMetrics => [
...getMotorData(data.lift, "Lift",),
MetricLine("------------------------------",),
...getMotorData(data.rotate, "Rotate"),
MetricLine("------------------------------",),
...getMotorData(data.pinch, "Pinch"),
];
}
38 changes: 0 additions & 38 deletions lib/src/data/metrics/mars.dart

This file was deleted.

24 changes: 23 additions & 1 deletion lib/src/data/metrics/metrics.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
// import "package:math";

import "dart:math";

import "package:flutter/foundation.dart";

import "package:rover_dashboard/data.dart";
import "package:rover_dashboard/services.dart";

/// Class to construct a Metric
class MetricLine {
/// Severity of the Metric
final Severity? severity;
/// Message for the Metric
final String text;
/// Constructor for the MetricLine class
MetricLine(this.text, {this.severity});
}

/// A readout of metrics reported by one of the rover's subsystems.
///
/// To use this class, create a subclass that extends this class with [T] as the generated
Expand All @@ -24,7 +38,15 @@ abstract class Metrics<T extends Message> with ChangeNotifier {
///
/// Be sure to store the actual values as fields. This property should be a list of one
/// user-friendly explanation per metric.
List<String> get allMetrics;
List<MetricLine> get allMetrics;

/// Fetch the overall Security
Severity? get overallSeverity {
final indexes = [for (final metric in allMetrics) metric.severity?.index ?? -1];
final index = indexes.reduce(max);
if (index == -1) return null;
return Severity.values[index];
}

/// Updates [data] with new data.
void update(T value) {
Expand Down
34 changes: 24 additions & 10 deletions lib/src/data/metrics/position.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,31 @@ class PositionMetrics extends Metrics<RoverPosition> {
notifyListeners();
}

/// Gets the severity of the rover's orientation for both pitch and roll.
Severity? getRotationSeverity(double orientation) {
final abs = orientation.abs();
if (abs >= 30) {
return Severity.critical;
} else if (abs >= 15) {
return Severity.warning;
} else if (abs >= 10) {
return Severity.info;
} else {
return null;
}
}

@override
List<String> get allMetrics => [
"GPS: ",
" Latitude: ${data.gps.latitude.toStringAsFixed(6)}°",
" Longitude: ${data.gps.longitude.toStringAsFixed(6)}°",
" Altitude: ${data.gps.altitude.toStringAsFixed(2)} m",
"Orientation:",
" X: ${data.orientation.x.toStringAsFixed(2)}°",
" Y: ${data.orientation.y.toStringAsFixed(2)}°",
" Z: ${data.orientation.z.toStringAsFixed(2)}°",
"Distance: ${data.gps.distanceTo(baseStation).toStringAsFixed(2)} m",
List<MetricLine> get allMetrics => [
MetricLine("GPS: "),
MetricLine(" Latitude: ${data.gps.latitude.toStringAsFixed(6)}°",),
MetricLine(" Longitude: ${data.gps.longitude.toStringAsFixed(6)}°",),
MetricLine(" Altitude: ${data.gps.altitude.toStringAsFixed(2)} m"),
MetricLine("Orientation:",),
MetricLine(" X: ${data.orientation.x.toStringAsFixed(2)}°", severity: getRotationSeverity(data.orientation.x)),
MetricLine(" Y: ${data.orientation.y.toStringAsFixed(2)}°", severity: getRotationSeverity(data.orientation.y)),
MetricLine(" Z: ${data.orientation.z.toStringAsFixed(2)}°"),
MetricLine("Distance: ${data.gps.distanceTo(baseStation).toStringAsFixed(2)} m",),
];

@override
Expand Down
12 changes: 6 additions & 6 deletions lib/src/data/metrics/science.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ class ScienceMetrics extends Metrics<ScienceData> {
String get name => "Science";

@override
List<String> get allMetrics => [
"Methane: ${data.methane.toStringAsFixed(3)}",
"CO2: ${data.co2.toStringAsFixed(3)}",
"Temperature: ${data.temperature.toStringAsFixed(3)}",
"Humidity: ${data.humidity.toStringAsFixed(3)}",
"pH: ${data.pH.toStringAsFixed(3)}",
List<MetricLine> get allMetrics => [
MetricLine("Methane: ${data.methane.toStringAsFixed(3)}"),
MetricLine("CO2: ${data.co2.toStringAsFixed(3)}"),
MetricLine("Temperature: ${data.temperature.toStringAsFixed(3)}"),
MetricLine("Humidity: ${data.humidity.toStringAsFixed(3)}"),
MetricLine("pH: ${data.pH.toStringAsFixed(3)}"),
];

@override
Expand Down
10 changes: 1 addition & 9 deletions lib/src/models/rover/metrics.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ class RoverMetrics extends Model {
/// Data from the drive subsystem.
final drive = DriveMetrics();

/// Data from the MARS subsystem.
final mars = MarsMetrics();

/// Data from the HREI subsystem about the arm base.
final arm = ArmMetrics();

Expand All @@ -26,7 +23,7 @@ class RoverMetrics extends Model {
///
/// NOTE: Keep this as a getter, NOT a field. If this is made a field, then it won't update
/// when new data is received. As a getter, every time it is called it will use new data.
List<Metrics> get allMetrics => [position, mars, drive, science, arm, gripper];
List<Metrics> get allMetrics => [position, drive, science, arm, gripper];

@override
Future<void> init() async {
Expand All @@ -45,11 +42,6 @@ class RoverMetrics extends Model {
decoder: RoverPosition.fromBuffer,
handler: position.update,
);
models.messages.registerHandler<MarsData>(
name: MarsData().messageName,
decoder: MarsData.fromBuffer,
handler: mars.update,
);
models.messages.registerHandler<ArmData>(
name: ArmData().messageName,
decoder: ArmData.fromBuffer,
Expand Down
50 changes: 0 additions & 50 deletions lib/src/models/view/mars.dart

This file was deleted.

22 changes: 20 additions & 2 deletions lib/src/widgets/navigation/sidebar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,33 @@ class MetricsList extends ReusableReactiveWidget<Metrics> {
childrenPadding: const EdgeInsets.symmetric(horizontal: 16),
title: Text(
model.name,
style: Theme.of(context).textTheme.headlineSmall,
style: Theme.of(context).textTheme.headlineSmall?.copyWith(
color: model.overallSeverity?.color,
),
),
children: [
for (final String metric in model.allMetrics) Text(metric),
for (final MetricLine metric in model.allMetrics) Text(
metric.text,
style: TextStyle(
color: metric.severity?.color,
),
),
const SizedBox(height: 4),
],
);
}

/// Extension for COlors on Severity
extension SeverityUtil on Severity {
/// Fetch the color based on the severity
Color? get color => switch (this) {
Severity.info => Colors.blueGrey,
Severity.warning => Colors.orange,
Severity.error => Colors.red,
Severity.critical => Colors.red.shade900,
};
}

/// Displays controls for the given [Controller].
class ControlsDisplay extends ReusableReactiveWidget<Controller> {
/// The number gamepad being used.
Expand Down
Loading