# Fuzzy Logic Control

## Fuzzy Logic Control

Some people describe fuzzy logic in mystical terms, as if it’s a magical way of making a computer think like a human. It’s not. It’s a simple way of mapping an input to an output. There’s nothing fuzzy about it.

Let’s look at an example. Say you’re making an auto-throttle for an autonomous go-kart.

The throttle can go from 1 up to 9. The kart goes for a test drive to see how fast it goes at each throttle setting.

Plotting the results, the result is the blue line on the graph.

For a given speed, how could the required throttle setting be calculated? Let’s try calculating the linear function for this problem. The top speed is 5 and there’s 8 throttle settings, so gain is 5/8 = 1.6. The first throttle setting is 1, so the bias must be 1. The linear function is y=x*1.6+1

To prove, let selected speed = 0

throttle = 0*1.6+1 = 1

Now let selected speed = 5

throttle = 5*1.6+1 = 9

Great, that all agrees with the graph. Let’s try selected speed = 1.5m/s

throttle = 1.5*1.6+1 = 3.4

Hang on, a throttle setting of 3.4 does not give a speed of 1.5m/s. Not even close! Looking at the graph, the throttle setting for 1.5m/s should be around 4.6.

Clearly linear control cannot be used here. What’s required is a better way of mapping the speed input to a throttle output.

This is where fuzzy logic becomes really useful. Using fuzzy logic, it’s possible to closely approximate non-linear functions. Using real measurements as guidance, fuzzy logic can calculate (almost) exactly the best throttle setting for a given speed.

This video actually explains the workings of fuzzy logic quite well.

## Terminology Review

Crisp input – A number. It’s just a flaming real number that will be mapped onto the output. Using the go-kart control example, it’s the selected speed.

Crisp output – It’s just a number! This one is the output. It’s the throttle setting.

Fuzzy set – One of those triangular shapes from the video. Each set has a minimum point, a peak and a maximum point that determine it’s range. It also has a weight – or a value. Fuzzy logic requires at least one, but usually 3 or more sets.

DOM – Degree of membership. The DOM ranges from 0 to 1. It represents how close the crisp input is to the peak of the triangle. If the input is outside the triangle, the DOM is 0. The closer the input is to the peak of the triangle, the closer the DOM will be to 1.

Output component – The output component of a set is the DOM multiplied by the weight of the set. The output components are added together to produce the crisp output.

## Example Using ETK

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
#include <etk/etk.h> #include <fstream> int main() { etk::Fuzzy<9> fuzzy; etk::Fuzzy<9>::Set set; //the first set will map a speed of 0 to throttle setting 1 //set the min, peak and max range for the set //the min value is unimportant for the first set, but it must be less than the peak //the max point is the peak of the next set set.set_points(-0.2, 0, 0.2); //set the value of this set. set.set_value(1); fuzzy.add_set(set); //the second set maps speed of 0.2 to throttle setting 2 set.set_points(0, 0.2, 0.5); set.set_value(2); fuzzy.add_set(set); //map speed of 0.5 to throttle 3 set.set_points(0.2, 0.5, 1); set.set_value(3); fuzzy.add_set(set); set.set_points(0.5, 1, 1.8); set.set_value(4); fuzzy.add_set(set); set.set_points(1, 1.8, 3.3); set.set_value(5); fuzzy.add_set(set); set.set_points(1.8, 3.3, 4.4); set.set_value(6); fuzzy.add_set(set); set.set_points(3.3, 4.4, 4.8); set.set_value(7); fuzzy.add_set(set); set.set_points(4.4, 4.8, 5); set.set_value(8); fuzzy.add_set(set); set.set_points(4.8, 5, 6); //the upper value is unimportant for the last set set.set_value(9); fuzzy.add_set(set); //now plot a bunch of points to a file std::ofstream file("out.csv"); for(float i = 0; i < 5; i += 0.1) file << fuzzy.crisp_out(i) << "\n"; file.close(); } |

Graphing the output file gives this image

Now, with this new Fuzzy logic generated graph, selecting a speed of 1.5 gives a throttle of 4.65. Awesome!