Step 1
Position, velocity & acceleration
Read a joint's encoder every 10 ms and work out how fast it is moving, without drowning in the jitter.
Position, velocity, acceleration
A joint's angle (theta, in radians) says where it is. Its angular velocity (omega, rad/s) says how fast that angle changes, and its angular acceleration (alpha, rad/s²) says how fast the velocity changes. Velocity is the derivative (rate of change) of position; acceleration is the derivative of velocity.
Finite differences
A robot never sees a smooth curve. It reads its sensors at fixed moments , one sampling period apart (10 ms here). Between two readings, the velocity is about
This is a finite difference. Apply it to the velocities and you get the acceleration. Going the other way, adding up step by step, is integration: it turns velocity back into position.
Encoders count ticks
The shoulder's angle comes from an encoder, which counts whole ticks, per revolution. encoder.read(1) gives the shoulder's count , rounded down, so the angle is . One tick is rad (0.7°).
Because the angle moves in whole ticks, every difference is a multiple of rad/s, even while the arm glides at 0.5 rad/s. Halve and those steps double.
Smoothing
A moving average of the last velocities cancels most of that jitter:
The price is lag: the average describes the last seconds, so it trails the truth by about half of that. Small is jumpy, large is late. The table gives each window's typical error, its RMS (root mean square: square the errors, average, take the square root).
| Window | 1 | 2 | 5 | 20 | 40 |
|---|---|---|---|---|---|
| RMS error (rad/s) | 0.50 | 0.25 | 0.09 | 0.14 | 0.27 |
Finite differences come back in Jacobians, step 1, where they measure how the gripper moves.
Your task
The program swings the shoulder from 0.2 to 1.0 rad and back, reading the encoder every 10 ms. Write:
ticks_to_angle(ticks, ticks_per_rev): the angle in radians.finite_difference(ts, xs): for , one value fewer thanxs.moving_average(xs, n): entry is the mean of the lastnvalues up toxs[k](fewer at the start), so it's as long asxs.
Your smoothed velocities, stored in velocity, must have an RMS error under 0.2 rad/s. Open Plots and try N_AVG = 1, 5 and 20.