Fuzzy control in C++, first steps

After investigating over tons of information on the Internet, I realized that everybody talks about Fuzzy Logic, but no one says anything useful, e.g. a there is not a single practical example for embedded systems. So I decided to write my own code.

This first draft is a proof of concept, that’s way I’m using the std::vector library and floating point types. When it’s time to port it to a microcontroller I will need to figure out how to change from vector to array, and from double to int, painless.

This code will be public and open source once it’s polished, meanwhile I’m showing you an abstraction of the fuzzy engine (or controller). I guess anyone can infer from this abstractions what lays under the hood. Right now I would like to hear from you about the usability of this abstraction.

Next two images show my mental process so that I was able to write any code, and for you to follow the source code. I compiled it with GNU g++ 11 in a Linux box.

Any comment is welcomed, and I hope to make this project public ASAP.

int main(void)
 GradeInverted cold (18., 20.);
 Triangle cool (18.0, 20., 22.);
 Triangle ok (20., 22., 24.);
 Triangle warm (22., 24., 26.);
 Grade hot (24., 26.);

 Rules ifCold (&cold, 70.);
 Rules ifCool (&cool, 30.);
 Rules ifOk (&ok, 0.);
 Rules ifWarm (&warm, -30.);
 Rules ifHot (&hot, -70.);

 rules.push_back (&ifCold);
 rules.push_back (&ifCool);
 rules.push_back (&ifOk);
 rules.push_back (&ifWarm);
 rules.push_back (&ifHot);

 double temp;
 cout << "Please enter current temperature: ";  cin >> temp;

 double acum1 = 0.0;
 double acum2 = 0.0;

 for (auto x : rules) {
   acum1 += x->Fire (temp);
   acum2 += x->GetMembership (temp);

 double cog = acum1 / acum2;
 cout << "COG = " << cog << endl;

return 0;

In this image you can see some results:

Captura de pantalla de 2017-08-15 19-53-17


Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión /  Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión /  Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión /  Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión /  Cambiar )

Conectando a %s