# WPILib Example Projects¶

Warning

While every attempt is made to keep WPILib examples functional, they are not intended to be used “as-is.” At the very least, robot-specific constants will need to be changed for the code to work on a user robot. Many empirical constants have their values “faked” for demonstration purposes. Users are strongly encouraged to write their own code (from scratch or from an existing template) rather than copy example code.

WPILib example projects demonstrate a large number of library features and use patterns. Projects range from simple demonstrations of a single functionality to complete, competition-capable robot programs. All of these examples are available in VS Code by entering Ctrl+Shift+P, then selecting WPILib: Create a new project and choosing example.

## Basic Examples¶

These examples demonstrate basic/minimal robot functionality. They are useful for beginning teams who are gaining initial familiarity with robot programming, but are highly limited in functionality.

• Arcade Drive (Java, C++): Demonstrates a simple differential drive implementation using “arcade”-style controls through the DifferentialDrive class.

• Arcade Drive Xbox Controller (Java, C++): Demonstrates the same functionality seen in the previous example, except using an XboxController instead of an ordinary joystick.

• Getting Started (Java, C++): Demonstrates a simple autonomous routine that drives forwards for two seconds at half speed.

• Mecanum Drive (Java, C++): Demonstrates a simple mecanum drive implementation using the MecanumDrive class.

• Motor Controller (Java, C++): Demonstrates how to control the output of a motor with a joystick.

• Motor Control With Encoder (Java, C++): Identical to the above example, except with the addition of an encoder to read the motor position.

• Simple Vision (Java, C++): Demonstrates how to stream video from a USB camera to the dashboard.

• Relay (Java, C++): Demonstrates the use of the Relay class to control a relay output with a set of joystick buttons.

• Solenoids (Java, C++): Demonstrates the use of the Solenoid and DoubleSolenoid classes to control solenoid outputs with a set of joystick buttons.

• TankDrive (Java): Demonstrates a simple differential drive implementation using “tank”-style controls through the DifferentialDrive class.

• Tank Drive Xbox Controller (Java, C++): Demonstrates the same functionality seen in the previous example, except using an XboxController instead of an ordinary joystick.

## Control Examples¶

These examples demonstrate WPILib implementations of common robot controls. Sensors may be present, but are not the emphasized concept of these examples.

• DifferentialDriveBot (Java, C++): Demonstrates an advanced differential drive implementation, including encoder-and-gyro odometry through the DifferentialDriveOdometry class, and composition with PID velocity control through the DifferentialDriveKinematics and PIDController classes.

• Elevator with profiled PID controller (Java, C++): Demonstrates the use of the ProfiledPIDController class to control the position of an elevator mechanism.

• Elevator with trapezoid profiled PID (Java, C++): Demonstrates the use of the TrapezoidProfile class in conjunction with a “smart motor controller” to control the position of an elevator mechanism.

• Gyro Mecanum (Java, C++): Demonstrates field-oriented control of a mecanum robot through the MecanumDrive class in conjunction with a gyro.

• MecanumBot (Java, C++): Demonstrates an advanced mecanum drive implementation, including encoder-and-gyro odometry through the MecanumDriveOdometry class, and composition with PID velocity control through the MecanumDriveKinematics and PIDController classes.

• PotentiometerPID (Java, C++): Demonstrates the use of the PIDController class and a potentiometer to control the position of an elevator mechanism.

• RamseteController (Java, C++): Demonstrates the use of the RamseteController class to follow a trajectory during the autonomous period.

• SwerveBot (Java, C++): Demonstrates an advanced swerve drive implementation, including encoder-and-gyro odometry through the SwerveDriveOdometry class, and composition with PID position and velocity control through the SwerveDriveKinematics and PIDController classes.

• UltrasonicPID (Java, C++): Demonstrates the use of the PIDController class in conjunction with an ultrasonic sensor to drive to a set distance from an object.

## Sensor Examples¶

These examples demonstrate sensor reading and data processing using WPILib. Mechanisms control may be present, but is not the emphasized concept of these examples.

• Axis Camera Sample (Java, C++): Demonstrates the use of OpenCV and an Axis Netcam to overlay a rectangle on a captured video feed and stream it to the dashboard.

• PDP CAN Monitoring (Java, C++): Demonstrates obtaining sensor information from the PDP over CAN using the PowerDistributionPanel class.

• Duty Cycle Encoder (Java, C++): Demonstrates the use of the DutyCycleEncoder class to read values from a PWM-type absolute encoder.

• DutyCycleInput (Java, C++): Demonstrates the use of the DutyCycleInput class to read the frequency and fractional duty cycle of a PWM input.

• Encoder (Java, C++): Demonstrates the use of the Encoder class to read values from a quadrature encoder.

• Gyro (Java, C++): Demonstrates the use of the AnalogGyro class to measure robot heading and stabilize driving.

