Skip to main content
WPILibC is the C++ implementation of WPILib, providing high-performance robot control with modern C++20 features.

Namespace Structure

All WPILibC classes are in the frc namespace.
#include <frc/TimedRobot.h>
#include <frc/DriverStation.h>

namespace frc {
  // All WPILib C++ classes
}

Core Robot Classes

TimedRobot

Event-driven robot framework with periodic callbacks

DriverStation

Interface to FMS and driver station data

RobotController

Access to roboRIO system-level functions

RobotBase

Base class for all robot programs

Hardware I/O

Digital I/O

ClassHeaderDescription
DigitalInputfrc/DigitalInput.hRead digital input signals
DigitalOutputfrc/DigitalOutput.hControl digital output signals
Counterfrc/Counter.hCount digital pulses and edges
Encoderfrc/Encoder.hQuadrature encoder support
DutyCyclefrc/DutyCycle.hRead PWM duty cycle signals
DutyCycleEncoderfrc/DutyCycleEncoder.hAbsolute encoders using duty cycle

Analog I/O

ClassHeaderDescription
AnalogInputfrc/AnalogInput.hRead analog voltage inputs (0-5V)
AnalogOutputfrc/AnalogOutput.hGenerate analog voltage outputs
AnalogEncoderfrc/AnalogEncoder.hAnalog absolute encoder support
AnalogPotentiometerfrc/AnalogPotentiometer.hPotentiometer input with scaling
AnalogTriggerfrc/AnalogTrigger.hTrigger actions based on analog thresholds

Motor Controllers

ClassHeaderDescription
PWMSparkMaxfrc/motorcontrol/PWMSparkMax.hControl REV Robotics SPARK MAX
PWMTalonSRXfrc/motorcontrol/PWMTalonSRX.hControl CTRE Talon SRX
PWMVictorSPXfrc/motorcontrol/PWMVictorSPX.hControl CTRE Victor SPX
Sparkfrc/motorcontrol/Spark.hGeneric SPARK controller

Pneumatics

ClassHeaderDescription
Compressorfrc/Compressor.hControl pneumatic compressor
Solenoidfrc/Solenoid.hSingle solenoid valve control
DoubleSolenoidfrc/DoubleSolenoid.hDouble solenoid valve control

Sensors

Gyroscopes

  • ADIS16448_IMU - Analog Devices IMU
  • ADIS16470_IMU - Analog Devices IMU
  • ADXRS450_Gyro - Analog Devices SPI gyro
  • AnalogGyro - Analog gyroscope

Accelerometers

  • ADXL345_I2C - I2C accelerometer
  • ADXL345_SPI - SPI accelerometer
  • ADXL362 - SPI accelerometer
  • BuiltInAccelerometer - roboRIO accelerometer

TimedRobot

The TimedRobot class provides a framework where methods are called periodically at a fixed rate.
#include <frc/TimedRobot.h>
#include <units/time.h>

namespace frc {

class TimedRobot : public IterativeRobotBase {
 public:
  // Default loop period (20ms = 50Hz)
  static constexpr auto kDefaultPeriod = 20_ms;
  
  // Constructors
  explicit TimedRobot(units::second_t period = kDefaultPeriod);
  explicit TimedRobot(units::hertz_t frequency);
  
  // Add custom periodic callback
  void AddPeriodic(std::function<void()> callback,
                   units::second_t period,
                   units::second_t offset = 0_s);
  
  void StartCompetition() override;
  void EndCompetition() override;
};

}  // namespace frc

Example Usage

#include <frc/TimedRobot.h>
#include <frc/motorcontrol/Spark.h>

class MyRobot : public frc::TimedRobot {
 public:
  void RobotInit() override {
    m_motor.SetInverted(false);
  }
  
  void TeleopPeriodic() override {
    // Called every 20ms during teleop
    m_motor.Set(0.5);
  }
  
  void DisabledInit() override {
    m_motor.Set(0.0);
  }
  
 private:
  frc::Spark m_motor{0};
};

