[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.
*/