blob: 0a185b132eba7215a906dd0a02366538291d35ca [file] [log] [blame]
Stephan Massalta769ca22019-01-09 05:29:13 +00001#include "gtest/gtest.h"
Stephan Massalta769ca22019-01-09 05:29:13 +00002
Philipp Schrader790cb542023-07-05 21:06:52 -07003#include "frc971/zeroing/wrap.h"
4
Stephan Pleinesf63bde82024-01-13 15:59:33 -08005namespace frc971::zeroing::testing {
Stephan Massalta769ca22019-01-09 05:29:13 +00006
7TEST(SensorTest, UnwrapOnce) {
8 // Test the sensor moving over the maximum range value and wrapping once
9 // then move sensor in oppsite direction to unwrap and test result.
10
11 // Initialize with the offset and range
12 UnwrapSensor sensor(-1.5, 6); // min = -1.5 & max = 4.5 & move > 3
13 EXPECT_EQ(sensor.Unwrap(1.5), 1.5); // move n/a
14 EXPECT_EQ(sensor.sensor_wrapped(), 0);
15 EXPECT_EQ(sensor.Unwrap(4.0), 4.0); // move 2.5
16 EXPECT_EQ(sensor.Unwrap(-1.0), 5.0); // move -5.0 -> wrap+
17 EXPECT_EQ(sensor.sensor_wrapped(), 1);
18 EXPECT_EQ(sensor.Unwrap(-1.5), 4.5); // move -0.5
19 EXPECT_EQ(sensor.Unwrap(4.0), 4.0); // move 5.5 -> wrap-
20 EXPECT_EQ(sensor.sensor_wrapped(), 0);
21 sensor.Reset();
22}
23
24TEST(SensorTest, UnwrapTwice) {
25 // Test the sensor wrapping twice over the lower value of the range.
26
27 // Initialize with the offset and range
28 UnwrapSensor sensor(-1.5, 6);
29 EXPECT_EQ(sensor.Unwrap(1.0), 1.0);
30 EXPECT_EQ(sensor.Unwrap(-1.0), -1.0);
31 EXPECT_EQ(sensor.Unwrap(4.0), -2.0);
32 EXPECT_EQ(sensor.sensor_wrapped(), -1);
33 EXPECT_EQ(sensor.Unwrap(2.0), -4.0);
34 EXPECT_EQ(sensor.Unwrap(-1.0), -7.0);
35 EXPECT_EQ(sensor.Unwrap(4.0), -8.0);
36 EXPECT_EQ(sensor.sensor_wrapped(), -2);
37}
38
39TEST(SensorTest, UnwrapOutRange) {
40 // Test if values out side range are handled proporly.
41 // Not wrapped scenario only.
42
43 UnwrapSensor sensor(-1.5, 6);
44 EXPECT_EQ(sensor.Unwrap(-3.0), -3.0); // Passed by the init stage
45 EXPECT_EQ(sensor.Unwrap(-3.0), -3.0); // Caught by the exeption handler
46 EXPECT_EQ(sensor.Unwrap(6.5), 6.5);
47}
48
49TEST(SensorTest, UnwrapInit) {
50 // Test the case where the start value and offset will be far enough apart to
51 // trigger a wrap. By ignoring the fisrt value for evaluation and set that for
52 // the next evaluation, this should not trigger the wrap.
53
54 UnwrapSensor sensor(-0.6, 1.0); // min = -0.6 & max = 0.4 & move > 0.5
55 EXPECT_EQ(sensor.Unwrap(0.0), 0.0); // move = n/a
56 EXPECT_EQ(sensor.sensor_wrapped(), 0);
57 EXPECT_EQ(sensor.Unwrap(0.0), 0.0);
58 sensor.Reset();
59 EXPECT_EQ(sensor.Unwrap(0.4), 0.4); // move = n/a
60 EXPECT_EQ(sensor.Unwrap(-0.4), 0.6); // move = -0.8, wrap 1
61 EXPECT_EQ(sensor.Unwrap(0.2), 0.2); // move = 1.0, wrap -1
62}
63
Stephan Pleinesf63bde82024-01-13 15:59:33 -080064} // namespace frc971::zeroing::testing