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 + +#include + +#include "Commands/Command.h" + +namespace frc { + +class InstantCommand : public Command { + public: + explicit InstantCommand(const std::string& name); + InstantCommand() = default; + virtual ~InstantCommand() = default; + + protected: + virtual bool IsFinished(); +}; + +} // namespace frc diff --git a/wpilibc/shared/include/Commands/PrintCommand.h b/wpilibc/shared/include/Commands/PrintCommand.h index 779a743f6c..b66417c531 100644 --- a/wpilibc/shared/include/Commands/PrintCommand.h +++ b/wpilibc/shared/include/Commands/PrintCommand.h @@ -9,21 +9,17 @@ #include -#include "Commands/Command.h" +#include "Commands/InstantCommand.h" namespace frc { -class PrintCommand : public Command { +class PrintCommand : public InstantCommand { public: explicit PrintCommand(const std::string& message); virtual ~PrintCommand() = default; protected: virtual void Initialize(); - virtual void Execute(); - virtual bool IsFinished(); - virtual void End(); - virtual void Interrupted(); private: std::string m_message; diff --git a/wpilibc/shared/include/Commands/StartCommand.h b/wpilibc/shared/include/Commands/StartCommand.h index 1ce7277895..094f4825c6 100644 --- a/wpilibc/shared/include/Commands/StartCommand.h +++ b/wpilibc/shared/include/Commands/StartCommand.h @@ -7,21 +7,17 @@ #pragma once -#include "Commands/Command.h" +#include "Commands/InstantCommand.h" namespace frc { -class StartCommand : public Command { +class StartCommand : public InstantCommand { public: explicit StartCommand(Command* commandToStart); virtual ~StartCommand() = default; protected: virtual void Initialize(); - virtual void Execute(); - virtual bool IsFinished(); - virtual void End(); - virtual void Interrupted(); private: Command* m_commandToFork; diff --git a/wpilibc/shared/include/Commands/TimedCommand.h b/wpilibc/shared/include/Commands/TimedCommand.h new file mode 100644 index 0000000000..246e5fb311 --- /dev/null +++ b/wpilibc/shared/include/Commands/TimedCommand.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 + +#include + +#include "Commands/Command.h" + +namespace frc { + +class TimedCommand : public Command { + public: + TimedCommand(const std::string& name, double timeout); + explicit TimedCommand(double timeout); + virtual ~TimedCommand() = default; + + protected: + virtual bool IsFinished(); +}; + +} // namespace frc diff --git a/wpilibc/shared/include/Commands/WaitCommand.h b/wpilibc/shared/include/Commands/WaitCommand.h index bb70bcabae..28c45c7ce6 100644 --- a/wpilibc/shared/include/Commands/WaitCommand.h +++ b/wpilibc/shared/include/Commands/WaitCommand.h @@ -9,22 +9,15 @@ #include -#include "Commands/Command.h" +#include "Commands/TimedCommand.h" namespace frc { -class WaitCommand : public Command { +class WaitCommand : public TimedCommand { public: explicit WaitCommand(double timeout); WaitCommand(const std::string& name, double timeout); virtual ~WaitCommand() = default; - - protected: - virtual void Initialize(); - virtual void Execute(); - virtual bool IsFinished(); - virtual void End(); - virtual void Interrupted(); }; } // namespace frc diff --git a/wpilibc/shared/include/Commands/WaitForChildren.h b/wpilibc/shared/include/Commands/WaitForChildren.h index 882688594b..0ee1af279f 100644 --- a/wpilibc/shared/include/Commands/WaitForChildren.h +++ b/wpilibc/shared/include/Commands/WaitForChildren.h @@ -20,11 +20,7 @@ class WaitForChildren : public Command { virtual ~WaitForChildren() = default; protected: - virtual void Initialize(); - virtual void Execute(); virtual bool IsFinished(); - virtual void End(); - virtual void Interrupted(); }; } // namespace frc diff --git a/wpilibc/shared/include/Commands/WaitUntilCommand.h b/wpilibc/shared/include/Commands/WaitUntilCommand.h index 89755317f5..be1777848a 100644 --- a/wpilibc/shared/include/Commands/WaitUntilCommand.h +++ b/wpilibc/shared/include/Commands/WaitUntilCommand.h @@ -20,11 +20,7 @@ class WaitUntilCommand : public Command { virtual ~WaitUntilCommand() = default; protected: - virtual void Initialize(); - virtual void Execute(); virtual bool IsFinished(); - virtual void End(); - virtual void Interrupted(); private: double m_time; diff --git a/wpilibc/shared/src/Commands/Command.cpp b/wpilibc/shared/src/Commands/Command.cpp index 2a3fc16e00..b29ccd3e98 100644 --- a/wpilibc/shared/src/Commands/Command.cpp +++ b/wpilibc/shared/src/Commands/Command.cpp @@ -188,6 +188,56 @@ bool Command::Run() { return !IsFinished(); } +/** + * The initialize method is called the first time this Command is run after + * being started. + */ +void Command::Initialize() {} + +/** + * The execute method is called repeatedly until this Command either finishes + * or is canceled. + */ +void Command::Execute() {} + +/** + * 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. + */ +void Command::End() {} + +/** + * 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.

