Part 1: Model, Sensor and Error
Does a model derived on paper predict what a real swinging rod does?
Table of contents
Table of contents
Mission Statement
A steel rod swings freely on a bearing while a high-resolution magnetic encoder watches its angle.
We build the rod’s equation of motion from first principles, simulate it, and hold the simulation up against the measured swing. Along the way we answer a second question: how much of what the encoder reports is the pendulum, and how much is noise?
The Setup
| Part | Choice |
|---|---|
| Pendulum | 8 mm EN31 steel rod, pivoting at one end |
| Bearings | 608ZZ |
| Encoder | MT6835, 21-bit magnetic (one tick ≈ 0.00017°) |
| Controller | ESP32, reading the encoder over SPI |
| Actuation | None. The rod is released and left alone |
Scope and Assumptions
- The rod is rigid and uniform.
- The angle is measured from the vertical, hanging straight down.
- No motor, no input. Only gravity and friction act on the rod.
The Model
The textbook pendulum puts all of its mass at a single point on the end of a weightless string. A rod spreads its mass along its whole length. How much does that change the swing?
The Schematic
What are we modelling, and which quantities describe it?
A rod of mass \(m\) and length \(L\) hangs from a pivot at its top end. Its position at any instant is a single number, the angle \(\theta\) between the rod and the vertical. A free rigid body has six degrees of freedom, but the pivot pins the top of the rod in place and the bearing lets it turn about one axis only, so one degree of freedom is left, and \(\theta\) is it. Because the rod is uniform, its weight \(mg\) acts at its midpoint, a distance \(L/2\) from the pivot, and always points straight down.
The rest of the model, one step per page:
- Force Decomposition: which part of gravity turns the rod
- Torque: how hard gravity twists it
- Moment of Inertia: how hard the rod is to spin up
- Equation of Motion: the angular acceleration at any angle
The Simulation
Released from a known angle, what does the model say the rod will do?
The equation gives the acceleration at one instant. To get the whole swing, step it forward in tiny increments of time \(\Delta t\). At each step:
- The current angle gives the torque, and the torque gives the angular acceleration: \(\alpha = \tau / I\)
- The acceleration nudges the angular velocity: \(\omega \leftarrow \omega + \alpha\,\Delta t\)
- The angular velocity nudges the angle: \(\theta \leftarrow \theta + \omega\,\Delta t\)
This is Euler’s method. Step 3 comes straight from the geometry of the arc. The centre of mass travels a distance \(s = r\theta\), so its speed is \(v = r\omega\). Advancing the distance by \(v\,\Delta t\) and dividing back by \(r\) leaves exactly \(\theta + \omega\,\Delta t\).
Nothing in the loop needs to know when the rod changes direction. Once the rod passes the bottom, \(\sin\theta\) changes sign, the acceleration reverses, and the swing turns around by itself.
torque = -mass * gravity * lever_arm * math.sin(theta)
alpha = torque / inertia
omega = omega + alpha * dt
theta = theta + omega * dt
The same four lines as equations, with time written out:
\[\begin{aligned} \tau(t) &= -m\,g\,\ell\,\sin\theta(t) \\ \alpha(t) &= \tau(t) / I \\ \omega(t+\Delta t) &= \omega(t) + \alpha(t)\,\Delta t \\ \theta(t+\Delta t) &= \theta(t) + \omega(t+\Delta t)\,\Delta t \end{aligned}\]Here \(\ell = L/2\) is the lever arm, the distance from the pivot to the centre of mass, and \(I\) is the rod’s moment of inertia about the pivot. The angle is updated with the new angular velocity, the one just computed in the line above it.
The 0.3 m rod, released from rest at 20°, with \(\Delta t = 1\) ms:
This curve is the model’s prediction. Next, the encoder records the real rod, and we find out how close the prediction comes.
Swing Time
How long does one full swing take, and how do we know the loop gets it right?
- Swing Time: timing the swing from the loop, and checking it by hand for small swings
Energy Check
If energy only changes form, does the loop keep the books balanced?
- Energy Check: the rod’s speed from its energy, and whether the loop invents or leaks any over 5 s
The Sensor
With the rod at rest, any change in the reading is noise. Logging the still rod tells us how noisy the encoder is, and whether that noise is random. From there, we look at what happens when speed is computed from successive angle readings.
Model vs Reality
Release the rod from a known angle, record the swing, and compare it with the model’s prediction. Wherever the two disagree, the model is missing something.
Prerequisites
- Angular motion: angle, angular velocity, angular acceleration
- Torque
- Moment of inertia
- Integration as a sum of thin slices
- Mean, variance and autocorrelation