Package org.wpilib.commands3
Class Mechanism
java.lang.Object
org.wpilib.commands3.Mechanism
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 Summary
ConstructorsModifierConstructorDescriptionprotectedCreates a new mechanism registered with the default scheduler instance and named using the name of the class.Creates a new mechanism, registered with the default scheduler instance.Creates a new mechanism, registered with the given scheduler instance. -
Method Summary
Modifier and TypeMethodDescriptionGets the default command that was set by the latest call tosetDefaultCommand(Command).getName()Gets the name of this mechanism.Gets all running commands that require this mechanism.idle()Returns a command that idles this mechanism until another command claims it.Returns a command that idles this mechanism for the given duration of time.Starts building a command that requires this mechanism.runRepeatedly(Runnable loopBody) Starts building a command that requires this mechanism.voidsetDefaultCommand(Command defaultCommand) Sets the default command to run on the mechanism when no other command is scheduled.toString()
-
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
Creates a new mechanism, registered with the default scheduler instance.- Parameters:
name- The name of the mechanism. Cannot be null.
-
Mechanism
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
Gets the name of this mechanism.- Returns:
- The name of the mechanism.
-
setDefaultCommand
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 thanCommand.DEFAULT_PRIORITYso 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 tosetDefaultCommand(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
Returns a command that idles this mechanism until another command claims it. The idle command hasthe lowest priorityand can be interrupted by any other command.The
default commandfor every mechanism is an idle command unless a different default command has been configured.- Returns:
- A new idle command.
-
idleFor
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
-