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-23 11:18:27 -04:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/Encoder.h" // NOLINT(build/include_order)
|
2016-05-25 22:38:11 -07:00
|
|
|
|
2023-08-28 15:13:34 -07:00
|
|
|
#include <gtest/gtest.h>
|
2021-05-28 22:06:59 -07:00
|
|
|
#include <units/time.h>
|
|
|
|
|
|
2016-05-25 22:38:11 -07:00
|
|
|
#include "TestBench.h"
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/AnalogOutput.h"
|
|
|
|
|
#include "frc/AnalogTrigger.h"
|
|
|
|
|
#include "frc/DigitalOutput.h"
|
|
|
|
|
#include "frc/Timer.h"
|
2016-05-25 22:38:11 -07:00
|
|
|
|
2021-05-28 22:06:59 -07:00
|
|
|
static constexpr auto kDelayTime = 1_ms;
|
2014-06-23 11:18:27 -04:00
|
|
|
|
|
|
|
|
class FakeEncoderTest : public testing::Test {
|
2015-06-25 15:07:55 -04:00
|
|
|
protected:
|
2021-05-31 10:21:34 -07:00
|
|
|
frc::DigitalOutput m_outputA{TestBench::kLoop2OutputChannel};
|
|
|
|
|
frc::DigitalOutput m_outputB{TestBench::kLoop1OutputChannel};
|
|
|
|
|
frc::AnalogOutput m_indexOutput{TestBench::kAnalogOutputChannel};
|
|
|
|
|
|
|
|
|
|
frc::Encoder m_encoder{TestBench::kLoop1InputChannel,
|
|
|
|
|
TestBench::kLoop2InputChannel};
|
|
|
|
|
frc::AnalogTrigger m_indexAnalogTrigger{TestBench::kFakeAnalogOutputChannel};
|
|
|
|
|
std::shared_ptr<frc::AnalogTriggerOutput> m_indexAnalogTriggerOutput =
|
|
|
|
|
m_indexAnalogTrigger.CreateOutput(frc::AnalogTriggerType::kState);
|
|
|
|
|
|
|
|
|
|
FakeEncoderTest() {
|
|
|
|
|
m_outputA.Set(false);
|
|
|
|
|
m_outputB.Set(false);
|
|
|
|
|
m_indexAnalogTrigger.SetLimitsVoltage(2.0, 3.0);
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Output pulses to the encoder's input channels to simulate a change of 100
|
|
|
|
|
* ticks
|
|
|
|
|
*/
|
|
|
|
|
void Simulate100QuadratureTicks() {
|
2016-09-06 00:01:45 -07:00
|
|
|
for (int32_t i = 0; i < 100; i++) {
|
2021-05-31 10:21:34 -07:00
|
|
|
m_outputA.Set(true);
|
|
|
|
|
frc::Wait(kDelayTime);
|
|
|
|
|
m_outputB.Set(true);
|
|
|
|
|
frc::Wait(kDelayTime);
|
|
|
|
|
m_outputA.Set(false);
|
|
|
|
|
frc::Wait(kDelayTime);
|
|
|
|
|
m_outputB.Set(false);
|
|
|
|
|
frc::Wait(kDelayTime);
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SetIndexHigh() {
|
2021-05-31 10:21:34 -07:00
|
|
|
m_indexOutput.SetVoltage(5.0);
|
|
|
|
|
frc::Wait(kDelayTime);
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SetIndexLow() {
|
2021-05-31 10:21:34 -07:00
|
|
|
m_indexOutput.SetVoltage(0.0);
|
|
|
|
|
frc::Wait(kDelayTime);
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
2014-06-23 11:18:27 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
2023-02-26 15:06:37 -08:00
|
|
|
* Test the encoder by resetting it to 0 and reading the value.
|
2014-06-23 11:18:27 -04:00
|
|
|
*/
|
2021-09-17 22:51:51 -07:00
|
|
|
TEST_F(FakeEncoderTest, DefaultState) {
|
2021-05-31 10:21:34 -07:00
|
|
|
EXPECT_DOUBLE_EQ(0.0, m_encoder.Get()) << "The encoder did not start at 0.";
|
2014-06-23 11:18:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test the encoder by setting the digital outputs and reading the value.
|
|
|
|
|
*/
|
2021-09-17 22:51:51 -07:00
|
|
|
TEST_F(FakeEncoderTest, CountUp) {
|
2021-05-31 10:21:34 -07:00
|
|
|
m_encoder.Reset();
|
2015-06-25 15:07:55 -04:00
|
|
|
Simulate100QuadratureTicks();
|
2014-06-23 11:18:27 -04:00
|
|
|
|
2021-05-31 10:21:34 -07:00
|
|
|
EXPECT_DOUBLE_EQ(100.0, m_encoder.Get()) << "Encoder did not count to 100.";
|
2014-06-23 11:18:27 -04:00
|
|
|
}
|
2015-01-06 16:39:24 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test that the encoder can stay reset while the index source is high
|
|
|
|
|
*/
|
2021-09-17 22:51:51 -07:00
|
|
|
TEST_F(FakeEncoderTest, ResetWhileHigh) {
|
2021-05-31 10:21:34 -07:00
|
|
|
m_encoder.SetIndexSource(*m_indexAnalogTriggerOutput,
|
|
|
|
|
frc::Encoder::IndexingType::kResetWhileHigh);
|
2015-01-06 16:39:24 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
SetIndexLow();
|
|
|
|
|
Simulate100QuadratureTicks();
|
|
|
|
|
SetIndexHigh();
|
2021-05-31 10:21:34 -07:00
|
|
|
EXPECT_EQ(0, m_encoder.Get());
|
2015-01-06 16:39:24 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
Simulate100QuadratureTicks();
|
2021-05-31 10:21:34 -07:00
|
|
|
EXPECT_EQ(0, m_encoder.Get());
|
2015-01-06 16:39:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test that the encoder can reset when the index source goes from low to high
|
|
|
|
|
*/
|
2021-09-17 22:51:51 -07:00
|
|
|
TEST_F(FakeEncoderTest, ResetOnRisingEdge) {
|
2021-05-31 10:21:34 -07:00
|
|
|
m_encoder.SetIndexSource(*m_indexAnalogTriggerOutput,
|
|
|
|
|
frc::Encoder::IndexingType::kResetOnRisingEdge);
|
2015-01-06 16:39:24 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
SetIndexLow();
|
|
|
|
|
Simulate100QuadratureTicks();
|
|
|
|
|
SetIndexHigh();
|
2021-05-31 10:21:34 -07:00
|
|
|
EXPECT_EQ(0, m_encoder.Get());
|
2015-01-06 16:39:24 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
Simulate100QuadratureTicks();
|
2021-05-31 10:21:34 -07:00
|
|
|
EXPECT_EQ(100, m_encoder.Get());
|
2015-01-06 16:39:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test that the encoder can stay reset while the index source is low
|
|
|
|
|
*/
|
2021-09-17 22:51:51 -07:00
|
|
|
TEST_F(FakeEncoderTest, ResetWhileLow) {
|
2021-05-31 10:21:34 -07:00
|
|
|
m_encoder.SetIndexSource(*m_indexAnalogTriggerOutput,
|
|
|
|
|
frc::Encoder::IndexingType::kResetWhileLow);
|
2015-01-06 16:39:24 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
SetIndexHigh();
|
|
|
|
|
Simulate100QuadratureTicks();
|
|
|
|
|
SetIndexLow();
|
2021-05-31 10:21:34 -07:00
|
|
|
EXPECT_EQ(0, m_encoder.Get());
|
2015-01-06 16:39:24 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
Simulate100QuadratureTicks();
|
2021-05-31 10:21:34 -07:00
|
|
|
EXPECT_EQ(0, m_encoder.Get());
|
2015-01-06 16:39:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test that the encoder can reset when the index source goes from high to low
|
|
|
|
|
*/
|
2021-09-17 22:51:51 -07:00
|
|
|
TEST_F(FakeEncoderTest, ResetOnFallingEdge) {
|
2021-05-31 10:21:34 -07:00
|
|
|
m_encoder.SetIndexSource(*m_indexAnalogTriggerOutput,
|
|
|
|
|
frc::Encoder::IndexingType::kResetOnFallingEdge);
|
2015-01-06 16:39:24 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
SetIndexHigh();
|
|
|
|
|
Simulate100QuadratureTicks();
|
|
|
|
|
SetIndexLow();
|
2021-05-31 10:21:34 -07:00
|
|
|
EXPECT_EQ(0, m_encoder.Get());
|
2015-01-06 16:39:24 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
Simulate100QuadratureTicks();
|
2021-05-31 10:21:34 -07:00
|
|
|
EXPECT_EQ(100, m_encoder.Get());
|
2015-01-06 16:39:24 -05:00
|
|
|
}
|