Inverse Kinematics Basics Tutorial

by Oscar

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.

Some of the links on this page are affiliate links. I receive a commission (at no extra cost to you) if you make a purchase after clicking on one of these affiliate links. This helps support the free content for the community on this website. Please read our Affiliate Link Policy for more information.

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:

https://oscarliang.com/inverse-kinematics-ik-implementation-for-3dof-hexapod-robot/

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

https://oscarliang.com/hexapod-robot-with-3-dof-legs-degree-of-freedom/

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

 2-IK-side

Gamma is easy, from diagram one, we have:

3-gamma

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.

1

6

For Alpha2 and Beta, we need some help from Cosine Rules:
Cosine_Rule
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,
7
And Alpha is
8
And Finally, Beta
9

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

 

Leave a Comment

By using this form, you agree with the storage and handling of your data by this website. Note that all comments are held for moderation before appearing.

24 comments

D 15th February 2023 - 12:14 am

So how do you figure out L then if the legs are extended or in another configuration from the Y, Z coordinates. The second part aeems like forward kinematics to me, theres no refrence to Y or Z in any of those formulas. And on a robot those angles would already be known.

Reply
Jake Keiper 2nd July 2021 - 9:23 pm

This is greats, thanks for documenting this. Do you know if this approach will work if I have one more segment of leg and joint, i.e. 4 DoF. If it it does not work, can I approximate my 4 DoF leg as 3 DoF and lock a joint, or is there a different way to approach this problem? To be specific, the robot leg is the Lynx Motion 4DOF T-Hex Leg.

Reply
Joel 14th June 2021 - 11:44 pm

Thank you so much for this clear explanation! Ive been searching everywhere for an article like this. Thank you

Reply
fitri 5th January 2017 - 10:51 am

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

Reply
fitri 5th January 2017 - 10:49 am

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 :)

Reply
William de Witt 26th October 2015 - 9:31 am

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!

Reply
andi 30th May 2015 - 12:43 pm

what is z_offset

Reply
waqar ali 17th May 2015 - 1:48 pm

Hi,
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.
Regards,

Reply
Rajeev Kumar 6th October 2014 - 2:02 pm

Good Blog. Could you pl. tell me how the equation for L1 changes when z is non zero ?
Thanks and regards,

Reply
Oscar 6th October 2014 - 3:00 pm

sorry what is Z? you mean Zoffset?

Reply
James 22nd March 2014 - 5:52 pm

Hi,

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:

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

c&a
(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 :)

Reply
EEK 27th November 2013 - 4:06 pm

Figured it out. Thanks!

Reply
EEK 22nd November 2013 - 10:34 pm

Is zero degrees gamma centered along the positive x axis and oriented right hand rule about Z axis?

Reply
EEK 22nd November 2013 - 5:19 pm

Ah! Thanks!
Thanks for this web page and explanation, too!

Reply
EEK 22nd November 2013 - 3:58 pm

What is L1 ? Does L1 = y ?

Reply
Oscar 22nd November 2013 - 4:10 pm

Hi,
L1 = sqrt(x^2 + y^2)

Reply
DVS 18th July 2013 - 3:55 pm

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

Reply
Oscar 18th July 2013 - 11:38 pm

yes, you are absolutely right, it’s an error in the formula, I will correct it now.
thank you very much for pointing that out! :-)

Reply
rahim 30th June 2013 - 9:17 pm

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 :)

Reply
Oscar 30th June 2013 - 11:02 pm

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!

Reply
nocturno 14th June 2013 - 10:13 am

Hi Oscar,

in code why u calculte coxa angle with fatan2 z/y. not like in this page fatan2 y/x?

Reply
Oscar 14th June 2013 - 4:37 pm

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.

Reply
Irawan 19th August 2015 - 9:30 am

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

Reply
Oscar 20th August 2015 - 10:01 am

Zoffset is just the vertical distance between the tip of the feet labelled (y,z) to coxa

Reply