Skip to content

Subsystems

Mohammad Awwad edited this page Mar 21, 2021 · 3 revisions

What are Subsystems?

Subsystems allow us to organize related code into a single file and allows us to maintain it for longer periods of time. A subsystem is an abstraction for a collection of robot hardware that operates together as a unit. Doing so makes the code more scalable over time and can be much more convenient than writing all your code into one file. A good example of a Subsystem would be your Drivetrain since all parts of the drivetrain need to work together to make your robot drivable it makes sense to all be put into a file together in this case you would name the file DriveTrain.java and it would be under the folder called subsystems.

What to Include

We will continue to be using the Drivetrain example.

A Drivetrain usually consist of 4 - 6 Motors meaning 2-3 on each side, which are controlled each by a motor controller. They typically would also include encoders which are sensors used for calculating distance. A Gyroscope would also be often used to calculate the angle of the robot.

To conclude a Drivetrain subsystem would typically include:

  1. Motors
  2. Motor Controllers
  3. Encoders
  4. Gyros

Subsystems are the Backbones of your Code

In each subsystem you would have methods called voids which are reusable parts of your code that can do pretty much anything you want to create a method simply add private void exampleMethod(){...yourCodeHere...} methods don't always need to be private it really depends on your needs, they can be public, static and final

A good example of a method would be one that sets your robots left motors speed and right motors speed, instead of individually setting each motors speed each time.

public void setLeftMotors(speed){ 
 //speed is a parameter/argument that can be passed to other parts of the code     
 leftMotor1.set(ControlMode.PercentOutput, speed); 
 leftMotor2.set(ControlMode.PercentOutput, speed); 
 leftMotor3.set(ControlMode.PercentOutput, speed); 
}

After completely setting up your subsystem, your methods can later be called in commands that can take controller inputs to and pass them to your methods to control your robot.

setLeftMotors(yAxisLeftStick); 
setRightMotos(yAxisRightStick);
Clone this wiki locally