2014-06-07 17:37:51 -04:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2016-01-02 03:02:34 -08:00
|
|
|
/* Copyright (c) FIRST 2014-2016. All Rights Reserved. */
|
2014-06-07 17:37:51 -04:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
Replaced WPILib.h includes in integration tests with the minimum required subheaders to improve compilation times
I ran the benchmark in a tmpfs with an Intel Core i5-2430M. I ran it three times for each combination of build invokation and source tree.
First, I tested "make". For master (eb7d55f), I measured an average of 42.751s with a standard deviation of 0.372s. For this commit, I measured an average of 33.394s with a standard deviation of 0.140s. There was a 9.356s, or 22%, improvement with a total error of 1.3%.
Second, I tested "make -j4". For master (eb7d55f), I measured an average of 21.723s with a standard deviation of 0.158s. For this commit, I measured an average of 16.823s with a standard deviation of 0.340s. There was a 4.900s, or 23%, improvement with a total error of 2.7%.
Change-Id: Idb3adce62ed8ef449360c6583896b6da3565cf58
2015-07-22 02:34:12 -07:00
|
|
|
#include <Counter.h>
|
|
|
|
|
#include <DigitalInput.h>
|
|
|
|
|
#include <DigitalOutput.h>
|
2015-12-21 22:25:53 -08:00
|
|
|
#include <InterruptableSensorBase.h>
|
Replaced WPILib.h includes in integration tests with the minimum required subheaders to improve compilation times
I ran the benchmark in a tmpfs with an Intel Core i5-2430M. I ran it three times for each combination of build invokation and source tree.
First, I tested "make". For master (eb7d55f), I measured an average of 42.751s with a standard deviation of 0.372s. For this commit, I measured an average of 33.394s with a standard deviation of 0.140s. There was a 9.356s, or 22%, improvement with a total error of 1.3%.
Second, I tested "make -j4". For master (eb7d55f), I measured an average of 21.723s with a standard deviation of 0.158s. For this commit, I measured an average of 16.823s with a standard deviation of 0.340s. There was a 4.900s, or 23%, improvement with a total error of 2.7%.
Change-Id: Idb3adce62ed8ef449360c6583896b6da3565cf58
2015-07-22 02:34:12 -07:00
|
|
|
#include <Timer.h>
|
2014-06-07 17:37:51 -04:00
|
|
|
#include "TestBench.h"
|
2016-05-20 17:30:37 -07:00
|
|
|
#include "gtest/gtest.h"
|
2014-06-07 17:37:51 -04:00
|
|
|
|
2014-08-15 11:22:01 -04:00
|
|
|
static const double kCounterTime = 0.001;
|
|
|
|
|
|
2014-06-12 11:17:57 -04:00
|
|
|
static const double kDelayTime = 0.1;
|
|
|
|
|
|
2014-08-06 09:46:50 -04:00
|
|
|
static const double kSynchronousInterruptTime = 2.0;
|
|
|
|
|
static const double kSynchronousInterruptTimeTolerance = 0.01;
|
|
|
|
|
|
2014-06-07 17:37:51 -04:00
|
|
|
/**
|
|
|
|
|
* A fixture with a digital input and a digital output physically wired
|
|
|
|
|
* together.
|
|
|
|
|
*/
|
|
|
|
|
class DIOLoopTest : public testing::Test {
|
2015-06-25 15:07:55 -04:00
|
|
|
protected:
|
2016-05-20 17:30:37 -07:00
|
|
|
DigitalInput* m_input;
|
|
|
|
|
DigitalOutput* m_output;
|
2015-06-25 15:07:55 -04:00
|
|
|
|
|
|
|
|
virtual void SetUp() override {
|
|
|
|
|
m_input = new DigitalInput(TestBench::kLoop1InputChannel);
|
|
|
|
|
m_output = new DigitalOutput(TestBench::kLoop1OutputChannel);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void TearDown() override {
|
|
|
|
|
delete m_input;
|
|
|
|
|
delete m_output;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Reset() { m_output->Set(false); }
|
2014-06-07 17:37:51 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test the DigitalInput and DigitalOutput classes by setting the output and
|
|
|
|
|
* reading the input.
|
|
|
|
|
*/
|
|
|
|
|
TEST_F(DIOLoopTest, Loop) {
|
2015-06-25 15:07:55 -04:00
|
|
|
Reset();
|
2014-06-12 11:17:57 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
m_output->Set(false);
|
|
|
|
|
Wait(kDelayTime);
|
|
|
|
|
EXPECT_FALSE(m_input->Get()) << "The digital output was turned off, but "
|
|
|
|
|
<< "the digital input is on.";
|
2014-06-12 11:17:57 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
m_output->Set(true);
|
|
|
|
|
Wait(kDelayTime);
|
|
|
|
|
EXPECT_TRUE(m_input->Get()) << "The digital output was turned on, but "
|
|
|
|
|
<< "the digital input is off.";
|
2014-06-07 17:37:51 -04:00
|
|
|
}
|
2015-12-21 22:25:53 -08:00
|
|
|
/**
|
|
|
|
|
* Tests to see if the DIO PWM functionality works.
|
|
|
|
|
*/
|
|
|
|
|
TEST_F(DIOLoopTest, DIOPWM) {
|
|
|
|
|
Reset();
|
|
|
|
|
|
|
|
|
|
m_output->Set(false);
|
|
|
|
|
Wait(kDelayTime);
|
|
|
|
|
EXPECT_FALSE(m_input->Get()) << "The digital output was turned off, but "
|
|
|
|
|
<< "the digital input is on.";
|
|
|
|
|
|
2016-05-20 17:30:37 -07:00
|
|
|
// Set frequency to 2.0 Hz
|
2015-12-21 22:25:53 -08:00
|
|
|
m_output->SetPWMRate(2.0);
|
2016-05-20 17:30:37 -07:00
|
|
|
// Enable PWM, but leave it off
|
2015-12-21 22:25:53 -08:00
|
|
|
m_output->EnablePWM(0.0);
|
|
|
|
|
Wait(0.5);
|
|
|
|
|
m_output->UpdateDutyCycle(0.5);
|
|
|
|
|
m_input->RequestInterrupts();
|
|
|
|
|
m_input->SetUpSourceEdge(false, true);
|
2016-05-20 17:30:37 -07:00
|
|
|
InterruptableSensorBase::WaitResult result =
|
|
|
|
|
m_input->WaitForInterrupt(3.0, true);
|
2015-12-21 22:25:53 -08:00
|
|
|
|
|
|
|
|
Wait(0.5);
|
|
|
|
|
bool firstCycle = m_input->Get();
|
|
|
|
|
Wait(0.5);
|
|
|
|
|
bool secondCycle = m_input->Get();
|
|
|
|
|
Wait(0.5);
|
|
|
|
|
bool thirdCycle = m_input->Get();
|
|
|
|
|
Wait(0.5);
|
|
|
|
|
bool forthCycle = m_input->Get();
|
|
|
|
|
Wait(0.5);
|
|
|
|
|
bool fifthCycle = m_input->Get();
|
|
|
|
|
Wait(0.5);
|
|
|
|
|
bool sixthCycle = m_input->Get();
|
|
|
|
|
Wait(0.5);
|
|
|
|
|
bool seventhCycle = m_input->Get();
|
|
|
|
|
m_output->DisablePWM();
|
|
|
|
|
Wait(0.5);
|
|
|
|
|
bool firstAfterStop = m_input->Get();
|
|
|
|
|
Wait(0.5);
|
|
|
|
|
bool secondAfterStop = m_input->Get();
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(InterruptableSensorBase::WaitResult::kFallingEdge, result)
|
2016-05-20 17:30:37 -07:00
|
|
|
<< "WaitForInterrupt was not falling.";
|
2015-12-21 22:25:53 -08:00
|
|
|
|
|
|
|
|
EXPECT_FALSE(firstCycle) << "Input not low after first delay";
|
|
|
|
|
EXPECT_TRUE(secondCycle) << "Input not high after second delay";
|
|
|
|
|
EXPECT_FALSE(thirdCycle) << "Input not low after third delay";
|
|
|
|
|
EXPECT_TRUE(forthCycle) << "Input not high after forth delay";
|
|
|
|
|
EXPECT_FALSE(fifthCycle) << "Input not low after fifth delay";
|
|
|
|
|
EXPECT_TRUE(sixthCycle) << "Input not high after sixth delay";
|
|
|
|
|
EXPECT_FALSE(seventhCycle) << "Input not low after seventh delay";
|
|
|
|
|
EXPECT_FALSE(firstAfterStop) << "Input not low after stopping first read";
|
|
|
|
|
EXPECT_FALSE(secondAfterStop) << "Input not low after stopping second read";
|
|
|
|
|
}
|
2014-06-07 17:37:51 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test a fake "counter" that uses the DIO loop as an input to make sure the
|
|
|
|
|
* Counter class works
|
|
|
|
|
*/
|
|
|
|
|
TEST_F(DIOLoopTest, FakeCounter) {
|
2015-06-25 15:07:55 -04:00
|
|
|
Reset();
|
2014-06-12 11:17:57 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
Counter counter(m_input);
|
2014-06-12 11:17:57 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
EXPECT_EQ(0, counter.Get()) << "Counter did not initialize to 0.";
|
2014-06-12 11:17:57 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
/* Count 100 ticks. The counter value should be 100 after this loop. */
|
|
|
|
|
for (int i = 0; i < 100; i++) {
|
|
|
|
|
m_output->Set(true);
|
|
|
|
|
Wait(kCounterTime);
|
|
|
|
|
m_output->Set(false);
|
|
|
|
|
Wait(kCounterTime);
|
|
|
|
|
}
|
2014-08-01 18:11:02 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
EXPECT_EQ(100, counter.Get()) << "Counter did not count up to 100.";
|
2014-06-07 17:37:51 -04:00
|
|
|
}
|
2014-08-01 18:11:02 -04:00
|
|
|
|
2016-05-20 17:30:37 -07:00
|
|
|
static void InterruptHandler(uint32_t interruptAssertedMask, void* param) {
|
|
|
|
|
*(int*)param = 12345;
|
2014-08-01 18:11:02 -04:00
|
|
|
}
|
|
|
|
|
|
2014-08-06 09:46:50 -04:00
|
|
|
TEST_F(DIOLoopTest, AsynchronousInterruptWorks) {
|
2015-06-25 15:07:55 -04:00
|
|
|
int param = 0;
|
2014-08-01 18:11:02 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
// Given an interrupt handler that sets an int to 12345
|
|
|
|
|
m_input->RequestInterrupts(InterruptHandler, ¶m);
|
|
|
|
|
m_input->EnableInterrupts();
|
2014-08-01 18:11:02 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
// If the voltage rises
|
|
|
|
|
m_output->Set(false);
|
|
|
|
|
m_output->Set(true);
|
|
|
|
|
m_input->CancelInterrupts();
|
2014-08-01 18:11:02 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
// Then the int should be 12345
|
|
|
|
|
Wait(kDelayTime);
|
|
|
|
|
EXPECT_EQ(12345, param) << "The interrupt did not run.";
|
2014-08-01 18:11:02 -04:00
|
|
|
}
|
2014-08-06 09:46:50 -04:00
|
|
|
|
2016-05-20 17:30:37 -07:00
|
|
|
static void* InterruptTriggerer(void* data) {
|
|
|
|
|
DigitalOutput* output = static_cast<DigitalOutput*>(data);
|
2015-06-25 15:07:55 -04:00
|
|
|
output->Set(false);
|
|
|
|
|
Wait(kSynchronousInterruptTime);
|
|
|
|
|
output->Set(true);
|
2015-06-23 04:49:51 -07:00
|
|
|
return nullptr;
|
2014-08-06 09:46:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(DIOLoopTest, SynchronousInterruptWorks) {
|
2015-06-25 15:07:55 -04:00
|
|
|
// Given a synchronous interrupt
|
|
|
|
|
m_input->RequestInterrupts();
|
|
|
|
|
|
|
|
|
|
// If we have another thread trigger the interrupt in a few seconds
|
|
|
|
|
pthread_t interruptTriggererLoop;
|
2016-05-20 17:30:37 -07:00
|
|
|
pthread_create(&interruptTriggererLoop, nullptr, InterruptTriggerer,
|
|
|
|
|
m_output);
|
2015-06-25 15:07:55 -04:00
|
|
|
|
|
|
|
|
// Then this thread should pause and resume after that number of seconds
|
|
|
|
|
Timer timer;
|
|
|
|
|
timer.Start();
|
|
|
|
|
m_input->WaitForInterrupt(kSynchronousInterruptTime + 1.0);
|
|
|
|
|
EXPECT_NEAR(kSynchronousInterruptTime, timer.Get(),
|
|
|
|
|
kSynchronousInterruptTimeTolerance);
|
2014-08-06 09:46:50 -04:00
|
|
|
}
|