By Kevin McAleer, 7 Minutes
Page last updated November 24, 2026

So far you’ve told Buddy what each joint should do. But when you reach for a cup, you don’t think “shoulder 30, elbow 60”. You think “hand there”. Kinematics is the maths that links the two, and kinematics.py does it with nothing but the math module.
This lesson’s programs run on your computer as well as on the Pico. They only need math, so you can explore the maths without the arm. Save kinematics.py from the repo in the same folder as each script, and run it with python3 on a computer, or copy it to the Pico.
kinematics.py describes Buddy as five links with lengths in millimetres, and five rotating joints:
L1 is the base column, L2 the upper arm, L3 the forearm, L4 the wrist, L5 the gripper(0, 0, L1+L2+L3+L4+L5)The link lengths in the file are placeholders (60, 110, 110, 40 and 70 mm). The mechanical design is Kev’s own, so measure your arm, from joint centre to joint centre, and change the five constants at the top of kinematics.py before you trust any position it gives you.
FK walks the chain. It starts at the base, then for each joint it rotates by the joint angle and translates along the link. Each step is a 4 by 4 matrix, and multiplying them together gives the tip’s position and orientation. In plain terms: rotate, move up a link, rotate again, move up a link, and so on.
from kinematics import forward_kinematics, L1, L2, L3, L4, L5 # kinematics.py from the repo
home = {"base": 0, "shoulder": 0, "elbow": 0,
"wrist_pitch": 0, "wrist_roll": 0, "gripper": 0}
x, y, z, roll, pitch, yaw = forward_kinematics(home)
print("Home pose: x=%.0f y=%.0f z=%.0f mm" % (x, y, z))
print("Total link length:", L1 + L2 + L3 + L4 + L5)
# Lean the arm: shoulder forward 30, elbow bent 60, wrist back 30, base turned 20
leaning = dict(home, base=20, shoulder=30, elbow=60, wrist_pitch=-30)
x, y, z, roll, pitch, yaw = forward_kinematics(leaning)
print("Leaning: x=%.0f y=%.0f z=%.0f mm" % (x, y, z))
print(" pitch=%.0f yaw=%.0f degrees" % (pitch, yaw))
At home the tip is straight up at the sum of the link lengths. The shoulder (30) plus elbow (60) plus wrist (-30) add up to a pitch of 60, because in a vertical plane the three tilts just add. The base turned 20 degrees shows up as a yaw of 20.
IK has more than one possible answer, so the code makes the problem smaller in three steps:
atan2(y, x)L4 + L5 back from the tip along the direction the hand points. Step back by that distance, and what’s left is a two-link arm, the upper arm and forearmpitch - shoulder - elbowThe triangle can bend two ways, elbow down and elbow up, and elbow_up=True picks the other one. If the target is further than L2 + L3 can reach, or too close, there’s no triangle, and inverse_kinematics returns None.
The pose is (x, y, z, roll, pitch, yaw). The arm only has five effective degrees of freedom, so the yaw has to agree with atan2(y, x), and the check rejects anything that doesn’t.
This complete program asks IK for angles, then runs FK on the answer to prove it lands back on the target.
from kinematics import forward_kinematics, inverse_kinematics # kinematics.py from the repo
# (x_mm, y_mm, z_mm, roll, pitch, yaw)
targets = [
(150.0, 0.0, 150.0, 0.0, 90.0, 0.0), # forward, hand horizontal
(220.0, 0.0, 60.0, 0.0, 90.0, 0.0), # as far forward as it can go
(100.0, 100.0, 200.0, 0.0, 45.0, 45.0), # off to the side
(400.0, 0.0, 60.0, 0.0, 90.0, 0.0), # too far away
]
for pose in targets:
angles = inverse_kinematics(pose)
print("Target", pose)
if angles is None:
print(" unreachable")
continue
print(" angles:", {name: round(a, 1) for name, a in angles.items()})
back = forward_kinematics(angles)
print(" forward check:", tuple(round(v, 1) for v in back))
# The same reachable pose, with the elbow bent the other way
flipped = inverse_kinematics(targets[0], elbow_up=True)
print("Elbow up:", {name: round(a, 1) for name, a in flipped.items()})
The forward check should match each target, apart from tiny rounding. The fourth target prints “unreachable”, because 400 mm is further than the arm can stretch.
Why this matters: now the control panel can offer a “move the gripper here” box, and the arm works out the rest.
On the Pico, Buddy.move_to_pose() wraps all of this. It solves IK, skips the gripper (a pose doesn’t say how open it is), and hands the joint targets to move_all_sync, so the motion is smooth:
buddy.move_to_pose(150, 0, 150, pitch=90, duration_ms=1500, wait=True)
If the pose can’t be reached it raises ValueError, so put it in a try block. IK knows the arm’s geometry but not your joint limits, so a solution may ask for an angle your limits clamp. You then see buddy: clamped, and the arm lands somewhere else. The repo’s demo_ik.py drives the arm through a sequence of poses this way.
Notice the first target above needs a negative shoulder angle. That’s one more reason lesson 10 told you to widen the limits.
kinematics.py to match your arm, then rerun the FK home pose. Does the height match a tape measure?x the IK program can reach at z=60. Is it close to L2 + L3?x and z, and prints the joint angles at each step.Problem: inverse_kinematics returns None for a target that looks reachable.
Solution: Leave yaw as atan2(y, x) for off-axis targets, and check the distance against L2 + L3.
Why: The yaw check rejects poses whose yaw doesn’t agree with the base angle, and the reach limit is real.
Problem: The arm ends up in the wrong place compared to the numbers.
Solution: Measure and set L1 to L5, and recheck the calibration.
Why: IK uses the link lengths you give it, and placeholder values don’t match your arm.
Quick quiz 1 of 3
What does forward kinematics compute?
Quick quiz 2 of 3
Why can inverse_kinematics return None?
Quick quiz 3 of 3
What do you change so the maths matches your own arm?
Comments