Class Mechanism

java.lang.Object
org.wpilib.commands3.Mechanism

public class Mechanism extends Object
Generic base class to represent mechanisms on a robot. Commands can require sole ownership of a mechanism; when a command that requires a mechanism is running, no other commands may use it at the same time.

Even though this class is named "Mechanism", it may be used to represent other physical hardware on a robot that should be controlled with commands - for example, an LED strip or a vision processor that can switch between different pipelines could be represented as mechanisms.

  • Constructor Details

    • Mechanism

      protected Mechanism()
      Creates a new mechanism registered with the default scheduler instance and named using the name of the class. Intended to be used by subclasses to get sane defaults without needing to manually declare a constructor.
    • Mechanism

      public Mechanism(String name)
      Creates a new mechanism, registered with the default scheduler instance.
      Parameters:
      name - The name of the mechanism. Cannot be null.
    • Mechanism

      public Mechanism(String name, Scheduler scheduler)
      Creates a new mechanism, registered with the given scheduler instance.
      Parameters:
      name - The name of the mechanism. Cannot be null.
      scheduler - The registered scheduler. Cannot be null.
  • Method Details

    • getName

      public String getName()
      Gets the name of this mechanism.
      Returns:
      The name of the mechanism.
    • setDefaultCommand

      public void setDefaultCommand(Command defaultCommand)
      Sets the default command to run on the mechanism when no other command is scheduled. The default command's priority is effectively the minimum allowable priority for any command requiring a mechanism. For this reason, it's recommended that a default command have a priority less than Command.DEFAULT_PRIORITY so it doesn't prevent low-priority commands from running.

      The default command is initially an idle command that only owns the mechanism without doing anything. This command has the lowest possible priority to allow any other command to run.

      Parameters:
      defaultCommand - the new default command
    • getDefaultCommand

      Gets the default command that was set by the latest call to setDefaultCommand(Command).
      Returns:
      The currently configured default command
    • run

      Starts building a command that requires this mechanism.
      Parameters:
      commandBody - The main function body of the command.
      Returns:
      The command builder, for further configuration.
    • runRepeatedly

      Starts building a command that requires this mechanism. The given function will be called repeatedly in an infinite loop. Useful for building commands that don't need state or multiple stages of logic.
      Parameters:
      loopBody - The body of the infinite loop.
      Returns:
      The command builder, for further configuration.
    • idle

      public Command idle()
      Returns a command that idles this mechanism until another command claims it. The idle command has the lowest priority and can be interrupted by any other command.

      The default command for every mechanism is an idle command unless a different default command has been configured.

      Returns:
      A new idle command.
    • idleFor

      public Command idleFor(Time duration)
      Returns a command that idles this mechanism for the given duration of time.
      Parameters:
      duration - How long the mechanism should idle for.
      Returns:
      A new idle command.
    • getRunningCommands

      Gets all running commands that require this mechanism. Commands are returned in the order in which they were scheduled. The returned list is read-only. Every command in the list will have been scheduled by the previous entry in the list or by intermediate commands that do not require the mechanism.
      Returns:
      The currently running commands that require the mechanism.
    • toString

      public String toString()
      Overrides:
      toString in class Object