mirror of
https://github.com/jonasbark/swiftcontrol.git
synced 2026-02-18 00:17:40 +01:00
107 lines
3.9 KiB
Dart
107 lines
3.9 KiB
Dart
import 'package:flutter_test/flutter_test.dart';
|
|
|
|
void main() {
|
|
group('Gyroscope Steering Calibration Tests', () {
|
|
test('Should compute correct calibration offset from samples', () {
|
|
// Test offset calculation
|
|
final samples = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0];
|
|
final offset = samples.reduce((a, b) => a + b) / samples.length;
|
|
|
|
expect(offset, equals(5.5));
|
|
});
|
|
|
|
test('Should round angles to whole degrees', () {
|
|
// Test rounding behavior
|
|
expect(4.3.round(), equals(4));
|
|
expect(4.6.round(), equals(5));
|
|
expect(4.7.round(), equals(5));
|
|
expect((-4.3).round(), equals(-4));
|
|
expect((-4.6).round(), equals(-5));
|
|
});
|
|
|
|
test('Should apply low-pass filter correctly', () {
|
|
// Test low-pass filter with alpha = 0.9
|
|
const alpha = 0.9;
|
|
double filteredValue = 0.0;
|
|
final newValue = 10.0;
|
|
|
|
filteredValue = alpha * filteredValue + (1 - alpha) * newValue;
|
|
|
|
expect(filteredValue, closeTo(1.0, 0.01)); // 0.9 * 0 + 0.1 * 10
|
|
});
|
|
|
|
test('Should apply complementary filter correctly', () {
|
|
// Test complementary filter with alpha = 0.98
|
|
const alpha = 0.98;
|
|
final gyroValue = 20.0;
|
|
final accelValue = 19.0;
|
|
|
|
final filtered = alpha * gyroValue + (1 - alpha) * accelValue;
|
|
|
|
expect(filtered, closeTo(19.98, 0.01)); // 0.98 * 20 + 0.02 * 19
|
|
});
|
|
});
|
|
|
|
group('Gyroscope Steering PWM Keypress Tests', () {
|
|
test('Should calculate correct keypress levels', () {
|
|
// Test level calculation with STEERING_THRESHOLD = 15 and LEVEL_DEGREE_STEP = 10
|
|
const steeringThreshold = 15.0;
|
|
const levelDegreeStep = 10.0;
|
|
const maxLevels = 5;
|
|
|
|
int calculateLevels(int absAngle) {
|
|
final levels = ((absAngle - steeringThreshold) / levelDegreeStep).floor() + 1;
|
|
return levels.clamp(1, maxLevels);
|
|
}
|
|
|
|
expect(calculateLevels(15), equals(1)); // (15 - 15) / 10 = 0, floor + 1 = 1
|
|
expect(calculateLevels(20), equals(1)); // (20 - 15) / 10 = 0.5, floor + 1 = 1
|
|
expect(calculateLevels(25), equals(2)); // (25 - 15) / 10 = 1.0, floor + 1 = 2
|
|
expect(calculateLevels(35), equals(3)); // (35 - 15) / 10 = 2.0, floor + 1 = 3
|
|
expect(calculateLevels(45), equals(4)); // (45 - 15) / 10 = 3.0, floor + 1 = 4
|
|
expect(calculateLevels(55), equals(5)); // (55 - 15) / 10 = 4.0, floor + 1 = 5
|
|
expect(calculateLevels(100), equals(5)); // (100 - 15) / 10 = 8.5, floor + 1 = 9 but clamped to 5
|
|
});
|
|
|
|
test('Should determine correct steering direction', () {
|
|
// Test direction determination
|
|
expect(25 > 0, isTrue); // Positive = RIGHT
|
|
expect(-25 > 0, isFalse); // Negative = LEFT
|
|
});
|
|
});
|
|
|
|
group('Gyroscope Steering Threshold Tests', () {
|
|
test('Should correctly apply steering threshold', () {
|
|
const steeringThreshold = 15.0;
|
|
|
|
// Test threshold logic
|
|
expect(10.abs() > steeringThreshold, isFalse); // Below threshold
|
|
expect(15.abs() > steeringThreshold, isFalse); // At threshold
|
|
expect(16.abs() > steeringThreshold, isTrue); // Above threshold
|
|
expect((-16).abs() > steeringThreshold, isTrue); // Above threshold (negative)
|
|
});
|
|
});
|
|
|
|
group('Gyroscope Steering Integration Tests', () {
|
|
test('Should integrate gyroscope readings correctly', () {
|
|
// Test gyroscope integration for angle calculation
|
|
const angularVelocity = 10.0; // degrees per second
|
|
const timeInterval = 0.1; // seconds
|
|
|
|
final angleDelta = angularVelocity * timeInterval;
|
|
|
|
expect(angleDelta, equals(1.0)); // 10 * 0.1 = 1 degree
|
|
});
|
|
|
|
test('Should handle time deltas within reasonable bounds', () {
|
|
// Test that time deltas are validated
|
|
const minDt = 0.0;
|
|
const maxDt = 1.0;
|
|
|
|
expect(0.01 > minDt && 0.01 < maxDt, isTrue);
|
|
expect(0.5 > minDt && 0.5 < maxDt, isTrue);
|
|
expect(1.5 > minDt && 1.5 < maxDt, isFalse); // Too large
|
|
});
|
|
});
|
|
}
|