Skip to content

Commit

Permalink
[commands] Add CommandRobot
Browse files Browse the repository at this point in the history
  • Loading branch information
spacey-sooty committed Jul 20, 2024
1 parent e9efb05 commit c467506
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.

package edu.wpi.first.wpilibj2.command;

import edu.wpi.first.hal.FRCNetComm.tInstances;
import edu.wpi.first.hal.FRCNetComm.tResourceType;
import edu.wpi.first.hal.HAL;
import edu.wpi.first.wpilibj.TimedRobot;

/**
* CommandRobot is a wrapper over TimedRobot designed to work well with Command based programming.
*
* <p>The CommandRobot class is intended to be subclassed by a user creating a robot program.
*/
public class CommandRobot extends TimedRobot {
/// The CommandScheduler instance.
protected CommandScheduler m_scheduler = CommandScheduler.getInstance();

/// The Command run during autonomous.
protected Command m_autonomousCommand =
Commands.print("Default auto Command override autonomousInit to add your own.");

/** Constructor for CommandRobot. */
protected CommandRobot() {
this(kDefaultPeriod);
}

/**
* Constructor for CommandRobot.
*
* @param period Period in seconds.
*/
protected CommandRobot(double period) {
super(period);
HAL.report(tResourceType.kResourceType_Framework, tInstances.kFramework_CommandControl);
}

@Override
public final void robotInit() {}

@Override
public final void robotPeriodic() {
m_scheduler.run();
}

@Override
public void autonomousInit() {
if (m_autonomousCommand != null) {
m_scheduler.schedule(m_autonomousCommand);
}
}

@Override
public final void autonomousPeriodic() {}

@Override
public void autonomousExit() {}

@Override
public final void teleopInit() {
if (m_autonomousCommand != null) {
m_scheduler.cancel(m_autonomousCommand);
}
}

@Override
public final void teleopPeriodic() {}

@Override
public final void teleopExit() {}

@Override
public final void disabledInit() {}

@Override
public final void disabledPeriodic() {}

@Override
public final void disabledExit() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "frc2/command/CommandRobot.h"
#include "units/time.h"

frc2::CommandRobot::CommandRobot(units::second_t period)
: frc::TimedRobot(period) {
HAL_Report(HALUsageReporting::kResourceType_Framework,
HALUsageReporting::kFramework_CommandControl);
}

void frc2::CommandRobot::RobotInit() {}

void frc2::CommandRobot::RobotPeriodic() {
m_scheduler.Run();
}

void frc2::CommandRobot::AutonomousInit() {
if (m_autonomousCommand) {
m_scheduler.Schedule(m_autonomousCommand);
}
}

void frc2::CommandRobot::AutonomousPeriodic() {}

void frc2::CommandRobot::AutonomousExit() {}

void frc2::CommandRobot::TeleopInit() {
if (m_autonomousCommand) {
m_scheduler.Cancel(m_autonomousCommand);
}
}

void frc2::CommandRobot::TeleopPeriodic() {}

void frc2::CommandRobot::TeleopExit() {}

void frc2::CommandRobot::DisabledInit() {}

void frc2::CommandRobot::DisabledPeriodic() {}

void frc2::CommandRobot::DisabledExit() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.

#pragma once

#include <frc/TimedRobot.h>
#include <hal/FRCUsageReporting.h>
#include <units/time.h>

#include "frc2/command/CommandPtr.h"
#include "frc2/command/CommandScheduler.h"
#include "frc2/command/Commands.h"

namespace frc2 {
/**
CommandRobot is a wrapper over TimedRobot designed to work well with Command
* based programming.
*
* <p>The CommandRobot class is intended to be subclassed by a user creating a
* robot program.
*/
class CommandRobot : public frc::TimedRobot {
protected:
/// The CommandScheduler instance.
frc2::CommandScheduler& m_scheduler = frc2::CommandScheduler::GetInstance();

/// The Command run during autonomous.
frc2::CommandPtr m_autonomousCommand = frc2::cmd::Print(
"Default auto Command override autonomousInit to add your own.");

/**
* Constructor for CommandRobot.
*
* @param period Period in seconds.
*/
explicit CommandRobot(units::second_t period = kDefaultPeriod);

public:
void RobotInit() final;

void RobotPeriodic() final;

void AutonomousInit() final;

void AutonomousPeriodic() final;

void AutonomousExit() final;

void TeleopInit() final;

void TeleopPeriodic() final;

void TeleopExit() final;

void DisabledInit() final;

void DisabledPeriodic() final;

void DisabledExit() final;
};
} // namespace frc2

0 comments on commit c467506

Please sign in to comment.