Gimbals have existed since ancient times, historically they worked by aligning the object you wanted to stabilise at the exact centre of the concentric rings such that gravity always held it upright no matter which way the item moved or was turned.
Nowadays they also use computerised motion sensors (an “inertial measurement unit”) for additional stability, which then activate motors which help keep the object still and upright.
Latest Answers