Skip to content

Subsystems

Subsystems are a unit of organization for your code. A subsystem represents a hardware component on your robot. Although they are completely optional, it is highly recommended that you use them. They provide a great alternative to the "robot class" antipattern.

Creating Subsystems

To create a subsystem, just implement the Subsystem interface! You can optionally implement the initialize and/or periodic functions as well.

kotlin
class MySubsystem : Subsystem {
    // put hardware, commands, etc here
    
    override fun initialize() {
        // initialization logic (runs on init)
    }
    
    override fun periodic() {
        // periodic logic (runs every loop)
    }
}

Registering Subsystems

To register a subsystem in your OpMode, pass it to a SubsystemComponent as one of your components. A SubsystemComponent can take one or many subsystems. Registering a subsystem ensures that its initialize and periodic functions are called at the appropriate times.

Subsystems as Requirements

It is generally best to have a subsystem as the requirement for the commands in that subsystem. For example:

kotlin
val open = SetPositon(claw, 1.0).requires(this)

However, if you have multiple degrees of freedom in one subsystem, commands for each degree of freedom should get its own requirement. A simple way to do this is by using the hardware objects as requirements. See the following example.

kotlin
val openClaw = SetPosition(clawServo, 1.0).requires(clawServo)
val closeClaw = SetPosition(clawServo, 0.0).requires(clawServo)

val pivotLeft = SetPosition(pivotServo, 0.0).requires(pivotServo)
val pivotRight = SetPostion(pivotServo, 1.0).requires(pivotServo)

Subsystem Groups

NextFTC provides a SubsystemGroup class that can be used to group multiple subsystems into a single object. This is useful for commands that require multiple subsystems to be active at the same time. It can also be used to simplify the process of adding multiple subsystems to a SubsystemComponent.

For examples of SubsystemGroups, see the corresponding guide page.