Intro to FRC Programming - Romi
  • INTRODUCTION
    • Overview
    • Important Links
  • 💽Setup
    • Setting up the Romi's Software
    • Downloading Essential Tools
    • APCS vs FRC
  • How To Code in VSC
    • VSC- Intro
    • A Tour Through VSC
    • Creating a Regular Java Project
  • Intro to Java
    • What is Java?
    • Beginning Steps
    • 🟩Fundimentals of Java
      • Variables
      • Operations
      • Methods
      • Comments
      • If Statements and Conditions
      • Boolean Zen
      • Loops
      • Challenge- Create a Box House
    • 🟨Advanced Concepts
      • Objects
      • Scanners
      • Null Objects
      • Arrays
      • Errors
      • For-Each Loops
    • 🟥Object Oriented Programming
      • Basics of OOP
      • Instance Classes
      • Static Classes
  • 🕑Romi Curriculum- Timed Base
    • Romi Curriculum- Introduction
    • Creating a WPILIB Project
    • Running Your Code
    • The Robot Class
    • Subsystems
      • RomiDrivetrain
    • Cool stuff i will rename this category later
      • Spark Motors
      • PIDs
      • External Controllers
      • Encoders
  • 🖥️Romi Curriculum- Command Based
    • Command Based Code
    • RobotContainer
    • Commands
    • CommandScheduler
  • UNRELATED IMPORTANT CODING KNOWLEDGE
    • Constants
  • SAMPLE CODE
    • Tank Drive Example
      • RobotContainer
      • TankDriveSubsystem
      • MoveRobotCommand
    • Worktop Systems Sample Java Code
      • Belt Elevator Sample Code
      • Rotating Arm Sample Code
Powered by GitBook
On this page
  • void GetInstance()
  • Challenge— Write a GetInstance() Method
  • void Periodic()
  • Default Commands
  • SetDefaultCommand(Command c)
  • Challenge: Write a
  1. Romi Curriculum- Timed Base

Subsystems

Subsystems are helper classes that directly control the robot's hardware. Typically, there should be a subsystem for each major component of the robot's hardware (e.g., elevator, drivetrain, etc.) They can vary in size and functionality, but are designed as Singletons (classes with only one instance at a time) to prevent issues with syncing and even more unnecessary tears.

Subsystems' designs can have a ton of variation— a ClimbSubsystem subsystem wouldn't (hopefully) be the same length or design as something like a DriveSubsystem command.

void GetInstance()

As mentioned back in APCS vs FRC, people use GetInstance() to create objects. If you didn't read it, GetInstance() just returns an instance of an object. It's just a getter/accessor method in Encapsulation.

Challenge— Write a GetInstance() Method

Let's say you have a TankDriveSubsystem, and it has over a thousand lines of code. Now, create a GetInstance() method for it!

All your code should do is:

Note: This is only one potential answer. There could be dozens of others, but as long as they return an instance, they're fine.

Hint #1

If a variable in a class is static, all objects from that class will have access to this variable. However, static variables can be taxing, so use them sparingly (cough cough— subsystem object cough cough)

Answer + Explanation
/* class header */
/* This is what I alluded to in Hint #1. */
private static TankDriveSubsystem subsystem;

/* this should only run once- for creating the subsystem object above. */
public TankDriveSubsystem() {
    /* [code] */
}

/* Returns a DriveSubsystem instance. */
public static TankDriveSubsystem getInstance() {
    if (subsystem != null) {
      subsystem = new TankDriveSubsystem(); /* create the aforementioned instance */
    }
    
    return subsystem;
}

void Periodic()

The Periodic() method is continuously called by CommandScheduler while the subsystem is in use by a command. Because this method runs frequently, optimize the code for the sake of you three months later when rereading your code, the compiler, and the rest of the programmers.

Default Commands

When no other commands are using a subsystem, it automatically runs its default command. Default Commands are commands that are run when nothing is using the subsystem. They're useful for handling background tasks, such as preventing gravity from dragging down an arm while idle.

It would be a shame if I "taught" you what default commands were without actually explaining how to set them! That's where SetDefaultCommand() becomes relevant!

SetDefaultCommand(Command c)

SetDefaultCommand() has one parameter, c. Take a guess at what this method does with this command! (sets default command to whatever's in its parameters)


Challenge: Write a

Hint #1

Expandable

PreviousThe Robot ClassNextRomiDrivetrain

Last updated 1 month ago

🕑