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:
Oblarg
2019-08-25 23:55:59 -04:00
committed by Peter Johnson
parent a0be07c370
commit 076ed7770c
196 changed files with 10687 additions and 163 deletions

View File

@@ -1,12 +0,0 @@
/*----------------------------------------------------------------------------*/
/* 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. */
/*----------------------------------------------------------------------------*/
#include "OI.h"
OI::OI() {
// Process operator interface input here.
}

View File

@@ -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,27 +7,21 @@
#include "Robot.h"
#include <frc/commands/Scheduler.h>
#include <frc2/command/CommandScheduler.h>
#include <frc/smartdashboard/SmartDashboard.h>
ExampleSubsystem Robot::m_subsystem;
OI Robot::m_oi;
void Robot::RobotInit() {
m_chooser.SetDefaultOption("Default Auto", &m_defaultAuto);
m_chooser.AddOption("My Auto", &m_myAuto);
frc::SmartDashboard::PutData("Auto Modes", &m_chooser);
}
void Robot::RobotInit() {}
/**
* This function is called every robot packet, no matter the mode. Use
* this for items like diagnostics that you want ran during disabled,
* this for items like diagnostics that you want to run during disabled,
* autonomous, teleoperated and test.
*
* <p> This runs after the mode specific periodic functions, but before
* LiveWindow and SmartDashboard integrated updating.
*/
void Robot::RobotPeriodic() {}
void Robot::RobotPeriodic() { frc2::CommandScheduler::GetInstance().Run(); }
/**
* This function is called once each time the robot enters Disabled mode. You
@@ -36,36 +30,21 @@ void Robot::RobotPeriodic() {}
*/
void Robot::DisabledInit() {}
void Robot::DisabledPeriodic() { frc::Scheduler::GetInstance()->Run(); }
void Robot::DisabledPeriodic() {}
/**
* This autonomous (along with the chooser code above) shows how to select
* between different autonomous modes using the dashboard. The sendable chooser
* code works with the Java SmartDashboard. If you prefer the LabVIEW Dashboard,
* remove all of the chooser code and uncomment the GetString code to get the
* auto name from the text box below the Gyro.
*
* You can add additional auto modes by adding additional commands to the
* chooser code above (like the commented example) or additional comparisons to
* the if-else structure below with additional strings & commands.
* This autonomous runs the autonomous command selected by your {@link
* RobotContainer} class.
*/
void Robot::AutonomousInit() {
// std::string autoSelected = frc::SmartDashboard::GetString(
// "Auto Selector", "Default");
// if (autoSelected == "My Auto") {
// m_autonomousCommand = &m_myAuto;
// } else {
// m_autonomousCommand = &m_defaultAuto;
// }
m_autonomousCommand = m_chooser.GetSelected();
m_autonomousCommand = m_container.GetAutonomousCommand();
if (m_autonomousCommand != nullptr) {
m_autonomousCommand->Start();
m_autonomousCommand->Schedule();
}
}
void Robot::AutonomousPeriodic() { frc::Scheduler::GetInstance()->Run(); }
void Robot::AutonomousPeriodic() {}
void Robot::TeleopInit() {
// This makes sure that the autonomous stops running when
@@ -78,8 +57,14 @@ void Robot::TeleopInit() {
}
}
void Robot::TeleopPeriodic() { frc::Scheduler::GetInstance()->Run(); }
/**
* This function is called periodically during operator control.
*/
void Robot::TeleopPeriodic() {}
/**
* This function is called periodically during test mode.
*/
void Robot::TestPeriodic() {}
#ifndef RUNNING_FRC_TESTS

View File

@@ -0,0 +1,24 @@
/*----------------------------------------------------------------------------*/
/* 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. */
/*----------------------------------------------------------------------------*/
#include "RobotContainer.h"
RobotContainer::RobotContainer() : m_autonomousCommand(&m_subsystem) {
// Initialize all of your commands and subsystems here
// Configure the button bindings
ConfigureButtonBindings();
}
void RobotContainer::ConfigureButtonBindings() {
// Configure your button bindings here
}
frc2::Command* RobotContainer::GetAutonomousCommand() {
// An example command will be run in autonomous
return &m_autonomousCommand;
}

View File

@@ -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,25 +7,5 @@
#include "commands/ExampleCommand.h"
#include "Robot.h"
ExampleCommand::ExampleCommand() {
// Use Requires() here to declare subsystem dependencies
Requires(&Robot::m_subsystem);
}
// Called just before this Command runs the first time
void ExampleCommand::Initialize() {}
// Called repeatedly when this Command is scheduled to run
void ExampleCommand::Execute() {}
// Make this return true when this Command no longer needs to run execute()
bool ExampleCommand::IsFinished() { return false; }
// Called once after isFinished returns true
void ExampleCommand::End() {}
// Called when another command which requires one or more of the same
// subsystems is scheduled to run
void ExampleCommand::Interrupted() {}
ExampleCommand::ExampleCommand(ExampleSubsystem* subsystem)
: m_subsystem{subsystem} {}

View File

@@ -1,31 +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. */
/*----------------------------------------------------------------------------*/
#include "commands/MyAutoCommand.h"
#include "Robot.h"
MyAutoCommand::MyAutoCommand() {
// Use Requires() here to declare subsystem dependencies
Requires(&Robot::m_subsystem);
}
// Called just before this Command runs the first time
void MyAutoCommand::Initialize() {}
// Called repeatedly when this Command is scheduled to run
void MyAutoCommand::Execute() {}
// Make this return true when this Command no longer needs to run execute()
bool MyAutoCommand::IsFinished() { return false; }
// Called once after isFinished returns true
void MyAutoCommand::End() {}
// Called when another command which requires one or more of the same
// subsystems is scheduled to run
void MyAutoCommand::Interrupted() {}

View File

@@ -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,10 @@
#include "subsystems/ExampleSubsystem.h"
#include "RobotMap.h"
ExampleSubsystem::ExampleSubsystem() : frc::Subsystem("ExampleSubsystem") {}
void ExampleSubsystem::InitDefaultCommand() {
// Set the default command for a subsystem here.
// SetDefaultCommand(new MySpecialCommand());
ExampleSubsystem::ExampleSubsystem() {
// Implementation of subsystem constructor goes here.
}
// Put methods for controlling this subsystem
// here. Call these from Commands.
void ExampleSubsystem::Periodic() {
// Implementation of subsystem periodic method goes here.
}

View File

@@ -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,12 @@
#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;
};
/**
* 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.
*/

View File

@@ -1,13 +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
class OI {
public:
OI();
};

View File

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

View File

@@ -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();
};

View File

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

View File

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

View File

@@ -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.
};