Simulating robot code - frc-7603/VespaRobotics2024-25 GitHub Wiki
About
It is entirely possible to simulate robot code from the FRC VSCode. This works on all platforms.
Information Webpages:
You can find all information on the simulating the robot code here
This wiki page is a WIP!
Running the simulator
From the FRC VSCode, open the command palette by clicking on the WPILib icon or pressing Ctrl+Shift+P (or Cmd+Shift+P for macOS/Darwin systems). Search for "simulate robot code", and run it. Make sure you click the Sim GUI option if you want an option that works on all platforms. It is possible to use the real DriverStation to control the robot if you use Windows NT.
Launch the GUI by checking "Simulate Robot Code", and press OK.
NOTE:
You must have desktop support enabled to run the simulator GUI. If you are creating the file with the intention of running the simulator GUI, there will be an optional checkbox at the bottom that says "Enable Sim GUI". Ensure that it is it selected. If you have a file that has already been created without the Sim GUI enabled, simply open command pallet and type in "Change Desktop Support Enabled Setting" and select the option. Change the state to "true".
General Usage
You will be presented with a purple-looking GUI. The information on the important/understood subwindows will be listed below.
- Robot State
In this, you can select between disabled, autonomous, teleoperated, and test. These modes map to the ones in the Robot.java methods.
- Timing
Unknown, mostly seeing all timers set in code possibly. (eg. If you have a timer object)
- System joysticks
These list all joysticks that you may have connected to your computer (such as USB joysticks).
- Joysticks
This can assign joysticks to be used by the simulated robot.
Joystick mapping
USB joystick
To have a joystick be used by the code, drag and drop a joystick from the "System Joysticks" to the "Joysticks" window.
Keyboard to Joystick
If you do not have a joystick, drag it like you would a USB joystick, but you would have to map keyboard inputs.
Go to DS > Keyboard > Settings. Usually, x will be zero. Only use Axis 0 and Axis 1. Axis 0 is y (so use something like w and s), and axis 1 is x (so use something like a and d).
Robot code compilation issues:
Some robot libraries will not while the robot is being simulated. To check if the robot is NOT being simulated, you can use the RobotBase.isReal() property.
Device Simulation
There are many devices that can be simulated, such as encoders. Encoders are esentially mechanisms that count direction and senses direction.
However, the most important ones are things such as motors.
Generally, many devices that are considered "core classes" such as encoders have the class plus "Sim" at the end, and all of the constructors accept the original as the argument. (this will have a seperate wiki page soon)
Here is an example for encoders:
// Create encoder object, pin numbers 0 and 1
Encoder encoderToUse = new Encoder(0,1);
// Simulated encoder
EncoderSim encoder = new EncoderSim(encoderToUse);
For other devices, such as motors (including VictorSPX motors), you may need to use the SimDeviceSim class. You can instantiate it like so:
SimDeviceSim device = new SimDeviceSim(deviceKey, index);
The devicekey will show up in the "Other Devices" tab in the GUI to tell you what device is what, and the index is anything that needs to pass (eg. device number for the specific device, such as a motor).
Shuffleboard usage
To have Shuffleboard connect to a simulator, go to File > NetworkTables > Plugins and set server to "localhost".
For more info
See:
https://docs.wpilib.org/en/stable/docs/software/wpilib-tools/robot-simulation/simulation-gui.html