Table of Contents

Getting started with FRC coding and control

(Version 1, Joan and Rich, 11/2022)

This page describes how to get started coding with FRC robotics. It was written assuming C++, so some minor mods will be needed if coding in Java. Links and physics remain the same!

This does not cover learning how to code in Java or C++ per se and assumes some familiarity with programming terminology.

Downloads

WPILib Package

Download the WPILib package and install it. It will include VS code. https://docs.wpilib.org/en/stable/docs/zero-to-robot/step-2/index.html

Driver Station Software

Instructions to download the driver station software: https://docs.wpilib.org/en/stable/docs/software/driverstation/driver-station.html

Wiring Setup Diagram of how to wire up a testbed https://docs.wpilib.org/en/stable/docs/zero-to-robot/step-1/how-to-wire-a-robot.html (note: this confusingly shows a POE injector AND a barrel jack pigtail for powering the radio, but only one or the other is needed).

Our setup for the 2022 season code testbed:

Code Documentation

You will write code for the robot in an appropriate development environment. FRC assumes you are using the Visual Studio environment.

Most of the interfacing with the physical library is in the WPILib code libraries, including path planning and more. Read those documents carefully before embarking on trying to create any low-level hardware interfaces. In particular be careful of bus IDs and read up on how the CAN bus works. CAN bus IDs need to be unique, and some types of hardware are hard-coded to certain CAN bus IDs.

Visual Studio commands and interface https://docs.wpilib.org/en/stable/docs/software/vscode-overview/wpilib-commands-vscode.html

Detailed function documentation https://first.wpi.edu/wpilib/allwpilib/docs/release/cpp/modules.html

Team codebase: https://github.com/Team1452

RoboRio documentation https://docs.wpilib.org/en/stable/docs/software/roborio-info/index.html

WPILib documentation for how to use various hardware https://docs.wpilib.org/en/stable/docs/software/hardware-apis/index.html

WPILib Github https://github.com/wpilibsuite

WPILib C++ examples on github https://github.com/wpilibsuite/allwpilib/tree/main/wpilibcExamples/src/main/cpp/examples

Third-party writeup (may be a bit dated) https://stemrobotics.cs.pdx.edu/sites/default/files/WPILib_programming.pdf

Creating a Project

Open VSCode

Click the WPILib icon (top right of the screen, or use ctrl+shift+p and type “wpilib”)

Choose “WPILib: Create a new project”

Click on “Select a project type (Example or Template)” and use the menu that appears at the top to select the desired example/template.

Fill in base folder, Project name, and team number (1452).

Deploying Robot Code

Turn on the robot's main breaker. It will need a moment to boot up.

Attach the RoboRIO to the computer with an ethernet connector (or to the radio's wifi network).

Click the WPILib icon (top right of the screen, or use ctrl+shift+p and type “wpilib”)

Choose “WPILib: Deploy Robot Code”.

Additional info here: https://docs.wpilib.org/en/stable/docs/software/vscode-overview/deploying-robot-code.html

Wait until you see “Build Successful” at the bottom of the screen. If you get a connection error, you may need to disable the Windows firewall.

Running Code on the Robot

First, you need to install the driver station software as noted in the “Downloads” section.

Then, do the following:

Turn on the robot's main breaker. It will need a moment to boot up. Attach the RoboRio to the computer with an ethernet connector (or to the radio's wifi network). Attach the computer to a joystick (or other controller) via USB Launch the driver station software and choose the mode (Teleoperated, Autonomous, Practice, or Test) that corresponds to the code you want to run (for example, if your code is in TeleopPeriodic(), use Teleoperated mode).

In the center of the window, check that Communications, Robot Code, and Joysticks are all green. If Communications is red, you may need to disable the Windows firewall.

BE SURE THE ROBOT IS SAFE TO RUN THINGS THAT MIGHT PLAUSIBLY HAPPEN (e.g. motors spin, servos going to an unexpected position, etc) since code will start immediately upon upload. Plan ahead for the fact that what the code WILL do might or might not be what you THINK it will do.

