Skip to main content

CommandScheduler

The CommandScheduler class runs commands, updates subsystems, and manages gamepad-related commands. The main functions you will be using are scheduleCommand(), registerGamepads(), registerSubsystems(), and run().

Root

  • Extends: N/A
  • Declaration: object
  • Package: com.atomicrobotics.cflib

Variables

runningCommands

runningCommands keeps track of all of the commands that are currently running.

  • Default value: mutableListOf<Command>()
  • Type: val
  • Declaration: private val

commandsToSchedule

commandsToSchedule keeps track of all of the commands that are not yet running, but need to be started.

  • Default value: mutableListOf<Command>()
  • Type: val
  • Declaration: private val

commandsToCancel

commandsToCancel is the list of all of the commands that are currently running but should be cancelled.

  • Default value: mutableMapOf<Command, Boolean>()
  • Type: val
  • Declaration: private val

gamepads

gamepads is the list of the gamepads that are actively being used.

  • Default value: mutableListOf<GamepadEx>()
  • Type: val
  • Declaration: private val

subsystems

subsystems is the list of actively running subsystems, like claws, lifts, or drivetrains

  • Default value: mutableListOf<Subsystem>()
  • Type: val
  • Declaration: private val

Functions

run()

This function should be run repeatedly; once every loop. It adds commands if the corresponding gamepad buttons are being pushedm it runs the periodic functions in Subsystems, it schedules & cancels any commands that need to be started and stopped, it executes running commands, and it does all that with a smile on its face. The reason why it uses a seperate function to cancel commands instead of cancelling them itself is because removing items from a list while iterating through that list is a wacky idea.

  • Declaration: fun
  • Parameters: none
  • Returns: Unit

scheduleCommand(command: Command)

scheduleCommand schedules a command. When multiple commands are scheduled, they are all run in parallel. Note that CommandGroups extend Command, so they can be scheduled the same way commands are scheduled.

  • Declaration: fun
  • Parameters:
    • command:
  • Returns: Unit

registerSubsystems(vararg subsystems: Subsystem)

registerSubsystems registers one or more subsystems. This list is used to run the subsystems' periodic functions.

  • Declaration: fun
  • Parameters:
    • subsystems:
      • Type: Subsystem
      • Default value: null
  • Returns: Unit

registerGamepads(vararg gamepads: GamepadEx)

registerGamepads registers one or more CustomGamepads. These gamepads will be scanned every loop to see if any buttons are being pressed, and if so, their corresponding commands will be scheduled.

  • Declaration: fun
  • Parameters:
    • gamepads:
      • Type: GamepadEx
      • Default value: null
  • Returns: Unit

unregisterAll()

unregisterAll removes every subsystem and gamepad. This function should generally only be used when an OpMode ends.

  • Declaration: fun
  • Parameters: none
  • Returns: Unit

cancelAll()

cancelAll cancels every command. This function should generally only be used when an OpMode ends.

  • Declaration: fun
  • Parameters: none
  • Returns: Unit

hasCommands()

hasCommands returns whether there are commands that are running.

  • Declaration: fun
  • Parameters: none
  • Returns: Boolean

cancelCommands()

cancelCommands cancels every command in the commandsToCancel list.

  • Declaration: private fun
  • Parameters: none
  • Returns: Unit

initCommand(command: Command)

initCommand initializes a command. This function first scans to find any conflics (other commands using the same subsystem). It then checks to see if any of those commands are not interruptible. If some of them aren't interruptible, it ends the initialization process and does not schedule the new command. Otherwise, it cancels the conflicts, runs the new command's start function, and adds it to the list of runningCommands.

  • Declaration: private fun
  • Parameters:
    • command:
  • Returns: Unit

cancel(command: Command, interrupted: Boolean = false)

cancel ends a command and removes it from the runningCommands list.

  • Declaration: private fun
  • Parameters:
    • command:
    • interrupted:
      • Type: Boolean
      • Default value: false
  • Returns: Unit

updateGamepads()

updateGamepads runs the update function for each gamepad

  • Declaration: private fun
  • Parameters: none
  • Returns: Unit

updateSubsystems()

updateSubsystems runs the periodic function for each subsystem and the inUsePeriodic function for each subsystem being used by at least one command.

  • Declaration: private fun
  • Parameters: none
  • Returns: Unit

findCommand(check: (Command) -> Boolean, commands: List<Command> = runningCommands)

Calls the findCommands() function and uses the first result, or null if there are none.

  • Declaration: private fun
  • Parameters:
    • check: The check function used on each command
      • Type: lambda
      • Returns: boolean
      • Parameters:
      • Default value: null
    • commands: The list of commands to scan
  • Returns: The first Command in the list (as defined by the commands parameter) that is of the same type as the check parameter, or null if there are none.

findCommands(check: (Command) -> Boolean, commands : List<Command> = runningCommands): List<Command>

Returns a list of every command in the given list that passes a check. Also scans CommandGroups by recursively calling itself.

  • Declaration: private fun
  • Parameters:
    • check: The check function used on each command
      • Type: lambda
      • Returns: boolean
      • Parameters:
      • Default value: null
    • commands: The list of commands to scan
  • Returns: mutableListOf<Command> containing all of the commands that passed the check, or empty if none passed.

*Definition in com.atomicrobotics.cflib/CommandScheduler.kt