Hardware Academy
← Back to labs
Robotics

Robot simulator

Write a controller in JavaScript and watch a differential-drive robot run it with ultrasonic, line and encoder sensors.

About 60 min

Simulated robot in the Open room arena

Ready. Press Run.

Lab goal: 0 / 100 cm without a crash

Rays turn green when an echo returns within 150 cm and red (dashed) when the way is open. Amber dots are line sensors over the tape; the dashed amber ring is the goal zone.

Preset

Cruise, slow down when something is ahead, and turn away from the nearest short ray.

Arena

Tab inserts two spaces. Press Esc, then Tab, to move on from the editor.

Speed

Telemetry

t
0.00 s
x
40.0 cm
y
40.0 cm
θ
28.6°
Ultrasonic L / F / R
159.7 / 216.4 / 135.1 cm
Line L / R
0 / 0
Encoders L / R
0 / 0 ticks
Crashed
no
Controller reference
  • control(s, memory) → { left, right }, each -1..1
  • s.ultrasonic.left / front / right: cm from the robot's edge (rays at +45°, 0°, -45°)
  • s.line.left / right: 1 over the tape, else 0
  • s.encoders.left / right: ticks, 10 per cm
  • s.yawRate: rad/s, positive turning left
  • s.pose.x / y / theta: cm and rad (-π..π), y points up
  • s.goal.x / y: only in arenas with a goal
  • s.t: seconds since reset
  • memory: an object kept between calls until Reset

Sensors only change between calls, so a loop that waits for one never ends. Loops stop with an error after 1,000,000 passes in one call; to wait, remember the state in memory and check again next tick.

The loop runs at 100 Hz. Wheel base 12 cm, top speed 40 cm/s, body radius 7 cm. Your code is saved in this browser; Load preset replaces it.