How do I make the !%&*@-ing robot move??

If you’re lucky enough to have written code from this guide that compiles, make sure that your robot is in a position where it will not hurt anyone if something goes wrong when you run it. I am not responsible for any damaged persons or property.

Red indicates a note to myself or unfinished section. Apologies for sharing this guide prematurely :)

Before we get to that we need to cover some background info...

What is a “Black Box”

“Black Box” is not a technical term. It is a way of referring to some abstract thing whose task is known, but the way that it has been designed internally is unknown.

You treat a car like a “black box” as well. When you drive a car you are never aware of what is happening on the inside from gears shifting to the engine turning to the... well I actually don’t know what else, because I’ve never built a car. Although you don’t know what is happening on the inside, you know there are certain rules that need to be followed.

At no point while driving the car are you thinking about the brake pads clamping onto the wheels or the operation of your fuel injector. You only interact with the car in terms of the operations “start”, “stop”, “turn”, “park”, etc. Expert car users may also know about “signaling”!

Let’s use another example so that you might be able to see how it relates to writing our code. Consider for example, a black box whose task is to add two numbers together. We know that it takes two numbers as input, and that it returns the sum as output, but we don’t know the exact way that it does this. Maybe this box sends instructions to our CPU to add the numbers together or maybe there is an elf inside the box who sees the numbers and quickly computes the sum with an abacus. The point is, we don’t need to know right now. Someone figured it out for us and now we can use it to write our programs as long as we provide it two numbers that can be added.

In order to create and maintain BIG systems, we need to allow for some (even most) details to be ignored.

/*
 *             ┌─────────┐
 * 30─────────►│    ?    │
 *             │  Black  ├────────►53
 *             │   Box   │
 * 23─────────►│    ?    │
 *             └─────────┘
 */

When writing the instructions for this robot we will use some “black boxes” that have been created for us and we will also use some that we create ourselves. By doing this we keep our code organized and easy to understand.

What is a “Class”?

Sometimes a thing that you are writing code for gets too big for one file. It becomes so big that it is no longer reasonable for ONE person, even its creator, to know exactly what every single line of code does in the program.

This is where classes become useful. Instead of having a giant soup of code, we will divide the code up into groups of variables and functions that act as “black boxes” in our system. Once we divide the code into these sections, all that the programmer will need to know is what each of these boxes can do in general, and what its role is in the overall system.

Bottom line: a class is just a way of dividing code into groups of variables and functions. By using them, we make our code organized and easy to use.

I HIGHLY RECOMMEND that you use Codecademy to become familiar with the syntax of Java and the concept of a class before moving forward. You will only need to complete sections 1-3. I will not be covering the basics of working with classes and the syntax of Java here, because I don’t think I would be able to write anything here that is better than learning by following their exercises.

Additionally, the first 9 sections of W3schools tutorials on Java Classes do a great job going further in depth if you would like additional support. It will make the rest of this guide a lot easier, because we will be working a lot with classes.

The WPI Library

Let’s remember what our main goal is. We want to connect our inputs with some kind of real mechanical motion/action in the robot. Right now this is our understanding of the system in terms of “black boxes”. It might not seem that useful to draw out our understanding at this point, but I think this helps to frame the problem that the WPI Library solves.

/*
 *                          ┌─────────────────────────────┐
 *                          │                             │
 *                          │                             ├───►Motor Movement
 * Xbox Controller─────────►│                             │
 *                          │                             ├───►Actuator Movement
 * Button──────────────────►│                             │
 *                          │             ???             ├───►Cargo Gate Opening/Closing
 * Autonomous Routine──────►│                             │
 *                          │                             │
 * On-Screen Buttons───────►│                             │
 *                          │                             │
 *                          │                             │
 *                          └─────────────────────────────┘
 */

This is a diagram of the system we will build in this guide. This collection of classes will be used to drive the robot forward at a certain speed for a certain duration when a button is pressed. To do this we will need to create our own “DriveTrain” Subsystem class, a “DriveForward” Command class, and write code in the RobotContainer class so that both these are used properly and so our button will be connected to the DriveForward command. We will cover all of this in this guide.

/*
 *                                                                    ┌─────────────────────┐
 *                                                                    │Physical Button Input│
 *                                                                    └───────┬─────────────┘
 *                                                                            │
 *                                                                            │
 * ┌────────────────────────┬───────────────┬─────────────────────────────────┼──────────────┐
 * │                        │Robot Container│                                 │              │
 * │                        └───────────────┘                                 │              │
 * │  ┌───────────────────┐                                          ┌────────▼───────┐      │
 * │  │DriveTrain Instance│                                          │Button Variable │      │
 * │  └─────┬─────────────┘                                          └────────────────┘      │
 * │        │                                                                                │
 * │        │  ┌────────────────────┬───────────────────────────────────────────────────┐    │
 * │        │  │DriveForward Command│                                                   │    │
 * │        │  ├────────────────────┘                                                   │    │
 * │        │  │                                              ┌─────┐     ┌────────┐    │    │
 * │        │  │                                              │Speed│     │Duration│    │    │
 * │        │  │                                              └──┬──┘     └───┬────┘    │    │
 * │        │  │     ┌────────────────────────────────────────┬──▼────────────▼─┐       │    │
 * │        └──┼─────►DriveForward's Local DriveTrain Variable│                 │       │    │
 * │           │     ┌────────────────────────────────────────┘                 │       │    │
 * │           │     │                                                          │       │    │
 * │           │     │                                                          │       │    │
 * │           │     │                                  ┌───────────┬───────┐   │       │    │
 * │           │     │                                  │PWMSparkMax│       │   │       │    │
 * │           │     │                                  ├───────────┘       │   │       │    │
 * │           │     │                                  │                   │   │       │    │
 * │           │     │                                  │                   │   │       │    │
 * │           │     │                                  │                   │   │       │    │
 * │           │     │                                  └──────────┬────────┘   │       │    │
 * │           │     │                                             │            │       │    │
 * │           │     └─────────────────────────────────────────────┼────────────┘       │    │
 * │           │                                                   │                    │    │
 * │           │                                                   │                    │    │
 * │           └───────────────────────────────────────────────────┼────────────────────┘    │
 * │                                                               │                         │
 * │                                                               │                         │
 * └───────────────────────────────────────────────────────────────┼─────────────────────────┘
 *                                                                 │
 *                                                                 │
 *                                                                 │
 *                                                      ┌──────────▼─────────┐
 *                                                      │Physical Motor Speed│
 *                                                      └────────────────────┘
 */
/* needs to show that the local drivetrain variable is assigned THROUGH the use of the constructor for that 
 class and show what other methods do as well*/

The Major Classes in the WPI Library

There are a few major classes used to organize the process of programming the robot.

Constants” Class

*Each type of physical part has its own class*

Subsystem” Class

RobotContainer” Class

Command” Class

RobotContainer” Class (continued...)

Completed Project Files (I have tested them on the Robo Romi)

Deploying Our Code...

I don’t think I have time for this, put some resources here about loading up the driver’s station and adding joysticks...

Further Reading