2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2014-06-12 16:29:47 -04:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/Counter.h" // NOLINT(build/include_order)
|
2016-05-25 22:38:11 -07:00
|
|
|
|
|
|
|
|
#include "TestBench.h"
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/Jaguar.h"
|
|
|
|
|
#include "frc/Talon.h"
|
|
|
|
|
#include "frc/Timer.h"
|
|
|
|
|
#include "frc/Victor.h"
|
2016-09-05 13:55:31 -07:00
|
|
|
#include "gtest/gtest.h"
|
2016-05-25 22:38:11 -07:00
|
|
|
|
2016-11-01 22:33:12 -07:00
|
|
|
using namespace frc;
|
|
|
|
|
|
2014-07-01 14:06:26 -04:00
|
|
|
static const double kMotorDelay = 2.5;
|
2014-06-12 16:29:47 -04:00
|
|
|
|
2014-07-01 14:06:26 -04:00
|
|
|
static const double kMaxPeriod = 2.0;
|
2014-06-12 16:29:47 -04:00
|
|
|
|
2014-07-01 14:06:26 -04:00
|
|
|
class CounterTest : public testing::Test {
|
2015-06-25 15:07:55 -04:00
|
|
|
protected:
|
2016-05-20 17:30:37 -07:00
|
|
|
Counter* m_talonCounter;
|
|
|
|
|
Counter* m_victorCounter;
|
|
|
|
|
Counter* m_jaguarCounter;
|
|
|
|
|
Talon* m_talon;
|
|
|
|
|
Victor* m_victor;
|
|
|
|
|
Jaguar* m_jaguar;
|
2014-07-01 14:06:26 -04:00
|
|
|
|
2016-07-10 17:47:44 -07:00
|
|
|
void SetUp() override {
|
2014-07-01 14:06:26 -04:00
|
|
|
m_talonCounter = new Counter(TestBench::kTalonEncoderChannelA);
|
|
|
|
|
m_victorCounter = new Counter(TestBench::kVictorEncoderChannelA);
|
|
|
|
|
m_jaguarCounter = new Counter(TestBench::kJaguarEncoderChannelA);
|
|
|
|
|
m_victor = new Victor(TestBench::kVictorChannel);
|
|
|
|
|
m_talon = new Talon(TestBench::kTalonChannel);
|
|
|
|
|
m_jaguar = new Jaguar(TestBench::kJaguarChannel);
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-10 17:47:44 -07:00
|
|
|
void TearDown() override {
|
2014-07-01 14:06:26 -04:00
|
|
|
delete m_talonCounter;
|
|
|
|
|
delete m_victorCounter;
|
|
|
|
|
delete m_jaguarCounter;
|
|
|
|
|
delete m_victor;
|
|
|
|
|
delete m_talon;
|
|
|
|
|
delete m_jaguar;
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
2014-07-01 14:06:26 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void Reset() {
|
|
|
|
|
m_talonCounter->Reset();
|
|
|
|
|
m_victorCounter->Reset();
|
|
|
|
|
m_jaguarCounter->Reset();
|
2016-11-20 07:25:03 -08:00
|
|
|
m_talon->Set(0.0);
|
|
|
|
|
m_victor->Set(0.0);
|
|
|
|
|
m_jaguar->Set(0.0);
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
2014-07-01 14:06:26 -04:00
|
|
|
};
|
2014-06-12 16:29:47 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Tests the counter by moving the motor and determining if the
|
2014-07-01 14:06:26 -04:00
|
|
|
* counter is counting.
|
2014-06-12 16:29:47 -04:00
|
|
|
*/
|
2014-07-01 14:06:26 -04:00
|
|
|
TEST_F(CounterTest, CountTalon) {
|
|
|
|
|
Reset();
|
2017-08-07 17:36:34 -07:00
|
|
|
|
2014-07-01 14:06:26 -04:00
|
|
|
/* Run the motor forward and determine if the counter is counting. */
|
2016-11-20 07:25:03 -08:00
|
|
|
m_talon->Set(1.0);
|
2014-07-01 14:06:26 -04:00
|
|
|
Wait(0.5);
|
2017-08-07 17:36:34 -07:00
|
|
|
|
2016-11-20 07:25:03 -08:00
|
|
|
EXPECT_NE(0.0, m_talonCounter->Get()) << "The counter did not count (talon)";
|
2017-08-07 17:36:34 -07:00
|
|
|
|
2014-07-01 14:06:26 -04:00
|
|
|
/* Set the motor to 0 and determine if the counter resets to 0. */
|
2016-11-20 07:25:03 -08:00
|
|
|
m_talon->Set(0.0);
|
2014-07-01 14:06:26 -04:00
|
|
|
Wait(0.5);
|
|
|
|
|
m_talonCounter->Reset();
|
2017-08-07 17:36:34 -07:00
|
|
|
|
2016-11-20 07:25:03 -08:00
|
|
|
EXPECT_FLOAT_EQ(0.0, m_talonCounter->Get())
|
2015-06-25 15:07:55 -04:00
|
|
|
<< "The counter did not restart to 0 (talon)";
|
2014-07-01 14:06:26 -04:00
|
|
|
}
|
2014-06-12 16:29:47 -04:00
|
|
|
|
2014-07-01 14:06:26 -04:00
|
|
|
TEST_F(CounterTest, CountVictor) {
|
|
|
|
|
Reset();
|
2017-08-07 17:36:34 -07:00
|
|
|
|
2014-07-01 14:06:26 -04:00
|
|
|
/* Run the motor forward and determine if the counter is counting. */
|
2016-11-20 07:25:03 -08:00
|
|
|
m_victor->Set(1.0);
|
2014-07-01 14:06:26 -04:00
|
|
|
Wait(0.5);
|
2017-08-07 17:36:34 -07:00
|
|
|
|
2016-11-20 07:25:03 -08:00
|
|
|
EXPECT_NE(0.0, m_victorCounter->Get())
|
2015-06-25 15:07:55 -04:00
|
|
|
<< "The counter did not count (victor)";
|
2017-08-07 17:36:34 -07:00
|
|
|
|
2014-07-01 14:06:26 -04:00
|
|
|
/* Set the motor to 0 and determine if the counter resets to 0. */
|
2016-11-20 07:25:03 -08:00
|
|
|
m_victor->Set(0.0);
|
2014-07-01 14:06:26 -04:00
|
|
|
Wait(0.5);
|
|
|
|
|
m_victorCounter->Reset();
|
2017-08-07 17:36:34 -07:00
|
|
|
|
2016-11-20 07:25:03 -08:00
|
|
|
EXPECT_FLOAT_EQ(0.0, m_victorCounter->Get())
|
2015-06-25 15:07:55 -04:00
|
|
|
<< "The counter did not restart to 0 (jaguar)";
|
2014-07-01 14:06:26 -04:00
|
|
|
}
|
2014-06-12 16:29:47 -04:00
|
|
|
|
2014-07-01 14:06:26 -04:00
|
|
|
TEST_F(CounterTest, CountJaguar) {
|
|
|
|
|
Reset();
|
2017-08-07 17:36:34 -07:00
|
|
|
|
2014-07-01 14:06:26 -04:00
|
|
|
/* Run the motor forward and determine if the counter is counting. */
|
2016-11-20 07:25:03 -08:00
|
|
|
m_jaguar->Set(1.0);
|
2014-07-01 14:06:26 -04:00
|
|
|
Wait(0.5);
|
2017-08-07 17:36:34 -07:00
|
|
|
|
2016-11-20 07:25:03 -08:00
|
|
|
EXPECT_NE(0.0, m_jaguarCounter->Get())
|
2015-06-25 15:07:55 -04:00
|
|
|
<< "The counter did not count (jaguar)";
|
2017-08-07 17:36:34 -07:00
|
|
|
|
2014-07-01 14:06:26 -04:00
|
|
|
/* Set the motor to 0 and determine if the counter resets to 0. */
|
2016-11-20 07:25:03 -08:00
|
|
|
m_jaguar->Set(0.0);
|
2014-07-01 14:06:26 -04:00
|
|
|
Wait(0.5);
|
|
|
|
|
m_jaguarCounter->Reset();
|
2017-08-07 17:36:34 -07:00
|
|
|
|
2016-11-20 07:25:03 -08:00
|
|
|
EXPECT_FLOAT_EQ(0.0, m_jaguarCounter->Get())
|
2015-06-25 15:07:55 -04:00
|
|
|
<< "The counter did not restart to 0 (jaguar)";
|
2014-07-01 14:06:26 -04:00
|
|
|
}
|
2014-06-12 16:29:47 -04:00
|
|
|
|
2014-07-01 14:06:26 -04:00
|
|
|
/**
|
|
|
|
|
* Tests the GetStopped and SetMaxPeriod methods by setting the Max Period and
|
|
|
|
|
* getting the value after a period of time.
|
|
|
|
|
*/
|
|
|
|
|
TEST_F(CounterTest, TalonGetStopped) {
|
|
|
|
|
Reset();
|
2017-08-07 17:36:34 -07:00
|
|
|
|
2014-07-01 14:06:26 -04:00
|
|
|
/* Set the Max Period of the counter and run the motor */
|
|
|
|
|
m_talonCounter->SetMaxPeriod(kMaxPeriod);
|
2016-11-20 07:25:03 -08:00
|
|
|
m_talon->Set(1.0);
|
2014-07-01 14:06:26 -04:00
|
|
|
Wait(0.5);
|
2017-08-07 17:36:34 -07:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
EXPECT_FALSE(m_talonCounter->GetStopped()) << "The counter did not count.";
|
2017-08-07 17:36:34 -07:00
|
|
|
|
2014-07-01 14:06:26 -04:00
|
|
|
/* Stop the motor and wait until the Max Period is exceeded */
|
2016-11-20 07:25:03 -08:00
|
|
|
m_talon->Set(0.0);
|
2014-07-01 14:06:26 -04:00
|
|
|
Wait(kMotorDelay);
|
2017-08-07 17:36:34 -07:00
|
|
|
|
2014-07-01 14:06:26 -04:00
|
|
|
EXPECT_TRUE(m_talonCounter->GetStopped())
|
2015-06-25 15:07:55 -04:00
|
|
|
<< "The counter did not stop counting.";
|
2014-07-01 14:06:26 -04:00
|
|
|
}
|
2014-06-12 16:29:47 -04:00
|
|
|
|
2014-07-01 14:06:26 -04:00
|
|
|
TEST_F(CounterTest, VictorGetStopped) {
|
|
|
|
|
Reset();
|
2017-08-07 17:36:34 -07:00
|
|
|
|
2014-07-01 14:06:26 -04:00
|
|
|
/* Set the Max Period of the counter and run the motor */
|
|
|
|
|
m_victorCounter->SetMaxPeriod(kMaxPeriod);
|
2016-11-20 07:25:03 -08:00
|
|
|
m_victor->Set(1.0);
|
2014-07-01 14:06:26 -04:00
|
|
|
Wait(0.5);
|
2017-08-07 17:36:34 -07:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
EXPECT_FALSE(m_victorCounter->GetStopped()) << "The counter did not count.";
|
2017-08-07 17:36:34 -07:00
|
|
|
|
2014-07-01 14:06:26 -04:00
|
|
|
/* Stop the motor and wait until the Max Period is exceeded */
|
2016-11-20 07:25:03 -08:00
|
|
|
m_victor->Set(0.0);
|
2014-07-01 14:06:26 -04:00
|
|
|
Wait(kMotorDelay);
|
2017-08-07 17:36:34 -07:00
|
|
|
|
2014-07-01 14:06:26 -04:00
|
|
|
EXPECT_TRUE(m_victorCounter->GetStopped())
|
2015-06-25 15:07:55 -04:00
|
|
|
<< "The counter did not stop counting.";
|
2014-07-01 14:06:26 -04:00
|
|
|
}
|
2014-06-12 16:29:47 -04:00
|
|
|
|
2014-07-01 14:06:26 -04:00
|
|
|
TEST_F(CounterTest, JaguarGetStopped) {
|
|
|
|
|
Reset();
|
2017-08-07 17:36:34 -07:00
|
|
|
|
2014-07-01 14:06:26 -04:00
|
|
|
/* Set the Max Period of the counter and run the motor */
|
|
|
|
|
m_jaguarCounter->SetMaxPeriod(kMaxPeriod);
|
2016-11-20 07:25:03 -08:00
|
|
|
m_jaguar->Set(1.0);
|
2014-07-01 14:06:26 -04:00
|
|
|
Wait(0.5);
|
2017-08-07 17:36:34 -07:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
EXPECT_FALSE(m_jaguarCounter->GetStopped()) << "The counter did not count.";
|
2017-08-07 17:36:34 -07:00
|
|
|
|
2014-07-01 14:06:26 -04:00
|
|
|
/* Stop the motor and wait until the Max Period is exceeded */
|
2016-11-20 07:25:03 -08:00
|
|
|
m_jaguar->Set(0.0);
|
2014-07-01 14:06:26 -04:00
|
|
|
Wait(kMotorDelay);
|
2017-08-07 17:36:34 -07:00
|
|
|
|
2014-07-01 14:06:26 -04:00
|
|
|
EXPECT_TRUE(m_jaguarCounter->GetStopped())
|
2015-06-25 15:07:55 -04:00
|
|
|
<< "The counter did not stop counting.";
|
2014-07-01 14:06:26 -04:00
|
|
|
}
|