Skip to content

Commit

Permalink
Additional support for command-based robots (#116)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
msoucy authored and ThadHouse committed Feb 19, 2017
1 parent cc6700f commit 62ce5dc
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
26 changes: 26 additions & 0 deletions WPILib.Extras/CommandRobot.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using WPILib.Commands;

namespace WPILib.Extras
{
/// <summary>
/// Implements the boilerplate needed to use an <see cref="IterativeRobot"/>
/// with the command-based model, by running the scheduler as needed.
/// </summary>
public abstract class CommandRobot : IterativeRobot
{
// This function is called periodically while disabled
public override void DisabledPeriodic() => Scheduler.Instance.Run();

// This function is called periodically during autonomous
public override void AutonomousPeriodic() => Scheduler.Instance.Run();

// This function is called when the disabled button is hit.
public override void DisabledInit() { }

// This function is called periodically during operator control
public override void TeleopPeriodic() => Scheduler.Instance.Run();

// This function is called periodically during test mode
public override void TestPeriodic() => LiveWindow.LiveWindow.Run();
}
}
37 changes: 37 additions & 0 deletions WPILib.Extras/SubsystemCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using WPILib.Commands;

namespace WPILib.Extras
{
/// <summary>
/// A <see cref="SubsystemCommand"/> depends on the given <see cref="Subsystem"/>.
/// It stores a local reference to its <see cref="Subsystem" /> to ease access.
/// </summary>
public abstract class SubsystemCommand : Command
{
protected readonly Subsystem m_subsystem;

/// <summary>
/// Creates a new <see cref="SubsystemCommand"/> which will depend on the given <see cref="Subsystem"/>.
/// </summary>
/// <param name="action">The <see cref="Action"/> to run.</param>
public SubsystemCommand(Subsystem subsystem)
{
Requires(subsystem);
m_subsystem = subsystem;
}

/// <summary>
/// Creates a new <see cref="SubsystemCommand"/> with a specific name,
/// which will depend on the given <see cref="Subsystem"/>.
/// </summary>
/// <param name="subsystem">The <see cref="Subsystem"/> to require.</param>
/// <param name="name">The name for the command.</param>
public SubsystemCommand(Subsystem subsystem, string name)
: base(name)
{
Requires(subsystem);
m_subsystem = subsystem;
}
}
}
2 changes: 2 additions & 0 deletions WPILib.Extras/WPILib.Extras.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="SubsystemCommand.cs" />
<Compile Include="ActionCommand.cs" />
<Compile Include="AttributedCommandModel\AttributedRobot.cs" />
<Compile Include="AttributedCommandModel\ButtonMethod.cs" />
Expand All @@ -89,6 +90,7 @@
<Compile Include="AttributedCommandModel\RunCommandAttribute.cs" />
<Compile Include="AttributedCommandModel\RunCommandOnJoystickAttribute.cs" />
<Compile Include="AttributedCommandModel\RunCommandOnNetworkKeyAttribute.cs" />
<Compile Include="CommandRobot.cs" />
<Compile Include="LabVIEWRobot.cs" />
<Compile Include="NavX\AHRS.cs" />
<Compile Include="NavX\ContinuousAngleTracker.cs" />
Expand Down

0 comments on commit 62ce5dc

Please sign in to comment.