[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

@@ -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!")},