When I created the eight degree of freedom (8 DOF) biped robot in my last blog post, I wrote a C# application to calculate servo positions, which in turn generated a smooth, life-like, walking gait. In this post I will walk through the application logic in more detail. The application generates a motion plan, runs the inverse kinematics calculations and allows me to visualise the results as rendered a stick man. The complete source code is below.
According to Wikipedia, “Inverse kinematics refers to the use of the kinematics equations of a robot to determine the joint parameters that provide a desired position of the end-effector. Specification of the movement of a robot so that its end-effector achieves a desired task is known as motion planning. Inverse kinematics transforms the motion plan into joint actuator trajectories for the robot.”
Starting with the motion plan and using the two rules established in my previous post (#1 static hip height and #2 clipped sinusoidal foot motion), I knew roughly what I wanted each joint to do. Next I had to model that mathematically. Hip height was easy, as it’s constant. Left and right feet follow the pattern illustrated below.
I created a system of triangles to represent each of the robot joints and could now begin to calculate the relative angles between them for each time interval. To preserve symmetry, I decided that the feet would always remain parallel with the body (and floor) and that the horizontal plane would always bisect the knee angle. These principles helped determine many of the joint angles using some simple trigonometry.
law of cosines can be used in a number of ways - such as calculating the third side of a triangle when two sides and their enclosed angle are known, or to determine the angles of a triangle if all three sides are known. Once the angles are known, these can be translated into servo positions, with the appropriate amount of offset and direction applied.
This could now be implemented in code – the motion plan (determining foot geometry), the inverse kinematics (determining servo angle) calculation and the joint visualisation. I won’t walk through every step as the source code is pretty easy to decipher.
Note about the code: I separate the code into a few classes to keep key objects and values partitioned (legs, hip, etc). In this post I’ve compressed everything into a single file, which will execute - just paste the entire block into a new Windows Form project. However, if you want to modify the code, for maintainability, it would be best to break out again into discrete class files.
I hope you find this useful.