The SHEP AI Project

Home About Downloads Contact

GA self learning robot

Posted 10/March/2021

Overview

In this tutorial we will explore genetic algorithms in robotics and how to make a robot learn to walk. There are several hardware choices you can make with this, as the aim is for the robot to learn. I have used the Bob the Biped chassis as it is simple and cheap.

Implementation

To implement this, I used 4 AAA batteries to provide enough current to each of the micro servos. Of course, this will depend on your servos and the amount. A good way to think about it is the maximum draw of your servos multiplied by the number of servos. This is for a worst-case scenario that all servos are drawing from the battery. With the MG996R servos the maximum is 900ma and having a spider biped of 8 servos will require a battery of minimum 7200ma (8*900). We aren’t using anything near that with 4 micro servos.

The biped used the feather M0 from Adafruit, 8-channel servo controller from Adafruit and an ultrasound sensor. All screwed into each other and a small bit of glue. All libraries were found in the Adafruit library bundle, and are seen in the code.

I used a genetic algorithm outlined in the development page which looped through a series of motor position changes such as [[0,0,0,10],[0,-10,0,0]]. This list would move servo 4 by 10 degrees in the first step and move servo 2 by -10 degrees. This is how I encoded the genotype. I gave it a maximum number of steps it can take as 10 to make it find an efficient way.

At each generation it would make several changes; I set it to one in the end after finding that le to the least amount of errors and most optimized solution within 50 generations. These changes are random, we call mutations. It will randomly mutate a servo to be 0, -10, or 10.

If you use the code, you can define your start positions as the robot standing up, this gives it a place to start from at each generation. You can change the sizes of these arrays by editing the following variables:

num_of_servos=4
num_of_steps=10

Make sure to edit your start array to have the number of positions (and angles) you wish it to turn to (see line 12 of the code). num_of_steps relates to how many steps it has in each generation to walk. The feather has low memory in comparison to controllers such as the Raspberry Pi, so may be limited in size.

You can find out more about the algorithm here.

Appendix

Download code

Download alternative code