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 18:07:45 -04:00
|
|
|
|
2016-05-25 22:38:11 -07:00
|
|
|
#include "TestBench.h"
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/AnalogInput.h"
|
|
|
|
|
#include "frc/AnalogOutput.h"
|
|
|
|
|
#include "frc/AnalogTrigger.h"
|
|
|
|
|
#include "frc/Counter.h"
|
|
|
|
|
#include "frc/Timer.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-29 16:36:36 -04:00
|
|
|
static const double kDelayTime = 0.01;
|
2014-06-12 18:07:45 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A fixture with an analog input and an analog output wired together
|
|
|
|
|
*/
|
|
|
|
|
class AnalogLoopTest : public testing::Test {
|
2015-06-25 15:07:55 -04:00
|
|
|
protected:
|
2016-05-20 17:30:37 -07:00
|
|
|
AnalogInput* m_input;
|
|
|
|
|
AnalogOutput* m_output;
|
2015-06-25 15:07:55 -04:00
|
|
|
|
2016-07-10 17:47:44 -07:00
|
|
|
void SetUp() override {
|
2015-06-25 15:07:55 -04:00
|
|
|
m_input = new AnalogInput(TestBench::kFakeAnalogOutputChannel);
|
|
|
|
|
m_output = new AnalogOutput(TestBench::kAnalogOutputChannel);
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-10 17:47:44 -07:00
|
|
|
void TearDown() override {
|
2015-06-25 15:07:55 -04:00
|
|
|
delete m_input;
|
|
|
|
|
delete m_output;
|
|
|
|
|
}
|
2014-06-12 18:07:45 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test analog inputs and outputs by setting one and making sure the other
|
|
|
|
|
* matches.
|
|
|
|
|
*/
|
2014-07-29 16:36:36 -04:00
|
|
|
TEST_F(AnalogLoopTest, AnalogInputWorks) {
|
2015-06-25 15:07:55 -04:00
|
|
|
// Set the output voltage and check if the input measures the same voltage
|
2016-09-06 00:01:45 -07:00
|
|
|
for (int32_t i = 0; i < 50; i++) {
|
2016-11-20 07:25:03 -08:00
|
|
|
m_output->SetVoltage(i / 10.0);
|
2014-07-29 16:36:36 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
Wait(kDelayTime);
|
2014-07-29 16:36:36 -04:00
|
|
|
|
2016-11-20 07:25:03 -08:00
|
|
|
EXPECT_NEAR(m_output->GetVoltage(), m_input->GetVoltage(), 0.01);
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
2014-07-29 16:36:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test if we can use an analog trigger to check if the output is within a
|
|
|
|
|
* range correctly.
|
|
|
|
|
*/
|
|
|
|
|
TEST_F(AnalogLoopTest, AnalogTriggerWorks) {
|
2015-06-25 15:07:55 -04:00
|
|
|
AnalogTrigger trigger(m_input);
|
2016-11-20 07:25:03 -08:00
|
|
|
trigger.SetLimitsVoltage(2.0, 3.0);
|
2014-07-29 16:36:36 -04:00
|
|
|
|
2016-11-20 07:25:03 -08:00
|
|
|
m_output->SetVoltage(1.0);
|
2015-06-25 15:07:55 -04:00
|
|
|
Wait(kDelayTime);
|
2014-07-29 16:36:36 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
EXPECT_FALSE(trigger.GetInWindow())
|
|
|
|
|
<< "Analog trigger is in the window (2V, 3V)";
|
|
|
|
|
EXPECT_FALSE(trigger.GetTriggerState()) << "Analog trigger is on";
|
2014-07-29 16:36:36 -04:00
|
|
|
|
2016-11-20 07:25:03 -08:00
|
|
|
m_output->SetVoltage(2.5);
|
2015-06-25 15:07:55 -04:00
|
|
|
Wait(kDelayTime);
|
2014-07-29 16:36:36 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
EXPECT_TRUE(trigger.GetInWindow())
|
|
|
|
|
<< "Analog trigger is not in the window (2V, 3V)";
|
|
|
|
|
EXPECT_FALSE(trigger.GetTriggerState()) << "Analog trigger is on";
|
2014-07-29 16:36:36 -04:00
|
|
|
|
2016-11-20 07:25:03 -08:00
|
|
|
m_output->SetVoltage(4.0);
|
2015-06-25 15:07:55 -04:00
|
|
|
Wait(kDelayTime);
|
2014-07-29 16:36:36 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
EXPECT_FALSE(trigger.GetInWindow())
|
|
|
|
|
<< "Analog trigger is in the window (2V, 3V)";
|
|
|
|
|
EXPECT_TRUE(trigger.GetTriggerState()) << "Analog trigger is not on";
|
2014-07-29 16:36:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test if we can count the right number of ticks from an analog trigger with
|
|
|
|
|
* a counter.
|
|
|
|
|
*/
|
|
|
|
|
TEST_F(AnalogLoopTest, AnalogTriggerCounterWorks) {
|
2015-06-25 15:07:55 -04:00
|
|
|
AnalogTrigger trigger(m_input);
|
2016-11-20 07:25:03 -08:00
|
|
|
trigger.SetLimitsVoltage(2.0, 3.0);
|
2015-06-25 15:07:55 -04:00
|
|
|
|
|
|
|
|
Counter counter(trigger);
|
|
|
|
|
|
|
|
|
|
// Turn the analog output low and high 50 times
|
2016-09-06 00:01:45 -07:00
|
|
|
for (int32_t i = 0; i < 50; i++) {
|
2015-06-25 15:07:55 -04:00
|
|
|
m_output->SetVoltage(1.0);
|
|
|
|
|
Wait(kDelayTime);
|
|
|
|
|
m_output->SetVoltage(4.0);
|
|
|
|
|
Wait(kDelayTime);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The counter should be 50
|
|
|
|
|
EXPECT_EQ(50, counter.Get())
|
|
|
|
|
<< "Analog trigger counter did not count 50 ticks";
|
2014-06-12 18:07:45 -04:00
|
|
|
}
|
2014-08-04 12:19:31 -04:00
|
|
|
|
2016-05-20 17:30:37 -07:00
|
|
|
static void InterruptHandler(uint32_t interruptAssertedMask, void* param) {
|
2016-09-06 00:01:45 -07:00
|
|
|
*reinterpret_cast<int32_t*>(param) = 12345;
|
2014-08-04 12:19:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(AnalogLoopTest, AsynchronusInterruptWorks) {
|
2016-09-06 00:01:45 -07:00
|
|
|
int32_t param = 0;
|
2015-06-25 15:07:55 -04:00
|
|
|
AnalogTrigger trigger(m_input);
|
2016-11-20 07:25:03 -08:00
|
|
|
trigger.SetLimitsVoltage(2.0, 3.0);
|
2015-06-25 15:07:55 -04:00
|
|
|
|
2016-09-06 00:01:45 -07:00
|
|
|
// Given an interrupt handler that sets an int32_t to 12345
|
2016-05-20 17:30:37 -07:00
|
|
|
std::shared_ptr<AnalogTriggerOutput> triggerOutput =
|
2016-07-09 00:24:26 -07:00
|
|
|
trigger.CreateOutput(AnalogTriggerType::kState);
|
2015-06-25 15:07:55 -04:00
|
|
|
triggerOutput->RequestInterrupts(InterruptHandler, ¶m);
|
|
|
|
|
triggerOutput->EnableInterrupts();
|
|
|
|
|
|
|
|
|
|
// If the analog output moves from below to above the window
|
|
|
|
|
m_output->SetVoltage(0.0);
|
|
|
|
|
Wait(kDelayTime);
|
|
|
|
|
m_output->SetVoltage(5.0);
|
|
|
|
|
triggerOutput->CancelInterrupts();
|
|
|
|
|
|
2016-09-06 00:01:45 -07:00
|
|
|
// Then the int32_t should be 12345
|
2015-06-25 15:07:55 -04:00
|
|
|
Wait(kDelayTime);
|
|
|
|
|
EXPECT_EQ(12345, param) << "The interrupt did not run.";
|
2014-08-04 12:19:31 -04:00
|
|
|
}
|