How to integrate eigen library with PX4 firmware?

Hello,

I’m trying to implement a MPC multicopter rate controller.

In order to perform a large number of matrix operations, I want to integrate eigen library with PX4 firmware.

To achieve this goal, I took the following steps:

  1. Download the eigen library from the official website.
  2. Extract the directory ‘Eigen’ from the zip file and place it in ‘src/lib’ directory of PX4 firmware.
  3. Add ’ #include <Eigen/Core> ’ in 'src / lib / rate_control / ratecontrol.hpp '.

However, when I build the PX4 Autopilot code with “make px4_fmu-v6c_default”, I get below error:

/home/conley/UAV/14.3/PX4-Autopilot/src/lib/Eigen/Core:50:10: fatal error: complex: No such file or directory
   50 | #include <complex>
      |          ^~~~~~~~~

Through research, it was found that the issue was caused by not linking the C++ standard library during compilation. How can the standard library be added? or are there other ways to resolve this issue?

Thank you.

I have worked around the same problem with the TFLM library, you can see how I’ve solved it (very hacky solution) in the CMakeLists.txt files of this PR:

You also have to make sure to exclude tests, I had this problem with TFLM, and I know another who have used Eigen for other hardware who had problems with tests there as well

First of all, thank you very much for your prompt response.

  • I have reviewed the information you provided, and your approach involves following steps:
  1. Add a ‘tflm’ folder under ‘src/lib’, which contains the source files and a ‘CMakeLists.txt’.

  2. In the ‘tflm/CMakeLists.txt’, add code snippet about including the C++ standard library.

    if(CONFIG_BOARD_TOOLCHAIN STREQUAL “arm-none-eabi”)
    list(APPEND TFLM_INCLUDE_DIRS
    ${TFLITE_DOWNLOADS_DIR}/include/13.2.1
    ${TFLITE_DOWNLOADS_DIR}/include/13.2.1/arm-none-eabi
    )
    endif()

  3. In ‘lib/CMakeLists.txt’, add ‘add_subdirectory(tflm EXCLUDE_FROM_ALL)’

  4. Finally, in ‘src/modules/mc_nn_control/mc_nn_control.hpp’, it can be directly referenced.
    Is my understanding correct, or are there any necessary steps missing?

  • Currently, I have doubt about this solution:
  1. Is it sufficient to just perform step 2 in ‘tflm/CMakeLists.txt’, or is it necessary to download the C++ standard library as well?

  2. Why is that the case?

    I would greatly appreciate if you can reply.

I understand your doubts, I’m very inexperienced with CMakeLists etc, but this works at least. I’ve tried several days to make a better solution, but I’ve not been able to.

  1. Yes you have to download it, when I build TFLM by itself it downloads GCC 13.2.1 which is what I use. There is a flag in the Nuttx builds (-nostdinc++) that stops you from including standard c++ libraries, so therefore I copied the c++ standard headers out of the gcc folder into a separate include folder to circumvent til flag. If you remove the flag the normal build fails so I would reccomend not doing that, but if you want to try the line is found at platforms/nuttx/cmake/px4_impl_os.cmake at line 74.

In my case I also need C++ version 17, so in the main /CMakeLists.txt I changed that in line 270.

I also changed the toolchain in platforms/nuttx/cmake/Toolchain-arm-none-eabi.cmake

  1. You also need to reference the library in the CMakeLists.txt of the module so src/modules/mc_nn_control/CMakeLists.txt

If you find a better solution please reach out to me, as I would love to impove my PR as well! And if you get Eigen to work at all I would love for you to tell me that as well so I can compare Eigen vs TFLM for inference in my master thesis

Thank you very much for your reply again.
I will follow your advice to integrate the Eigen library with PX4 firmware. If I get any results, I will contact you at the first opportunity.

1 Like