If you have been following this blog for a while, you might have noticed some recurring themes. Inverse Kinematics is definitely one them, and I have dedicated an entire series on how to apply it to robotic arms and tentacles. If you have not read them, do not fear: this new series will be self-contained, as it reviews the problem of Inverse Kinematics from a new perspective.
You can read the rest of this online course here:
We are so used to interact with the world around us that is easy to underestimate how complex moving our hands and arms really is. In the academic literature, the task of controlling a robotic arm is known as inverse kinematics. Kinematics stands for movements, and inverse refers to the fact that we don’t usually control the arm itself. What we control are the motors that rotate each individual joint. Inverse kinematics is the task of deciding how to drive these motors to move the arm to a certain point of position. And in its general form, it is an exceptionally challenging task. To give a feeling for how challenging it is, you can think about games such as QWOP, GIRP or even Lunar Lander, where you do not decide where to go, but which muscles (or thrusters) to activate.
The task of controlling moving actuators predates even the field of robotics. It should not come as a surprise that, throughout the centuries, mathematicians and engineers have been developing countless solutions. Most 3D modelling softwares and game engines (including Unity) come with a set of tools that allow the rigging of human-like and dog-like creatures. For all different setups (such as robotic arms, tails, tentacles, wings, …) no built-in solution is usually offered.
This is why in the previous series on Procedural Animations and Inverse Kinematics, I have introduced a very general and effective solution that works on potentially any setup. But such a power comes at a cost: efficiency. One of the main criticism that the series has received is that it was too time-consuming and expensive to be used on hundreds of characters at the same time. This is why I have decided to start a new series that is specifically focused on Inverse Kinematics for arms with two degrees of freedom. The technique you will discover in this tutorial is exceptionally efficient and can truly be run on dozens (if not hundreds!) of characters at the same time.
Let’s imagine a robotic arm with two segments and two joints, like the one seen in the diagram below. At the end of the arm, there is the end effector that we want to control. We do not have direct control on the position of the end effector; we can only rotate the joints. The problem of inverse kinematics is to find the best way to rotate the joints to move the end effector to the desired position.
The solution that is proposing in this new series will only work on robotic arms with two joints. In the academic literature is often said that these robotic arms have two degrees of freedom. The reason for this will be very clear by looking at the diagram below. A robotic arm with two degrees of freedom can be modelled as a triangle, which is one of the most studied geometrical figures in geometry.
Let’s start by formalising the problem a little bit more. The two joints, and (both in black), can be rotated by (in blue) and (in green) degrees, respectively. This cause the end effector to move to a position .
We can use the three points , and to construct a triangle with internal angles , and , like the one below.
While these three angles are unknown, we know the length of all edges.
- The segment represents the arm and has length ;
- The segment represents the forearm and has length ;
- The segment represents the distance between the shoulder joint and the hand, and has length .
Knowing the three sides of a triangle is enough to find all of its angles. This is possible thanks to the law of cosines, a generalisation of Pythagora’s theorem for triangles that are not necessarily right.
The two angles that are needed to control the robotic arm are and . Let’s start with , which can be calculated using the law of cosines on :
We can refector (1) to extract :
What is now needed is to use the inverse cosine function (also known as arcosine) to find the :
With the same procedure, we can apply the law of cosine once again to find :
(3)❓ Show me the derivation for the law of cosines!
The segment divides the triangle into two right triangles: and . This proof is a natural consequence of applying Pythagora’s theorem to both triangles:
which can be rearrenged as:
Equation (6) is how the Law of cosines was transcribed in Euclid’s Proposition 12 from Book 2 of the Elements.
To get to the modern form of the equation, we need to apply some trigonometry. Since (5) is a right triangle, we can express the segment as:
where can be found by remembering that the angles of a triangle sums up to (or, equivalently, radians):
Substituting into (7) we obtain:
The last step is a consequence of the well-known co-function identity which states that:
Using the law of cosines we have calculated the values for and , which are the internal angles of the triangle created by the robotic arm. However, the angles we really need are (in blue) and (in green).
Let’s start by calculating , which is easier. It is obvious from the diagram above that and sum up to (equal to radians). This means that:
Calculating is not much different. The only different here is that we need to take into account (in purple), which is the angle of the segment . This can be calculated using the arcotangent function :
Which leads to:
The sign of the angles , and is mostly arbitrary and depends on the way each joint move.❓ Multiple solutions?
This post concludes the introduction on the Mathematics of Inverse Kinematics for robotic arms with two degrees of freedom.
The next post will explore how we can use the equations derived to efficiently move a robotic arm in Unity.
You can read the rest of this online course here:
The line art animals that have been featured in this tutorials have been inspired by the work of WithOneLine.
You can download all the assets used in this tutorial to have a fully functional robotic arm for Unity.