Subsystems
Subsystems - Understanding the Foundation
Subsystems form the building blocks of command-based programming. Each subsystem models a physical part of the robot and provides safe, organized methods to control it. Subsystems can vary in scope—from a single motor to an entire mechanism—depending on how you choose to structure your code.
Key Concept: One subsystem per mechanism. Each subsystem manages its own hardware and state.
Subsystem Structure & Code Examples
📦 Basic Subsystem Example
ExampleSubsystem.javaJAVA
🔧 Hardware Instantiation
Motors, sensors, and other hardware objects are declared as private fields at the top of the class.
TalonFX motor = new TalonFX(1);⚙️ Configuration Location
Motor configurations, current limits, and mode settings go in the constructor to run once at startup.
motor.getConfigurator()
.apply(config);🔄 Periodic Method
Runs every 20ms (50Hz). Use for telemetry, monitoring, and updating dashboard values - not for control!
SmartDashboard.putNumber(
"Value", sensor.get());Workshop Implementation
Before & After: Implementation
Before
- • Basic WPILib project structure
- • No hardware integration
- • No subsystem implementation
After
- • Complete Arm subsystem class
- • TalonFX motor (ID: 31) configured
- • CANcoder sensor (ID: 22) integrated
- • Basic voltage control methods
Loading file...
Code Walkthrough
Hardware Setup:
- • TalonFX Motor: Main drive motor with integrated controller
- • CANcoder: Absolute position feedback sensor
- • Remote Sensor: CANcoder connected as remote feedback
Key Methods:
- • setVoltage(): Direct voltage control for basic movement
- • stop(): Safe motor stop with neutral output
- • periodic(): Understand that periodic runs every robot loop
This subsystem is ready for command integration! Next, we'll add commands to control this Arm subsystem through user input.