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.
|
2016-01-02 03:02:34 -08:00
|
|
|
|
2014-08-04 14:19:01 -04:00
|
|
|
package edu.wpi.first.wpilibj;
|
|
|
|
|
|
2016-05-20 12:07:40 -04:00
|
|
|
import static org.hamcrest.Matchers.both;
|
|
|
|
|
import static org.hamcrest.Matchers.greaterThan;
|
|
|
|
|
import static org.hamcrest.Matchers.lessThan;
|
|
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
|
import static org.junit.Assert.assertThat;
|
|
|
|
|
|
2020-12-29 22:45:16 -08:00
|
|
|
import edu.wpi.first.wpilibj.test.AbstractComsSetup;
|
|
|
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
|
import java.util.concurrent.atomic.AtomicLong;
|
|
|
|
|
import org.junit.After;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
2014-08-04 14:19:01 -04:00
|
|
|
/**
|
2016-05-20 12:07:40 -04:00
|
|
|
* This class should not be run as a test explicitly. Instead it should be extended by tests that
|
2021-06-05 11:25:21 -07:00
|
|
|
* use DigitalSource.
|
2014-08-04 14:19:01 -04:00
|
|
|
*/
|
|
|
|
|
public abstract class AbstractInterruptTest extends AbstractComsSetup {
|
2021-06-05 11:25:21 -07:00
|
|
|
private DigitalSource m_source = null;
|
2015-06-25 15:07:55 -04:00
|
|
|
|
2021-06-05 11:25:21 -07:00
|
|
|
private DigitalSource getSource() {
|
|
|
|
|
if (m_source == null) {
|
|
|
|
|
m_source = giveSource();
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
2021-06-05 11:25:21 -07:00
|
|
|
return m_source;
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@After
|
|
|
|
|
public void interruptTeardown() {
|
2021-06-05 11:25:21 -07:00
|
|
|
if (m_source != null) {
|
|
|
|
|
freeSource();
|
|
|
|
|
m_source = null;
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-05 11:25:21 -07:00
|
|
|
/** Give the sensor source that interrupts can be attached to. */
|
|
|
|
|
abstract DigitalSource giveSource();
|
2015-06-25 15:07:55 -04:00
|
|
|
|
2021-06-05 11:25:21 -07:00
|
|
|
/** Cleans up the sensor source. This is only called if {@link #giveSource()} is called. */
|
|
|
|
|
abstract void freeSource();
|
2015-06-25 15:07:55 -04:00
|
|
|
|
2020-12-29 22:45:16 -08:00
|
|
|
/** Perform whatever action is required to set the interrupt high. */
|
2015-06-25 15:07:55 -04:00
|
|
|
abstract void setInterruptHigh();
|
|
|
|
|
|
2020-12-29 22:45:16 -08:00
|
|
|
/** Perform whatever action is required to set the interrupt low. */
|
2015-06-25 15:07:55 -04:00
|
|
|
abstract void setInterruptLow();
|
|
|
|
|
|
|
|
|
|
@Test(timeout = 1000)
|
|
|
|
|
public void testSingleInterruptsTriggering() throws Exception {
|
|
|
|
|
// Given
|
2021-06-05 11:25:21 -07:00
|
|
|
// final InterruptCounter counter = new InterruptCounter();
|
|
|
|
|
// TestInterruptHandlerFunction function = new
|
|
|
|
|
// TestInterruptHandlerFunction(counter);
|
|
|
|
|
|
|
|
|
|
AtomicBoolean hasFired = new AtomicBoolean(false);
|
|
|
|
|
AtomicInteger counter = new AtomicInteger(0);
|
|
|
|
|
AtomicLong interruptFireTime = new AtomicLong();
|
|
|
|
|
|
|
|
|
|
try (AsynchronousInterrupt interrupt =
|
|
|
|
|
new AsynchronousInterrupt(
|
|
|
|
|
getSource(),
|
|
|
|
|
(a, b) -> {
|
|
|
|
|
interruptFireTime.set(RobotController.getFPGATime());
|
|
|
|
|
hasFired.set(true);
|
|
|
|
|
counter.incrementAndGet();
|
|
|
|
|
})) {
|
|
|
|
|
interrupt.enable();
|
|
|
|
|
setInterruptLow();
|
|
|
|
|
Timer.delay(0.01);
|
|
|
|
|
final long interruptTriggerTime = RobotController.getFPGATime();
|
|
|
|
|
setInterruptHigh();
|
|
|
|
|
while (!hasFired.get()) {
|
|
|
|
|
Timer.delay(0.005);
|
|
|
|
|
}
|
2015-06-25 15:07:55 -04:00
|
|
|
|
2021-06-05 11:25:21 -07:00
|
|
|
assertEquals("The interrupt did not fire the expected number of times", 1, counter.get());
|
|
|
|
|
|
|
|
|
|
final long range = 10000; // in microseconds
|
|
|
|
|
assertThat(
|
|
|
|
|
"The interrupt did not fire within the expected time period (values in milliseconds)",
|
|
|
|
|
interruptFireTime.get(),
|
|
|
|
|
both(greaterThan(interruptTriggerTime - range))
|
|
|
|
|
.and(lessThan(interruptTriggerTime + range)));
|
|
|
|
|
assertThat(
|
|
|
|
|
"The readRisingTimestamp() did not return the correct value (values in seconds)",
|
|
|
|
|
interrupt.getRisingTimestamp(),
|
|
|
|
|
both(greaterThan((interruptTriggerTime - range) / 1e6))
|
|
|
|
|
.and(lessThan((interruptTriggerTime + range) / 1e6)));
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-01 09:11:52 -08:00
|
|
|
@Test(timeout = 2000)
|
2019-08-01 01:19:48 -04:00
|
|
|
public void testMultipleInterruptsTriggering() {
|
2021-06-05 11:25:21 -07:00
|
|
|
AtomicBoolean hasFired = new AtomicBoolean(false);
|
|
|
|
|
AtomicInteger counter = new AtomicInteger(0);
|
|
|
|
|
|
|
|
|
|
try (AsynchronousInterrupt interrupt =
|
|
|
|
|
new AsynchronousInterrupt(
|
|
|
|
|
getSource(),
|
|
|
|
|
(a, b) -> {
|
|
|
|
|
hasFired.set(true);
|
|
|
|
|
counter.incrementAndGet();
|
|
|
|
|
})) {
|
|
|
|
|
interrupt.enable();
|
|
|
|
|
|
|
|
|
|
final int fireCount = 50;
|
|
|
|
|
for (int i = 0; i < fireCount; i++) {
|
|
|
|
|
setInterruptLow();
|
|
|
|
|
setInterruptHigh();
|
|
|
|
|
// Wait for the interrupt to complete before moving on
|
|
|
|
|
while (!hasFired.getAndSet(false)) {
|
|
|
|
|
Timer.delay(0.005);
|
|
|
|
|
}
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
2021-06-05 11:25:21 -07:00
|
|
|
// Then
|
|
|
|
|
assertEquals(
|
|
|
|
|
"The interrupt did not fire the expected number of times", fireCount, counter.get());
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-29 22:45:16 -08:00
|
|
|
/** The timeout length for this test in seconds. */
|
2015-06-25 15:07:55 -04:00
|
|
|
private static final int synchronousTimeout = 5;
|
|
|
|
|
|
|
|
|
|
@Test(timeout = (long) (synchronousTimeout * 1e3))
|
|
|
|
|
public void testSynchronousInterruptsTriggering() {
|
2021-06-05 11:25:21 -07:00
|
|
|
try (SynchronousInterrupt interrupt = new SynchronousInterrupt(getSource())) {
|
|
|
|
|
final double synchronousDelay = synchronousTimeout / 2.0;
|
|
|
|
|
final Runnable runnable =
|
|
|
|
|
() -> {
|
|
|
|
|
Timer.delay(synchronousDelay);
|
|
|
|
|
setInterruptLow();
|
|
|
|
|
setInterruptHigh();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// When
|
|
|
|
|
|
|
|
|
|
// Note: the long time value is used because doubles can flip if the robot
|
|
|
|
|
// is left running for long enough
|
|
|
|
|
final long startTimeStamp = RobotController.getFPGATime();
|
|
|
|
|
new Thread(runnable).start();
|
|
|
|
|
// Delay for twice as long as the timeout so the test should fail first
|
|
|
|
|
interrupt.waitForInterrupt(synchronousTimeout * 2);
|
|
|
|
|
final long stopTimeStamp = RobotController.getFPGATime();
|
|
|
|
|
|
|
|
|
|
// Then
|
|
|
|
|
// The test will not have timed out and:
|
|
|
|
|
final double interruptRunTime = (stopTimeStamp - startTimeStamp) * 1e-6;
|
|
|
|
|
assertEquals(
|
|
|
|
|
"The interrupt did not run for the expected amount of time (units in seconds)",
|
|
|
|
|
synchronousDelay,
|
|
|
|
|
interruptRunTime,
|
|
|
|
|
0.1);
|
|
|
|
|
}
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
|
2015-12-24 21:10:47 -08:00
|
|
|
@Test(timeout = (long) (synchronousTimeout * 1e3))
|
|
|
|
|
public void testSynchronousInterruptsWaitResultTimeout() {
|
2021-06-05 11:25:21 -07:00
|
|
|
try (SynchronousInterrupt interrupt = new SynchronousInterrupt(getSource())) {
|
|
|
|
|
SynchronousInterrupt.WaitResult result = interrupt.waitForInterrupt(synchronousTimeout / 2);
|
2015-12-24 21:10:47 -08:00
|
|
|
|
2021-06-05 11:25:21 -07:00
|
|
|
assertEquals(
|
|
|
|
|
"The interrupt did not time out correctly.",
|
|
|
|
|
result,
|
|
|
|
|
SynchronousInterrupt.WaitResult.kTimeout);
|
|
|
|
|
}
|
2015-12-24 21:10:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test(timeout = (long) (synchronousTimeout * 1e3))
|
|
|
|
|
public void testSynchronousInterruptsWaitResultRisingEdge() {
|
2021-06-05 11:25:21 -07:00
|
|
|
try (SynchronousInterrupt interrupt = new SynchronousInterrupt(getSource())) {
|
|
|
|
|
final double synchronousDelay = synchronousTimeout / 2.0;
|
|
|
|
|
final Runnable runnable =
|
|
|
|
|
() -> {
|
|
|
|
|
Timer.delay(synchronousDelay);
|
|
|
|
|
setInterruptLow();
|
|
|
|
|
setInterruptHigh();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
new Thread(runnable).start();
|
|
|
|
|
// Delay for twice as long as the timeout so the test should fail first
|
|
|
|
|
SynchronousInterrupt.WaitResult result = interrupt.waitForInterrupt(synchronousTimeout * 2);
|
|
|
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
|
"The interrupt did not fire on the rising edge.",
|
|
|
|
|
result,
|
|
|
|
|
SynchronousInterrupt.WaitResult.kRisingEdge);
|
|
|
|
|
}
|
2015-12-24 21:10:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test(timeout = (long) (synchronousTimeout * 1e3))
|
|
|
|
|
public void testSynchronousInterruptsWaitResultFallingEdge() {
|
2021-06-05 11:25:21 -07:00
|
|
|
try (SynchronousInterrupt interrupt = new SynchronousInterrupt(getSource())) {
|
|
|
|
|
// Given
|
|
|
|
|
interrupt.setInterruptEdges(false, true);
|
|
|
|
|
|
|
|
|
|
final double synchronousDelay = synchronousTimeout / 2.0;
|
|
|
|
|
final Runnable runnable =
|
|
|
|
|
() -> {
|
|
|
|
|
Timer.delay(synchronousDelay);
|
|
|
|
|
setInterruptHigh();
|
|
|
|
|
setInterruptLow();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
new Thread(runnable).start();
|
|
|
|
|
// Delay for twice as long as the timeout so the test should fail first
|
|
|
|
|
SynchronousInterrupt.WaitResult result = interrupt.waitForInterrupt(synchronousTimeout * 2);
|
|
|
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
|
"The interrupt did not fire on the falling edge.",
|
|
|
|
|
result,
|
|
|
|
|
SynchronousInterrupt.WaitResult.kFallingEdge);
|
|
|
|
|
}
|
2015-12-24 21:10:47 -08:00
|
|
|
}
|
|
|
|
|
|
2015-11-01 09:11:52 -08:00
|
|
|
@Test(timeout = 4000)
|
2015-06-25 15:07:55 -04:00
|
|
|
public void testDisableStopsInterruptFiring() {
|
2021-06-05 11:25:21 -07:00
|
|
|
AtomicBoolean interruptComplete = new AtomicBoolean(false);
|
|
|
|
|
AtomicInteger counter = new AtomicInteger(0);
|
|
|
|
|
try (AsynchronousInterrupt interrupt =
|
|
|
|
|
new AsynchronousInterrupt(
|
|
|
|
|
getSource(),
|
|
|
|
|
(a, b) -> {
|
|
|
|
|
interruptComplete.set(true);
|
|
|
|
|
counter.incrementAndGet();
|
|
|
|
|
})) {
|
|
|
|
|
interrupt.enable();
|
|
|
|
|
final int fireCount = 50;
|
|
|
|
|
for (int i = 0; i < fireCount; i++) {
|
|
|
|
|
setInterruptLow();
|
|
|
|
|
setInterruptHigh();
|
|
|
|
|
// Wait for the interrupt to complete before moving on
|
|
|
|
|
while (!interruptComplete.getAndSet(false)) {
|
|
|
|
|
Timer.delay(0.005);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
interrupt.disable();
|
|
|
|
|
for (int i = 0; i < fireCount; i++) {
|
|
|
|
|
setInterruptLow();
|
|
|
|
|
setInterruptHigh();
|
|
|
|
|
// Just wait because the interrupt should not fire
|
2019-11-20 23:13:15 -05:00
|
|
|
Timer.delay(0.005);
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
|
2021-06-05 11:25:21 -07:00
|
|
|
assertEquals(
|
|
|
|
|
"The interrupt did not fire the expected number of times", fireCount, counter.get());
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
}
|
2014-08-04 14:19:01 -04:00
|
|
|
}
|