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 40cd203
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// 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 {
private 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,73 @@
// 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:
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)
: frc::TimedRobot(period) {
HAL_Report(HALUsageReporting::kResourceType_Framework,
HALUsageReporting::kFramework_CommandControl);
}

public:
void RobotInit() final {}

void RobotPeriodic() final { m_scheduler.Run(); }

void AutonomousInit() final {
if (m_autonomousCommand) {
m_scheduler.Schedule(m_autonomousCommand);
}
}

void AutonomousPeriodic() final {}

void AutonomousExit() final {}

void TeleopInit() final {
if (m_autonomousCommand) {
m_scheduler.Cancel(m_autonomousCommand);
}
}

void TeleopPeriodic() final {}

void TeleopExit() final {}

void DisabledInit() final {}

void DisabledPeriodic() final {}

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

0 comments on commit 40cd203

Please sign in to comment.