[SOLVED] Adding fuzzylite libraries to PX4


I am developing a fuzzy logic control algorithm for PX4.
I have compiled fuzzylite properly and run some examples. It worked.

Now I’d like to add fuzzylite libraries to PX4 in order to use the classes and functions. But I’m still stuck with it.
Has anybody combined another library with PX4 firmware? Can you show me how to do it or try combining fuzzylite with PX4, please?

Here is the URL to fuzzylite website https://www.fuzzylite.com/.

Thanks for your attention.

The PX4 build system is just cmake with a few helper functions. First take a look at how you’d use fuzzylite with a standalone cmake example, then do it within PX4 as a module.

On the PX4 side you’ll need a module with a main that starts a task (task on nuttx, thread on posix), but it depends where you want to integrate this.

Hello @dagar !

Have you used PSP Pixhawk PX4 Support from Embedded Coder of matlab simulink?

I am interested in designing a altitude controller.


Hello @dagar
Thanks for your advice.

I’ve done what you said, and there are still some problems.

First, I’ve built a standalone project using Cmake to link it with fuzzylite, the CMakeLists are below

and the program has run well.

Second, I’ve added the CMake configuration and definition to Firmware/CMakeLists.txt and (the library that I’ve created in PX4 firmware) Firmware/src/lib/fuzzylb/CMakeLists.txt. I’ve also registered for the library in cmake/configs/posix_sitl_default.cmake. I provide the details in the picture below

And that generated the error [‘-std=c++11’ is valid for C++/ObjC++ but not for C [-Werror]].

Third, I’ve removed the line add_definitions(-std=c++11) from Firmware/CMakeLists.txt, and that generated the error [exception handling disabled, use -fexceptions to enable]

I started to think that fuzzylite contains some functions or prototypes which can’t be compiled in C or can’t be compiled without the definition -std=c++11 (or above).

Lastly, I’ve added the line add_definitions(-fexceptions) to FIrmware/CMakeLists.txt, it solved the problem about exception handling, but generated another problem about dynamic_cast, here are the detailed pictures.

Where dynamic_cast is located

So I’d like to ask 3 things:
1/ How to make PX4 CMakeLists compile something purely in C++.
2/ How to add some equivalent definitions to -std=c++11 in order to make the code of fuzzylite compilable.
3/ If changing CMakeLists is not able to solve these problems, do I have to change all fuzzylite source codes into C?

Thank you for your time and attention.

If you do not use C++11, then you should add the definition FL_CPP98 for C++98.
Check the https://github.com/fuzzylite/fuzzylite#compile-link-and-execute

Hello @jcrada

Thank you for replying.

I’ve just successfully linked fuzzylite to PX4 firmware
I’ll present step by step with these pictures below:

First, copying fl (FL) folder of fuzzylite to src/Firmware/src (to not modify the include path in headers of fuzzylite). And create a folder named fuzzylib (or whatever)

Second, cd fuzzylib and paste Engine.cpp from src folder of fuzzylite, and create a CMakeLists.txt.

Third, modify the CMakeLists.txt like this

It will find the path of fuzzylite library and link to the module created.

Forth, modify src/Firmware/src/cmake/common/px4_base.cmake to make fuzzylite source code compiable

I’ve changed -fno-exceptions to -fexceptions and -fno-rtti to -frtti. I added -std=c++11, too but it warned error so I’ve kept the rest original.

Finally, register for lib/fuzzylib in src/Firmware/src/cmake/configs/posix_sitl_default.cmake

Then compile it, it will warn for -Wfloat-equal, so we’ll change == and != to abs(a-b) < 1e-6 (or whatever).
I haven’t modify the warning flags in px4_base.cmake yet so I don’t know if that could make the float be able to be compared by == or not.
Anyway, these above steps will surely link fuzzylite and PX4 firmware together and make them compiable.

This is the fuzzylite example I ran on posix_sitl_default


You’re aware that fuzzylite is gpl v3 licensed?

Hello @auturgy

Thanks for your notice.

I’ve read the GPL v3 license but actually I don’t fully understand it.

I’m working on fuzzylite and PX4 firmware for my school project. If my work is illegal, please inform me and show me how to make thing right. It’s the first time I’ve done modifying a software.
If there have been something harmful to the development of fuzzylite and PX4 firmware, please let me know to cancel them. Or show me what to do if I want to continue my work.

I’m sorry for not fully understanding the license due to my subjectivity and a small part of my bad English.
My email is ttdat235@gmail.com, please inform me via this address or send me an message for not bothering other users. I’m looking forward to your reply.

Thank you once again.

Nothing illegal/wrong, but something to be aware of. It basically means that you can’t distribute your work except under the gpl, as the combined work (fuzzylite, px4 and your linking code) must use the gpl.
It also means that if you make a pull request to upstream your work, I’d guess it wouldn’t be accepted (DroneCode doesn’t like gpl).

Thank you for pointing this out. fuzzylite version 6 is licensed under the GNU GPL, although commercial licenses are available to suit the need for closed source applications. Alternatively, fuzzylite version 5 is licensed under GNU LGPL.

There’s nothing wrong with adding hooks in the build system to make it easy to use this library for users that want to. The main project just can’t depend on (or include obviously) the library.