Extension Structure
A simulation extension is a dynamically loaded library (.dll on Windows, .so on Linux, .dylib on macOS) with a specific entry point that the robot program calls during initialization.Minimum Extension Code
Every extension must implement theHALSIM_InitExtension function:
Function Requirements
- Return value:
0for success,-1for failure - extern “C”: Prevents C++ name mangling
- __declspec(dllexport): Required on Windows for DLL exports
- No parameters: The function takes no arguments
Complete Extension Example
Here’s a more complete example based on the XRP extension:Using HALSIM Callback Functions
Available Device APIs
Simulation functions are available for many HAL devices inhal/simulation/:
- AccelerometerData: Update accelerometer X, Y, Z values
- AnalogInData: Set analog input voltages
- DIOData: Control digital I/O states
- EncoderData: Update encoder counts and rates
- GyroData: Set gyro angles and rates
- I2CData: Simulate I2C device communication
- PWMData: Monitor PWM outputs
- SPIData: Simulate SPI device communication
- SimDeviceData: Create custom simulation devices
Registering Callbacks
Callbacks allow you to respond to HAL data changes:Setting HAL Values
Update simulated hardware values from your extension:Building Your Extension
Setup build.gradle
Create abuild.gradle file for your extension. Use the XRP extension as a template:
Key Configuration
- pluginName: Must match your extension’s library name
- includeWpiutil: Include WPIUtil library
- linkage: ‘shared’: Link with shared libraries (important!)
- roborio check: Disable building for roboRIO platform
Building the Extension
Using Your Custom Extension
In a Robot Project
After publishing WPILib locally, add your extension to a robot project’sbuild.gradle:
Configure Robot Project
Follow the development builds guide to configure your robot project to use your local WPILib build. Add tobuild.gradle:
Extension Communication
Registering Your Extension
Allow other extensions to detect your extension:Listening for Other Extensions
Detect when specific extensions are loaded:Advanced: I2C/SPI Simulation
Simulate real I2C or SPI devices:Performance Considerations
Callback Performance
Callbacks run synchronously in the robot program thread:- Keep callbacks fast - avoid heavy computation
- Don’t block - no long delays or I/O waits
- Process data in background threads if needed
Use Background Processing
For heavy operations:Example Extensions in WPILib
Study these built-in extensions for reference:- halsim_xrp: Simple client protocol implementation
- halsim_ws_server: WebSocket server with HTTP support
- halsim_gui: Complex GUI with ImGui integration
- halsim_ds_socket: Driver Station protocol implementation
allwpilib/simulation/halsim_*/
Next Steps
Simulation Overview
Review the simulation architecture
Running Examples
Test your extension with example programs