This site is version 0.0.1. Most of the code on this site is drafted by an agent and needs review. I apologize for the AI-speak, we are working on rewriting it!
viv-lang.org

← Posts

demoAIRobotics

A cart-pole, balanced by a PD controller

The pole starts 15 degrees from upright. A controller that pushes the cart in proportion to the pole's angle and rate, and a little toward the origin, catches it in under two seconds and holds it. At a fifth of the gains it falls.

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

The dynamics, the integrator and the drawing come from lib/cartpole, loaded with import cartpole; this post adds the controller and the tests. The force is , with the cart terms small and positive: to move the cart left toward the origin, the controller must first lean the pole left, and the way to do that is a push to the right.

The gains are top-level constants, so viv run --set K_THETA=30 ... tries another controller without editing the program.

Printed

balanced: [0.0226366, 0.0114201, 0.00684689]
fallen: [7.25936, 69.127, -1.68533]
Cart-pole, released at 15 degrees

 

0.0 / 6.0 s

Tests 3 of 3 pass

  • after two seconds the pole stays within two degrees of upright (passes)
  • and the cart comes back to within 10 cm of where it started (passes)
  • at a fifth of the gains the pole falls over (passes)

Checked when the site was built: 3 of 3 tests passed, in 0.11 s. Download 2026-09-22-cart-pole-balanced.viv