This repository has been archived by the owner on Jan 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor main robot class to use CommandRobot (#85)
- Loading branch information
1 parent
be24647
commit 7b00f57
Showing
2 changed files
with
121 additions
and
106 deletions.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
src/main/java/edu/wpi/first/wpilibj2/command/CommandRobot.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Copyright (c) 2024 CurtinFRC | ||
// Open Source Software, you can modify it according to the terms | ||
// of the MIT License at the root of this project | ||
|
||
package edu.wpi.first.wpilibj2.command; | ||
|
||
import edu.wpi.first.wpilibj.TimedRobot; | ||
import edu.wpi.first.wpilibj.smartdashboard.SendableChooser; | ||
|
||
/** | ||
* 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 autonomous chooser. */ | ||
protected SendableChooser<Command> m_autoChooser = new SendableChooser<>(); | ||
|
||
private Command m_autonomousCommand; | ||
|
||
/** Constructor for CommandRobot. */ | ||
protected CommandRobot() { | ||
this(kDefaultPeriod); | ||
} | ||
|
||
/** | ||
* Constructor for CommandRobot. | ||
* | ||
* @param period Period in seconds. | ||
*/ | ||
protected CommandRobot(double period) { | ||
super(period); | ||
addPeriodic(() -> m_scheduler.run(), kDefaultPeriod); | ||
m_autoChooser.setDefaultOption("No Auto Configured", Commands.print("No autos configured.")); | ||
} | ||
|
||
@Override | ||
public final void robotInit() {} | ||
|
||
@Override | ||
public final void robotPeriodic() {} | ||
|
||
@Override | ||
public void autonomousInit() { | ||
m_autonomousCommand = m_autoChooser.getSelected(); | ||
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() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters