Skip to main content
Torqheart
← The Lab

03 · Act · Free

A real board in the loop (hardware-in-the-loop)

The page simulates a pole that never stops falling; your ESP32 or Arduino is the brain that holds it, over USB or Bluetooth. See what a slow link does to balance.

round trip

0 ms

answers lost

0%

tilt

0.0°

Now: the virtual board in this page

This browser can't reach a board. Use Chrome or Edge on a computer (Bluetooth also works in Chrome on Android).

Download the firmware (hil-balance.ino) for Arduino IDE

controller gains (sent to the board automatically)

What's going on

Engineers don't try new control code on an expensive robot first. They connect the real controller board to a simulated robot: hardware-in-the-loop (HIL). This page does the same. The browser simulates a cart and a pole; every 20 ms it sends the tilt and position to the board, which computes a force and sends it back. While the answer is on its way, the pole keeps falling, so the link's delay becomes part of the system. No board? The in-page “virtual board” runs the firmware's exact formula.

Picture it like this

Like balancing a broom on your palm while watching yourself on a laggy video call. A little lag is fine. Half a second of lag, and the harder you move your hand the faster it falls. The way out is gentler hands.

On real robots

Real robots are full of links with delay: CAN bus, EtherCAT, Wi-Fi, remote teleoperation. Every humanoid is a pole that never stops falling, and when answers come late, gains that worked start to drop it. HIL shows that on your desk before it happens on a robot, the same way car ECUs and drone flight controllers are tested.

For engineers: the protocol, and why delay drops it

Every 20 ms the browser sends S,seq,θ(mrad),ω(mrad/s),x(mm),v(mm/s); the board answers F,seq,force(dN). All integers, so a line fits a small BLE packet and float printing never differs between boards. An answer older than the last one applied is dropped. K,kp,kd,ki,kx,kv changes gains without re-uploading.

The law is F = kp·θ + kd·ω + ki·∫θ + kx·x + kv·v, in the firmware and the virtual board alike. The site's tests compile the same .ino on a PC and check it answers exactly like the virtual board, line for line.

A delay τ multiplies the loop by e^(−sτ), taking ω_c·τ radians of phase margin at the crossover ω_c. Higher gains push ω_c up, so the margin runs out sooner; lower gains help, at the cost of a slower response. kx and kv are needed because the system is non-minimum phase: without them the pole stays up but the cart drifts off the track.

Web Serial and Web Bluetooth work in Chrome and Edge on desktop (Bluetooth also in Chrome on Android), over HTTPS, started by a click. With a real board the page steps the physics once per screen frame (about 16 ms), so an answer that lands mid-frame is used at the next one: the pole feels up to one frame more delay than the round trip shown. Nothing is scored: the server cannot replay a physical board.