Gray Matter
WorkshopHardware Simulation
WPILib 2027 is still in alpha: these pages change as the APIs settle.
LESSON 14

Hardware Simulation

Your laptop stands in for the robot controller and drives the real motors. You start the program, pick Teleop, enable it, and press the buttons bound in your MyTeleop.

Branchmech-2-Commands11 minutes
You’ll need
  • The mech-2-Commands branch checked out and building.
  • A bench checked in Tuner X, CANivore on this laptop: Hardware Setup and Motor Setup.
  • An Xbox-style controller plugged into the same laptop.

What mechanism are you working on?

The lesson below is written for the one you pick. Switch back any time to read it for the other.

Hardware simulation stands in for the robot controller and nothing else. Every motor request leaves the laptop over USB, crosses the CANivore onto the CAN bus, and lands in the Kraken motor controller.

Hand over the CAN bus

Make sure to turn off CANivore USB in Tuner X. If you leave it on, Tuner X fights Hardware Sim for the devices, and your code will never be able to control them.

It is the switch you turned on in Hardware Setup.

WATCH OUT · REAL MOTORS

Clear the bench before you enable

Enable the robot, hold a trigger, and a geared arm swings under 6 volts. Clear the path it travels and keep a hand near Disable. If anything looks wrong, disable first.

Run it

  1. In Tuner X, turn CANivore USB off. You should see: your devices drop out of the Tuner X list. They come back once Hardware Sim is running, and Tuner X plots them as usual.
  2. Plug the controller into the laptop. Check the mechanism is powered and its path clear.
  3. Open the … menu at the end of the editor toolbar and choose Hardware Sim Robot Code. Not Simulate Robot Code, one line above it: that one fakes the hardware, and you want the real motors.
Hardware Sim, not Simulate
The VS Code editor toolbar overflow menu, with Hardware Sim Robot Code circled in red and Simulate Robot Code directly above it
The two sit next to each other. Simulate Robot Code runs against simulated hardware and nothing on the bench moves.
  1. A Pick extensions to run prompt appears. Leave Sim GUI ticked and press OK. You should see: a simulation window and ********** Robot program startup complete ********** in the console.
  2. Drag your controller out of System Joysticks and drop it on Joystick[0]. Your Robot.java reads new CommandXboxController(0), so port 0 is the one it hears. Leave Map gamepad ticked.
Drop it on Joystick[0]
The simulation window, with an arrow from Xbox Controller in System Joysticks to Joystick[0] in the Joysticks panel
Once it lands, the axis rows change as you move the sticks.
  1. In Robot State, pick Teleoperated, then choose your OpMode from the dropdown under it. You should see: OpMode GOOD beside the dropdown, and ********** Starting OpMode Teleop ********** in the console. That print is your constructor running.
Teleoperated, then the OpMode
The simulation window with Teleoperated highlighted in Robot State and the Teleop OpMode selected in the dropdown below it
Other Devices on the right lists Talon FX 31, CANcoder 32 and Talon FX 21. Real devices on the bench, reached from a program running on your laptop. That panel is empty under plain Simulate Robot Code.
  1. Click Enable. You should see: nothing move. Every command here hangs off a button, and none is down.
  2. Now run it. Hold the buttons you bound, whichever ones those turned out to be on your mechanism. You should see: the mechanism move while a button is down, and do whatever that binding's whileFalse says when you let go.
  3. Click Disable. Leave the program running for now.

Two common failures:

  • Teleop is selected and no motor turns. CANivore USB is still on, or the controller is not on port 0, or the robot is still disabled.
  • Nothing on the bench moves, and it all looks fine. Check Other Devices in the simulation window. Empty means you started Simulate Robot Code rather than Hardware Sim Robot Code, so the program is driving motors that do not exist. Stop it and start the right one.

The latched request

One common issue is a binding with no whileFalse, and a motor that keeps running after its command has stopped. Hold the button below, then let go.

What a release sends
ControllerDriver
nothing sent
LaptopYour codeRunning: nothing
nothing sent
USB · CANCANivoreNothing to pass on
nothing sent
Motor controllerTalonFXLast request: none yet
Motor stopped

Hold the button.

Releasing the button cancels the command, and canceling sends nothing to the motor. The TalonFX keeps applying the last request it received until a new one arrives. whileFalse(stop()) sends that new one.

Check your work

Walk your bindings once more. You are done when each one repeats itself.

Check

You should see

  • The mechanism moving while a button is held, every time you hold it.
  • The same mechanism doing whatever that binding's whileFalse names when you let go, every time.

Deploy to a SystemCore

A robot runs the same program, deployed the same way. Connect the laptop to the robot, open the same … menu, and choose Deploy Robot Code instead of the hardware sim entry. You should see: BUILD SUCCESSFUL, and the robot runs your OpModes from the driver station.

The usual failure is a deploy that cannot find the robot. Check the team number in .wpilib/wpilib_preferences.json, and check that the laptop is on the robot's network.

Check yourself

In hardware simulation, what is being simulated and what is real?

What setting must be turned OFF in Tuner X before running your code in hardware simulation?

A binding has no whileFalse. You release the button and the mechanism keeps going at 6 V. Which fix makes the release stop it?

Pick an answer for each.