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