[command] Rename trigger methods (#4210)

Motivation

Feedback from 2022 showed that the Trigger API is rather confusing, mostly due to the following:
- duplicate Trigger and Button APIs were available; users were confused searching for a nonexistent difference between them.
- the when terminology was ambiguous and unclear whether it refers to the high state or specifically the rising edge.
- the Active terminology didn't unambiguously refer to the high state; it wasn't unintuitive to understand it as "when the binding is active/polled".
- whileHeld vs whenHeld was very confusing, and the difference between them wasn't obvious. The parallel Trigger verbs, whileActiveContinuously and whileActiveOnce are much less confusing.

Solution

Deprecating Button and its binding methods. The rationale for deprecating Button (and not Trigger) is because Button uses terminology that is needlessly more specific and restricting to the button use case, making the use case of arbitrary trigger conditions unintuitive.

After consideration, deprecation of Button's subclasses was decided against:

- NetworkButton (a trigger condition based on a boolean NT entry/topic) is a use case that is not necessarily intuitive for teams to implement themselves, so it is an abstraction that should be provided in the library. A parallel class for the BooleanEvent level, NetworkBooleanEvent, was also added as part of NT4. NT listeners were considered as a alternative solution, but they require attention to thread safety, and aren't interoperable with the EventLoop API.
- JoystickButton/POVButton provide abstractions around HID buttons. The new Trigger-returning factories on the HID classes are an equal (if not more concise) alternative, but there is no reason not to keep them for those who find their use preferable.

At a later date in the deprecation cycle (perhaps for 2024), when Button is removed, these subclasses should be changed to inherit directly from Trigger.

Trigger's bindings are changed to use True/False terminology, as it should be unambiguous. Each binding type has both True and False variants; for brevity, only the True variants are listed here:

- onTrue (replaces whenActive): schedule on rising edge.
- whileTrue (replaces whileActiveOnce): schedule on rising edge, cancel on falling edge.
- toggleOnTrue (replaces toggleWhenActive): on rising edge, schedule if unscheduled and cancel if scheduled.

Two binding types are completely deprecated:

- cancelWhenActive: this is a fairly niche use case which is better described as having the trigger's rising edge (Trigger.rising()) as an end condition for the command (using Command.until()).
- whileActiveContinuously: however common, this relied on the no-op behavior of scheduling an already-scheduled command. The more correct way to repeat the command if it ends before the falling edge is using Command.repeatedly/RepeatCommand or a RunCommand -- the only difference is if the command is interrupted, but that is more likely to result in two commands perpetually canceling each other than achieve the desired behavior. Manually implementing a blindly-scheduling binding like whileActiveContinuously is still possible, though might not be intuitive.

Notes

It was considered to share BooleanEvent's digital signal terminology; however, once it was decided that Trigger should not inherit from BooleanEvent (due to overload incompatibility) the common terminology was not worth the unintuitiveness stemming from users' unfamiliarity with the signal processing terms.
This commit is contained in:
Starlight220
2022-10-28 08:03:28 +03:00
committed by GitHub
parent 66157397c1
commit dcda09f90a
50 changed files with 1090 additions and 645 deletions

View File

@@ -1,220 +0,0 @@
// 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.
package edu.wpi.first.wpilibj2.command.button;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import edu.wpi.first.wpilibj.simulation.SimHooks;
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.CommandScheduler;
import edu.wpi.first.wpilibj2.command.CommandTestBase;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BooleanSupplier;
import org.junit.jupiter.api.Test;
class ButtonTest extends CommandTestBase {
@Test
void whenPressedTest() {
CommandScheduler scheduler = CommandScheduler.getInstance();
MockCommandHolder command1Holder = new MockCommandHolder(true);
Command command1 = command1Holder.getMock();
InternalButton button = new InternalButton();
button.setPressed(false);
button.whenPressed(command1);
scheduler.run();
verify(command1, never()).schedule();
button.setPressed(true);
scheduler.run();
scheduler.run();
verify(command1).schedule();
}
@Test
void whenReleasedTest() {
CommandScheduler scheduler = CommandScheduler.getInstance();
MockCommandHolder command1Holder = new MockCommandHolder(true);
Command command1 = command1Holder.getMock();
InternalButton button = new InternalButton();
button.setPressed(true);
button.whenReleased(command1);
scheduler.run();
verify(command1, never()).schedule();
button.setPressed(false);
scheduler.run();
scheduler.run();
verify(command1).schedule();
}
@Test
void whileHeldTest() {
CommandScheduler scheduler = CommandScheduler.getInstance();
MockCommandHolder command1Holder = new MockCommandHolder(true);
Command command1 = command1Holder.getMock();
InternalButton button = new InternalButton();
button.setPressed(false);
button.whileHeld(command1);
scheduler.run();
verify(command1, never()).schedule();
button.setPressed(true);
scheduler.run();
scheduler.run();
verify(command1, times(2)).schedule();
button.setPressed(false);
scheduler.run();
verify(command1).cancel();
}
@Test
void whenHeldTest() {
CommandScheduler scheduler = CommandScheduler.getInstance();
MockCommandHolder command1Holder = new MockCommandHolder(true);
Command command1 = command1Holder.getMock();
InternalButton button = new InternalButton();
button.setPressed(false);
button.whenHeld(command1);
scheduler.run();
verify(command1, never()).schedule();
button.setPressed(true);
scheduler.run();
scheduler.run();
verify(command1).schedule();
button.setPressed(false);
scheduler.run();
verify(command1).cancel();
}
@Test
void toggleWhenPressedTest() {
CommandScheduler scheduler = CommandScheduler.getInstance();
MockCommandHolder command1Holder = new MockCommandHolder(true);
Command command1 = command1Holder.getMock();
InternalButton button = new InternalButton();
button.setPressed(false);
button.toggleWhenPressed(command1);
scheduler.run();
verify(command1, never()).schedule();
button.setPressed(true);
scheduler.run();
when(command1.isScheduled()).thenReturn(true);
scheduler.run();
verify(command1).schedule();
button.setPressed(false);
scheduler.run();
verify(command1, never()).cancel();
button.setPressed(true);
scheduler.run();
verify(command1).cancel();
}
@Test
void cancelWhenPressedTest() {
CommandScheduler scheduler = CommandScheduler.getInstance();
MockCommandHolder command1Holder = new MockCommandHolder(true);
Command command1 = command1Holder.getMock();
InternalButton button = new InternalButton();
button.setPressed(false);
button.cancelWhenPressed(command1);
scheduler.run();
verify(command1, never()).cancel();
button.setPressed(true);
scheduler.run();
scheduler.run();
verify(command1).cancel();
}
@Test
void runnableBindingTest() {
InternalButton buttonWhenPressed = new InternalButton();
InternalButton buttonWhileHeld = new InternalButton();
InternalButton buttonWhenReleased = new InternalButton();
buttonWhenPressed.setPressed(false);
buttonWhileHeld.setPressed(true);
buttonWhenReleased.setPressed(true);
AtomicInteger counter = new AtomicInteger(0);
buttonWhenPressed.whenPressed(counter::incrementAndGet);
buttonWhileHeld.whileHeld(counter::incrementAndGet);
buttonWhenReleased.whenReleased(counter::incrementAndGet);
CommandScheduler scheduler = CommandScheduler.getInstance();
scheduler.run();
buttonWhenPressed.setPressed(true);
buttonWhenReleased.setPressed(false);
scheduler.run();
assertEquals(counter.get(), 4);
}
@Test
void buttonCompositionTest() {
InternalButton button1 = new InternalButton();
InternalButton button2 = new InternalButton();
button1.setPressed(true);
button2.setPressed(false);
assertFalse(button1.and(button2).getAsBoolean());
assertTrue(button1.or(button2).getAsBoolean());
assertFalse(button1.negate().getAsBoolean());
assertTrue(button1.and(button2.negate()).getAsBoolean());
}
@Test
void buttonCompositionSupplierTest() {
InternalButton button1 = new InternalButton();
BooleanSupplier booleanSupplier = () -> false;
button1.setPressed(true);
assertFalse(button1.and(booleanSupplier).getAsBoolean());
assertTrue(button1.or(booleanSupplier).getAsBoolean());
}
@Test
void debounceTest() {
CommandScheduler scheduler = CommandScheduler.getInstance();
MockCommandHolder commandHolder = new MockCommandHolder(true);
Command command = commandHolder.getMock();
InternalButton button = new InternalButton();
Trigger debounced = button.debounce(0.1);
debounced.whenActive(command);
button.setPressed(true);
scheduler.run();
verify(command, never()).schedule();
SimHooks.stepTiming(0.3);
button.setPressed(true);
scheduler.run();
verify(command).schedule();
}
@Test
void booleanSupplierTest() {
InternalButton button = new InternalButton();
assertFalse(button.getAsBoolean());
button.setPressed(true);
assertTrue(button.getAsBoolean());
}
}

View File

@@ -37,7 +37,7 @@ class NetworkButtonTest extends CommandTestBase {
var button = new NetworkButton(m_inst, "TestTable", "Test");
pub.set(false);
button.whenPressed(command);
button.onTrue(command);
scheduler.run();
verify(command, never()).schedule();
pub.set(true);

View File

@@ -0,0 +1,278 @@
// 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.
package edu.wpi.first.wpilibj2.command.button;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import edu.wpi.first.wpilibj.simulation.SimHooks;
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.CommandScheduler;
import edu.wpi.first.wpilibj2.command.CommandTestBase;
import edu.wpi.first.wpilibj2.command.FunctionalCommand;
import edu.wpi.first.wpilibj2.command.RunCommand;
import edu.wpi.first.wpilibj2.command.StartEndCommand;
import edu.wpi.first.wpilibj2.command.WaitUntilCommand;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BooleanSupplier;
import org.junit.jupiter.api.Test;
class TriggerTest extends CommandTestBase {
@Test
void onTrueTest() {
CommandScheduler scheduler = CommandScheduler.getInstance();
AtomicBoolean finished = new AtomicBoolean(false);
Command command1 = new WaitUntilCommand(finished::get);
InternalButton button = new InternalButton();
button.setPressed(false);
button.onTrue(command1);
scheduler.run();
assertFalse(command1.isScheduled());
button.setPressed(true);
scheduler.run();
assertTrue(command1.isScheduled());
finished.set(true);
scheduler.run();
assertFalse(command1.isScheduled());
}
@Test
void onFalseTest() {
CommandScheduler scheduler = CommandScheduler.getInstance();
AtomicBoolean finished = new AtomicBoolean(false);
Command command1 = new WaitUntilCommand(finished::get);
InternalButton button = new InternalButton();
button.setPressed(true);
button.onFalse(command1);
scheduler.run();
assertFalse(command1.isScheduled());
button.setPressed(false);
scheduler.run();
assertTrue(command1.isScheduled());
finished.set(true);
scheduler.run();
assertFalse(command1.isScheduled());
}
@Test
void whileTrueRepeatedlyTest() {
CommandScheduler scheduler = CommandScheduler.getInstance();
AtomicInteger inits = new AtomicInteger(0);
AtomicInteger counter = new AtomicInteger(0);
// the repeatedly() here is the point!
Command command1 =
new FunctionalCommand(
inits::incrementAndGet,
() -> {},
interrupted -> {},
() -> counter.incrementAndGet() % 2 == 0)
.repeatedly();
InternalButton button = new InternalButton();
button.setPressed(false);
button.whileTrue(command1);
scheduler.run();
assertEquals(0, inits.get());
button.setPressed(true);
scheduler.run();
assertEquals(1, inits.get());
scheduler.run();
assertEquals(2, inits.get());
button.setPressed(false);
scheduler.run();
assertEquals(2, inits.get());
}
@Test
void whileTrueLambdaRunTest() {
CommandScheduler scheduler = CommandScheduler.getInstance();
AtomicInteger counter = new AtomicInteger(0);
// the repeatedly() here is the point!
Command command1 = new RunCommand(counter::incrementAndGet);
InternalButton button = new InternalButton();
button.setPressed(false);
button.whileTrue(command1);
scheduler.run();
assertEquals(0, counter.get());
button.setPressed(true);
scheduler.run();
assertEquals(1, counter.get());
scheduler.run();
assertEquals(2, counter.get());
button.setPressed(false);
scheduler.run();
assertEquals(2, counter.get());
}
@Test
void whileTrueOnceTest() {
CommandScheduler scheduler = CommandScheduler.getInstance();
AtomicInteger startCounter = new AtomicInteger(0);
AtomicInteger endCounter = new AtomicInteger(0);
Command command1 =
new StartEndCommand(startCounter::incrementAndGet, endCounter::incrementAndGet);
InternalButton button = new InternalButton();
button.setPressed(false);
button.whileTrue(command1);
scheduler.run();
assertEquals(0, startCounter.get());
assertEquals(0, endCounter.get());
button.setPressed(true);
scheduler.run();
scheduler.run();
assertEquals(1, startCounter.get());
assertEquals(0, endCounter.get());
button.setPressed(false);
scheduler.run();
assertEquals(1, startCounter.get());
assertEquals(1, endCounter.get());
}
@Test
void toggleOnTrueTest() {
CommandScheduler scheduler = CommandScheduler.getInstance();
AtomicInteger startCounter = new AtomicInteger(0);
AtomicInteger endCounter = new AtomicInteger(0);
Command command1 =
new StartEndCommand(startCounter::incrementAndGet, endCounter::incrementAndGet);
InternalButton button = new InternalButton();
button.setPressed(false);
button.toggleOnTrue(command1);
scheduler.run();
assertEquals(0, startCounter.get());
assertEquals(0, endCounter.get());
button.setPressed(true);
scheduler.run();
scheduler.run();
assertEquals(1, startCounter.get());
assertEquals(0, endCounter.get());
button.setPressed(false);
scheduler.run();
assertEquals(1, startCounter.get());
assertEquals(0, endCounter.get());
button.setPressed(true);
scheduler.run();
assertEquals(1, startCounter.get());
assertEquals(1, endCounter.get());
}
@Test
void cancelWhenActiveTest() {
CommandScheduler scheduler = CommandScheduler.getInstance();
AtomicInteger startCounter = new AtomicInteger(0);
AtomicInteger endCounter = new AtomicInteger(0);
InternalButton button = new InternalButton();
Command command1 =
new StartEndCommand(startCounter::incrementAndGet, endCounter::incrementAndGet)
.until(button.rising());
button.setPressed(false);
command1.schedule();
scheduler.run();
assertEquals(1, startCounter.get());
assertEquals(0, endCounter.get());
button.setPressed(true);
scheduler.run();
assertEquals(1, startCounter.get());
assertEquals(1, endCounter.get());
scheduler.run();
assertEquals(1, startCounter.get());
assertEquals(1, endCounter.get());
}
// Binding runnables directly is deprecated -- users should create the command manually
@SuppressWarnings("deprecation")
@Test
void runnableBindingTest() {
InternalButton buttonWhenActive = new InternalButton();
InternalButton buttonWhileActiveContinuous = new InternalButton();
InternalButton buttonWhenInactive = new InternalButton();
buttonWhenActive.setPressed(false);
buttonWhileActiveContinuous.setPressed(true);
buttonWhenInactive.setPressed(true);
AtomicInteger counter = new AtomicInteger(0);
buttonWhenActive.whenPressed(counter::incrementAndGet);
buttonWhileActiveContinuous.whileActiveContinuous(counter::incrementAndGet);
buttonWhenInactive.whenInactive(counter::incrementAndGet);
CommandScheduler scheduler = CommandScheduler.getInstance();
scheduler.run();
buttonWhenActive.setPressed(true);
buttonWhenInactive.setPressed(false);
scheduler.run();
assertEquals(counter.get(), 4);
}
@Test
void triggerCompositionTest() {
InternalButton button1 = new InternalButton();
InternalButton button2 = new InternalButton();
button1.setPressed(true);
button2.setPressed(false);
assertFalse(button1.and(button2).getAsBoolean());
assertTrue(button1.or(button2).getAsBoolean());
assertFalse(button1.negate().getAsBoolean());
assertTrue(button1.and(button2.negate()).getAsBoolean());
}
@Test
void triggerCompositionSupplierTest() {
InternalButton button1 = new InternalButton();
BooleanSupplier booleanSupplier = () -> false;
button1.setPressed(true);
assertFalse(button1.and(booleanSupplier).getAsBoolean());
assertTrue(button1.or(booleanSupplier).getAsBoolean());
}
@Test
void debounceTest() {
CommandScheduler scheduler = CommandScheduler.getInstance();
MockCommandHolder commandHolder = new MockCommandHolder(true);
Command command = commandHolder.getMock();
InternalButton button = new InternalButton();
Trigger debounced = button.debounce(0.1);
debounced.onTrue(command);
button.setPressed(true);
scheduler.run();
verify(command, never()).schedule();
SimHooks.stepTiming(0.3);
button.setPressed(true);
scheduler.run();
verify(command).schedule();
}
@Test
void booleanSupplierTest() {
InternalButton button = new InternalButton();
assertFalse(button.getAsBoolean());
button.setPressed(true);
assertTrue(button.getAsBoolean());
}
}