[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

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