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-Commands13 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.

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 on your bench.

No part of this project models the arm, and nothing here works without powered motors.

Hand over the CAN bus

Two programs cannot own the CAN bus at once. Leave CANivore USB on and Tuner X keeps the bus, so your code reaches nothing.

Open Tuner X, open the CANivore settings, and turn CANivore USB off. Your devices drop off the Tuner X list, which is correct. It is the switch you turned on for bench testing, covered on Motor 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. Check that nothing is clamped in the way, and keep a hand near Disable. If anything looks wrong, disable first and ask afterwards.

Run it

  1. In Tuner X, turn CANivore USB off and close the control window. You should see: your devices drop out of the Tuner X list.
  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 code reads new CommandNiDsXboxController(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 twitch as you move the sticks. That is the fastest way to tell the GUI is reading the controller and not the keyboard.
  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 before you walk away. Leave the program running for the next section.

Two things go wrong here more than anything else.

  • Teleop is selected and no motor turns. CANivore USB is still on, or the controller is not on port 0, or the driver station still says Disabled. A burst of CAN message is stale at startup is normal; the same line a few seconds later means the bus is unreachable.
  • 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.

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.

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?

Pick an answer for each.