Skip to content

Commit 62ce5dc

Browse files
msoucyThadHouse
authored andcommitted
Additional support for command-based robots (#116)
* Add a CommandRobot base Takes care of the boilerplate for running the scheduler. Does not implement any of the logic for selecting or creating autonomous modes. * Add SubsystemCommand to extras It behaves as a regular command, but automatically calls `.Requires` on the provided subsystem and stores a reference to it as `m_subsystem` * Fix comments to provide more accurate description
1 parent cc6700f commit 62ce5dc

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

WPILib.Extras/CommandRobot.cs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using WPILib.Commands;
2+
3+
namespace WPILib.Extras
4+
{
5+
/// <summary>
6+
/// Implements the boilerplate needed to use an <see cref="IterativeRobot"/>
7+
/// with the command-based model, by running the scheduler as needed.
8+
/// </summary>
9+
public abstract class CommandRobot : IterativeRobot
10+
{
11+
// This function is called periodically while disabled
12+
public override void DisabledPeriodic() => Scheduler.Instance.Run();
13+
14+
// This function is called periodically during autonomous
15+
public override void AutonomousPeriodic() => Scheduler.Instance.Run();
16+
17+
// This function is called when the disabled button is hit.
18+
public override void DisabledInit() { }
19+
20+
// This function is called periodically during operator control
21+
public override void TeleopPeriodic() => Scheduler.Instance.Run();
22+
23+
// This function is called periodically during test mode
24+
public override void TestPeriodic() => LiveWindow.LiveWindow.Run();
25+
}
26+
}

WPILib.Extras/SubsystemCommand.cs

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using WPILib.Commands;
3+
4+
namespace WPILib.Extras
5+
{
6+
/// <summary>
7+
/// A <see cref="SubsystemCommand"/> depends on the given <see cref="Subsystem"/>.
8+
/// It stores a local reference to its <see cref="Subsystem" /> to ease access.
9+
/// </summary>
10+
public abstract class SubsystemCommand : Command
11+
{
12+
protected readonly Subsystem m_subsystem;
13+
14+
/// <summary>
15+
/// Creates a new <see cref="SubsystemCommand"/> which will depend on the given <see cref="Subsystem"/>.
16+
/// </summary>
17+
/// <param name="action">The <see cref="Action"/> to run.</param>
18+
public SubsystemCommand(Subsystem subsystem)
19+
{
20+
Requires(subsystem);
21+
m_subsystem = subsystem;
22+
}
23+
24+
/// <summary>
25+
/// Creates a new <see cref="SubsystemCommand"/> with a specific name,
26+
/// which will depend on the given <see cref="Subsystem"/>.
27+
/// </summary>
28+
/// <param name="subsystem">The <see cref="Subsystem"/> to require.</param>
29+
/// <param name="name">The name for the command.</param>
30+
public SubsystemCommand(Subsystem subsystem, string name)
31+
: base(name)
32+
{
33+
Requires(subsystem);
34+
m_subsystem = subsystem;
35+
}
36+
}
37+
}

WPILib.Extras/WPILib.Extras.csproj

+2
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
<Reference Include="System.Xml" />
8181
</ItemGroup>
8282
<ItemGroup>
83+
<Compile Include="SubsystemCommand.cs" />
8384
<Compile Include="ActionCommand.cs" />
8485
<Compile Include="AttributedCommandModel\AttributedRobot.cs" />
8586
<Compile Include="AttributedCommandModel\ButtonMethod.cs" />
@@ -89,6 +90,7 @@
8990
<Compile Include="AttributedCommandModel\RunCommandAttribute.cs" />
9091
<Compile Include="AttributedCommandModel\RunCommandOnJoystickAttribute.cs" />
9192
<Compile Include="AttributedCommandModel\RunCommandOnNetworkKeyAttribute.cs" />
93+
<Compile Include="CommandRobot.cs" />
9294
<Compile Include="LabVIEWRobot.cs" />
9395
<Compile Include="NavX\AHRS.cs" />
9496
<Compile Include="NavX\ContinuousAngleTracker.cs" />

0 commit comments

Comments
 (0)