in maths, tutorial, Unity3D

Inverse Kinematics in 2D – Part 1

Share Button

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:

Introduction

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 QWOPGIRP 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.

Inverse Kinematics

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, A and B (both in black), can be rotated by A (in blue) and B (in green) degrees, respectively. This cause the end effector to move to a position C.

Internal Angles

We can use the three points A, B and C to construct a triangle with internal angles \alpha, \beta and \gamma, like the one below.

While these three angles are unknown, we know the length of all edges.

  • The segment \overline{AB} represents the arm and has length c;
  • The segment \overline{BC} represents the forearm and has length a;
  • The segment \overline{CA} represents the distance between the shoulder joint and the hand, and has length b.

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 \alpha and \beta. Let’s start with \alpha, which can be calculated using the law of cosines on \alpha:

(1)   \begin{equation*}  a^2  = b^2 + c^2 - 2 bc \cos{\alpha} \end{equation*}

We can refector (1) to extract \cos\alpha:

    \[ \begin{split} \cos{\alpha} & =\frac{a^2-b^2-c^2}{-2bc} = \\ & =\boxed{\frac{b^2+c^2-a^2}{2bc}} \end{split} \]

What is now needed is to use the inverse cosine function \cos^{-1} (also known as arcosine) to find the \alpha:

(2)   \begin{equation*}  \alpha = \cos^{-1}{\left(\boxed{\frac{b^2+c^2-a^2}{2bc}}\right)} \end{equation*}

With the same procedure, we can apply the law of cosine once again to find \beta:

    \[b^2 = a^2 + c^2 - 2ac \cos{\beta}\]

    \[\cos{\beta}=\frac{a^2 + c^2 -b^2}{2ac}\]

(3)   \begin{equation*}  \beta=\cos^{-1}{\left(\frac{a^2 + c^2 -b^2}{2ac}\right)} \end{equation*}

❓ Show me the derivation for the law of cosines!

The segment h divides the triangle \overset{\triangle}{ABC} into two right triangles: \overset{\triangle}{ABH} and \overset{\triangle}{BCH}. This proof is a natural consequence of applying Pythagora’s theorem to both triangles:

(4)   \begin{equation*}  \begin{split} \overset{\triangle}{ABH}:  & \\ & c^2 = h^2 + \left(b-d\right)^2 \\ & h^2 = \boxed{ c^2 -  \left(b-d\right)^2} \end{split} \end{equation*}

(5)   \begin{equation*}  \begin{split} \overset{\triangle}{BCH}:  & \\ & a^2 = d^2 + h^2 \\ & h^2 = \boxed{a^2 -d^2} \end{split} \end{equation*}

Both (4) and (5) are now expressed in terms of h^2. We can equate them to obtain:

    \[ \begin{split} \boxed{a^2 -d^2} & =  \boxed{c^2 -  \left(b-d\right)^2} \\ a^2 - d^2 &= c^2 - \left(b^2 + d^2 - 2 bd\right) \\ a^2 - d^2 & = c^2 - b^2 - d^2 + 2 bd \\ a^2 \xcancel{- d^2} & = c^2 - b^2 \xcancel{- d^2} + 2 bd\\ a^2  & = c^2 - b^2 + 2 bd \\ \end{split} \]

which can be rearrenged as:

(6)   \begin{equation*}  c^2=a^2+b^2-2bd \end{equation*}

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 d as:

(7)   \begin{equation*}  d = a \sin{\delta} \end{equation*}

where \delta can be found by remembering that the angles of a triangle sums up to 180^{\circ} (or, equivalently, \pi radians):

(8)   \begin{equation*}  \begin{align} \delta + \gamma + \frac{\pi}{2} = \pi \\ \delta = \pi - \frac{\pi}{2} - \gamma \\ \delta = \frac{\pi}{2} - \gamma \end{align} \end{equation*}

Substituting \delta into (7) we obtain:

(9)   \begin{equation*}  \begin{split} d & = a \sin{\delta}\\ d & = a \boxed{\sin{\left(\frac{\pi}{2} - \gamma\right)}}\\ d & = a \boxed{\cos{\gamma}} \end{split} \end{equation*}

The last step is a consequence of the well-known co-function identity which states that:

    \[ \sin\left(\frac{\pi}{2} - \gamma\right) = \cos{\gamma} \]

We can now use  (9) to replace d in (6), which gives us the modern form of the law of cosines:

(10)   \begin{equation*}  \begin{split} c^2&=a^2+b^2-2b\boxed{d} \\ c^2&=a^2+b^2-2b \boxed{a \cos{\gamma}} \\ c^2&=a^2+b^2-2ab \cos{\gamma} \end{split} \end{equation*}

Joint Angles

Using the law of cosines we have calculated the values for \alpha and beta, which are the internal angles of the triangle created by the robotic arm. However, the angles we really need are A (in blue) and B (in green).

 

Let’s start by calculating B, which is easier. It is obvious from the diagram above that \beta and B sum up to 180^{\circ} (equal to \pi radians). This means that:

(11)   \begin{equation*}  \begin{split} \beta + B = \pi \\ B = \pi - \beta \end{split} \end{equation*}

Calculating A is not much different. The only different here is that we need to take into account A' (in purple), which is the angle of the segment \overline{AC}. This can be calculated using the arcotangent function \tan^{-1}:

(12)   \begin{equation*}  A' = \tan^{-1}{\left(\frac{C_Y-A_Y}{C_X-A_X}\right)} \end{equation*}

Which leads to:

(13)   \begin{equation*}  A = \alpha + A' \end{equation*}

The sign of the angles A, A' and B is mostly arbitrary and depends on the way each joint move.

❓ Multiple solutions?
The problem of inverse kinematics typically admits several solutions. Even in this case with only two degrees of freedom, we have two distinct solutions.

While the angles A and B are indeed different, the derivation remains essentially the same with the only exception of  (13) and (11).

What’s Next…

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.

Download

You can download all the assets used in this tutorial to have a fully functional robotic arm for Unity.

Feature Standard Premium
Inverse Kinematics
Multiple Solutions
Smooth Reaching
Test Scene
Test Animations
Download Standard Premium

📧 Stay updated

A new tutorial is released every week.

💖 Support this blog

This websites exists thanks to the contribution of patrons on Patreon. If you think these posts have either helped or inspired you, please consider supporting this blog.

Paypal
Ko-fi
Twitter_logo

Write a Comment

Comment