#ifndef RUNNING_FRC_TESTS
int main() {
  return frc::StartRobot<MyRobot>();
}
#endif

DriverStation

Provides access to driver station data and FMS information.
#include <frc/DriverStation.h>

namespace frc {

class DriverStation {
 public:
  // Get singleton instance
  static DriverStation& GetInstance();
  
  // Robot state
  bool IsEnabled() const;
  bool IsDisabled() const;
  bool IsAutonomous() const;
  bool IsTeleop() const;
  bool IsTest() const;
  
  // Alliance and location
  std::optional<Alliance> GetAlliance() const;
  std::optional<int> GetLocation() const;
  
  // Match info
  double GetMatchTime() const;
  MatchType GetMatchType() const;
  int GetMatchNumber() const;
  
  // Joystick input
  double GetStickAxis(int stick, int axis) const;
  bool GetStickButton(int stick, int button) const;
  int GetStickPOV(int stick, int pov) const;
  
  enum class Alliance { kRed, kBlue };
  enum class MatchType { kNone, kPractice, kQualification, kElimination };
};

}  // namespace frc

AddressableLED

Control addressable RGB LED strips.
#include <frc/AddressableLED.h>

namespace frc {

class AddressableLED {
 public:
  explicit AddressableLED(int port);
  
  void SetLength(int length);
  void SetData(const AddressableLEDBuffer& data);
  void Start();
  void Stop();
};

class AddressableLEDBuffer {
 public:
  explicit AddressableLEDBuffer(int length);
  
  void SetRGB(int index, int r, int g, int b);
  void SetHSV(int index, int h, int s, int v);
  void SetLED(int index, const Color& color);
};

}  // namespace frc

Units Library Integration

WPILibC uses the units library for type-safe physical quantities.
#include <units/length.h>
#include <units/velocity.h>
#include <units/acceleration.h>
#include <units/angle.h>
#include <units/angular_velocity.h>
#include <units/time.h>
#include <units/voltage.h>
#include <units/current.h>

// Example usage
units::meter_t distance = 1.5_m;
units::meters_per_second_t velocity = 2.0_mps;
units::degree_t angle = 90.0_deg;
units::radian_t radians = 1.57_rad;

Filesystem

Access robot filesystem paths.
#include <frc/Filesystem.h>

namespace frc::filesystem {

// Get deploy directory path
std::string GetDeployDirectory();

// Get operating directory
std::string GetOperatingDirectory();

// Get launch directory
std::string GetLaunchDirectory();

}  // namespace frc::filesystem

Smart Pointers and RAII

WPILibC uses RAII principles and smart pointers for automatic resource management.
#include <frc/DigitalInput.h>
#include <memory>

class MyRobot : public frc::TimedRobot {
 private:
  // Automatically cleaned up when MyRobot is destroyed
  frc::DigitalInput m_limitSwitch{0};
  
  // Or use smart pointers for dynamic allocation
  std::unique_ptr<frc::DigitalInput> m_sensor;
  
  void RobotInit() override {
    m_sensor = std::make_unique<frc::DigitalInput>(1);
  }
};

Error Handling

WPILibC uses exceptions for error handling.
#include <frc/Errors.h>

try {
  frc::DigitalInput input(99);  // Invalid channel
} catch (const std::exception& e) {
  frc::ReportError(frc::err::Error, "{}", e.what());
}

Data Logging

ClassHeaderDescription
DataLogManagerfrc/DataLogManager.hManage robot data logging
Alertfrc/Alert.hCreate and manage robot alerts

Utility Classes

ClassHeaderDescription
Timerfrc/Timer.hTiming and delay utilities
Watchdogfrc/Watchdog.hMonitor loop timing
Notifierfrc/Notifier.hSchedule callbacks
EventLoopfrc/event/EventLoop.hEvent-driven programming

Source Code

View the full source code on GitHub:

WPILibJ API

Java implementation of WPILib

Command Framework

Command-based programming framework

HAL API

Hardware Abstraction Layer

WPIMath

Mathematics and control libraries

Build docs developers (and LLMs) love