blob: cbf9dc56475457717fbf7f6f30fd3bdc70e07a63 [file] [log] [blame]
Brian Silverman26e4e522015-12-17 01:56:40 -05001/*----------------------------------------------------------------------------*/
Brian Silverman1a675112016-02-20 20:42:49 -05002/* Copyright (c) FIRST 2014-2016. All Rights Reserved. */
Brian Silverman26e4e522015-12-17 01:56:40 -05003/* Open Source Software - may be modified and shared by FRC teams. The code */
4/* must be accompanied by the FIRST BSD license file in the root directory of */
5/* the project. */
6/*----------------------------------------------------------------------------*/
7
8#include <Counter.h>
9#include <Jaguar.h>
10#include <Talon.h>
11#include <Timer.h>
12#include <Victor.h>
13#include "gtest/gtest.h"
14#include "TestBench.h"
15
16static const double kMotorDelay = 2.5;
17
18static const double kMaxPeriod = 2.0;
19
20class CounterTest : public testing::Test {
21 protected:
22 Counter *m_talonCounter;
23 Counter *m_victorCounter;
24 Counter *m_jaguarCounter;
25 Talon *m_talon;
26 Victor *m_victor;
27 Jaguar *m_jaguar;
28
29 virtual void SetUp() override {
30 m_talonCounter = new Counter(TestBench::kTalonEncoderChannelA);
31 m_victorCounter = new Counter(TestBench::kVictorEncoderChannelA);
32 m_jaguarCounter = new Counter(TestBench::kJaguarEncoderChannelA);
33 m_victor = new Victor(TestBench::kVictorChannel);
34 m_talon = new Talon(TestBench::kTalonChannel);
35 m_jaguar = new Jaguar(TestBench::kJaguarChannel);
36 }
37
38 virtual void TearDown() override {
39 delete m_talonCounter;
40 delete m_victorCounter;
41 delete m_jaguarCounter;
42 delete m_victor;
43 delete m_talon;
44 delete m_jaguar;
45 }
46
47 void Reset() {
48 m_talonCounter->Reset();
49 m_victorCounter->Reset();
50 m_jaguarCounter->Reset();
51 m_talon->Set(0.0f);
52 m_victor->Set(0.0f);
53 m_jaguar->Set(0.0f);
54 }
55};
56
57/**
58 * Tests the counter by moving the motor and determining if the
59 * counter is counting.
60 */
61TEST_F(CounterTest, CountTalon) {
62 Reset();
63 /* Run the motor forward and determine if the counter is counting. */
64 m_talon->Set(1.0f);
65 Wait(0.5);
66 EXPECT_NE(0.0f, m_talonCounter->Get()) << "The counter did not count (talon)";
67 /* Set the motor to 0 and determine if the counter resets to 0. */
68 m_talon->Set(0.0f);
69 Wait(0.5);
70 m_talonCounter->Reset();
71 EXPECT_FLOAT_EQ(0.0f, m_talonCounter->Get())
72 << "The counter did not restart to 0 (talon)";
73}
74
75TEST_F(CounterTest, CountVictor) {
76 Reset();
77 /* Run the motor forward and determine if the counter is counting. */
78 m_victor->Set(1.0f);
79 Wait(0.5);
80 EXPECT_NE(0.0f, m_victorCounter->Get())
81 << "The counter did not count (victor)";
82 /* Set the motor to 0 and determine if the counter resets to 0. */
83 m_victor->Set(0.0f);
84 Wait(0.5);
85 m_victorCounter->Reset();
86 EXPECT_FLOAT_EQ(0.0f, m_victorCounter->Get())
87 << "The counter did not restart to 0 (jaguar)";
88}
89
90TEST_F(CounterTest, CountJaguar) {
91 Reset();
92 /* Run the motor forward and determine if the counter is counting. */
93 m_jaguar->Set(1.0f);
94 Wait(0.5);
95 EXPECT_NE(0.0f, m_jaguarCounter->Get())
96 << "The counter did not count (jaguar)";
97 /* Set the motor to 0 and determine if the counter resets to 0. */
98 m_jaguar->Set(0.0f);
99 Wait(0.5);
100 m_jaguarCounter->Reset();
101 EXPECT_FLOAT_EQ(0.0f, m_jaguarCounter->Get())
102 << "The counter did not restart to 0 (jaguar)";
103}
104
105/**
106 * Tests the GetStopped and SetMaxPeriod methods by setting the Max Period and
107 * getting the value after a period of time.
108 */
109TEST_F(CounterTest, TalonGetStopped) {
110 Reset();
111 /* Set the Max Period of the counter and run the motor */
112 m_talonCounter->SetMaxPeriod(kMaxPeriod);
113 m_talon->Set(1.0f);
114 Wait(0.5);
115 EXPECT_FALSE(m_talonCounter->GetStopped()) << "The counter did not count.";
116 /* Stop the motor and wait until the Max Period is exceeded */
117 m_talon->Set(0.0f);
118 Wait(kMotorDelay);
119 EXPECT_TRUE(m_talonCounter->GetStopped())
120 << "The counter did not stop counting.";
121}
122
123TEST_F(CounterTest, VictorGetStopped) {
124 Reset();
125 /* Set the Max Period of the counter and run the motor */
126 m_victorCounter->SetMaxPeriod(kMaxPeriod);
127 m_victor->Set(1.0f);
128 Wait(0.5);
129 EXPECT_FALSE(m_victorCounter->GetStopped()) << "The counter did not count.";
130 /* Stop the motor and wait until the Max Period is exceeded */
131 m_victor->Set(0.0f);
132 Wait(kMotorDelay);
133 EXPECT_TRUE(m_victorCounter->GetStopped())
134 << "The counter did not stop counting.";
135}
136
137TEST_F(CounterTest, JaguarGetStopped) {
138 Reset();
139 /* Set the Max Period of the counter and run the motor */
140 m_jaguarCounter->SetMaxPeriod(kMaxPeriod);
141 m_jaguar->Set(1.0f);
142 Wait(0.5);
143 EXPECT_FALSE(m_jaguarCounter->GetStopped()) << "The counter did not count.";
144 /* Stop the motor and wait until the Max Period is exceeded */
145 m_jaguar->Set(0.0f);
146 Wait(kMotorDelay);
147 EXPECT_TRUE(m_jaguarCounter->GetStopped())
148 << "The counter did not stop counting.";
149}