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

[commands] Add CommandRobot #6859

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
[commands, documentation] Update templates to use CommandRobot
spacey-sooty committed Jul 26, 2024
commit 7e8aec45b9f52ebe04366fe1a9cf10dac2583bf1
76 changes: 12 additions & 64 deletions wpilibcExamples/src/main/cpp/templates/commandbased/cpp/Robot.cpp
Original file line number Diff line number Diff line change
@@ -4,75 +4,23 @@

#include "Robot.h"

#include <frc2/command/CommandScheduler.h>
#include "commands/ExampleCommand.h"

Robot::Robot() {}
Robot::Robot() {
// Schedule `ExampleCommand` when `exampleCondition` changes to `true`
frc2::Trigger([this] {
return m_subsystem.ExampleCondition();
}).OnTrue(ExampleCommand(&m_subsystem).ToPtr());

/**
* This function is called every 20 ms, no matter the mode. Use
* 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() {
frc2::CommandScheduler::GetInstance().Run();
}

/**
* This function is called once each time the robot enters Disabled mode. You
* can use it to reset any subsystem information you want to clear when the
* robot is disabled.
*/
void Robot::DisabledInit() {}

void Robot::DisabledPeriodic() {}
// Schedule `ExampleMethodCommand` when the Xbox controller's B button is
// pressed, cancelling on release.
m_driverController.B().WhileTrue(m_subsystem.ExampleMethodCommand());

/**
* This autonomous runs the autonomous command selected by your {@link
* RobotContainer} class.
*/
void Robot::AutonomousInit() {
m_autonomousCommand = m_container.GetAutonomousCommand();

if (m_autonomousCommand) {
m_autonomousCommand->Schedule();
}
m_autoChooser.SetDefaultOption(
"Example Auto",
m_exampleAuto.get()); // An example Command that will be run in auto.
}

void Robot::AutonomousPeriodic() {}

void Robot::TeleopInit() {
// This makes sure that the autonomous stops running when
// 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) {
m_autonomousCommand->Cancel();
}
}

/**
* This function is called periodically during operator control.
*/
void Robot::TeleopPeriodic() {}

/**
* This function is called periodically during test mode.
*/
void Robot::TestPeriodic() {}

/**
* This function is called once when the robot is first started up.
*/
void Robot::SimulationInit() {}

/**
* This function is called periodically whilst in simulation.
*/
void Robot::SimulationPeriodic() {}

#ifndef RUNNING_FRC_TESTS
int main() {
return frc::StartRobot<Robot>();

This file was deleted.

Original file line number Diff line number Diff line change
@@ -4,31 +4,25 @@

#pragma once

#include <optional>

#include <frc/TimedRobot.h>
#include <frc2/command/CommandPtr.h>
#include <frc2/command/CommandRobot.h>
#include <frc2/command/button/CommandXboxController.h>

#include "RobotContainer.h"
#include "Constants.h"
#include "commands/Autos.h"
#include "subsystems/ExampleSubsystem.h"

class Robot : public frc::TimedRobot {
class Robot : public frc2::CommandRobot {
public:
Robot();
void RobotPeriodic() override;
void DisabledInit() override;
void DisabledPeriodic() override;
void AutonomousInit() override;
void AutonomousPeriodic() override;
void TeleopInit() override;
void TeleopPeriodic() override;
void TestPeriodic() override;
void SimulationInit() override;
void SimulationPeriodic() override;

private:
// Have it empty by default so that if testing teleop it
// doesn't have undefined behavior and potentially crash.
std::optional<frc2::CommandPtr> m_autonomousCommand;
// Replace with CommandPS4Controller or CommandJoystick if needed
frc2::CommandXboxController m_driverController{
OperatorConstants::kDriverControllerPort};

// The robot's subsystems are defined here.
ExampleSubsystem m_subsystem;

RobotContainer m_container;
frc2::CommandPtr m_exampleAuto{autos::ExampleAuto(&m_subsystem)};
};

This file was deleted.

Original file line number Diff line number Diff line change
@@ -4,50 +4,8 @@

#include "Robot.h"

#include <frc2/command/CommandScheduler.h>

Robot::Robot() {}

void Robot::RobotPeriodic() {
frc2::CommandScheduler::GetInstance().Run();
}

void Robot::DisabledInit() {}

void Robot::DisabledPeriodic() {}

void Robot::DisabledExit() {}

void Robot::AutonomousInit() {
m_autonomousCommand = m_container.GetAutonomousCommand();

if (m_autonomousCommand) {
m_autonomousCommand->Schedule();
}
}

void Robot::AutonomousPeriodic() {}

void Robot::AutonomousExit() {}

void Robot::TeleopInit() {
if (m_autonomousCommand) {
m_autonomousCommand->Cancel();
}
}

void Robot::TeleopPeriodic() {}

void Robot::TeleopExit() {}

void Robot::TestInit() {
frc2::CommandScheduler::GetInstance().CancelAll();
}

void Robot::TestPeriodic() {}

void Robot::TestExit() {}

#ifndef RUNNING_FRC_TESTS
int main() {
return frc::StartRobot<Robot>();

This file was deleted.

Original file line number Diff line number Diff line change
@@ -4,32 +4,9 @@

#pragma once

#include <optional>
#include <frc2/command/CommandRobot.h>

#include <frc/TimedRobot.h>
#include <frc2/command/CommandPtr.h>

#include "RobotContainer.h"

class Robot : public frc::TimedRobot {
class Robot : public frc2::CommandRobot {
public:
Robot();
void RobotPeriodic() override;
void DisabledInit() override;
void DisabledPeriodic() override;
void DisabledExit() override;
void AutonomousInit() override;
void AutonomousPeriodic() override;
void AutonomousExit() override;
void TeleopInit() override;
void TeleopPeriodic() override;
void TeleopExit() override;
void TestInit() override;
void TestPeriodic() override;
void TestExit() override;

private:
std::optional<frc2::CommandPtr> m_autonomousCommand;

RobotContainer m_container;
};
Loading