Robot Arm Kinematics: From Servo PWM to Inverse Kinematics
You can make a servo arm reach a point you choose—understanding forward and inverse kinematics, the reachable workspace, and singularities before you touch hardware.
Robot arm kinematics is the geometry that connects two things a manipulator has to reconcile: the joint angles it can actually command, and the tool position you want in the world. Going one way—angles to position—is forward kinematics, and it is easy. Going the other way—position to angles—is inverse kinematics, and it is where the interesting problems live: two solutions, edges you cannot reach, and poses where the arm briefly loses control of a direction.
This path builds that understanding from the ground up. You start with the hardware—getting a servo to move to a commanded angle through a PCA9685 driver, calibration and all. Then you learn the geometry: how a two-link arm’s angles place its tool, and how to invert that to reach a target. Finally you explore it live in the simulator, dragging a target and watching the solver pick joint angles, hit the workspace boundary, and flag the unreachable.
Follow the nodes in order—each unlocks the next once you have done it, and your progress saves on this device. When you are ready to build the real thing, this path feeds straight into the 4-DOF robot arm project.
Before you start
You need trigonometry — specifically sin, cos, and atan2 — and enough programming to
write a function that returns two values. Servo experience helps but the path covers it.
Nothing here requires the physical arm; the whole geometry can be learned in the simulator.
What you will be able to do
- Compute where a two-link arm’s tool sits from its joint angles, and invert that to find the angles that reach a given point.
- Choose deliberately between the two solutions — elbow-up and elbow-down — instead of taking whichever one the maths returned.
- Predict the boundary of the reachable workspace and detect a target outside it before the arm tries to move.
- Recognise a singularity as the arm approaches full extension, where a small change in tool position demands an enormous change in joint angle.
- Measure the play in your own joints, and say how much of the gripper’s error is the mechanism rather than the maths.
Where people get stuck
Using atan instead of atan2. Plain atan cannot tell which quadrant the target is
in, so the arm reaches correctly in front and mirrors backwards behind. atan2 takes the
two components separately and gets all four quadrants right.
A domain error on acos. When the target is further away than the links can reach, the
cosine rule produces an argument outside −1 to 1 and acos returns NaN. That NaN flows into
the servo command as a garbage angle. Check reachability before the trigonometry, not after.
Confusing joint angle with servo angle. A servo’s zero is wherever its horn was fitted, and it may rotate opposite to your maths. Every joint needs a calibration offset and possibly a sign flip. Skipping this produces an arm that moves smoothly and lands nowhere near the target.
Ignoring gravity at extension. The geometry assumes the arm goes where told. A hobby servo holding a fully extended link is at its worst mechanical advantage, and the real position sags below the commanded one — most visibly at the far edge of the workspace.
Tuning a mechanical error. If the arm reaches the same point differently depending on which side it came from, no gain will fix it: that is gearbox play, multiplied by the whole length of the arm. Four degrees at a shoulder driving a 150 mm link is over 10 mm at the gripper.
Driving through a singularity. Near full extension the inverse solution becomes ill-conditioned, and a smooth tool path turns into a violent joint movement. Keep paths inside a margin of the boundary rather than up against it.
What you need
| What | Why this path needs it | Notes |
|---|---|---|
sin, cos and atan2 |
The entire mathematical toolkit | If you know what atan2 is for, you are ready |
| A function that returns two values | IK returns a pair of angles, and often two pairs | A tuple, a struct, an out-parameter — any of them |
| A protractor and a ruler | Comparing where the arm went against where it was told | The only honest test of a kinematics implementation |
| An arm and a PCA9685 (optional) | The hardware half | The geometry can be learned entirely in the simulator |
Why the path runs in this order
Servo driving comes before geometry, and it is not obvious that it should. The reason is calibration. Once you can command a joint and see where it actually lands, you have a way to test every later result against reality. Learn the geometry first and you have no way to distinguish a wrong solver from a mis-fitted servo horn — two very different faults that look identical from across the room.
Arm types come next because they explain what a “link” is. Cartesian, SCARA and delta arms solve the same problem with completely different geometry, and seeing three of them makes it clear that kinematics is about the arrangement of joints rather than about any particular arm. It also tells you why a SCARA’s IK is nearly trivial and a delta’s is not.
Inverse kinematics comes third, when both prerequisites are in place. It is the heart of the path, and it is the only node where you will spend real time on maths.
The simulator comes fourth because it makes the invisible visible. Reachability, the two elbow branches, and singularities are all things you can prove on paper and will not truly understand until you have dragged a target to the workspace edge and watched the solver’s behaviour change. Dragging is also the fastest way to find the shape of a workspace, which is an annulus rather than a disc whenever the links are unequal.
Backlash closes the path deliberately. It is the point at which you stop being able to improve the arm’s accuracy with better maths. Every node before it is about computing the right angle; this one is about the millimetres between the right angle and the right position.
Checkpoints: how to know a stage landed
| Stage | The check | What a pass looks like |
|---|---|---|
| Servo driving | Command 0°, 90° and 180° on each joint | The horn lands where commanded, and you have written down each joint’s offset and sign |
| PWM calibration | Find the pulse widths for each servo’s true end stops | Your min and max counts are per-servo, not the library default, and nothing buzzes at the limits |
| Forward kinematics | Set known angles, compute the tool position, measure it | Computed and measured agree within a few millimetres |
| Round trip | Feed a computed position back through IK | The angles you get out match the ones you put in |
| Quadrant handling | Ask for targets in all four quadrants | All four work — if two mirror, you are using atan |
| Reachability | Request a target well beyond the arm’s reach | You get a clean “unreachable”, not a NaN and not a lunge |
| Workspace shape | Map the reachable boundary in the simulator | You can describe it, and explain why unequal links give an annulus with a hole |
| Elbow branches | Reach the same point both ways | Both work, you chose one deliberately, and you can say why |
| Singularity | Drive a straight tool path through near-full extension | You have seen the joint velocity spike, and you now plan a margin |
| Backlash | Approach the same point from each direction, measure | You have a number in millimetres at the gripper — and it is not zero |
Where this path stops
This path covers the planar two-link solution solved in closed form — the geometry behind the shoulder and elbow of almost every hobby arm. It is the right thing to learn first because you can derive it, check it, and see exactly why each failure happens.
It stops short of several things you will meet next. General n-DOF kinematics uses Denavit–Hartenberg parameters and homogeneous transformation matrices to describe any chain of joints systematically, rather than solving each arm’s geometry by hand. Numerical IK — Jacobian-based iteration — is what you use when no closed-form solution exists, which is the normal situation past four or five joints; it also degrades gracefully near singularities where a closed-form solution simply breaks.
Redundancy resolution is the question of what to do when an arm has more joints than the task constrains and therefore infinitely many valid solutions — a genuinely interesting problem this path never has to face, because a two-link arm has exactly two.
And dynamics is the whole other half of manipulation: kinematics says nothing about forces, inertia, or how fast a joint can move without overshooting. Everything here assumes the arm goes where it is told, slowly enough that the assumption holds.
Learning roadmap
The path
Follow the nodes in order—each unlocks the next once you have done it. Your progress saves on this device.
0 / 5 done
Common questions
Frequently asked questions
What is the difference between forward and inverse kinematics?
Forward kinematics answers 'given these joint angles, where is the tool?' It is a direct calculation, it always has exactly one answer, and it is genuinely easy. Inverse kinematics answers the useful question — 'to put the tool here, what angles do I need?' — and it is harder in every way. It can have two answers, or none at all if the target is out of reach, or infinitely many if the arm has more joints than the task constrains. Every interesting problem in arm control lives on the inverse side.
Why does my robot arm reach the wrong way for targets behind it?
You are almost certainly using atan instead of atan2. Plain atan takes a single ratio and cannot distinguish a target in front from its mirror image behind, because both produce the same quotient — so the arm reaches correctly in two quadrants and mirrors in the other two. atan2 takes the two components separately and resolves all four quadrants. This is the single most common bug in a first IK implementation, and it presents as an arm that works perfectly until you ask for something behind it.
What is elbow-up versus elbow-down in inverse kinematics?
For a two-link arm, almost every reachable point can be reached in two different ways — with the elbow bent upward or downward — and the maths returns both because both are correct. Which one you want depends on the arm's surroundings: one may collide with the table, or with the workpiece, or twist the cable. The important thing is to choose deliberately and consistently, because silently switching between branches mid-path makes the arm flip through a large, fast, unplanned motion.
Why does my IK code return NaN?
Because the target is further away than the links can reach and the cosine rule produced an argument outside the −1 to 1 range that acos accepts. The NaN then flows straight into the servo command as a garbage angle, which is why an unreachable target so often produces a violent movement rather than a refusal to move. Test reachability before doing the trigonometry — compare the distance to the target against the sum and the difference of the link lengths — and return 'unreachable' as a real result rather than letting acos discover it.
What is a singularity on a robot arm?
A configuration where the arm momentarily loses the ability to move the tool in some direction, no matter how it moves its joints. On a two-link arm the obvious one is full extension: with the arm straight out, moving the tool further away is impossible, and moving it slightly along that direction demands an enormous change in joint angle. The practical consequence is that a smooth tool path passing near full extension turns into a violent joint motion. Keep planned paths inside a margin of the workspace boundary rather than up against it.
Why does my arm not land where the maths says it should?
Three causes, in rough order of likelihood. Calibration: a servo's zero is wherever its horn happened to be fitted, and it may rotate opposite to your convention, so every joint needs an offset and possibly a sign flip. Gravity: a hobby servo holding a fully extended link is at its worst mechanical advantage and simply sags below the commanded angle. And backlash: four degrees of play at a shoulder driving a 150 mm link is over 10 mm of error at the gripper, and it changes sign depending on which direction you approached from.