[examples] Update C++ examples to use CommandPtr (#5988)

Co-authored-by: Tyler Veness <calcmogul@gmail.com>
This commit is contained in:
ncorrea210
2023-12-04 00:39:29 -05:00
committed by GitHub
parent 9bc5fcf886
commit d32c10487c
26 changed files with 139 additions and 111 deletions

View File

@@ -39,7 +39,7 @@ void Robot::DisabledPeriodic() {}
void Robot::AutonomousInit() {
m_autonomousCommand = m_container.GetAutonomousCommand();
if (m_autonomousCommand != nullptr) {
if (m_autonomousCommand) {
m_autonomousCommand->Schedule();
}
}
@@ -51,9 +51,9 @@ 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;
m_autonomousCommand.reset();
}
}

View File

@@ -60,6 +60,6 @@ void RobotContainer::DisablePIDSubsystems() {
m_arm.Disable();
}
frc2::Command* RobotContainer::GetAutonomousCommand() {
return nullptr;
frc2::CommandPtr RobotContainer::GetAutonomousCommand() {
return frc2::cmd::None();
}

View File

@@ -4,6 +4,8 @@
#pragma once
#include <optional>
#include <frc/TimedRobot.h>
#include <frc2/command/Command.h>
@@ -24,7 +26,7 @@ class Robot : public frc::TimedRobot {
private:
// Have it null 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

@@ -5,6 +5,7 @@
#pragma once
#include <frc2/command/Command.h>
#include <frc2/command/CommandPtr.h>
#include <frc2/command/button/CommandXboxController.h>
#include "Constants.h"
@@ -28,7 +29,7 @@ class RobotContainer {
*/
void DisablePIDSubsystems();
frc2::Command* GetAutonomousCommand();
frc2::CommandPtr GetAutonomousCommand();
private:
// The driver's controller

View File

@@ -37,7 +37,7 @@ void Robot::DisabledPeriodic() {}
void Robot::AutonomousInit() {
m_autonomousCommand = m_container.GetAutonomousCommand();
if (m_autonomousCommand != nullptr) {
if (m_autonomousCommand) {
m_autonomousCommand->Schedule();
}
}
@@ -49,9 +49,9 @@ 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;
m_autonomousCommand.reset();
}
}

View File

@@ -4,6 +4,7 @@
#include "RobotContainer.h"
#include <frc2/command/Commands.h>
#include <units/angle.h>
RobotContainer::RobotContainer() {
@@ -34,6 +35,6 @@ void RobotContainer::ConfigureButtonBindings() {
.OnFalse(m_drive.SetMaxOutputCommand(1.0));
}
frc2::Command* RobotContainer::GetAutonomousCommand() {
return nullptr;
frc2::CommandPtr RobotContainer::GetAutonomousCommand() {
return frc2::cmd::None();
}

View File

@@ -4,6 +4,8 @@
#pragma once
#include <optional>
#include <frc/TimedRobot.h>
#include <frc2/command/Command.h>
@@ -24,7 +26,7 @@ class Robot : public frc::TimedRobot {
private:
// Have it null 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

@@ -5,6 +5,7 @@
#pragma once
#include <frc2/command/Command.h>
#include <frc2/command/CommandPtr.h>
#include <frc2/command/button/CommandXboxController.h>
#include "Constants.h"
@@ -24,7 +25,7 @@ class RobotContainer {
public:
RobotContainer();
frc2::Command* GetAutonomousCommand();
frc2::CommandPtr GetAutonomousCommand();
private:
// The driver's controller

View File

@@ -37,7 +37,7 @@ void Robot::DisabledPeriodic() {}
void Robot::AutonomousInit() {
m_autonomousCommand = m_container.GetAutonomousCommand();
if (m_autonomousCommand != nullptr) {
if (m_autonomousCommand) {
m_autonomousCommand->Schedule();
}
}
@@ -49,9 +49,9 @@ 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;
m_autonomousCommand.reset();
}
}

View File

@@ -4,6 +4,8 @@
#include "RobotContainer.h"
#include <frc2/command/Commands.h>
#include "commands/DriveDistanceProfiled.h"
RobotContainer::RobotContainer() {
@@ -60,7 +62,7 @@ void RobotContainer::ConfigureButtonBindings() {
.WithTimeout(10_s));
}
frc2::Command* RobotContainer::GetAutonomousCommand() {
frc2::CommandPtr RobotContainer::GetAutonomousCommand() {
// Runs the chosen command in autonomous
return nullptr;
return frc2::cmd::None();
}

View File

@@ -4,6 +4,8 @@
#pragma once
#include <optional>
#include <frc/TimedRobot.h>
#include <frc2/command/Command.h>
@@ -24,7 +26,7 @@ class Robot : public frc::TimedRobot {
private:
// Have it null 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

@@ -23,7 +23,7 @@ class RobotContainer {
public:
RobotContainer();
frc2::Command* GetAutonomousCommand();
frc2::CommandPtr GetAutonomousCommand();
private:
// The driver's controller

View File

@@ -37,7 +37,7 @@ void Robot::DisabledPeriodic() {}
void Robot::AutonomousInit() {
m_autonomousCommand = m_container.GetAutonomousCommand();
if (m_autonomousCommand != nullptr) {
if (m_autonomousCommand) {
m_autonomousCommand->Schedule();
}
}
@@ -49,9 +49,9 @@ 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;
m_autonomousCommand.reset();
}
}

View File

@@ -64,7 +64,7 @@ void RobotContainer::ConfigureButtonBindings() {
.OnFalse(frc2::cmd::RunOnce([this] { m_drive.SetMaxOutput(1); }, {}));
}
frc2::Command* RobotContainer::GetAutonomousCommand() {
frc2::CommandPtr RobotContainer::GetAutonomousCommand() {
// no auto
return nullptr;
return frc2::cmd::None();
}

View File

@@ -4,6 +4,8 @@
#pragma once
#include <optional>
#include <frc/TimedRobot.h>
#include <frc2/command/Command.h>
@@ -24,7 +26,7 @@ class Robot : public frc::TimedRobot {
private:
// Have it null 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

@@ -8,6 +8,7 @@
#include <frc/controller/PIDController.h>
#include <frc/smartdashboard/SendableChooser.h>
#include <frc2/command/Command.h>
#include <frc2/command/CommandPtr.h>
#include <frc2/command/InstantCommand.h>
#include "Constants.h"
@@ -28,7 +29,7 @@ class RobotContainer {
public:
RobotContainer();
frc2::Command* GetAutonomousCommand();
frc2::CommandPtr GetAutonomousCommand();
private:
// The driver's controller
@@ -39,8 +40,5 @@ class RobotContainer {
// The robot's subsystems
DriveSubsystem m_drive;
// The chooser for the autonomous routines
frc::SendableChooser<frc2::Command*> m_chooser;
void ConfigureButtonBindings();
};

View File

@@ -37,7 +37,7 @@ void Robot::DisabledPeriodic() {}
void Robot::AutonomousInit() {
m_autonomousCommand = m_container.GetAutonomousCommand();
if (m_autonomousCommand != nullptr) {
if (m_autonomousCommand) {
m_autonomousCommand->Schedule();
}
}
@@ -49,9 +49,9 @@ 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;
m_autonomousCommand.reset();
}
}

View File

@@ -12,6 +12,7 @@
#include <frc/trajectory/Trajectory.h>
#include <frc/trajectory/TrajectoryGenerator.h>
#include <frc/trajectory/constraint/MecanumDriveKinematicsConstraint.h>
#include <frc2/command/Commands.h>
#include <frc2/command/InstantCommand.h>
#include <frc2/command/MecanumControllerCommand.h>
#include <frc2/command/SequentialCommandGroup.h>
@@ -47,7 +48,7 @@ void RobotContainer::ConfigureButtonBindings() {
.OnFalse(&m_driveFullSpeed);
}
frc2::Command* RobotContainer::GetAutonomousCommand() {
frc2::CommandPtr RobotContainer::GetAutonomousCommand() {
// Set up config for trajectory
frc::TrajectoryConfig config(AutoConstants::kMaxSpeed,
AutoConstants::kMaxAcceleration);
@@ -65,48 +66,52 @@ frc2::Command* RobotContainer::GetAutonomousCommand() {
// Pass the config
config);
frc2::MecanumControllerCommand mecanumControllerCommand(
exampleTrajectory, [this]() { return m_drive.GetPose(); },
frc::SimpleMotorFeedforward<units::meters>(ks, kv, ka),
DriveConstants::kDriveKinematics,
frc::PIDController{AutoConstants::kPXController, 0, 0},
frc::PIDController{AutoConstants::kPYController, 0, 0},
frc::ProfiledPIDController<units::radians>(
AutoConstants::kPThetaController, 0, 0,
AutoConstants::kThetaControllerConstraints),
AutoConstants::kMaxSpeed,
[this]() {
return frc::MecanumDriveWheelSpeeds{
units::meters_per_second_t{m_drive.GetFrontLeftEncoder().GetRate()},
units::meters_per_second_t{
m_drive.GetFrontRightEncoder().GetRate()},
units::meters_per_second_t{m_drive.GetRearLeftEncoder().GetRate()},
units::meters_per_second_t{
m_drive.GetRearRightEncoder().GetRate()}};
},
frc::PIDController{DriveConstants::kPFrontLeftVel, 0, 0},
frc::PIDController{DriveConstants::kPRearLeftVel, 0, 0},
frc::PIDController{DriveConstants::kPFrontRightVel, 0, 0},
frc::PIDController{DriveConstants::kPRearRightVel, 0, 0},
[this](units::volt_t frontLeft, units::volt_t rearLeft,
units::volt_t frontRight, units::volt_t rearRight) {
m_drive.SetMotorControllersVolts(frontLeft, rearLeft, frontRight,
rearRight);
},
{&m_drive});
// Reset odometry to the starting pose of the trajectory.
m_drive.ResetOdometry(exampleTrajectory.InitialPose());
// no auto
return new frc2::SequentialCommandGroup(
std::move(mecanumControllerCommand),
frc2::InstantCommand([this]() { m_drive.Drive(0, 0, 0, false); }, {}));
frc2::CommandPtr mecanumDriveCommand =
frc2::MecanumControllerCommand(
exampleTrajectory, [this]() { return m_drive.GetPose(); },
frc::SimpleMotorFeedforward<units::meters>(ks, kv, ka),
DriveConstants::kDriveKinematics,
frc::PIDController{AutoConstants::kPXController, 0, 0},
frc::PIDController{AutoConstants::kPYController, 0, 0},
frc::ProfiledPIDController<units::radians>(
AutoConstants::kPThetaController, 0, 0,
AutoConstants::kThetaControllerConstraints),
AutoConstants::kMaxSpeed,
[this]() {
return frc::MecanumDriveWheelSpeeds{
units::meters_per_second_t{
m_drive.GetFrontLeftEncoder().GetRate()},
units::meters_per_second_t{
m_drive.GetFrontRightEncoder().GetRate()},
units::meters_per_second_t{
m_drive.GetRearLeftEncoder().GetRate()},
units::meters_per_second_t{
m_drive.GetRearRightEncoder().GetRate()}};
},
frc::PIDController{DriveConstants::kPFrontLeftVel, 0, 0},
frc::PIDController{DriveConstants::kPRearLeftVel, 0, 0},
frc::PIDController{DriveConstants::kPFrontRightVel, 0, 0},
frc::PIDController{DriveConstants::kPRearRightVel, 0, 0},
[this](units::volt_t frontLeft, units::volt_t rearLeft,
units::volt_t frontRight, units::volt_t rearRight) {
m_drive.SetMotorControllersVolts(frontLeft, rearLeft, frontRight,
rearRight);
},
{&m_drive})
.ToPtr();
return frc2::cmd::Sequence(
std::move(mecanumDriveCommand),
frc2::InstantCommand([this] { m_drive.Drive(0, 0, 0, false); }, {})
.ToPtr());
}

View File

@@ -4,6 +4,8 @@
#pragma once
#include <optional>
#include <frc/TimedRobot.h>
#include <frc2/command/Command.h>
@@ -24,7 +26,7 @@ class Robot : public frc::TimedRobot {
private:
// Have it null 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

@@ -9,6 +9,7 @@
#include <frc/controller/ProfiledPIDController.h>
#include <frc/smartdashboard/SendableChooser.h>
#include <frc2/command/Command.h>
#include <frc2/command/CommandPtr.h>
#include <frc2/command/InstantCommand.h>
#include <frc2/command/PIDCommand.h>
#include <frc2/command/ParallelRaceGroup.h>
@@ -28,7 +29,7 @@ class RobotContainer {
public:
RobotContainer();
frc2::Command* GetAutonomousCommand();
frc2::CommandPtr GetAutonomousCommand();
private:
// The driver's controller
@@ -44,8 +45,5 @@ class RobotContainer {
frc2::InstantCommand m_driveFullSpeed{[this] { m_drive.SetMaxOutput(1); },
{}};
// The chooser for the autonomous routines
frc::SendableChooser<frc2::Command*> m_chooser;
void ConfigureButtonBindings();
};

View File

@@ -4,9 +4,17 @@
#include "RobotContainer.h"
#include <frc/smartdashboard/SmartDashboard.h>
RobotContainer::RobotContainer() {
// Initialize all of your commands and subsystems here
m_chooser.SetDefaultOption("ONE", CommandSelector::ONE);
m_chooser.AddOption("TWO", CommandSelector::TWO);
m_chooser.AddOption("THREE", CommandSelector::THREE);
frc::SmartDashboard::PutData("Auto Chooser", &m_chooser);
// Configure the button bindings
ConfigureButtonBindings();
}

View File

@@ -4,7 +4,9 @@
#pragma once
#include <frc/smartdashboard/SendableChooser.h>
#include <frc2/command/Command.h>
#include <frc2/command/CommandPtr.h>
#include <frc2/command/Commands.h>
/**
@@ -24,10 +26,8 @@ class RobotContainer {
// The enum used as keys for selecting the command to run.
enum CommandSelector { ONE, TWO, THREE };
// An example selector method for the selectcommand. Returns the selector
// that will select which command to run. Can base this choice on logical
// conditions evaluated at runtime.
CommandSelector Select() { return ONE; }
// An example of how command selector may be used with SendableChooser
frc::SendableChooser<CommandSelector> m_chooser;
// The robot's subsystems and commands are defined here...
@@ -36,7 +36,7 @@ class RobotContainer {
// takes a generic type, so the selector does not have to be an enum; it could
// be any desired type (string, integer, boolean, double...)
frc2::CommandPtr m_exampleSelectCommand = frc2::cmd::Select<CommandSelector>(
[this] { return Select(); },
[this] { return m_chooser.GetSelected(); },
// Maps selector values to commands
std::pair{ONE, frc2::cmd::Print("Command one was selected!")},
std::pair{TWO, frc2::cmd::Print("Command two was selected!")},

View File

@@ -37,7 +37,7 @@ void Robot::DisabledPeriodic() {}
void Robot::AutonomousInit() {
m_autonomousCommand = m_container.GetAutonomousCommand();
if (m_autonomousCommand != nullptr) {
if (m_autonomousCommand) {
m_autonomousCommand->Schedule();
}
}
@@ -49,9 +49,9 @@ 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;
m_autonomousCommand.reset();
}
}

View File

@@ -11,6 +11,7 @@
#include <frc/shuffleboard/Shuffleboard.h>
#include <frc/trajectory/Trajectory.h>
#include <frc/trajectory/TrajectoryGenerator.h>
#include <frc2/command/Commands.h>
#include <frc2/command/InstantCommand.h>
#include <frc2/command/SequentialCommandGroup.h>
#include <frc2/command/SwerveControllerCommand.h>
@@ -48,7 +49,7 @@ RobotContainer::RobotContainer() {
void RobotContainer::ConfigureButtonBindings() {}
frc2::Command* RobotContainer::GetAutonomousCommand() {
frc2::CommandPtr RobotContainer::GetAutonomousCommand() {
// Set up config for trajectory
frc::TrajectoryConfig config(AutoConstants::kMaxSpeed,
AutoConstants::kMaxAcceleration);
@@ -73,24 +74,27 @@ frc2::Command* RobotContainer::GetAutonomousCommand() {
thetaController.EnableContinuousInput(units::radian_t{-std::numbers::pi},
units::radian_t{std::numbers::pi});
frc2::SwerveControllerCommand<4> swerveControllerCommand(
exampleTrajectory, [this]() { return m_drive.GetPose(); },
m_drive.kDriveKinematics,
frc::PIDController{AutoConstants::kPXController, 0, 0},
frc::PIDController{AutoConstants::kPYController, 0, 0}, thetaController,
[this](auto moduleStates) { m_drive.SetModuleStates(moduleStates); },
{&m_drive});
// Reset odometry to the starting pose of the trajectory.
m_drive.ResetOdometry(exampleTrajectory.InitialPose());
// no auto
return new frc2::SequentialCommandGroup(
std::move(swerveControllerCommand),
frc2::CommandPtr swerveDriveCommand =
frc2::SwerveControllerCommand<4>(
exampleTrajectory, [this]() { return m_drive.GetPose(); },
m_drive.kDriveKinematics,
frc::PIDController{AutoConstants::kPXController, 0, 0},
frc::PIDController{AutoConstants::kPYController, 0, 0},
thetaController,
[this](auto moduleStates) { m_drive.SetModuleStates(moduleStates); },
{&m_drive})
.ToPtr();
return frc2::cmd::Sequence(
std::move(swerveDriveCommand),
frc2::InstantCommand(
[this]() { m_drive.Drive(0_mps, 0_mps, 0_rad_per_s, false); }, {}));
[this] { m_drive.Drive(0_mps, 0_mps, 0_rad_per_s, false); }, {})
.ToPtr());
}

View File

@@ -4,6 +4,8 @@
#pragma once
#include <optional>
#include <frc/TimedRobot.h>
#include <frc2/command/Command.h>
@@ -24,7 +26,7 @@ class Robot : public frc::TimedRobot {
private:
// Have it null 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

@@ -9,6 +9,7 @@
#include <frc/controller/ProfiledPIDController.h>
#include <frc/smartdashboard/SendableChooser.h>
#include <frc2/command/Command.h>
#include <frc2/command/CommandPtr.h>
#include <frc2/command/InstantCommand.h>
#include <frc2/command/PIDCommand.h>
#include <frc2/command/ParallelRaceGroup.h>
@@ -28,7 +29,7 @@ class RobotContainer {
public:
RobotContainer();
frc2::Command* GetAutonomousCommand();
frc2::CommandPtr GetAutonomousCommand();
private:
// The driver's controller
@@ -39,8 +40,5 @@ class RobotContainer {
// The robot's subsystems
DriveSubsystem m_drive;
// The chooser for the autonomous routines
frc::SendableChooser<frc2::Command*> m_chooser;
void ConfigureButtonBindings();
};