Inverse Kinematics Implementation for Hexapod Robots and Quadruped Robots Introduction
In my hexapod robot, I don’t normally control the leg directly, I only control the position of the body. Changes to the position of the body should be translated into changes to leg position. By using Inverse Kinematics, we can work out the angles of each servos, and the robot moves the way we wish.
Here are the Hexapod and Quadruped Robots that I have applied this IK implementation
So in my implementation, there are two parts in the algorithm:
- Body IK
- Leg IK
In this article, I will talk about Body IK and Leg IK algorithm and how they work together. I have talked about Leg IK here, so I won’t be going through this in this article in detail:
And this is a Leg IK demostration using Nunchuck to control a single leg:
Actually, there is one more for Hexapod Robot, and that is
4. Translate (moving horizontally on the X, Z plane)
For example, if we are moving the robot to the right (centre of robot moving from red to the blue point), the end of feet coordinates would shift to the right of the same distance as the centre of the body.
To make this more efficient and faster we can write our own trigonometric functions using hacks, for example, look up table.
Improvements – Rotation Matrix
We have been working with 3D coordinates (x,z,y) in our Inverse Kinematics algorithm, find the change in each dimension, and calculate the change of angles. So far each dimension is calculated using very basic trigonometric functions and ideas, this mathematical model works, but it’s not close enough to reality, and as you can probably see from the videos the movement isn’t very smooth nor natural. Here is one of the reasons.
Notice when calculating body rotation (roll and pitch), we used the trigonometric function “tan” to estimate change in y axis, but x and/or z will change as well (although it’s small, that’s why I ignored it there in the first place). If you want more accurate/natural movements, it’s best to include x and z changes as well.
We can do that by using a more advance and complex way to calculate change of coordinates from rotations – Rotation Matrix. (Please see here for detail) It might seem complicated and make no sense at all at first, check out youtube for some 2D rotation examples, and move on to 3D, it will become clearer.
Anyway, to implement it in programming doesn’t require you to understand it. If you are feeling really impatient, just copy and paste my code into yours and you won’t need to worry about it.
Improvements – Coordinate Transformation
As you progress to design implement Gaits, you need to understand this concept first.
The Body IK described so far still isn’t perfect. I have always been assuming the Body and the legs are in the same axis system, actually they are NOT. Each leg is in a different frame (frame – Axis system) from the body. For example, the frame of the front right leg is rotated -30 degree from the frame of the body (see below picture), so before we calculate the change of angles from Leg IK algorithm, we need to do coordinate transformation first, to convert it from body frame to the coxa frame.
A common way would be using rotation matrix (again :D ).
Excel Spreadsheets of Inverse Kinematics Downloads
I made this Excel Spreadsheet simulator to help understand the algorithm, enjoy!