*8.8K*

# 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

## Iterative approach (not important)

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.

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

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.

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

## 24 comments

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.

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.

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

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

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

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!

what is z_offset

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,

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

Thanks and regards,

sorry what is Z? you mean Zoffset?

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

Figured it out. Thanks!

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

Ah! Thanks!

Thanks for this web page and explanation, too!

What is L1 ? Does L1 = y ?

Hi,

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

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

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

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

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!

Hi Oscar,

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

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.

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

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