+ * + *

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" -#include -#include +#include using namespace frc; PrintCommand::PrintCommand(const std::string& message) - : Command(((std::stringstream&)(std::stringstream("Print \"") << message - << "\"")) - .str() - .c_str()) { + : InstantCommand("Print \"" + message + "\"") { m_message = message; } -void PrintCommand::Initialize() { std::printf("%s", m_message.c_str()); } - -void PrintCommand::Execute() {} - -bool PrintCommand::IsFinished() { return true; } - -void PrintCommand::End() {} - -void PrintCommand::Interrupted() {} +void PrintCommand::Initialize() { std::cout << m_message << "\n"; } diff --git a/wpilibc/shared/src/Commands/StartCommand.cpp b/wpilibc/shared/src/Commands/StartCommand.cpp index 070aafe04b..9671c6a75d 100644 --- a/wpilibc/shared/src/Commands/StartCommand.cpp +++ b/wpilibc/shared/src/Commands/StartCommand.cpp @@ -9,16 +9,9 @@ using namespace frc; -StartCommand::StartCommand(Command* commandToStart) : Command("StartCommand") { +StartCommand::StartCommand(Command* commandToStart) + : InstantCommand("StartCommand") { m_commandToFork = commandToStart; } void StartCommand::Initialize() { m_commandToFork->Start(); } - -void StartCommand::Execute() {} - -void StartCommand::End() {} - -void StartCommand::Interrupted() {} - -bool StartCommand::IsFinished() { return true; } diff --git a/wpilibc/shared/src/Commands/TimedCommand.cpp b/wpilibc/shared/src/Commands/TimedCommand.cpp new file mode 100644 index 0000000000..b93599db38 --- /dev/null +++ b/wpilibc/shared/src/Commands/TimedCommand.cpp @@ -0,0 +1,17 @@ +/*----------------------------------------------------------------------------*/ +/* 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/TimedCommand.h" + +using namespace frc; + +TimedCommand::TimedCommand(const std::string& name, double timeout) + : Command(name, timeout) {} + +TimedCommand::TimedCommand(double timeout) : Command(timeout) {} + +bool TimedCommand::IsFinished() { return IsTimedOut(); } diff --git a/wpilibc/shared/src/Commands/WaitCommand.cpp b/wpilibc/shared/src/Commands/WaitCommand.cpp index fcc8e40cdd..fee8d7f309 100644 --- a/wpilibc/shared/src/Commands/WaitCommand.cpp +++ b/wpilibc/shared/src/Commands/WaitCommand.cpp @@ -7,26 +7,10 @@ #include "Commands/WaitCommand.h" -#include - using namespace frc; WaitCommand::WaitCommand(double timeout) - : Command( - ((std::stringstream&)(std::stringstream("Wait(") << timeout << ")")) - .str() - .c_str(), - timeout) {} + : TimedCommand("Wait(" + std::to_string(timeout) + ")", timeout) {} WaitCommand::WaitCommand(const std::string& name, double timeout) - : Command(name, timeout) {} - -void WaitCommand::Initialize() {} - -void WaitCommand::Execute() {} - -bool WaitCommand::IsFinished() { return IsTimedOut(); } - -void WaitCommand::End() {} - -void WaitCommand::Interrupted() {} + : TimedCommand(name, timeout) {} diff --git a/wpilibc/shared/src/Commands/WaitForChildren.cpp b/wpilibc/shared/src/Commands/WaitForChildren.cpp index acbd10d482..ef75372cb8 100644 --- a/wpilibc/shared/src/Commands/WaitForChildren.cpp +++ b/wpilibc/shared/src/Commands/WaitForChildren.cpp @@ -17,14 +17,6 @@ WaitForChildren::WaitForChildren(double timeout) WaitForChildren::WaitForChildren(const std::string& name, double timeout) : Command(name, timeout) {} -void WaitForChildren::Initialize() {} - -void WaitForChildren::Execute() {} - -void WaitForChildren::End() {} - -void WaitForChildren::Interrupted() {} - bool WaitForChildren::IsFinished() { return GetGroup() == nullptr || GetGroup()->GetSize() == 0; } diff --git a/wpilibc/shared/src/Commands/WaitUntilCommand.cpp b/wpilibc/shared/src/Commands/WaitUntilCommand.cpp index ce79b56d5f..2915193c41 100644 --- a/wpilibc/shared/src/Commands/WaitUntilCommand.cpp +++ b/wpilibc/shared/src/Commands/WaitUntilCommand.cpp @@ -28,15 +28,7 @@ WaitUntilCommand::WaitUntilCommand(const std::string& name, double time) m_time = time; } -void WaitUntilCommand::Initialize() {} - -void WaitUntilCommand::Execute() {} - /** * Check if we've reached the actual finish time. */ bool WaitUntilCommand::IsFinished() { return Timer::GetMatchTime() >= m_time; } - -void WaitUntilCommand::End() {} - -void WaitUntilCommand::Interrupted() {} diff --git a/wpilibj/src/shared/java/edu/wpi/first/wpilibj/command/Command.java b/wpilibj/src/shared/java/edu/wpi/first/wpilibj/command/Command.java index c11bcef0f0..fc0dc2e87f 100644 --- a/wpilibj/src/shared/java/edu/wpi/first/wpilibj/command/Command.java +++ b/wpilibj/src/shared/java/edu/wpi/first/wpilibj/command/Command.java @@ -248,7 +248,7 @@ public abstract class Command implements NamedSendable { /** * The initialize method is called the first time this Command is run after being started. */ - protected abstract void initialize(); + protected void initialize() {} /** * A shadow method called before {@link Command#initialize() initialize()}. @@ -261,7 +261,7 @@ public abstract class Command implements NamedSendable { * The execute method is called repeatedly until this Command either finishes or is canceled. */ @SuppressWarnings("MethodName") - protected abstract void execute(); + protected void execute() {} /** * A shadow method called before {@link Command#execute() execute()}. @@ -277,16 +277,22 @@ public abstract class Command implements NamedSendable { *

It 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() { - } }