This tutorial starts our journey into the world of inverse kinematics. There are countless ways to approach this problem, but they all starts with forward kinematics.
Inverse kinematics takes a point in space, and tells you how to move your arm to reach it. Forward kinematics solves the opposite, dual problem. Knowing how you are moving your arm, it tells which point in space it reaches.
The other post in this series can be found here:
- Part 1. An Introduction to Procedural Animations
- Part 2. The Mathematics of Forward Kinematics
- Part 3. Implementing Forward Kinematics
- Part 4. An Introduction to Gradient Descent
- Part 5. Inverse Kinematics for Robotic Arms
- Part 6. Inverse Kinematics for Tentacles
- Part 7. Inverse Kinematics for Spider Legs 🚧 (work in progress!)
At the end of this post you can find a link to download all the assets and scenes necessary to replicate this tutorial.
Robotic Arm
Inverse kinematics has been originally applied to control robotic arms. For this reason, this tutorial will make assumptions and use terminology related to robotics. This, however, does not limit the possible applications of inverse kinematics. Non-robotic scenarios, such as human arms, spider spiders and tentacles, are still possible.
First of all, let’s start showing what we mean with the term “robotic arm”:
The picture above shows a typical robotic arm, made of “limbs” connected by “joints”. Since the robotic arm from the picture has five independent joints, it is said to have five degrees of freedom. Each joint is controller by a motor, which allows to moves the connected link to a certain angle.
Without losing generalisation, we can draw a precise schematics of the joints. In this particular tutorial, we will assume that each joint can only rotate on a single axis.
The tool attached at the end of the robotic arm is known as end effector. Depending on the context, it can be counted or not as a degree of freedom. In this tutorial, the end effector will not be considered since we will focus solely on the reaching movement.
Forward Kinematics
In this toy example, each joint is able to rotate on a specific axis. The state of each joint is hence measured as an angle. By rotating each joint to a specific angle, we cause the end effector to reach different points in space. Knowing where the end effector is, given the angles of the joints, is known as forward kinematics.
The forward kinematics is an “easy” problem. This means that for each set of angles, there is one and only one result, which can be calculated with no ambiguity. Understanding how a robotic arm moves depending on the inputs we provide to its motors is an essential step to find a solution to its dual problem of inverse kinematics.
Geometrical Interpretation
Before writing even a single line of code, we need to understand the Mathematics behind forward kinematics. And even before that, we need to understand what that means spatially and geometrically.
Since visualising rotations in 3D is not that easy, let’s start with a simple robotic arm that lies in a 2D space. A robotic arm has a “resting position”; that is the configuration when all the joints are rotated back to their “zero angle”.
The diagram above shows a robotic arm with three degrees of freedom. Each joint is rotated to its zero angle, resulting in this initial configuration. We can see how such configuration changes by rotating the first joint at by degrees. This causes the entire chain of joints and links anchored to to move accordingly.
It is important to notice that the motors attached to other joints have not moved. Each joint contributes to the local rotation of its forward chain of links. The following diagram shows how the configuration changes when the second joint rotates by degrees.
While only determines the position of , is affected by both and . The rotational frame of reference (the red and blue arrows) are oriented according to the sum of the rotations of the earlier chain of links they are connected to.
The Maths
From the previous diagrams it should be clear to solve the problem of forward kinematics, we need to be able to calculate the position of nested objects due to their rotation.
Let’s see how this is calculated with just two joints. Once solved for two, we can just replicate it in sequence to solve chains of any length.
Let’s start with the easy case, the one in which the first joint is in its starting position. This means that , like in the following diagram:
This means that, simply:
When is not zero, what we have to do is rotate the distance vector at rest around by degrees:
Mathematically we can write this as:
We will see later how we can use the function AngleAxis
(Unity Documentation), without messing up with trigonometry.
By replicating the same logic, we can derive the equation for :
And finally, the general equation:
We will see in the next part of this tutorial, Implementing Forward Kinematics, how that equation will translated nicely to C# code.
❓ Forward Kinematics in 2D
If you are familiar with rotations in 2D, this can be done with trigonometry:
For more information on the derivation, you can read A Gentle Primer on 2D Rotations.
❓ What about the Denavit-Hartenberg matrix?
If you have a background in Engineering, you might have approached this problem differently. Forward and Inverse kinematics problems are very common, and there are several standardised approaches to solve them. One of them associates four parameters to each joint, called the Denavit-Hartenberg parameters (Wikipedia). They are easy to manipulate in a matrix format, and are perfect to solve inverse kinematics analitically.
This tutorial, however, will not rely on them. Solving a Denavit-Hartenberg matrix requires more Mathematics than most programmers are willing to do. The approach chosen relies on gradient descent, which is a more general optimisation algorithm.
📚 Recommended Books
Other Resources
- Part 1. An Introduction to Procedural Animations
- Part 2. The Mathematics of Forward Kinematics
- Part 3. Implementing Forward Kinematics
- Part 4. An Introduction to Gradient Descent
- Part 5. Inverse Kinematics for Robotic Arms
- Part 6. Inverse Kinematics for Tentacles
- Part 7. Inverse Kinematics for Spider Legs 🚧 (work in progress!)
Become a Patron!
Patreon You can download the Unity project for this tutorial on Patreon.
Credits for the 3D model of the robotic arm goes to Petr P.
Leave a Reply