[examples] Renovate command-based examples (#4409)

Refactor some examples to use newer features, such as HID factories, library-provided command factories, CommandPtr (C++), as well as new idioms such as static/instance command factories.
This commit is contained in:
Starlight220
2022-11-28 18:55:13 +02:00
committed by GitHub
parent 1a59737f40
commit 20dbae0cee
26 changed files with 401 additions and 417 deletions

View File

@@ -5,14 +5,14 @@
#include "RobotContainer.h"
#include <frc/shuffleboard/Shuffleboard.h>
#include <frc2/command/button/JoystickButton.h>
RobotContainer::RobotContainer() {
// Initialize all of your commands and subsystems here
// Add commands to the autonomous command chooser
m_chooser.SetDefaultOption("Simple Auto", &m_simpleAuto);
m_chooser.AddOption("Complex Auto", &m_complexAuto);
// Note that we do *not* move ownership into the chooser
m_chooser.SetDefaultOption("Simple Auto", m_simpleAuto.get());
m_chooser.AddOption("Complex Auto", m_complexAuto.get());
// Put the chooser on the dashboard
frc::Shuffleboard::GetTab("Autonomous").Add(m_chooser);
@@ -21,7 +21,7 @@ RobotContainer::RobotContainer() {
ConfigureButtonBindings();
// Set up default drive command
m_drive.SetDefaultCommand(frc2::RunCommand(
m_drive.SetDefaultCommand(frc2::cmd::Run(
[this] {
m_drive.ArcadeDrive(-m_driverController.GetLeftY(),
m_driverController.GetRightX());
@@ -33,15 +33,15 @@ void RobotContainer::ConfigureButtonBindings() {
// Configure your button bindings here
// Grab the hatch when the 'Circle' button is pressed.
frc2::JoystickButton(&m_driverController, frc::PS4Controller::Button::kCircle)
.OnTrue(&m_grabHatch);
m_driverController.Circle().OnTrue(
frc2::cmd::RunOnce([this] { m_hatch.GrabHatch(); }, {&m_hatch}));
// Release the hatch when the 'Square' button is pressed.
frc2::JoystickButton(&m_driverController, frc::PS4Controller::Button::kSquare)
.OnTrue(&m_releaseHatch);
m_driverController.Square().OnTrue(
frc2::cmd::RunOnce([this] { m_hatch.ReleaseHatch(); }, {&m_hatch}));
// While holding R1, drive at half speed
frc2::JoystickButton(&m_driverController, frc::PS4Controller::Button::kR1)
.OnTrue(&m_driveHalfSpeed)
.OnFalse(&m_driveFullSpeed);
m_driverController.R1()
.OnTrue(frc2::cmd::RunOnce([this] { m_drive.SetMaxOutput(0.5); }, {}))
.OnFalse(frc2::cmd::RunOnce([this] { m_drive.SetMaxOutput(1.0); }, {}));
}
frc2::Command* RobotContainer::GetAutonomousCommand() {

View File

@@ -2,16 +2,37 @@
// 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/ComplexAuto.h"
#include "commands/Autos.h"
#include <frc2/command/Commands.h>
#include <frc2/command/FunctionalCommand.h>
#include <frc2/command/InstantCommand.h>
#include <frc2/command/ParallelRaceGroup.h>
#include "Constants.h"
using namespace AutoConstants;
ComplexAuto::ComplexAuto(DriveSubsystem* drive, HatchSubsystem* hatch) {
AddCommands(
frc2::CommandPtr autos::SimpleAuto(DriveSubsystem* drive) {
return frc2::FunctionalCommand(
// Reset encoders on command start
[drive] { drive->ResetEncoders(); },
// Drive forward while the command is executing
[drive] { drive->ArcadeDrive(AutoConstants::kAutoDriveSpeed, 0); },
// Stop driving at the end of the command
[drive](bool interrupted) { drive->ArcadeDrive(0, 0); },
// End the command when the robot's driven distance exceeds the
// desired value
[drive] {
return drive->GetAverageEncoderDistance() >=
AutoConstants::kAutoDriveDistanceInches;
},
// Requires the drive subsystem
{drive})
.ToPtr();
}
frc2::CommandPtr autos::ComplexAuto(DriveSubsystem* drive,
HatchSubsystem* hatch) {
return frc2::cmd::Sequence(
// Drive forward the specified distance
frc2::FunctionalCommand(
// Reset encoders on command start
@@ -27,9 +48,10 @@ ComplexAuto::ComplexAuto(DriveSubsystem* drive, HatchSubsystem* hatch) {
kAutoDriveDistanceInches;
},
// Requires the drive subsystem
{drive}),
{drive})
.ToPtr(),
// Release the hatch
frc2::InstantCommand([hatch] { hatch->ReleaseHatch(); }, {hatch}),
frc2::cmd::RunOnce([hatch] { hatch->ReleaseHatch(); }, {hatch}),
// Drive backward the specified distance
// Drive forward the specified distance
frc2::FunctionalCommand(
@@ -46,5 +68,6 @@ ComplexAuto::ComplexAuto(DriveSubsystem* drive, HatchSubsystem* hatch) {
kAutoBackupDistanceInches;
},
// Requires the drive subsystem
{drive}));
{drive})
.ToPtr());
}

View File

@@ -4,17 +4,13 @@
#pragma once
#include <frc/PS4Controller.h>
#include <frc/smartdashboard/SendableChooser.h>
#include <frc2/command/Command.h>
#include <frc2/command/FunctionalCommand.h>
#include <frc2/command/InstantCommand.h>
#include <frc2/command/ParallelRaceGroup.h>
#include <frc2/command/RunCommand.h>
#include <frc2/command/SequentialCommandGroup.h>
#include <frc2/command/Commands.h>
#include <frc2/command/button/CommandPS4Controller.h>
#include "Constants.h"
#include "commands/ComplexAuto.h"
#include "commands/Autos.h"
#include "subsystems/DriveSubsystem.h"
#include "subsystems/HatchSubsystem.h"
@@ -35,7 +31,8 @@ class RobotContainer {
private:
// The driver's controller
frc::PS4Controller m_driverController{OIConstants::kDriverControllerPort};
frc2::CommandPS4Controller m_driverController{
OIConstants::kDriverControllerPort};
// The robot's subsystems and commands are defined here...
@@ -43,33 +40,11 @@ class RobotContainer {
DriveSubsystem m_drive;
HatchSubsystem m_hatch;
// Commands owned by RobotContainer
// The autonomous routines
frc2::FunctionalCommand m_simpleAuto = frc2::FunctionalCommand(
// Reset encoders on command start
[this] { m_drive.ResetEncoders(); },
// Drive forward while the command is executing
[this] { m_drive.ArcadeDrive(AutoConstants::kAutoDriveSpeed, 0); },
// Stop driving at the end of the command
[this](bool interrupted) { m_drive.ArcadeDrive(0, 0); },
// End the command when the robot's driven distance exceeds the desired
// value
[this] {
return m_drive.GetAverageEncoderDistance() >=
AutoConstants::kAutoDriveDistanceInches;
},
// Requires the drive subsystem
{&m_drive});
ComplexAuto m_complexAuto{&m_drive, &m_hatch};
// Assorted commands to be bound to buttons
frc2::InstantCommand m_grabHatch{[this] { m_hatch.GrabHatch(); }, {&m_hatch}};
frc2::InstantCommand m_releaseHatch{[this] { m_hatch.ReleaseHatch(); },
{&m_hatch}};
frc2::InstantCommand m_driveHalfSpeed{[this] { m_drive.SetMaxOutput(0.5); },
{}};
frc2::InstantCommand m_driveFullSpeed{[this] { m_drive.SetMaxOutput(1); },
{}};
frc2::CommandPtr m_simpleAuto = autos::SimpleAuto(&m_drive);
frc2::CommandPtr m_complexAuto = autos::ComplexAuto(&m_drive, &m_hatch);
// The chooser for the autonomous routines
frc::SendableChooser<frc2::Command*> m_chooser;

View File

@@ -0,0 +1,26 @@
// 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/DriveSubsystem.h"
#include "subsystems/HatchSubsystem.h"
/** Container for auto command factories. */
namespace autos {
/**
* A simple auto that drives forward, then stops.
*/
frc2::CommandPtr SimpleAuto(DriveSubsystem* drive);
/**
* A complex auto command that drives forward, releases a hatch, and then drives
* backward.
*/
frc2::CommandPtr ComplexAuto(DriveSubsystem* drive, HatchSubsystem* hatch);
} // namespace autos

View File

@@ -1,28 +0,0 @@
// 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/CommandHelper.h>
#include <frc2/command/SequentialCommandGroup.h>
#include "Constants.h"
#include "subsystems/DriveSubsystem.h"
#include "subsystems/HatchSubsystem.h"
/**
* A complex auto command that drives forward, releases a hatch, and then drives
* backward.
*/
class ComplexAuto
: public frc2::CommandHelper<frc2::SequentialCommandGroup, ComplexAuto> {
public:
/**
* Creates a new ComplexAuto.
*
* @param drive The drive subsystem this command will run on
* @param hatch The hatch subsystem this command will run on
*/
ComplexAuto(DriveSubsystem* drive, HatchSubsystem* hatch);
};