diff --git a/wpilibcExamples/src/main/cpp/templates/commandbased/cpp/Robot.cpp b/wpilibcExamples/src/main/cpp/templates/commandbased/cpp/Robot.cpp index 426b989a2f..c1c1ae6443 100644 --- a/wpilibcExamples/src/main/cpp/templates/commandbased/cpp/Robot.cpp +++ b/wpilibcExamples/src/main/cpp/templates/commandbased/cpp/Robot.cpp @@ -4,7 +4,6 @@ #include "Robot.h" -#include #include void Robot::RobotInit() {} @@ -37,7 +36,7 @@ void Robot::DisabledPeriodic() {} void Robot::AutonomousInit() { m_autonomousCommand = m_container.GetAutonomousCommand(); - if (m_autonomousCommand != nullptr) { + if (m_autonomousCommand) { m_autonomousCommand->Schedule(); } } @@ -49,9 +48,8 @@ void Robot::TeleopInit() { // teleop starts running. If you want the autonomous to // continue until interrupted by another command, remove // this line or comment it out. - if (m_autonomousCommand != nullptr) { + if (m_autonomousCommand) { m_autonomousCommand->Cancel(); - m_autonomousCommand = nullptr; } } diff --git a/wpilibcExamples/src/main/cpp/templates/commandbased/cpp/RobotContainer.cpp b/wpilibcExamples/src/main/cpp/templates/commandbased/cpp/RobotContainer.cpp index 8ba2094741..d56b84eeee 100644 --- a/wpilibcExamples/src/main/cpp/templates/commandbased/cpp/RobotContainer.cpp +++ b/wpilibcExamples/src/main/cpp/templates/commandbased/cpp/RobotContainer.cpp @@ -4,18 +4,20 @@ #include "RobotContainer.h" -RobotContainer::RobotContainer() : m_autonomousCommand(&m_subsystem) { +#include "commands/Autos.h" + +RobotContainer::RobotContainer() { // Initialize all of your commands and subsystems here // Configure the button bindings - ConfigureButtonBindings(); + ConfigureBindings(); } -void RobotContainer::ConfigureButtonBindings() { +void RobotContainer::ConfigureBindings() { // Configure your button bindings here } -frc2::Command* RobotContainer::GetAutonomousCommand() { +frc2::CommandPtr RobotContainer::GetAutonomousCommand() { // An example command will be run in autonomous - return &m_autonomousCommand; + return autos::ExampleAuto(&m_subsystem); } diff --git a/wpilibcExamples/src/main/cpp/templates/commandbased/cpp/commands/Autos.cpp b/wpilibcExamples/src/main/cpp/templates/commandbased/cpp/commands/Autos.cpp new file mode 100644 index 0000000000..3eb0958793 --- /dev/null +++ b/wpilibcExamples/src/main/cpp/templates/commandbased/cpp/commands/Autos.cpp @@ -0,0 +1,14 @@ +// 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. + +#include "commands/Autos.h" + +#include + +#include "commands/ExampleCommand.h" + +frc2::CommandPtr autos::ExampleAuto(ExampleSubsystem* subsystem) { + return frc2::cmd::Sequence(subsystem->ExampleMethodCommand(), + ExampleCommand(subsystem).ToPtr()); +} diff --git a/wpilibcExamples/src/main/cpp/templates/commandbased/cpp/commands/ExampleCommand.cpp b/wpilibcExamples/src/main/cpp/templates/commandbased/cpp/commands/ExampleCommand.cpp index e551aa1176..f7ab3e25e5 100644 --- a/wpilibcExamples/src/main/cpp/templates/commandbased/cpp/commands/ExampleCommand.cpp +++ b/wpilibcExamples/src/main/cpp/templates/commandbased/cpp/commands/ExampleCommand.cpp @@ -5,4 +5,7 @@ #include "commands/ExampleCommand.h" ExampleCommand::ExampleCommand(ExampleSubsystem* subsystem) - : m_subsystem{subsystem} {} + : m_subsystem{subsystem} { + // Register that this command requires the subsystem. + AddRequirements(m_subsystem); +} diff --git a/wpilibcExamples/src/main/cpp/templates/commandbased/cpp/subsystems/ExampleSubsystem.cpp b/wpilibcExamples/src/main/cpp/templates/commandbased/cpp/subsystems/ExampleSubsystem.cpp index 4a31aee030..4f428f833b 100644 --- a/wpilibcExamples/src/main/cpp/templates/commandbased/cpp/subsystems/ExampleSubsystem.cpp +++ b/wpilibcExamples/src/main/cpp/templates/commandbased/cpp/subsystems/ExampleSubsystem.cpp @@ -8,6 +8,12 @@ ExampleSubsystem::ExampleSubsystem() { // Implementation of subsystem constructor goes here. } +frc2::CommandPtr ExampleSubsystem::ExampleMethodCommand() { + // Inline construction of command goes here. + // Subsystem::RunOnce implicitly requires `this` subsystem. + return RunOnce([/* this */] { /* one-time action goes here */ }); +} + void ExampleSubsystem::Periodic() { // Implementation of subsystem periodic method goes here. } diff --git a/wpilibcExamples/src/main/cpp/templates/commandbased/include/Robot.h b/wpilibcExamples/src/main/cpp/templates/commandbased/include/Robot.h index 25e3229fa8..e9d45e7acd 100644 --- a/wpilibcExamples/src/main/cpp/templates/commandbased/include/Robot.h +++ b/wpilibcExamples/src/main/cpp/templates/commandbased/include/Robot.h @@ -4,8 +4,10 @@ #pragma once +#include + #include -#include +#include #include "RobotContainer.h" @@ -24,9 +26,9 @@ class Robot : public frc::TimedRobot { void SimulationPeriodic() override; private: - // Have it null by default so that if testing teleop it + // Have it empty by default so that if testing teleop it // doesn't have undefined behavior and potentially crash. - frc2::Command* m_autonomousCommand = nullptr; + std::optional m_autonomousCommand; RobotContainer m_container; }; diff --git a/wpilibcExamples/src/main/cpp/templates/commandbased/include/RobotContainer.h b/wpilibcExamples/src/main/cpp/templates/commandbased/include/RobotContainer.h index 2988968e21..9d51c8fbd6 100644 --- a/wpilibcExamples/src/main/cpp/templates/commandbased/include/RobotContainer.h +++ b/wpilibcExamples/src/main/cpp/templates/commandbased/include/RobotContainer.h @@ -4,9 +4,8 @@ #pragma once -#include +#include -#include "commands/ExampleCommand.h" #include "subsystems/ExampleSubsystem.h" /** @@ -20,12 +19,11 @@ class RobotContainer { public: RobotContainer(); - frc2::Command* GetAutonomousCommand(); + frc2::CommandPtr GetAutonomousCommand(); private: - // The robot's subsystems and commands are defined here... + // The robot's subsystems are defined here... ExampleSubsystem m_subsystem; - ExampleCommand m_autonomousCommand; - void ConfigureButtonBindings(); + void ConfigureBindings(); }; diff --git a/wpilibcExamples/src/main/cpp/templates/commandbased/include/commands/Autos.h b/wpilibcExamples/src/main/cpp/templates/commandbased/include/commands/Autos.h new file mode 100644 index 0000000000..62e2b5603d --- /dev/null +++ b/wpilibcExamples/src/main/cpp/templates/commandbased/include/commands/Autos.h @@ -0,0 +1,16 @@ +// 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. + +#pragma once + +#include + +#include "subsystems/ExampleSubsystem.h" + +namespace autos { +/** + * Example static factory for an autonomous command. + */ +frc2::CommandPtr ExampleAuto(ExampleSubsystem* subsystem); +} // namespace autos diff --git a/wpilibcExamples/src/main/cpp/templates/commandbased/include/subsystems/ExampleSubsystem.h b/wpilibcExamples/src/main/cpp/templates/commandbased/include/subsystems/ExampleSubsystem.h index a805151504..0649f88df8 100644 --- a/wpilibcExamples/src/main/cpp/templates/commandbased/include/subsystems/ExampleSubsystem.h +++ b/wpilibcExamples/src/main/cpp/templates/commandbased/include/subsystems/ExampleSubsystem.h @@ -4,12 +4,18 @@ #pragma once +#include #include class ExampleSubsystem : public frc2::SubsystemBase { public: ExampleSubsystem(); + /** + * Example command factory method. + */ + frc2::CommandPtr ExampleMethodCommand(); + /** * Will be called periodically whenever the CommandScheduler runs. */ diff --git a/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/templates/commandbased/RobotContainer.java b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/templates/commandbased/RobotContainer.java index 75f79ae8fd..b5721a754e 100644 --- a/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/templates/commandbased/RobotContainer.java +++ b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/templates/commandbased/RobotContainer.java @@ -6,7 +6,7 @@ package edu.wpi.first.wpilibj.templates.commandbased; import edu.wpi.first.wpilibj.GenericHID; import edu.wpi.first.wpilibj.XboxController; -import edu.wpi.first.wpilibj.templates.commandbased.commands.ExampleCommand; +import edu.wpi.first.wpilibj.templates.commandbased.commands.Autos; import edu.wpi.first.wpilibj.templates.commandbased.subsystems.ExampleSubsystem; import edu.wpi.first.wpilibj2.command.Command; @@ -20,12 +20,10 @@ public class RobotContainer { // The robot's subsystems and commands are defined here... private final ExampleSubsystem m_exampleSubsystem = new ExampleSubsystem(); - private final ExampleCommand m_autoCommand = new ExampleCommand(m_exampleSubsystem); - /** The container for the robot. Contains subsystems, OI devices, and commands. */ public RobotContainer() { // Configure the button bindings - configureButtonBindings(); + configureBindings(); } /** @@ -34,7 +32,7 @@ public class RobotContainer { * edu.wpi.first.wpilibj.Joystick} or {@link XboxController}), and then passing it to a {@link * edu.wpi.first.wpilibj2.command.button.JoystickButton}. */ - private void configureButtonBindings() {} + private void configureBindings() {} /** * Use this to pass the autonomous command to the main {@link Robot} class. @@ -42,7 +40,7 @@ public class RobotContainer { * @return the command to run in autonomous */ public Command getAutonomousCommand() { - // An ExampleCommand will run in autonomous - return m_autoCommand; + // An example command will be run in autonomous + return Autos.exampleAuto(m_exampleSubsystem); } } diff --git a/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/templates/commandbased/commands/Autos.java b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/templates/commandbased/commands/Autos.java new file mode 100644 index 0000000000..330a4aeda7 --- /dev/null +++ b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/templates/commandbased/commands/Autos.java @@ -0,0 +1,20 @@ +// 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.wpilibj.templates.commandbased.commands; + +import edu.wpi.first.wpilibj.templates.commandbased.subsystems.ExampleSubsystem; +import edu.wpi.first.wpilibj2.command.CommandBase; +import edu.wpi.first.wpilibj2.command.Commands; + +public final class Autos { + /** Example static factory for an autonomous command. */ + public static CommandBase exampleAuto(ExampleSubsystem subsystem) { + return Commands.sequence(subsystem.exampleMethodCommand(), new ExampleCommand(subsystem)); + } + + private Autos() { + throw new UnsupportedOperationException("This is a utility class!"); + } +} diff --git a/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/templates/commandbased/subsystems/ExampleSubsystem.java b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/templates/commandbased/subsystems/ExampleSubsystem.java index 8a3594b214..91719b2752 100644 --- a/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/templates/commandbased/subsystems/ExampleSubsystem.java +++ b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/templates/commandbased/subsystems/ExampleSubsystem.java @@ -4,12 +4,27 @@ package edu.wpi.first.wpilibj.templates.commandbased.subsystems; +import edu.wpi.first.wpilibj2.command.CommandBase; import edu.wpi.first.wpilibj2.command.SubsystemBase; public class ExampleSubsystem extends SubsystemBase { /** Creates a new ExampleSubsystem. */ public ExampleSubsystem() {} + /** + * Example command factory method. + * + * @return a command + */ + public CommandBase exampleMethodCommand() { + // Inline construction of command goes here. + // Subsystem::RunOnce implicitly requires `this` subsystem. + return runOnce( + () -> { + /* one-time action goes here */ + }); + } + @Override public void periodic() { // This method will be called once per scheduler run