skip to content

FRC Swerve Drive Tutorial - Part 1

An introduction to my FRC swerve drive programming tutorial series

Overview

In this post, I will go over what this tutorial series will entail, and introduce some swerve drive concepts.

Introduction

Writing code for a swerve drive train can be difficult. This difficulty level increases exponentially when you throw in autonomous and odometry. For this reason, WPILib provides some nice swerve drive utility classes. However, what happens if the WPILib swerve libraries don't function? What if you need a greater range of customization than WPILib can offer? What if you want to truly understand how a swerve drive train functions? The solution to all of these questions, unfortunately, is to create a swerve drive train controller from scratch.

When my team (2875) wanted to use a swerve drive robot, but the WPILib libraries were not functioning (they did not allow translation and rotation at the same time), we were forced to do this. Unfortunately, we lacked programmers with any experience. This meant that we had to spend several months trial-and-erroring our way through creating a custom swerve drive train controller. To prevent future teams from needing to undertake that, I am writing this tutorial series on writing a custom swerve drive train controller.

What is Swerve Drive?

Swerve drive is a form of drive train used in FRC robotics. It is generally regarded as the most complex drive train (to build, program, and drive); however, it is also considered to be the most agile. For this to happen, swerve drive utilizes a four-wheel configuration. This four wheel configuration is by no means standard, however. Each wheel (or "module" as they are referred to) has two separate motors. One motor is used to drive the wheel (rotate it vertically), while the other motor is used to control the angle of the wheel (rotate it horizontally). This allows each wheel to independently rotate, and drive in any direction. This comes together and not only allows the robot to deftly translate and rotate, but do both at the same time.

"But wait, isn't that just mecanum?". Yes, swerve drive offers similar capabilities, however, it also offers increased speed, mobility, and traction. The sole downside of swerve drive (other than its complexity) is its lack of traction/power when compared to tank drive. In short: don't get into a ramming contest if you have a swerve drive robot - you will lose.

Components of Swerve

Swerve drive has three major components that you will learn to program throughout the course of this tutorial:

Module

The simplest (and lowest level) piece of swerve drive. This controls each individual module's (wheel) turning and driving. These are all orchestrated together by the drive train.

Train

The "conductor" for the modules. This component will control each module and make sure they work together to move the robot in the correct direction. This component also handles user input.

Odometry

The odometer is the most complex piece of the swerve drive train. This component uses wheel angles and rotations to track the robot's position on the field. This is generally a more accurate tracking method than an accelerometer.

While technically optional, if you want a PID corrected teleoperated mode or a dynamic autonomous mode, it will be necessary.

Prerequisite Knowledge

Below you can find some quick descriptions on some prerequisite knowledge (most ⇒ least important) for this tutorial series.

Vector Math

This is used extensively within swerve drive. You will need to know how to add vectors and how to convert them between the polar and Cartesian coordinate systems.

PID Loops / Controllers

PID Controllers will also be used in this tutorial. While technically not a requirement for swerve drive, it is good practice to use them on most of your motors.

Feed Forward Loops / Controllers

Anywhere you have a PID Controller, you will also need a feedforward. Since we will be applying PIDs to every motor involved in the swerve drive, the same goes for feed forwards.

In this tutorial series, we will maintain a very simple definition of a "feed forward": the direct input given to a motor WITHOUT a PID or any sort of corrective feedback.

Java Concepts

This tutorial will assume that you have (at least) a basic knowledge of java. You will need to understand functions, classes, variables, types, loops, loops, conditionals, OOP, and the WPILib libraries.

Summary

In the future installments of this tutorial series, I will go in depth to the programming and logic behind each and every component of swerve drive. Stay tuned for future posts!