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