mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
Add new C++ Command framework (#1785)
This is the C++ version of #1682. The old command framework is still available, but will be deprecated. Due to name conflicts, the new framework is in the frc2 namespace. Eventually (after the old command framework is removed in a future year) it will be moved into the main frc namespace.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
|
||||
/* Copyright (c) 2019 FIRST. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
@@ -7,7 +7,12 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
class OI {
|
||||
public:
|
||||
OI();
|
||||
};
|
||||
/**
|
||||
* The Constants header provides a convenient place for teams to hold robot-wide
|
||||
* numerical or boolean constants. This should not be used for any other
|
||||
* purpose.
|
||||
*
|
||||
* It is generally a good idea to place constants into subsystem- or
|
||||
* command-specific namespaces within this header, which can then be used where
|
||||
* they are needed.
|
||||
*/
|
||||
@@ -1,5 +1,5 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
|
||||
/* Copyright (c) 2017-2019 FIRST. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
@@ -7,20 +7,14 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <frc/TimedRobot.h>
|
||||
#include <frc/commands/Command.h>
|
||||
#include <frc/smartdashboard/SendableChooser.h>
|
||||
#include <frc2/command/Command.h>
|
||||
|
||||
#include "OI.h"
|
||||
#include "commands/ExampleCommand.h"
|
||||
#include "commands/MyAutoCommand.h"
|
||||
#include "subsystems/ExampleSubsystem.h"
|
||||
#include <frc/TimedRobot.h>
|
||||
|
||||
#include "RobotContainer.h"
|
||||
|
||||
class Robot : public frc::TimedRobot {
|
||||
public:
|
||||
static ExampleSubsystem m_subsystem;
|
||||
static OI m_oi;
|
||||
|
||||
void RobotInit() override;
|
||||
void RobotPeriodic() override;
|
||||
void DisabledInit() override;
|
||||
@@ -34,8 +28,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.
|
||||
frc::Command* m_autonomousCommand = nullptr;
|
||||
ExampleCommand m_defaultAuto;
|
||||
MyAutoCommand m_myAuto;
|
||||
frc::SendableChooser<frc::Command*> m_chooser;
|
||||
frc2::Command* m_autonomousCommand = nullptr;
|
||||
|
||||
RobotContainer m_container;
|
||||
};
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2019 FIRST. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <frc2/command/Command.h>
|
||||
|
||||
#include "commands/ExampleCommand.h"
|
||||
#include "subsystems/ExampleSubsystem.h"
|
||||
|
||||
/**
|
||||
* This class is where the bulk of the robot should be declared. Since
|
||||
* 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.
|
||||
*/
|
||||
class RobotContainer {
|
||||
public:
|
||||
RobotContainer();
|
||||
|
||||
frc2::Command* GetAutonomousCommand();
|
||||
|
||||
private:
|
||||
// The robot's subsystems and commands are defined here...
|
||||
ExampleSubsystem m_subsystem;
|
||||
ExampleCommand m_autonomousCommand;
|
||||
|
||||
void ConfigureButtonBindings();
|
||||
};
|
||||
@@ -1,25 +0,0 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
/**
|
||||
* The RobotMap is a mapping from the ports sensors and actuators are wired into
|
||||
* to a variable name. This provides flexibility changing wiring, makes checking
|
||||
* the wiring easier and significantly reduces the number of magic numbers
|
||||
* floating around.
|
||||
*/
|
||||
|
||||
// For example to map the left and right motors, you could define the
|
||||
// following variables to use with your drivetrain subsystem.
|
||||
// constexpr int kLeftMotor = 1;
|
||||
// constexpr int kRightMotor = 2;
|
||||
|
||||
// If you are using multiple modules, make sure to define both the port
|
||||
// number and the module. For example you with a rangefinder:
|
||||
// constexpr int kRangeFinderPort = 1;
|
||||
// constexpr int kRangeFinderModule = 1;
|
||||
@@ -1,5 +1,5 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
|
||||
/* Copyright (c) 2019 FIRST. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
@@ -7,14 +7,28 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <frc/commands/Command.h>
|
||||
#include <frc2/command/CommandBase.h>
|
||||
#include <frc2/command/CommandHelper.h>
|
||||
|
||||
class ExampleCommand : public frc::Command {
|
||||
#include "subsystems/ExampleSubsystem.h"
|
||||
|
||||
/**
|
||||
* An example command that uses an example subsystem.
|
||||
*
|
||||
* <p>Note that this extends CommandHelper, rather extending CommandBase
|
||||
* directly; this is crucially important, or else the decorator functions in
|
||||
* Command will *not* work!
|
||||
*/
|
||||
class ExampleCommand
|
||||
: public frc2::CommandHelper<frc2::CommandBase, ExampleCommand> {
|
||||
public:
|
||||
ExampleCommand();
|
||||
void Initialize() override;
|
||||
void Execute() override;
|
||||
bool IsFinished() override;
|
||||
void End() override;
|
||||
void Interrupted() override;
|
||||
/**
|
||||
* Creates a new ExampleCommand.
|
||||
*
|
||||
* @param subsystem The subsystem used by this command.
|
||||
*/
|
||||
explicit ExampleCommand(ExampleSubsystem* subsystem);
|
||||
|
||||
private:
|
||||
ExampleSubsystem* m_subsystem;
|
||||
};
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <frc/commands/Command.h>
|
||||
|
||||
class MyAutoCommand : public frc::Command {
|
||||
public:
|
||||
MyAutoCommand();
|
||||
void Initialize() override;
|
||||
void Execute() override;
|
||||
bool IsFinished() override;
|
||||
void End() override;
|
||||
void Interrupted() override;
|
||||
};
|
||||
@@ -1,5 +1,5 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
|
||||
/* Copyright (c) 2019 FIRST. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
@@ -7,14 +7,20 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <frc/commands/Subsystem.h>
|
||||
#include <frc2/command/SubsystemBase.h>
|
||||
|
||||
class ExampleSubsystem : public frc::Subsystem {
|
||||
class ExampleSubsystem : public frc2::SubsystemBase {
|
||||
public:
|
||||
ExampleSubsystem();
|
||||
void InitDefaultCommand() override;
|
||||
|
||||
/**
|
||||
* Will be called periodically whenever the CommandScheduler runs.
|
||||
*/
|
||||
void Periodic() override;
|
||||
|
||||
// Subsystem methods go here.
|
||||
|
||||
private:
|
||||
// It's desirable that everything possible under private except
|
||||
// for methods that implement subsystem capabilities
|
||||
// Components (e.g. motor controllers and sensors) should generally be
|
||||
// declared private and exposed only through public methods.
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user