[examples] Update Command-based starter project (#4778)

This commit is contained in:
Starlight220
2022-12-15 19:40:14 +02:00
committed by GitHub
parent bf7068ac27
commit 701995d6cc
12 changed files with 104 additions and 26 deletions

View File

@@ -4,7 +4,6 @@
#include "Robot.h"
#include <frc/smartdashboard/SmartDashboard.h>
#include <frc2/command/CommandScheduler.h>
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;
}
}

View File

@@ -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);
}

View File

@@ -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 <frc2/command/Commands.h>
#include "commands/ExampleCommand.h"
frc2::CommandPtr autos::ExampleAuto(ExampleSubsystem* subsystem) {
return frc2::cmd::Sequence(subsystem->ExampleMethodCommand(),
ExampleCommand(subsystem).ToPtr());
}

View File

@@ -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);
}

View File

@@ -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.
}

View File

@@ -4,8 +4,10 @@
#pragma once
#include <optional>
#include <frc/TimedRobot.h>
#include <frc2/command/Command.h>
#include <frc2/command/CommandPtr.h>
#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<frc2::CommandPtr> m_autonomousCommand;
RobotContainer m_container;
};

View File

@@ -4,9 +4,8 @@
#pragma once
#include <frc2/command/Command.h>
#include <frc2/command/CommandPtr.h>
#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();
};

View File

@@ -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 <frc2/command/CommandPtr.h>
#include "subsystems/ExampleSubsystem.h"
namespace autos {
/**
* Example static factory for an autonomous command.
*/
frc2::CommandPtr ExampleAuto(ExampleSubsystem* subsystem);
} // namespace autos

View File

@@ -4,12 +4,18 @@
#pragma once
#include <frc2/command/CommandPtr.h>
#include <frc2/command/SubsystemBase.h>
class ExampleSubsystem : public frc2::SubsystemBase {
public:
ExampleSubsystem();
/**
* Example command factory method.
*/
frc2::CommandPtr ExampleMethodCommand();
/**
* Will be called periodically whenever the CommandScheduler runs.
*/

View File

@@ -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);
}
}

View File

@@ -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!");
}
}

View File

@@ -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