After doing safety checks, click “Enable” to begin running your robot code.

Using Hardware Lights for Debugging

Here is what the lights on the RoboRio mean: https://docs.wpilib.org/en/stable/docs/hardware/hardware-basics/status-lights-ref.html#

Telemetry for Debugging

If you want to see the value of a variable (“printing out” for debugging) add this to your code:

std::cout « “hello”;

This will display “hello” in a window on the driver’s station. You can do similar things with values of variables, etc. For example, to print out the variable foobar with a fixed width of 10 characters, use

std::cout « “foobar = ” « std::setw(10) « foobar « '\n';

or if you don’t care about formatting and just want one value per line,

std::cout « foobar « foobar « '\n';

For viewing this output, see: https://docs.wpilib.org/en/stable/docs/software/vscode-overview/viewing-console-output.html

More advanced telemetry options are explained at https://docs.wpilib.org/en/stable/docs/software/telemetry/index.html

Test #1. Spin a Motor

This is a basic test to see if the core hardware can spin a motor. It is a hardware equivalent of the software standard test of printing out, “Hello world.” Try this first to see if you have gotten all the downloads and setup right. The motor will spin very fast - be sure it is free to turn and away from people's hands and clothing, with the shaft somewhere it will spin without hitting anything. Also be sure it is mounted to something so that it does not go flying when turned on.

Minimum hardware needed

More on Motors

SparkMAX motor controllers https://docs.revrobotics.com/sparkmax/gs-sm/wiring-the-spark-max

NEO motors: https://www.revrobotics.com/content/docs/REV-21-1650-DS.pdf

Connect up the Motor

https://docs.wpilib.org/en/stable/docs/zero-to-robot/step-1/how-to-wire-a-robot.html#can-devices

The motor should also be mounted to something. It can jump off the table if spun up suddenly.

Assign the motor number to the Sparc controller via USB. Note that this requires a Windows machine and installing a driver: https://docs.revrobotics.com/sparkmax/rev-hardware-client/getting-started-with-the-rev-hardware-client#installation-instructions (If the motor has a number sticker on it that may already have been done.)

It is good practice to note the CAN bus motor number on the motor controller with a sticker.

Code

Use the “MotorControl” example. https://github.com/wpilibsuite/allwpilib/blob/main/wpilibcExamples/src/main/cpp/examples/MotorControl/cpp/Robot.cpp

In the line

frc::PWMSparkMax m_motor{0};

change the 0 to your motor’s CAN bus ID. Leave the joystick at 0 though unless you have two joysticks, in which case they would be numbered 0 and 1.

Test #2. Move a Servo

This test is the minimal one needed to try out moving a servo with FRC hardware.

Minimum hardware needed:

Physical Connections

Connect up the servo to a PWM port on the RoboRio and to power. This is described generally here: https://docs.wpilib.org/en/stable/docs/hardware/sensors/digital-inputs-hardware.html

Code

Modify the MotorControl example to control a servo, as described here (be sure to switch the examples from Java to C++ if using C++): https://docs.wpilib.org/en/stable/docs/software/hardware-apis/motors/servos.html

Add #include <frc/Servo.h> With the other library inclusions.

Next, change the lines in the motor code from:

frc::PWMSparkMax m_motor{0};

to

frc::Servo exampleServo {1};

where you also would change the {1} to correspond to the PWM port on the RoboRio you are using.

Also change the line:

m_motor.Set(m_stick.GetY()); to exampleServo.Set( (1 + m_stick.GetX() ) / 2);

or to do both motor and servo demos, add these lines AFTER the existing lines.

If you have both:

m_motor.Set(m_stick.GetY());

exampleServo.Set( (1 + m_stick.GetX() ) / 2);

then in this example the X direction on the joystick will control the servo position, and the Y direction on the joystick will control the motor speed/direction.