What is Simulation?
Simulation enables you to:- Test robot code on your development computer
- Develop and debug without access to robot hardware
- Validate control algorithms before deployment
- Run automated tests in CI/CD pipelines
- Visualize robot behavior through GUI tools
HALSIM Extension System
The simulation framework uses a modular extension system where each extension is a dynamically loaded library that can register callbacks to update HAL (Hardware Abstraction Layer) data.How Extensions Work
The robot program loads simulation extensions by reading theHALSIM_EXTENSIONS environment variable, which contains paths to extension libraries:
- Linux/Mac: Paths separated by colons (
:) - Windows: Paths separated by semicolons (
;)
Extension Entry Point
All simulation extensions must implement theHALSIM_InitExtension() function as their entry point:
- Be wrapped in an
extern "C"block for C++ compatibility - Include
__declspec(dllexport)on Windows platforms - Return
0on successful initialization,-1on failure
Extension Registration
Extensions can register themselves with the HAL to enable inter-extension communication:HAL_RegisterExtension will trigger listeners.
Built-in Extensions
WPILib includes several built-in simulation extensions:| Extension | Description |
|---|---|
| halsim_ds_socket | Allows the real Driver Station to control the robot program |
| halsim_gui | Provides the simulation GUI for visualizing and controlling hardware |
| halsim_ws_client | WebSockets client for transmitting hardware state over the network |
| halsim_ws_server | WebSockets server for transmitting hardware state over the network |
| halsim_ws_core | WebSockets library used by other extensions (not directly usable) |
| halsim_xrp | Client supporting the XRP protocol for controlling XRP robots |
Using HALSIM Functions
Extensions interact with the HAL using simulation-specific functions found inhal/simulation. These functions allow you to:
- Feed data into the HAL: Update sensor values, device states, etc.
- Register callbacks: Respond to HAL data changes
- Simulate hardware: Implement realistic device behavior
Example: Accelerometer Data
TheAccelerometerData header provides functions to update X, Y, and Z axis values:
Callbacks and Threading
Some HALSIM functions accept callbacks that are invoked when HAL data changes. These callbacks:- Are called synchronously in the same thread as the robot program
- Can cause loop overruns if they perform long operations
- Should complete quickly to avoid blocking the main thread
Simulation Architecture
The WPILib codebase is organized to support both real hardware and simulation:- athena: Code that runs on the roboRIO
- sim: Code that runs on your development computer
- shared: Platform-independent code shared between both
Next Steps
Running Examples
Learn how to run example programs in simulation
Custom Extensions
Create your own simulation extensions
GUI Tools
Explore the simulation GUI and visualization tools