User Tools

Site Tools


control:code:tutorial

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).

Downloading the Current Codebase

To start working on an existing codebase, first clone the existing repository from github. Open a new window in VS Code and click on the source control tab on the left side of the window.

Click on the “Clone Repository” button, and paste in the URL of the git repository (for example, https://github.com/Team1452/crescendo-2024). When prompted, choose a location to download to, and the repository will be cloned to your computer. You can also use the “Open Folder” button if you have already downloaded the repository using Github Desktop or the git clone command (but not if you used “Download as Zip”).

Using Git

In order to track changes to the code and keep it accessible to other team members, it's important to make use of Git as you are coding. After making changes to the code, you need to “commit” those changes to the repository. Save your changes to all files, then return to the source control tab. You'll see a box to enter a commit message describing what you've changed, followed by a list of files with un-commited changes. Be descriptive here, so that other users will understand what you added/removed/changed and why (i.e. adding a specific feature, or fixing a specific bug). This will also help you find the commit again if you need to the previous state, or to understand when and how a bug was introduced.

Below this is a list of changes. You'll need to “stage” the changes. You can do this by hovering over each file in the list and clicking the + icon. If you skip this step, you'll get a warning asking if you want to stage all un-committed changes.

Ideally, each commit should leave the code in a working state (i.e., if you begin a process that breaks something, try to fix if before committing), but it's better to have too many commits than too few. If you know that some functionality is broken when committing, be sure to mention it in your commit message.

Once your changes have been committed locally, you still need to sync them to github. This step is necessary to make your changes available to other users, or from other computers (if you want to keep working on the code at home, for example). This step will require you to log in with an authorized github account.

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. It is always best to test new code with the robot on blocks first, so that the wheels don't touch the ground.

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

Using Hardware Lights for Debugging

Telemetry for Debugging

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

C: std::cout << "hello";

Java: System.out.println("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

C: std::cout << "foobar = " << std::setw(10) << foobar << '\n';

Java: System.out.println("foobar = " + foobar);

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

C: std::cout << foobar << '\n';

Java: System.out.println(foobar);

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

  • Battery Main robot breaker (power switch)
  • Power Distro (PDP) RoboRio ethernet cable to laptop OR frc radio, connected by a shorter ethernet cable with POE injector
  • Voltage regulator module (VRM) (powers the radio, may not be needed if using direct ethernet)
  • At least one motor/controller pair with CANbus wiring. Remember to terminate the CANbus at the PDP (or with a 120-ohm resistor between the ends of the green and yellow wires)
  • Robot Signal light (RSL) (optional, but a good idea)

More on Motors

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 in C or Java.

In the line

C: frc::PWMSparkMax m_motor{0};

Java: private static final int kMotorPort = 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 example is in C. Please add a Java equivalent.

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

Minimum hardware needed:

  • Battery Main robot breaker (power switch)
  • Power Distro RoboRio ethernet cable to laptop OR frc radio, connected by a shorter ethernet cable with POE injector Voltage regulator module (VRM) (powers the radio, may not be needed if using direct ethernet)
  • At least one servo, plugged into one of the RoboRIO's PWM ports
  • Robot Signal light (RSL) (optional, but a good idea)

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.

dokuwiki\Exception\FatalException: Allowed memory size of 134217728 bytes exhausted (tried to allocate 20480 bytes)

dokuwiki\Exception\FatalException: Allowed memory size of 134217728 bytes exhausted (tried to allocate 20480 bytes)

An unforeseen error has occured. This is most likely a bug somewhere. It might be a problem in the authplain plugin.

More info has been written to the DokuWiki error log.