Inverse Kinematics Basics Tutorial

Inverse Kinematics Tutorial Introduction

What is Inverse kinematics in robotics? With your robot having legs the position of those legs dictates where its feet are. Where its feet are dictate its point of balance.

As you might know “balance” can be defined as the robot’s centre of mass (affectionately referred to as its centre of gravity) being between its centre of pivots (i.e. the edges of where its feet contact the ground). If the centre of mass is above the centre of pivots and between them the robot will balance (almost an unstable equilibrium, if you’re an applied mathematician. If the centre of mass is above but outside the centre of pivots (i.e. beyond the edges of his feet) the robot will overbalance and fall.

If you feel confident about the Inverse Kinematics basics, you can jump to 

Implementation of IK on Hexapod robot:

here is an implementation of a 3 DOF hexapod robot which I built using IK:

Kinematics and Robots?

If you’re a little unclear about Robot Kinematics, I recommend to start with something basic, a cube is a good start, and imagine that its centre of mass is right in the middle (which it will be if its density is even throughout). When the cube is just sat there it’s stable. The centre of mass is above the centre of pivot (the edges) but because it’s between them (when viewed from every direction) it will just sit there until you prod it.

Now you prod it and slowly tilt it. As the centre of mass approaches a point directly above one of the edges (our centre of pivot) the cube will feel lighter to your touch and if you can get the centre of mass directly over that centre of pivot it will balance. As soon as you push it past that point, so the centre of mass is the other side of the centre of pivot it will fall.

The robot is exactly the same. This is why the kinematics of the feet are important to you. If you want the robot to balance dynamically you NEED to know where the feet are and where they’re going to need to be. Please understand that I’m not going to do all your work for you, so the code or equations I share are not guaranteed on their accuracy but purely a demonstration of how the method is derived and works.

Forward and Inverse Kinematics – FK & IK

Forward kinematics is the method for determining the orientation and position of the end effector (x,y,z) coordinates relative to the centre of mass , given the joint angles and link lengths of the robot arm (servo positions). This equation is deterministic. You know absolutely from the servo positions exactly where the foot is.

Inverse kinematics is the opposite of forward kinematics. This is when you have a desired end effector position, but need to know the joint angles required to achieve it. This is harder than FK, and there could be more than one solution.

The FK is not very useful here, because if we are given a change of angle of a servo, only one effector moves in the chain. But if we are given a change of coordinate, the whole chain of effectors (servos) might have to move a certain angle for the end point to reach the desired position. And also the movement tend to be more natural as well!

Approaches To Solve IK

There are two approaches to solving inverse kinematics:

  • Analytical – requires a lot of trigonometry or matrix algebra
  • Iterative – better if there are lots of links and degrees of freedom.

Analytical approach

If there are only two or three links then it may be possible to solve it analytically. One possibly might be to draw out the arm with the angles shown on it, then solve for the angles using geometry. The problem is that this is not really a very general approach.
Another analytical approach is to represent each links rotation and translation by a matrix. The end point is then given by all these matrixes multiplied together, so we just need to solve this matrix equation. Then find what rotation each matrix represents.
There may be many solutions or there may not be any solutions. In other words there are lots of ways to reach to a given point, or it may be out of reach.
If there are many solutions, then you might need to apply additional constraints. For instance, human joints can only bend within certain limits.

Iterative approach (not important)

This is a more general approach for programming complex chains. It might be useful if you are building a snake robot or only if you are interested reading. :-p
Start off with the joints in any position, then move each of the joints in turn, so that each movement takes the endpoint toward the target
Starting with the joint nearest the end point, rotate the joint so that the current end point moves toward the required end point. Then do the same with the next joint toward the base and so on until the base is rotated. Then keep repeating this, until the end point is close enough to the required end point or if further iterations are not moving it closer to the required point.
It may be possible to have a more realistic strategy than this, for instance, if I am using my arm to pick up an object then, if the object is a long way away, I will move the bigger joints in the arm, then as the hand gets closer the smaller joints of the hand are used for the fine adjustments.
The angle of rotation for each joint is found by taking the dot product of the vectors from the joint to the current point and from the joint to the desired end point. Then taking the arcsin of this dot product.
To find the sign of this angle (ie which direction to turn), take the cross product of these vectors and checking the sign of the Z element of the vector.

Because we will be mainly dealing with 3DOF hexapod or Quadurped robot legs, Analytical, or simple trigonometry would do the trick for now.

Some Real Work

Enough theory, to turn this into progamming language, you’ll have to remember that the angles are unknown, and we need to work it out using equations and trigonometry.

leg planes

So, first thing is going to be simplify this problem from 3D into two 2D problems, to solve for α (alpha), β (beta) and γ (gamma).

Inverse Kinematics tutorial oscar


Gamma is easy, from diagram one, we have:


Now that you have gamma, you have two more angles to solve (and they are in the same plane)let’s move on to the second diagram.

Alpha is a bit tricky, so I tend to split it into Alpha1 and Alpha2.

We can get Alpha1 by working out L first.



For Alpha2 and Beta, we need some help from Cosine Rules:
From these formula, if we know 3 sides of a triangle, we can find out any angles inside it. Don’t doubt it, it just works! :-)
So now we have Alpha2,
And Alpha is
And Finally, Beta

