[commands] Enhance Command Sendable implementations (#4822)

This commit is contained in:
Starlight220
2022-12-16 04:28:52 +02:00
committed by GitHub
parent 7713f68772
commit fbabd0ef15
33 changed files with 239 additions and 17 deletions

View File

@@ -446,14 +446,15 @@ public interface Command {
default void setName(String name) {}
/**
* Decorates this Command with a name. Is an inline function for #setName(String);
* Decorates this Command with a name.
*
* @param name name
* @return the decorated Command
*/
default Command withName(String name) {
this.setName(name);
return this;
default WrapperCommand withName(String name) {
WrapperCommand wrapper = new WrapperCommand(Command.this) {};
wrapper.setName(name);
return wrapper;
}
/**

View File

@@ -56,12 +56,6 @@ public abstract class CommandBase implements Sendable, Command {
SendableRegistry.setName(this, name);
}
@Override
public CommandBase withName(String name) {
this.setName(name);
return this;
}
/**
* Gets the subsystem name of this Command.
*
@@ -105,5 +99,8 @@ public abstract class CommandBase implements Sendable, Command {
});
builder.addBooleanProperty(
".isParented", () -> CommandScheduler.getInstance().isComposed(this), null);
builder.addStringProperty(
"interruptBehavior", () -> getInterruptionBehavior().toString(), null);
builder.addBooleanProperty("runsWhenDisabled", this::runsWhenDisabled, null);
}
}

View File

@@ -6,6 +6,7 @@ package edu.wpi.first.wpilibj2.command;
import static edu.wpi.first.util.ErrorMessages.requireNonNullParam;
import edu.wpi.first.util.sendable.SendableBuilder;
import java.util.function.BooleanSupplier;
/**
@@ -71,4 +72,21 @@ public class ConditionalCommand extends CommandBase {
public boolean runsWhenDisabled() {
return m_onTrue.runsWhenDisabled() && m_onFalse.runsWhenDisabled();
}
@Override
public void initSendable(SendableBuilder builder) {
super.initSendable(builder);
builder.addStringProperty("onTrue", m_onTrue::getName, null);
builder.addStringProperty("onFalse", m_onFalse::getName, null);
builder.addStringProperty(
"selected",
() -> {
if (m_selectedCommand == null) {
return "null";
} else {
return m_selectedCommand.getName();
}
},
null);
}
}

View File

@@ -4,6 +4,7 @@
package edu.wpi.first.wpilibj2.command;
import edu.wpi.first.util.sendable.SendableBuilder;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@@ -130,4 +131,11 @@ public class ParallelDeadlineGroup extends CommandGroupBase {
public InterruptionBehavior getInterruptionBehavior() {
return m_interruptBehavior;
}
@Override
public void initSendable(SendableBuilder builder) {
super.initSendable(builder);
builder.addStringProperty("deadline", m_deadline::getName, null);
}
}

View File

@@ -6,6 +6,7 @@ package edu.wpi.first.wpilibj2.command;
import static edu.wpi.first.util.ErrorMessages.requireNonNullParam;
import edu.wpi.first.util.sendable.SendableBuilder;
import java.util.function.Supplier;
/**
@@ -36,6 +37,7 @@ public class ProxyCommand extends CommandBase {
*/
public ProxyCommand(Command command) {
this(() -> command);
setName("Proxy(" + command.getName() + ")");
}
@Override
@@ -73,4 +75,11 @@ public class ProxyCommand extends CommandBase {
public boolean runsWhenDisabled() {
return true;
}
@Override
public void initSendable(SendableBuilder builder) {
super.initSendable(builder);
builder.addStringProperty(
"proxied", () -> m_command == null ? "null" : m_command.getName(), null);
}
}

View File

@@ -14,6 +14,7 @@ import edu.wpi.first.math.kinematics.ChassisSpeeds;
import edu.wpi.first.math.kinematics.DifferentialDriveKinematics;
import edu.wpi.first.math.kinematics.DifferentialDriveWheelSpeeds;
import edu.wpi.first.math.trajectory.Trajectory;
import edu.wpi.first.util.sendable.SendableBuilder;
import edu.wpi.first.wpilibj.Timer;
import java.util.function.BiConsumer;
import java.util.function.Supplier;
@@ -211,4 +212,11 @@ public class RamseteCommand extends CommandBase {
public boolean isFinished() {
return m_timer.hasElapsed(m_trajectory.getTotalTimeSeconds());
}
@Override
public void initSendable(SendableBuilder builder) {
super.initSendable(builder);
builder.addDoubleProperty("leftVelocity", () -> m_prevSpeeds.leftMetersPerSecond, null);
builder.addDoubleProperty("rightVelocity", () -> m_prevSpeeds.rightMetersPerSecond, null);
}
}

View File

@@ -6,6 +6,8 @@ package edu.wpi.first.wpilibj2.command;
import static edu.wpi.first.util.ErrorMessages.requireNonNullParam;
import edu.wpi.first.util.sendable.SendableBuilder;
/**
* A command that runs another command repeatedly, restarting it when it ends, until this command is
* interrupted. Command instances that are passed to it cannot be added to any other groups, or
@@ -31,6 +33,7 @@ public class RepeatCommand extends CommandBase {
m_command = requireNonNullParam(command, "command", "RepeatCommand");
CommandScheduler.getInstance().registerComposedCommands(command);
m_requirements.addAll(command.getRequirements());
setName("Repeat(" + command.getName() + ")");
}
@Override
@@ -72,4 +75,10 @@ public class RepeatCommand extends CommandBase {
public InterruptionBehavior getInterruptionBehavior() {
return m_command.getInterruptionBehavior();
}
@Override
public void initSendable(SendableBuilder builder) {
super.initSendable(builder);
builder.addStringProperty("command", m_command::getName, null);
}
}

View File

@@ -6,6 +6,7 @@ package edu.wpi.first.wpilibj2.command;
import static edu.wpi.first.util.ErrorMessages.requireNonNullParam;
import edu.wpi.first.util.sendable.SendableBuilder;
import java.util.Map;
import java.util.function.Supplier;
@@ -108,4 +109,12 @@ public class SelectCommand extends CommandBase {
public InterruptionBehavior getInterruptionBehavior() {
return m_interruptBehavior;
}
@Override
public void initSendable(SendableBuilder builder) {
super.initSendable(builder);
builder.addStringProperty(
"selected", () -> m_selectedCommand == null ? "null" : m_selectedCommand.getName(), null);
}
}

View File

@@ -4,6 +4,7 @@
package edu.wpi.first.wpilibj2.command;
import edu.wpi.first.util.sendable.SendableBuilder;
import java.util.ArrayList;
import java.util.List;
@@ -104,4 +105,11 @@ public class SequentialCommandGroup extends CommandGroupBase {
public InterruptionBehavior getInterruptionBehavior() {
return m_interruptBehavior;
}
@Override
public void initSendable(SendableBuilder builder) {
super.initSendable(builder);
builder.addIntegerProperty("index", () -> m_currentCommandIndex, null);
}
}

View File

@@ -4,6 +4,7 @@
package edu.wpi.first.wpilibj2.command;
import edu.wpi.first.util.sendable.SendableBuilder;
import edu.wpi.first.util.sendable.SendableRegistry;
import edu.wpi.first.wpilibj.Timer;
@@ -47,4 +48,10 @@ public class WaitCommand extends CommandBase {
public boolean runsWhenDisabled() {
return true;
}
@Override
public void initSendable(SendableBuilder builder) {
super.initSendable(builder);
builder.addDoubleProperty("duration", () -> m_duration, null);
}
}

View File

@@ -26,6 +26,8 @@ public abstract class WrapperCommand extends CommandBase {
protected WrapperCommand(Command command) {
CommandScheduler.getInstance().registerComposedCommands(command);
m_command = command;
// copy the wrapped command's name
setName(command.getName());
}
/** The initial subroutine of a command. Called once when the command is initially scheduled. */