001// Copyright (c) FIRST and other WPILib contributors. 002// Open Source Software; you can modify and/or share it under the terms of 003// the WPILib BSD license file in the root directory of this project. 004 005package edu.wpi.first.wpilibj2.command; 006 007import static edu.wpi.first.wpilibj.util.ErrorMessages.requireNonNullParam; 008 009import java.util.function.BooleanSupplier; 010 011/** 012 * A command that runs a Runnable continuously. Has no end condition as-is; either subclass it or 013 * use {@link Command#withTimeout(double)} or {@link Command#withInterrupt(BooleanSupplier)} to give 014 * it one. If you only wish to execute a Runnable once, use {@link InstantCommand}. 015 */ 016public class RunCommand extends CommandBase { 017 protected final Runnable m_toRun; 018 019 /** 020 * Creates a new RunCommand. The Runnable will be run continuously until the command ends. Does 021 * not run when disabled. 022 * 023 * @param toRun the Runnable to run 024 * @param requirements the subsystems to require 025 */ 026 public RunCommand(Runnable toRun, Subsystem... requirements) { 027 m_toRun = requireNonNullParam(toRun, "toRun", "RunCommand"); 028 addRequirements(requirements); 029 } 030 031 @Override 032 public void execute() { 033 m_toRun.run(); 034 } 035}