• Intermediate Vision (Java, C++): Demonstrates the use of OpenCV and a USB camera to overlay a rectangle on a captured video feed and stream it to the dashboard.

• Ultrasonic (Java, C++): Demonstrates the use of the Ultrasonic class to read data from an ultrasonic sensor in conjunction with the MedianFilter class to reduce signal noise.

## Command-Based Examples¶

These examples demonstrate the use of the Command-Based framework.

• ArmBot (Java, C++): Demonstrates the use of a ProfiledPIDSubsystem to control a robot arm.

• ArmBotOffboard (Java, C++): Demonstrates the use of a TrapezoidProfileSubsystem in conjunction with a “smart motor controller” to control a robot arm.

• DriveDistanceOffboard (Java, C++): Demonstrates the use of a TrapezoidProfileCommand in conjunction with a “smart motor controller” to drive forward by a set distance with a trapezoidal motion profile.

• FrisbeeBot (Java, C++): A complete set of robot code for a simple frisbee-shooting robot typical of the 2013 FRC® game Ultimate Ascent. Demonstrates simple PID control through the PIDSubystem class.

• Gears Bot (Java, C++): A complete set of robot code for the WPI demonstration robot, GearsBot.

• Gyro Drive Commands (Java, C++): Demonstrates the use of PIDCommand and ProfiledPIDCommand in conjunction with a gyro to turn a robot to face a specified heading and to stabilize heading while driving.

• Inlined Hatchbot (Java, C++): A complete set of robot code for a simple hatch-delivery bot typical of the 2017 FRC game Steamworks. Commands are written in an “inline” style, in which explicit subclassing of Command is avoided.

• Traditional Hatchbot (Java, C++): A complete set of robot code for a simple hatch-delivery bot typical of the 2017 FRC game Steamworks. Commands are written in a “traditional” style, in which subclasses of Command are written for each robot action.

• MecanumControllerCommand (Java, C++): Demonstrates trajectory generation and following with a mecanum drive using the TrajectoryGenerator and MecanumControllerCommand classes.

• RamseteCommand (Java, C++): Demonstrates trajectory generation and following with a differential drive using the TrajectoryGenerator and RamseteCommand classes. A matching step-by-step tutorial can be found here.

• Scheduler Event Logging (Java, C++): Demonstrates the use of scheduler event actions to log dashboard event markers whenever a command starts, ends, or is interrupted.

• Select Command Example (Java, C++): Demonstrates the use of the SelectCommand class to run one of a selection of commands depending on a runtime-evaluated condition.

• SwerveControllerCommand (Java, C++): Demonstrates trajectory generation and following with a swerve drive using the TrajectoryGenerator and SwerveControllerCommand classes.

## State-Space Examples¶

These examples demonstrate the use of the State-Space Control.

• StateSpaceFlywheel (Java, C++): Demonstrates state-space control of a flywheel.

• StateSpaceFlywheelSysId (Java, C++): Demonstrates state-space control using FRC Characterization’s System Identification for controlling a flywheel.

• StateSpaceElevator (Java, C++): Demonstrates state-space control of an elevator.

• StateSpaceArm (Java, C++): Demonstrates state-space control of an Arm.

• StateSpaceDriveSimulation (Java, C++): Demonstrates state-space control of a differential drivetrain in combination with a RAMSETE path following controller and Field2d class.

## Simulation Physics Examples¶

These examples demonstrate the use of the physics simulation.

• ElevatorSimulation (Java, C++): Demonstrates the use of physics simulation with a simple elevator.

• ArmSimulation (Java, C++): Demonstrates the use of physics simulation with a simple single-jointed arm.

• StateSpaceDriveSimulation (Java, C++): Demonstrates state-space control of a differential drivetrain in combination with a RAMSETE path following controller and Field2d class.

## Miscellaneous Examples¶

These examples demonstrate miscellaneous WPILib functionality that does not fit into any of the above categories.

• Addressable LED (Java, C++): Demonstrates the use of the AddressableLED class to control RGB LEDs for robot decoration and/or driver feedback.

• DMA (C++): Demonstrates the use of DMA (Direct Memory Access) to read from sensors without using the RoboRIO’s CPU (C++ only).

• HAL (C++): Demonstrates the use of HAL (Hardware Abstraction Layer) without the use of the rest of WPILib. This example is for advanced users (C++ only).

• HID Rumble (Java, C++): Demonstrates the use of the “rumble” functionality for tactile feedback on supported HIDs (such as XboxControllers).

• PacGoat (Java, C++): A full command-based robot project from FRC Team 190’s 2014 robot. Uses the legacy version of the command framework; categorized as miscellaneous to avoid confusion.

• Shuffleboard (Java, C++): Demonstrates configuring tab/widget layouts on the “Shuffleboard” dashboard from robot code through the Shuffleboard class’s fluent builder API.

• RomiReference (Java, C++): A command based example of how to run the Romi robot.