Genetic Algorithm Robot: Evolving Altitude, using Python, C++ and an Arduino.

Designing a flight control system using a genetic algorithm instead of a predetermined mathematical model.

Daniel Wilczak
Towards Data Science

--

Two of the three robots used for testing of the Evolving Altitude Robot. Image by Author

Abstract:

The purpose of the study was to design a flight control system with no pre-determined mathematical model, but instead using a genetic algorithm to maintain the optimal altitude. The study is done through a quantitative empirical research method. In the process of conducting the research, we found that programming a genetic algorithm was cumbersome for novice users to implement. Due to this, we created and released an open-source Python package called EasyGA.

An initial population of 15 chromosomes, 10 genes per chromosome with 100 generations, were used during one trial. The throttle value of the device had an associated gene value of 1 second. A minimum of 30 trials per robot were used to show statistical significance in the study. When the trials were completed, machine learning was achieved. Results showed that optimizing a one degree of freedom(DOF) device, in real-time, is possible without using a pre-determined mathematical model.

How it works:

In order to run the genetic algorithms needed for this experiment to be successful, an Arduino Uno, Python, Electronic Ducked Fan(EDF), Electric Speed Controller(ESC), an ultrasonic ranging module (USRM, and a laptop are required.

Ardunio Uno: Is an open-source micro controller board. This micro controller board comes with digital and analog pins. The pins have different purposes. Some pins are used for sending 3.3V and 5V power to other devices, and other pins are used as input, and output pins, which send pulse width modulation (PWM) signals to other devices.

Evolving Altitude Robot Wiring Diagram. Image by Author

For this experiment, digital pins 9, 10, and 11 will be used to send PWM signals and receive sensory information from the ultrasonic range module. Digital pin 9 (TRIG) is used for the signal output, pin 10 (ECHO) is used for the signal input, and pin 11 is used for PWM signals to the ESC.

Arduino C++: The code written on the Arduino is to receive directions from the python script in the form of throttle values. The Arduino then executes those instructions while also reading the heights from the USRM and transmits them back to python using serial communication.

Python: Python was used as the main language to handle the genetic algorithm while also sending directions to the Arduino.

SQL Database: A Sqlite3 database is used to store all running and configuration data coming from the robot and genetic algorithm. It is also used to find trends and query data.

One DOF device: The device is made up of an EDF fan, the USRM, a carbon fiber tube, assorted wiring, and three 3D printed parts: the intermediary connector, and platform. The platform for the device is designed to work functionally with all parts and pieces needed for the EDF to run. The EDF required that the 120mm x 20mm carbon fiber tube connect directly to the fan.

Labeled image of all the main components of the Evolving Altitude Robot. Image by Author

The intermediary connector was designed to be wider than the platform. The connector is wider than the platform so the ultrasonic range module has a wide enough space to accurately measure the distance from the module to the device .

Showing 6cm from the Ultra Sonic Range Module (USRM) to the intermediary connector. Image by Author

A high-frequency ultrasonic sound signal is sent from the module to the intermediary connector and acts as sonar returning to the module allowing it to calculate the distance.

Robot Fitness Function:

The fitness function determines how well a chromosome does at a specific task. To test the fitness function in this study, chromosomes are encoded throttle values, parsed into a string and serially sent to the Arduino Uno. The Arduino Uno runs encoded chromosome and sends the fitness data back to the Python script. The Python script then evaluates the data. The fitness function of the study was to see how well the robot’s EDF can hold a position of 6cm above the USRM. The total score is calculated based on its overall distance from 6cm during the fitness evaluation. The goal is to have an overall fitness of 0.

Graphic representation of the robots fitness function. Line represents the desired fitness. Image by Author

Here you can find a graphical representation of the fitness function and the code used to evaluate the chromosomes.

Code View:

Code used to evaluate the robots fitness for one run.

Genetic Algorithm Setup:

The study had a search space of 6¹⁰ possible chromosome combinations or configurations. Using a genetic algorithm allows for rapid search through the search space for a relatively good solution. The solution may be one of many best solutions or a solution that is deemed good for use with the restraints put on the study.

During the study it was found that any clear interpretation of how a genetic algorithm should be structured or any good tool to also help store, track, and make sense of the information needed to accomplish such a study. The team, made up of 5 individuals, took one year to create the serial communication structure for the Arduino and the EasyGA python package itself.

For the robot the team decided to use the default configuration settings of EasyGA since it handled all the hyper parameters. It was a goal to minimize the error from the robot which ended up being one line of code. The python code can be found below and a more detailed list can be found on the Evolving Altitude Robot Github.

EasyGA configuration settings while testing the Evolving Robot.

The full code is not normally shared however, we feel that it is necessary to show the simplicity when using the EasyGA python package.

Full Python Code:

Results:

For the study, the goal was to prove that only a genetic algorithm is needed to optimize a one DOF robot. Not only was it proven that it was possible, the team wanted to ensure that it could be done by using more than one robot. Three robots were used during the study and through data showed they were all able to learn.

Each robot was tested with a minimum of 30 trials, each proving that the mechanical differences between them still came to the same result of machine learning from the genetic algorithm.

Three robots used in finding machine learning results. From left to right — Kyle,Jeff and Linda. Image by Author

The robots were all created identical to each other but through underlying mechanical differences, all of them are a little bit different. The differences being seen in the variation of values would come to be the near optimal solution for each robot.

Trial: Population size = 15, Chromosome Length = 10, Generations = 100

Left: Average of all trials lowest chromosome. Right: All trials lowest chromosome. Images by Author

Discussion:

In retrospect, the time it takes to test all the possible combination of chromosomes is time intensive. A better approach may be a combination of simulated trials followed by real-life testing and optimization. This two-way approach would have both benefits of running the initial generations in a simulation where trials are processed using computing power, followed by real-life trials and training to optimize the system further.

Contributions:

I would like to thank everyone who helped on this project. The following individuals all made an impact on the study.

  • Dylan Ballback with his amazing mechanical engineering design skills.
  • Rachel Secord reviewed and helped write all the content.
  • Jack Nguyen co-created EasyGA with myself and 4 other students.
  • Mohammad Alali for writing the C++ code and starter GA code.
  • Jacob Romeo helped with the literature review and running the robots.
  • Dr. Matthew Verleger being the faculty mentor of the research project.
  • Embry Riddle Undergraduate Research Ignite program for sponsoring the study.

Many more helped indirectly with the project but I would still like to say thank you to them.

Thank you for reading,

Daniel Wilczak

--

--