I will split this subject in different chapters so it can be followed easily.
Basically we know where the leg has to go and using IK we will calculate the angles that the servos need to rotate in order to achieve that known position.
In this page I will explain a geometric method and as I will develop this project I may review this and follow other approaches.
So let's start:
I will explain this using 1 leg but the same principles will apply to the other legs. The tetrapod has 3 motors in each leg and for now we are going to see how to use IK to control 2 motors (in the XZ plane), the third one will be discussed later but will allow us to move the leg in the XY plane.
The two green dots represent the motor and the two black lines are the femur (a) and tibia (b).
We want to know the angles of the black lines to achieve a known X and Z position. I call them X0 and Y0.
We also know the length of the femur and tibia (a and b). So we can find the hypotenuse (h) of the triangle generated by X0 and Y0.
Now, using the law of cosines we can find angle alpha (α):
The first angle we really need is the one that the femur makes with origin axis. This can be different in any case. But let's imagine that the servo is mounted so the angle 0 degrees is vertical (the same way as my tetrapod). So:
Then the true angle we need to move our first servo is beta (β).
To find this angle β we need to find first gamma (γ). Like:
But gamma is also located on top of alpha like:
So finally beta (my first servo angle) is:
Let's say that the second servo is mounted of the first one, so the angle I need is epsilon (ε), in red below.
So first we need to find delta (δ). Again using the law of cosines:
Now we can find epsilon like:
In the next chapter I will write these equations in Arduino code.