[templates] Add bindings to command-based template (#4838)

This commit is contained in:
Starlight220
2022-12-26 21:28:06 +02:00
committed by GitHub
parent 4534e75787
commit 87a34af367
8 changed files with 80 additions and 12 deletions

View File

@@ -4,7 +4,10 @@
#include "RobotContainer.h"
#include <frc2/command/button/Trigger.h>
#include "commands/Autos.h"
#include "commands/ExampleCommand.h"
RobotContainer::RobotContainer() {
// Initialize all of your commands and subsystems here
@@ -14,7 +17,16 @@ RobotContainer::RobotContainer() {
}
void RobotContainer::ConfigureBindings() {
// Configure your button bindings here
// Configure your trigger bindings here
// Schedule `ExampleCommand` when `exampleCondition` changes to `true`
frc2::Trigger([this] {
return m_subsystem.ExampleCondition();
}).OnTrue(ExampleCommand(&m_subsystem).ToPtr());
// Schedule `ExampleMethodCommand` when the Xbox controller's B button is
// pressed, cancelling on release.
m_driverController.B().WhileTrue(m_subsystem.ExampleMethodCommand());
}
frc2::CommandPtr RobotContainer::GetAutonomousCommand() {

View File

@@ -14,6 +14,11 @@ frc2::CommandPtr ExampleSubsystem::ExampleMethodCommand() {
return RunOnce([/* this */] { /* one-time action goes here */ });
}
bool ExampleSubsystem::ExampleCondition() {
// Query some boolean state, such as a digital sensor.
return false;
}
void ExampleSubsystem::Periodic() {
// Implementation of subsystem periodic method goes here.
}

View File

@@ -13,3 +13,9 @@
* command-specific namespaces within this header, which can then be used where
* they are needed.
*/
namespace OperatorConstants {
constexpr int kDriverControllerPort = 0;
} // namespace OperatorConstants

View File

@@ -5,7 +5,9 @@
#pragma once
#include <frc2/command/CommandPtr.h>
#include <frc2/command/button/CommandXboxController.h>
#include "Constants.h"
#include "subsystems/ExampleSubsystem.h"
/**
@@ -13,7 +15,7 @@
* Command-based is a "declarative" paradigm, very little robot logic should
* actually be handled in the {@link Robot} periodic methods (other than the
* scheduler calls). Instead, the structure of the robot (including subsystems,
* commands, and button mappings) should be declared here.
* commands, and trigger mappings) should be declared here.
*/
class RobotContainer {
public:
@@ -22,6 +24,10 @@ class RobotContainer {
frc2::CommandPtr GetAutonomousCommand();
private:
// Replace with CommandPS4Controller or CommandJoystick if needed
frc2::CommandXboxController m_driverController{
OperatorConstants::kDriverControllerPort};
// The robot's subsystems are defined here...
ExampleSubsystem m_subsystem;

View File

@@ -16,6 +16,14 @@ class ExampleSubsystem : public frc2::SubsystemBase {
*/
frc2::CommandPtr ExampleMethodCommand();
/**
* An example method querying a boolean state of the subsystem (for example, a
* digital sensor).
*
* @return value of some boolean subsystem state, such as a digital sensor.
*/
bool ExampleCondition();
/**
* Will be called periodically whenever the CommandScheduler runs.
*/