Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wpilibcExamples] Update examples to CommandPtr #5988

Merged
merged 15 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions wpilibcExamples/src/main/cpp/examples/ArmBot/cpp/Robot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ void Robot::DisabledPeriodic() {}
void Robot::AutonomousInit() {
m_autonomousCommand = m_container.GetAutonomousCommand();

if (m_autonomousCommand != nullptr) {
m_autonomousCommand->Schedule();
if (m_autonomousCommand) {
m_autonomousCommand.Schedule();
}
}

Expand All @@ -51,9 +51,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) {
m_autonomousCommand->Cancel();
m_autonomousCommand = nullptr;
ncorrea210 marked this conversation as resolved.
Show resolved Hide resolved
if (m_autonomousCommand) {
m_autonomousCommand.Cancel();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,6 @@ void RobotContainer::DisablePIDSubsystems() {
m_arm.Disable();
}

frc2::Command* RobotContainer::GetAutonomousCommand() {
return nullptr;
frc2::CommandPtr RobotContainer::GetAutonomousCommand() {
return frc2::CommandPtr(nullptr);
ncorrea210 marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#include <frc/TimedRobot.h>
#include <frc2/command/Command.h>
#include <frc2/command/Commands.h>


#include "RobotContainer.h"

Expand All @@ -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;
frc2::CommandPtr m_autonomousCommand = frc2::cmd::None();

RobotContainer m_container;
};
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -28,7 +29,7 @@ class RobotContainer {
*/
void DisablePIDSubsystems();

frc2::Command* GetAutonomousCommand();
frc2::CommandPtr GetAutonomousCommand();

private:
// The driver's controller
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ void Robot::DisabledPeriodic() {}
void Robot::AutonomousInit() {
m_autonomousCommand = m_container.GetAutonomousCommand();

if (m_autonomousCommand != nullptr) {
m_autonomousCommand->Schedule();
if (m_autonomousCommand) {
m_autonomousCommand.Schedule();
}
}

Expand All @@ -49,9 +49,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) {
m_autonomousCommand->Cancel();
m_autonomousCommand = nullptr;
ncorrea210 marked this conversation as resolved.
Show resolved Hide resolved
if (m_autonomousCommand) {
m_autonomousCommand.Cancel();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ void RobotContainer::ConfigureButtonBindings() {
.OnFalse(m_drive.SetMaxOutputCommand(1.0));
}

frc2::Command* RobotContainer::GetAutonomousCommand() {
return nullptr;
frc2::CommandPtr RobotContainer::GetAutonomousCommand() {
return frc2::CommandPtr(nullptr);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <frc/TimedRobot.h>
#include <frc2/command/Command.h>
#include <frc2/command/Commands.h>

#include "RobotContainer.h"

Expand All @@ -24,7 +25,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;
frc2::CommandPtr m_autonomousCommand = frc2::cmd::None();

RobotContainer m_container;
};
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -24,7 +25,7 @@ class RobotContainer {
public:
RobotContainer();

frc2::Command* GetAutonomousCommand();
frc2::CommandPtr GetAutonomousCommand();

private:
// The driver's controller
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ void Robot::DisabledPeriodic() {}
void Robot::AutonomousInit() {
m_autonomousCommand = m_container.GetAutonomousCommand();

if (m_autonomousCommand != nullptr) {
m_autonomousCommand->Schedule();
if (m_autonomousCommand) {
m_autonomousCommand.Schedule();
}
}

Expand All @@ -49,9 +49,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) {
m_autonomousCommand->Cancel();
m_autonomousCommand = nullptr;
if (m_autonomousCommand) {
m_autonomousCommand.Cancel();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,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::CommandPtr(nullptr);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <frc/TimedRobot.h>
#include <frc2/command/Command.h>
#include <frc2/command/Commands.h>

#include "RobotContainer.h"

Expand All @@ -24,7 +25,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;
frc2::CommandPtr m_autonomousCommand = frc2::cmd::None();

RobotContainer m_container;
};
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class RobotContainer {
public:
RobotContainer();

frc2::Command* GetAutonomousCommand();
frc2::CommandPtr GetAutonomousCommand();

private:
// The driver's controller
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ void Robot::DisabledPeriodic() {}
void Robot::AutonomousInit() {
m_autonomousCommand = m_container.GetAutonomousCommand();

if (m_autonomousCommand != nullptr) {
m_autonomousCommand->Schedule();
if (m_autonomousCommand) {
m_autonomousCommand.Schedule();
}
}

Expand All @@ -49,9 +49,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) {
m_autonomousCommand->Cancel();
m_autonomousCommand = nullptr;
if (m_autonomousCommand) {
m_autonomousCommand.Cancel();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,24 @@ void RobotContainer::ConfigureButtonBindings() {
.OnFalse(frc2::cmd::RunOnce([this] { m_drive.SetMaxOutput(1); }, {}));
}

frc2::Command* RobotContainer::GetAutonomousCommand() {
frc2::CommandPtr RobotContainer::GetAutonomousCommand() {
// Runs the chosen command in autonomous
return m_autonomousCommand.get();
return frc2::cmd::Sequence(
// Start the command by spinning up the shooter...
frc2::cmd::RunOnce([this] { m_shooter.Enable(); }, {&m_shooter}),
// Wait until the shooter is at speed before feeding the frisbees
frc2::cmd::WaitUntil([this] { return m_shooter.AtSetpoint(); }),
// Start running the feeder
frc2::cmd::RunOnce([this] { m_shooter.RunFeeder(); },
{&m_shooter}),
// Shoot for the specified time
frc2::cmd::Wait(ac::kAutoShootTimeSeconds))
// Add a timeout (will end the command if, for instance, the shooter
// never gets up to speed)
.WithTimeout(ac::kAutoTimeoutSeconds)
// When the command ends, turn off the shooter and the feeder
.AndThen(frc2::cmd::RunOnce([this] {
m_shooter.Disable();
m_shooter.StopFeeder();
}));
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <frc/TimedRobot.h>
#include <frc2/command/Command.h>
#include <frc2/command/Commands.h>

#include "RobotContainer.h"

Expand All @@ -24,7 +25,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;
frc2::CommandPtr m_autonomousCommand = frc2::cmd::None();

RobotContainer m_container;
};
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class RobotContainer {
RobotContainer();

// The chooser for the autonomous routines
frc2::Command* GetAutonomousCommand();
frc2::CommandPtr GetAutonomousCommand();

private:
// The driver's controller
Expand All @@ -48,25 +48,5 @@ class RobotContainer {
frc2::CommandPtr m_stopShooter =
frc2::cmd::RunOnce([this] { m_shooter.Disable(); }, {&m_shooter});

// An autonomous routine that shoots the loaded frisbees
frc2::CommandPtr m_autonomousCommand =
frc2::cmd::Sequence(
// Start the command by spinning up the shooter...
frc2::cmd::RunOnce([this] { m_shooter.Enable(); }, {&m_shooter}),
// Wait until the shooter is at speed before feeding the frisbees
frc2::cmd::WaitUntil([this] { return m_shooter.AtSetpoint(); }),
// Start running the feeder
frc2::cmd::RunOnce([this] { m_shooter.RunFeeder(); }, {&m_shooter}),
// Shoot for the specified time
frc2::cmd::Wait(ac::kAutoShootTimeSeconds))
// Add a timeout (will end the command if, for instance, the shooter
// never gets up to speed)
.WithTimeout(ac::kAutoTimeoutSeconds)
// When the command ends, turn off the shooter and the feeder
.AndThen(frc2::cmd::RunOnce([this] {
m_shooter.Disable();
m_shooter.StopFeeder();
}));

void ConfigureButtonBindings();
};
9 changes: 4 additions & 5 deletions wpilibcExamples/src/main/cpp/examples/GearsBot/cpp/Robot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ void Robot::DisabledPeriodic() {}
void Robot::AutonomousInit() {
m_autonomousCommand = m_container.GetAutonomousCommand();

if (m_autonomousCommand != nullptr) {
m_autonomousCommand->Schedule();
if (m_autonomousCommand) {
m_autonomousCommand.Schedule();
}
}

Expand All @@ -49,9 +49,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) {
m_autonomousCommand->Cancel();
m_autonomousCommand = nullptr;
if (m_autonomousCommand) {
m_autonomousCommand.Cancel();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
#include "commands/SetElevatorSetpoint.h"
#include "commands/TankDrive.h"

RobotContainer::RobotContainer()
: m_autonomousCommand(m_claw, m_wrist, m_elevator, m_drivetrain) {
RobotContainer::RobotContainer() {
frc::SmartDashboard::PutData(&m_drivetrain);
frc::SmartDashboard::PutData(&m_elevator);
frc::SmartDashboard::PutData(&m_wrist);
Expand Down Expand Up @@ -48,7 +47,7 @@ void RobotContainer::ConfigureButtonBindings() {
.OnTrue(PrepareToPickup(m_claw, m_wrist, m_elevator).ToPtr());
}

frc2::Command* RobotContainer::GetAutonomousCommand() {
frc2::CommandPtr RobotContainer::GetAutonomousCommand() {
// An example command will be run in autonomous
return &m_autonomousCommand;
return Autonomous(m_claw, m_wrist, m_elevator, m_drivetrain).ToPtr();
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <frc/TimedRobot.h>
#include <frc2/command/Command.h>
#include <frc2/command/Commands.h>

#include "RobotContainer.h"

Expand All @@ -24,7 +25,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;
frc2::CommandPtr m_autonomousCommand = frc2::cmd::None();

RobotContainer m_container;
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <frc/XboxController.h>
#include <frc2/command/Command.h>
#include <frc2/command/CommandPtr.h>

#include "commands/Autonomous.h"
#include "subsystems/Claw.h"
Expand All @@ -24,7 +25,7 @@ class RobotContainer {
public:
RobotContainer();

frc2::Command* GetAutonomousCommand();
frc2::CommandPtr GetAutonomousCommand();

private:
// The robot's subsystems and commands are defined here...
Expand All @@ -35,7 +36,5 @@ class RobotContainer {
Elevator m_elevator;
Drivetrain m_drivetrain;

Autonomous m_autonomousCommand;

void ConfigureButtonBindings();
};
Loading