The Secret in the torpedo
The first self-driving underwater vehicle was a weapon, and for two years it couldn't hold a straight line in depth. The fix was kept as a state secret, and it's in every robot you will ever build.
In the 1860s, in the Adriatic port of Fiume (today Rijeka, in Croatia), an English engineer named Robert Whitehead built a cigar-shaped machine that drove itself through the water on compressed air: the first modern torpedo. Steering it left and right was manageable. Depth was not. Whitehead fitted a hydrostatic valve, a disc that felt the water pressure and moved the diving rudders to push the torpedo back toward its set depth. The torpedo responded by porpoising: diving through its target depth, climbing back through it, over and over, sometimes broaching the surface or plunging into the mud.
In 1868 Whitehead added a heavy pendulum that felt the torpedo's nose tilting up or down and worked the same rudders. The hydrostat said how far off the depth was. The pendulum said how fast the torpedo was heading there. Together they settled the torpedo onto its depth and held it there. Navies paid for the design and swore to keep it confidential. In Whitehead's works it was simply called "the Secret".
What Whitehead found, mostly by trial and error, is what engineers now call feedback control. Look at the error, look at how fast the error is changing, and act on both. Every autopilot, every hovering drone and every station-keeping ROV is a descendant of that brass pendulum.
Feedback: P, D, and the I that finishes the job
The loop. A controller compares where the vehicle is with where it should be. That difference is the error. Each moment it turns the error into a thruster command, then checks again. This is Week 1's sense–plan–act loop, run hundreds of times a second.
Proportional. Push in proportion to the error. That was Whitehead's hydrostat. On its own it overshoots, because by the time the error reaches zero the vehicle is still moving, and nothing has told it to slow down. Raise the gain and it overshoots harder.
Derivative. Push against the rate of change. That was the pendulum. It brakes the vehicle as it nears the target and damps the porpoising out. Too much derivative, though, makes the response sluggish and twitchy with sensor noise.
Integral. A vehicle that's slightly buoyant, or held off by a steady current, will settle short of its target, because a small error only earns a small push. The integral term adds up error over time until the push is big enough to close the gap. Together these three terms are called PID, the most widely used controller on Earth. One caution: let the integral keep adding up while the thrusters are already at full power and it overshoots badly. That failure is called windup, and the instrument below guards against it.
Six directions. An underwater vehicle can move in six ways: surge, sway and heave (forward, sideways, up and down) and roll, pitch and yaw (the three rotations). Each direction you want to hold needs its own loop, and thrusters pointed so they can actually push that way. A vehicle with fewer independent pushes than directions is called underactuated, and it has to be clever, the way a torpedo turns with fins rather than sideways thrusters.
A vehicle dives from the surface to 20 metres and has to stay there, with a little positive buoyancy trying to lift it off. Tune the three gains yourself, or load Whitehead's two designs and a modern one, and read the overshoot, settling time and leftover error.
Fly to Rijeka on the Adriatic, once Fiume, where Whitehead's works tested torpedoes off its own pier, and where the Secret first held a machine level underwater.