At that point, you have your values for your servos!


21 thoughts on “Inverse Kinematics Basics Tutorial

  1. fitri

    hi, can you help me to give an idea for some implementation of hexapod robot? such as a fire fighting of hexapod robot. thankyou

  2. fitri

    I Want to build a hexapod robot, can you help me to give an idea for the implementation of hexapod robot like a hexapod robot as a fire fighting, thankyou :)

  3. William de Witt

    Hi Oscar,

    I have just finished printing and construction of my quadruped. Just waiting for a servo controller from Adafruit. In the mean time I am using an Arduino Mega. Your site is going to help me a ton to get it walking. I just wanted to say Thanks!

  4. waqar ali

    I’m a student and I have this project in university to make a hexapod.
    If you can please guide me how to code this algorithm.
    Or any thing near to it that could be helpful.

  5. James


    I’ve used your info as a guide while making a hexapod for a technical project at uni – your material is really helpful and well communicated, so thanks! :)

    I currently have one working, 3d printed leg… but I ran into some problems and I think I found some omissions in your formulas. The problem arose when I plugged some co-ordinates into the formulas to get the leg to move in a walking motion, the co-ords were spaced something like this:

    (side view) d . : . b …moving from position a -> b -> c -> d, repeat

    (top down) d . . . b

    I hope that makes sense!

    The problem is, when viewing the leg from top down, the ‘foot’ moves in an arc, not a straight line as I intended. I realised that this is due to your calculation of ‘L’:

    L = sqrt(Zoffset^2+[L1-coxa]^2).

    As angle gamma changes, the ‘length’ of the coxa should change too. For example, say you specify a fixed Y position and and then oscillate X between -something and +something, you would expect to get a straight line of movement from the foot. The above equation for L doesn’t change to adjust for this though, because as far as it’s concerned, the coxa length is constant and the Y position is constant – gamma then changes, sweeping the leg through an arc. What should happen is the coxa ‘length’ changes as it is offset from the Y plane by gamma…. like this:

    (from a top down view)

    +y ————————–+ <– origin
    | /
    | / <– coxa
    | /

    the coxa is offset by gamma from the y axis,
    a vertical line is drawn back from the tip of the coxa to the y axis
    length a is what your original equation considers as the coxa length,
    length b is what the length should now be.

    I hope this makes sense too!

    So, I've gone and done some extra trig to find out the new length 'b' to put into your original formula – easy enough.

    coxa = 55*cos(gamma)

    But then i found another problem… when calculating Z, your original equation should be applied, not the new one. This gets tricky to compensate for…

    I hope I've explained everything correctly, it's all a bit mind-boggling to me. For the sake of this long post I'll stop here, hopefully you can figure out what I'm talking about from what I supplied. Hope you reply!

    Thanks :)

  6. DVS

    I see that you calculate L by taking the sqrt of (Zoffset^2+L1^2). Should it not be sqrt(Zoffset^2+[L1-coxa]^2).

  7. rahim

    thanks Oscar.
    your blog is really help full.
    one question:
    Why don’t you use D-H parameter and go with trigonometry?
    I’m doing a same project (student version :D) and I need some reason for my supervisor :)

    1. Oscar

      Hi, to be honest, the I didn’t know about Denavit–Hartenberg parameters very well. The reason I chose simple Trigonometry is because it’s so basic and it can fully do the job anyway, there is no need to over-complicate it.

      Not sure if i am right, but when you get to the end of implementing optimized Inverse Kinematics (using rotation matrix), you will find it is similar to D-H parameters in some way. :-)

      Are you a mechanical engineer? I am sure either way is fine as long as it work!

    1. Oscar

      If you look careful the axis of the leg in the two places, you will find it to be different, for example, the vertical axis in the robot source code is Y, but in the tutorial (this post) is Z.

      I made this tutorial a few months after I built the robot, so I might have mistaken the axis labels.. hope that cleared it.

      1. Irawan

        Hi Oscar;

        Is it Zoffset is equal to moving z-axis? in your equation I saw other than Gamma, all angles are affected by changes of Zoffset. In my understanding the Zoffset is equal to the moving z-axis. Otherwise the output for Femur and Tibia will be constant. Correct me if I’m wrong.

        Thank you,

        p/s your sharing is very useful for the first timer

Leave a Reply

Your email address will not be published. Required fields are marked *

Are you Robot? *

I am moving all supports to this forum (at least for now)... So if you are looking for help, or want to help people, I strongly recommend joining this community. Feel free to share your projects, builds and knowledge too!