Unit 5: Basic FRC Coding
An API, or Application Programming Interface, is a set of rules and definitions that allows software components to communicate. It's the "contract" that tells you how to interact with a library or a service.
Imagine you're at a restaurant. You are the "application," and the kitchen is the "library" or "service" that can perform tasks. How do you communicate with the kitchen?
The menu lists what the kitchen can do for you (`getSteak()`), what it needs from you (`rare`, `medium`, `well-done`), and what you'll get in return (a cooked steak). You don't need to know how the chef cooks it—you just need to know how to order from the menu. The API is that menu.
This is a key distinction:
When you use a WPILib motor controller class, the library is the code provided by WPILib, and the API is the list of public methods like `.set()`, `.get()`, and `.setInverted()` that you can call.
In FRC, the API for a device is the set of public methods you can call on its object. This is how we command our hardware.
// We create a Spark motor controller object
Spark myMotor = new Spark(0);
// The API for the 'Spark' class includes these public methods:
myMotor.set(0.5); // Sets the motor to 50% forward speed
myMotor.setInverted(true); // Inverts the motor's direction
double speed = myMotor.get(); // Gets the current set speed of the motor
myMotor.stopMotor(); // A convenience method to stop the motor
// We interact with the motor controller ONLY through its defined API.
APIs provide abstraction. You don't need to know the complex, low-level details of how a Spark motor controller processes a CAN signal. You just need to know that calling `.set(0.5)` makes it spin. The API hides the complexity, letting you focus on your robot's logic.
Question: Which of the following best describes the API of the `Spark` motor controller class in WPILib?