diff --git a/wpilibc/shared/include/Commands/Command.h b/wpilibc/shared/include/Commands/Command.h index 008411b642..a3d7beb979 100644 --- a/wpilibc/shared/include/Commands/Command.h +++ b/wpilibc/shared/include/Commands/Command.h @@ -81,45 +81,13 @@ class Command : public ErrorBase, public NamedSendable, public ITableListener { bool IsTimedOut() const; bool AssertUnlocked(const std::string& message); void SetParent(CommandGroup* parent); - /** - * The initialize method is called the first time this Command is run after - * being started. - */ - virtual void Initialize() = 0; - /** - * The execute method is called repeatedly until this Command either finishes - * or is canceled. - */ - virtual void Execute() = 0; - /** - * Returns whether this command is finished. - * If it is, then the command will be removed and {@link Command#end() end()} - * will be called. - * - *
It may be useful for a team to reference the {@link Command#isTimedOut() - * isTimedOut()} method for time-sensitive commands.
- * @return whether this command is finished. - * @see Command#isTimedOut() isTimedOut() - */ - virtual bool IsFinished() = 0; - /** - * Called when the command ended peacefully. This is where you may want - * to wrap up loose ends, like shutting off a motor that was being used - * in the command. - */ - virtual void End() = 0; - /** - * Called when the command ends because somebody called - * {@link Command#cancel() cancel()} or another command shared the same - * requirements as this one, and booted it out. - * - *This is where you may want to wrap up loose ends, like shutting off a - * motor that was being used in the command.
- * - *Generally, it is useful to simply call the {@link Command#end() end()} - * method within this method
- */ - virtual void Interrupted() = 0; + + virtual void Initialize(); + virtual void Execute(); + virtual bool IsFinished(); + virtual void End(); + virtual void Interrupted(); + virtual void _Initialize(); virtual void _Interrupted(); virtual void _Execute(); diff --git a/wpilibc/shared/include/Commands/InstantCommand.h b/wpilibc/shared/include/Commands/InstantCommand.h new file mode 100644 index 0000000000..b0d96d93d4 --- /dev/null +++ b/wpilibc/shared/include/Commands/InstantCommand.h @@ -0,0 +1,26 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) FIRST 2016. All Rights Reserved. */ +/* 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. */ +/*----------------------------------------------------------------------------*/ + +#pragma once + +#includeIt may be useful for a team to reference the {@link Command#isTimedOut() + * isTimedOut()} method for time-sensitive commands.
+ * + *By default this will always return false, which means it will never end + * automatically. It may still be cancelled manually or interrupted by another + * command. For most real-world scenarios you will override this method with + * additional logic.
+ * + * @return whether this command is finished. + * @see Command#isTimedOut() isTimedOut() + */ +bool Command::IsFinished() { return false; } + +/** + * Called when the command ends because somebody called + * {@link Command#cancel() cancel()} or another command shared the same + * requirements as this one, and booted it out. + * + *This is where you may want to wrap up loose ends, like shutting off a + * motor that was being used in the command.
+ * + *Generally, it is useful to simply call the {@link Command#end() end()} + * method within this method, as done here.
+ */ +void Command::Interrupted() { End(); } + void Command::_Initialize() {} void Command::_Interrupted() {} diff --git a/wpilibc/shared/src/Commands/InstantCommand.cpp b/wpilibc/shared/src/Commands/InstantCommand.cpp new file mode 100644 index 0000000000..e1e57f97b1 --- /dev/null +++ b/wpilibc/shared/src/Commands/InstantCommand.cpp @@ -0,0 +1,14 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) FIRST 2016. All Rights Reserved. */ +/* 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. */ +/*----------------------------------------------------------------------------*/ + +#include "Commands/InstantCommand.h" + +using namespace frc; + +InstantCommand::InstantCommand(const std::string& name) : Command(name) {} + +bool InstantCommand::IsFinished() { return true; } diff --git a/wpilibc/shared/src/Commands/PrintCommand.cpp b/wpilibc/shared/src/Commands/PrintCommand.cpp index d1e921ccc8..2d93b21c84 100644 --- a/wpilibc/shared/src/Commands/PrintCommand.cpp +++ b/wpilibc/shared/src/Commands/PrintCommand.cpp @@ -7,25 +7,13 @@ #include "Commands/PrintCommand.h" -#includeIt may be useful for a team to reference the {@link Command#isTimedOut() isTimedOut()} * method for time-sensitive commands. * + *
By default this will always return false, which means it will never end automatically. It + * may still be cancelled manually or interrupted by another command. For most real-world + * scenarios you will override this method with additional logic. + * * @return whether this command is finished. * @see Command#isTimedOut() isTimedOut() */ - protected abstract boolean isFinished(); + protected boolean isFinished() { + return false; + } /** * Called when the command ended peacefully. This is where you may want to wrap up loose ends, * like shutting off a motor that was being used in the command. */ - protected abstract void end(); + protected void end() {} /** * A shadow method called after {@link Command#end() end()}. @@ -303,16 +309,17 @@ public abstract class Command implements NamedSendable { * used in the command. * *
Generally, it is useful to simply call the {@link Command#end() end()} method within this - * method. + * method, as done here. */ - protected abstract void interrupted(); + protected void interrupted() { + end(); + } /** * A shadow method called after {@link Command#interrupted() interrupted()}. */ @SuppressWarnings("MethodName") - void _interrupted() { - } + void _interrupted() {} /** * Called to indicate that the timer should start. This is called right before {@link diff --git a/wpilibj/src/shared/java/edu/wpi/first/wpilibj/command/InstantCommand.java b/wpilibj/src/shared/java/edu/wpi/first/wpilibj/command/InstantCommand.java new file mode 100644 index 0000000000..6c6bee6394 --- /dev/null +++ b/wpilibj/src/shared/java/edu/wpi/first/wpilibj/command/InstantCommand.java @@ -0,0 +1,21 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) FIRST 2016. All Rights Reserved. */ +/* 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. */ +/*----------------------------------------------------------------------------*/ + +package edu.wpi.first.wpilibj.command; + +public class InstantCommand extends Command { + public InstantCommand() { + } + + public InstantCommand(String name) { + super(name); + } + + protected boolean isFinished() { + return true; + } +} diff --git a/wpilibj/src/shared/java/edu/wpi/first/wpilibj/command/PrintCommand.java b/wpilibj/src/shared/java/edu/wpi/first/wpilibj/command/PrintCommand.java index fbbe8edb51..fdedc2b418 100644 --- a/wpilibj/src/shared/java/edu/wpi/first/wpilibj/command/PrintCommand.java +++ b/wpilibj/src/shared/java/edu/wpi/first/wpilibj/command/PrintCommand.java @@ -12,7 +12,7 @@ package edu.wpi.first.wpilibj.command; * immediately finishes. It is useful if you want a {@link CommandGroup} to print out a string when * it reaches a certain point. */ -public class PrintCommand extends Command { +public class PrintCommand extends InstantCommand { /** * The message to print out. @@ -32,17 +32,4 @@ public class PrintCommand extends Command { protected void initialize() { System.out.println(m_message); } - - protected void execute() { - } - - protected boolean isFinished() { - return true; - } - - protected void end() { - } - - protected void interrupted() { - } } diff --git a/wpilibj/src/shared/java/edu/wpi/first/wpilibj/command/StartCommand.java b/wpilibj/src/shared/java/edu/wpi/first/wpilibj/command/StartCommand.java index ada81fa4d5..607084697e 100644 --- a/wpilibj/src/shared/java/edu/wpi/first/wpilibj/command/StartCommand.java +++ b/wpilibj/src/shared/java/edu/wpi/first/wpilibj/command/StartCommand.java @@ -11,7 +11,7 @@ package edu.wpi.first.wpilibj.command; * A {@link StartCommand} will call the {@link Command#start() start()} method of another command * when it is initialized and will finish immediately. */ -public class StartCommand extends Command { +public class StartCommand extends InstantCommand { /** * The command to fork. @@ -32,17 +32,4 @@ public class StartCommand extends Command { protected void initialize() { m_commandToFork.start(); } - - protected void execute() { - } - - protected boolean isFinished() { - return true; - } - - protected void end() { - } - - protected void interrupted() { - } } diff --git a/wpilibj/src/shared/java/edu/wpi/first/wpilibj/command/TimedCommand.java b/wpilibj/src/shared/java/edu/wpi/first/wpilibj/command/TimedCommand.java new file mode 100644 index 0000000000..4e038c3b62 --- /dev/null +++ b/wpilibj/src/shared/java/edu/wpi/first/wpilibj/command/TimedCommand.java @@ -0,0 +1,22 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) FIRST 2016. All Rights Reserved. */ +/* 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. */ +/*----------------------------------------------------------------------------*/ + +package edu.wpi.first.wpilibj.command; + +public class TimedCommand extends Command { + public TimedCommand(String name, double timeout) { + super(name, timeout); + } + + public TimedCommand(double timeout) { + super(timeout); + } + + protected boolean isFinished() { + return isTimedOut(); + } +} diff --git a/wpilibj/src/shared/java/edu/wpi/first/wpilibj/command/WaitCommand.java b/wpilibj/src/shared/java/edu/wpi/first/wpilibj/command/WaitCommand.java index 906800094f..7bd3a589bf 100644 --- a/wpilibj/src/shared/java/edu/wpi/first/wpilibj/command/WaitCommand.java +++ b/wpilibj/src/shared/java/edu/wpi/first/wpilibj/command/WaitCommand.java @@ -13,7 +13,7 @@ package edu.wpi.first.wpilibj.command; * * @see CommandGroup */ -public class WaitCommand extends Command { +public class WaitCommand extends TimedCommand { /** * Instantiates a {@link WaitCommand} with the given timeout. @@ -33,20 +33,4 @@ public class WaitCommand extends Command { public WaitCommand(String name, double timeout) { super(name, timeout); } - - protected void initialize() { - } - - protected void execute() { - } - - protected boolean isFinished() { - return isTimedOut(); - } - - protected void end() { - } - - protected void interrupted() { - } } diff --git a/wpilibj/src/shared/java/edu/wpi/first/wpilibj/command/WaitForChildren.java b/wpilibj/src/shared/java/edu/wpi/first/wpilibj/command/WaitForChildren.java index 941faf4b68..3d361ba1cd 100644 --- a/wpilibj/src/shared/java/edu/wpi/first/wpilibj/command/WaitForChildren.java +++ b/wpilibj/src/shared/java/edu/wpi/first/wpilibj/command/WaitForChildren.java @@ -17,18 +17,6 @@ package edu.wpi.first.wpilibj.command; */ public class WaitForChildren extends Command { - protected void initialize() { - } - - protected void execute() { - } - - protected void end() { - } - - protected void interrupted() { - } - protected boolean isFinished() { return getGroup() == null || getGroup().m_children.isEmpty(); } diff --git a/wpilibj/src/shared/java/edu/wpi/first/wpilibj/command/WaitUntilCommand.java b/wpilibj/src/shared/java/edu/wpi/first/wpilibj/command/WaitUntilCommand.java index 4b7b659f15..49d2608e6f 100644 --- a/wpilibj/src/shared/java/edu/wpi/first/wpilibj/command/WaitUntilCommand.java +++ b/wpilibj/src/shared/java/edu/wpi/first/wpilibj/command/WaitUntilCommand.java @@ -22,22 +22,10 @@ public class WaitUntilCommand extends Command { m_time = time; } - public void initialize() { - } - - public void execute() { - } - /** * Check if we've reached the actual finish time. */ public boolean isFinished() { return Timer.getMatchTime() >= m_time; } - - public void end() { - } - - public void interrupted() { - } }