blob: 553fc96197ce5e8cf8bcfb0f67cc85b77d709f14 [file] [log] [blame]
Austin Schuh812d0d12021-11-04 20:16:48 -07001// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
Brian Silverman8fce7482020-01-05 13:18:21 -08004
5#include "frc/Counter.h" // NOLINT(build/include_order)
6
Austin Schuh812d0d12021-11-04 20:16:48 -07007#include <units/time.h>
8
Brian Silverman8fce7482020-01-05 13:18:21 -08009#include "TestBench.h"
Brian Silverman8fce7482020-01-05 13:18:21 -080010#include "frc/Timer.h"
Austin Schuh812d0d12021-11-04 20:16:48 -070011#include "frc/motorcontrol/Jaguar.h"
12#include "frc/motorcontrol/Talon.h"
13#include "frc/motorcontrol/Victor.h"
Brian Silverman8fce7482020-01-05 13:18:21 -080014#include "gtest/gtest.h"
15
Austin Schuh812d0d12021-11-04 20:16:48 -070016static constexpr auto kMotorDelay = 2.5_s;
Brian Silverman8fce7482020-01-05 13:18:21 -080017
Austin Schuh812d0d12021-11-04 20:16:48 -070018static constexpr auto kMaxPeriod = 2_s;
Brian Silverman8fce7482020-01-05 13:18:21 -080019
20class CounterTest : public testing::Test {
21 protected:
Austin Schuh812d0d12021-11-04 20:16:48 -070022 frc::Counter m_talonCounter{TestBench::kTalonEncoderChannelA};
23 frc::Counter m_victorCounter{TestBench::kVictorEncoderChannelA};
24 frc::Counter m_jaguarCounter{TestBench::kJaguarEncoderChannelA};
25 frc::Talon m_talon{TestBench::kVictorChannel};
26 frc::Victor m_victor{TestBench::kTalonChannel};
27 frc::Jaguar m_jaguar{TestBench::kJaguarChannel};
Brian Silverman8fce7482020-01-05 13:18:21 -080028
29 void Reset() {
Austin Schuh812d0d12021-11-04 20:16:48 -070030 m_talonCounter.Reset();
31 m_victorCounter.Reset();
32 m_jaguarCounter.Reset();
33 m_talon.Set(0.0);
34 m_victor.Set(0.0);
35 m_jaguar.Set(0.0);
Brian Silverman8fce7482020-01-05 13:18:21 -080036 }
37};
38
39/**
40 * Tests the counter by moving the motor and determining if the
41 * counter is counting.
42 */
43TEST_F(CounterTest, CountTalon) {
44 Reset();
45
46 /* Run the motor forward and determine if the counter is counting. */
Austin Schuh812d0d12021-11-04 20:16:48 -070047 m_talon.Set(1.0);
48 frc::Wait(0.5_s);
Brian Silverman8fce7482020-01-05 13:18:21 -080049
Austin Schuh812d0d12021-11-04 20:16:48 -070050 EXPECT_NE(0.0, m_talonCounter.Get()) << "The counter did not count (talon)";
Brian Silverman8fce7482020-01-05 13:18:21 -080051
52 /* Set the motor to 0 and determine if the counter resets to 0. */
Austin Schuh812d0d12021-11-04 20:16:48 -070053 m_talon.Set(0.0);
54 frc::Wait(0.5_s);
55 m_talonCounter.Reset();
Brian Silverman8fce7482020-01-05 13:18:21 -080056
Austin Schuh812d0d12021-11-04 20:16:48 -070057 EXPECT_FLOAT_EQ(0.0, m_talonCounter.Get())
Brian Silverman8fce7482020-01-05 13:18:21 -080058 << "The counter did not restart to 0 (talon)";
59}
60
61TEST_F(CounterTest, CountVictor) {
62 Reset();
63
64 /* Run the motor forward and determine if the counter is counting. */
Austin Schuh812d0d12021-11-04 20:16:48 -070065 m_victor.Set(1.0);
66 frc::Wait(0.5_s);
Brian Silverman8fce7482020-01-05 13:18:21 -080067
Austin Schuh812d0d12021-11-04 20:16:48 -070068 EXPECT_NE(0.0, m_victorCounter.Get()) << "The counter did not count (victor)";
Brian Silverman8fce7482020-01-05 13:18:21 -080069
70 /* Set the motor to 0 and determine if the counter resets to 0. */
Austin Schuh812d0d12021-11-04 20:16:48 -070071 m_victor.Set(0.0);
72 frc::Wait(0.5_s);
73 m_victorCounter.Reset();
Brian Silverman8fce7482020-01-05 13:18:21 -080074
Austin Schuh812d0d12021-11-04 20:16:48 -070075 EXPECT_FLOAT_EQ(0.0, m_victorCounter.Get())
Brian Silverman8fce7482020-01-05 13:18:21 -080076 << "The counter did not restart to 0 (jaguar)";
77}
78
79TEST_F(CounterTest, CountJaguar) {
80 Reset();
81
82 /* Run the motor forward and determine if the counter is counting. */
Austin Schuh812d0d12021-11-04 20:16:48 -070083 m_jaguar.Set(1.0);
84 frc::Wait(0.5_s);
Brian Silverman8fce7482020-01-05 13:18:21 -080085
Austin Schuh812d0d12021-11-04 20:16:48 -070086 EXPECT_NE(0.0, m_jaguarCounter.Get()) << "The counter did not count (jaguar)";
Brian Silverman8fce7482020-01-05 13:18:21 -080087
88 /* Set the motor to 0 and determine if the counter resets to 0. */
Austin Schuh812d0d12021-11-04 20:16:48 -070089 m_jaguar.Set(0.0);
90 frc::Wait(0.5_s);
91 m_jaguarCounter.Reset();
Brian Silverman8fce7482020-01-05 13:18:21 -080092
Austin Schuh812d0d12021-11-04 20:16:48 -070093 EXPECT_FLOAT_EQ(0.0, m_jaguarCounter.Get())
Brian Silverman8fce7482020-01-05 13:18:21 -080094 << "The counter did not restart to 0 (jaguar)";
95}
96
97/**
98 * Tests the GetStopped and SetMaxPeriod methods by setting the Max Period and
99 * getting the value after a period of time.
100 */
101TEST_F(CounterTest, TalonGetStopped) {
102 Reset();
103
104 /* Set the Max Period of the counter and run the motor */
Austin Schuh812d0d12021-11-04 20:16:48 -0700105 m_talonCounter.SetMaxPeriod(kMaxPeriod);
106 m_talon.Set(1.0);
107 frc::Wait(0.5_s);
Brian Silverman8fce7482020-01-05 13:18:21 -0800108
Austin Schuh812d0d12021-11-04 20:16:48 -0700109 EXPECT_FALSE(m_talonCounter.GetStopped()) << "The counter did not count.";
Brian Silverman8fce7482020-01-05 13:18:21 -0800110
111 /* Stop the motor and wait until the Max Period is exceeded */
Austin Schuh812d0d12021-11-04 20:16:48 -0700112 m_talon.Set(0.0);
113 frc::Wait(kMotorDelay);
Brian Silverman8fce7482020-01-05 13:18:21 -0800114
Austin Schuh812d0d12021-11-04 20:16:48 -0700115 EXPECT_TRUE(m_talonCounter.GetStopped())
Brian Silverman8fce7482020-01-05 13:18:21 -0800116 << "The counter did not stop counting.";
117}
118
119TEST_F(CounterTest, VictorGetStopped) {
120 Reset();
121
122 /* Set the Max Period of the counter and run the motor */
Austin Schuh812d0d12021-11-04 20:16:48 -0700123 m_victorCounter.SetMaxPeriod(kMaxPeriod);
124 m_victor.Set(1.0);
125 frc::Wait(0.5_s);
Brian Silverman8fce7482020-01-05 13:18:21 -0800126
Austin Schuh812d0d12021-11-04 20:16:48 -0700127 EXPECT_FALSE(m_victorCounter.GetStopped()) << "The counter did not count.";
Brian Silverman8fce7482020-01-05 13:18:21 -0800128
129 /* Stop the motor and wait until the Max Period is exceeded */
Austin Schuh812d0d12021-11-04 20:16:48 -0700130 m_victor.Set(0.0);
131 frc::Wait(kMotorDelay);
Brian Silverman8fce7482020-01-05 13:18:21 -0800132
Austin Schuh812d0d12021-11-04 20:16:48 -0700133 EXPECT_TRUE(m_victorCounter.GetStopped())
Brian Silverman8fce7482020-01-05 13:18:21 -0800134 << "The counter did not stop counting.";
135}
136
137TEST_F(CounterTest, JaguarGetStopped) {
138 Reset();
139
140 /* Set the Max Period of the counter and run the motor */
Austin Schuh812d0d12021-11-04 20:16:48 -0700141 m_jaguarCounter.SetMaxPeriod(kMaxPeriod);
142 m_jaguar.Set(1.0);
143 frc::Wait(0.5_s);
Brian Silverman8fce7482020-01-05 13:18:21 -0800144
Austin Schuh812d0d12021-11-04 20:16:48 -0700145 EXPECT_FALSE(m_jaguarCounter.GetStopped()) << "The counter did not count.";
Brian Silverman8fce7482020-01-05 13:18:21 -0800146
147 /* Stop the motor and wait until the Max Period is exceeded */
Austin Schuh812d0d12021-11-04 20:16:48 -0700148 m_jaguar.Set(0.0);
149 frc::Wait(kMotorDelay);
Brian Silverman8fce7482020-01-05 13:18:21 -0800150
Austin Schuh812d0d12021-11-04 20:16:48 -0700151 EXPECT_TRUE(m_jaguarCounter.GetStopped())
Brian Silverman8fce7482020-01-05 13:18:21 -0800152 << "The counter did not stop counting.";
153}