[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

@@ -7,10 +7,11 @@
#include <frc/shuffleboard/Shuffleboard.h>
#include <frc2/command/Commands.h>
#include <frc2/command/InstantCommand.h>
#include <frc2/command/RunCommand.h>
#include <frc2/command/button/JoystickButton.h>
#include <units/angle.h>
namespace ac = AutoConstants;
RobotContainer::RobotContainer() {
// Initialize all of your commands and subsystems here
@@ -18,7 +19,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());
@@ -30,32 +31,29 @@ void RobotContainer::ConfigureButtonBindings() {
// Configure your button bindings here
// Move the arm to 2 radians above horizontal when the 'A' button is pressed.
frc2::JoystickButton(&m_driverController, frc::XboxController::Button::kA)
.OnTrue(frc2::cmd::RunOnce(
[this] {
m_arm.SetGoal(2_rad);
m_arm.Enable();
},
{&m_arm}));
m_driverController.A().OnTrue(frc2::cmd::RunOnce(
[this] {
m_arm.SetGoal(2_rad);
m_arm.Enable();
},
{&m_arm}));
// Move the arm to neutral position when the 'B' button is pressed.
frc2::JoystickButton(&m_driverController, frc::XboxController::Button::kB)
.OnTrue(frc2::cmd::RunOnce(
[this] {
m_arm.SetGoal(ArmConstants::kArmOffset);
m_arm.Enable();
},
{&m_arm}));
m_driverController.B().OnTrue(frc2::cmd::RunOnce(
[this] {
m_arm.SetGoal(ArmConstants::kArmOffset);
m_arm.Enable();
},
{&m_arm}));
// Disable the arm controller when Y is pressed.
frc2::JoystickButton(&m_driverController, frc::XboxController::Button::kY)
.OnTrue(frc2::cmd::RunOnce([this] { m_arm.Disable(); }, {&m_arm}));
m_driverController.Y().OnTrue(
frc2::cmd::RunOnce([this] { m_arm.Disable(); }, {&m_arm}));
// While holding the shoulder button, drive at half speed
frc2::JoystickButton(&m_driverController,
frc::XboxController::Button::kRightBumper)
m_driverController.RightBumper()
.OnTrue(frc2::cmd::RunOnce([this] { m_drive.SetMaxOutput(0.5); }))
.OnFalse(frc2::cmd::RunOnce([this] { m_drive.SetMaxOutput(1); }));
.OnFalse(frc2::cmd::RunOnce([this] { m_drive.SetMaxOutput(1.0); }));
}
void RobotContainer::DisablePIDSubsystems() {
@@ -63,6 +61,5 @@ void RobotContainer::DisablePIDSubsystems() {
}
frc2::Command* RobotContainer::GetAutonomousCommand() {
// Runs the chosen command in autonomous
return new frc2::InstantCommand([] {});
return nullptr;
}

View File

@@ -4,16 +4,13 @@
#pragma once
#include <frc/XboxController.h>
#include <frc/smartdashboard/SendableChooser.h>
#include <frc2/command/Command.h>
#include <frc2/command/button/CommandXboxController.h>
#include "Constants.h"
#include "subsystems/ArmSubsystem.h"
#include "subsystems/DriveSubsystem.h"
namespace ac = AutoConstants;
/**
* This class is where the bulk of the robot should be declared. Since
* Command-based is a "declarative" paradigm, very little robot logic should
@@ -35,7 +32,8 @@ class RobotContainer {
private:
// The driver's controller
frc::XboxController m_driverController{OIConstants::kDriverControllerPort};
frc2::CommandXboxController m_driverController{
OIConstants::kDriverControllerPort};
// The robot's subsystems and commands are defined here...
@@ -43,7 +41,5 @@ class RobotContainer {
DriveSubsystem m_drive;
ArmSubsystem m_arm;
// The chooser for the autonomous routines
void ConfigureButtonBindings();
};