Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Neural Control Mode #24366

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
Draft

Conversation

SindreMHegre
Copy link

@SindreMHegre SindreMHegre commented Feb 19, 2025

Solved Problem

Using neural networks for various tasks on UAVs in research is becoming more and more commonplace. My aim is to provide a module that can be used to replace the MC controller with NN to lower the barrier for using RL and NN on UAVs in general, and give researchers and hobbyist a place to start for NN use.

Solution

  • Add TensorFlow Lite Micro as an inference lib into PX4
  • Make custom board configs with neural controllers
  • Make a module for NN control

Changelog Entry

For release notes:

Feature/Bugfix XYZ
New parameter: XYZ_Z
Documentation: Need to clarify page ... / done, read docs.px4.io/...

Alternatives

We could also look into using Eigen or executorch

Test coverage

Context

Docs:
PX4/PX4-user_guide#3617

Help:

  • I'm not very experienced with CMakeLists.txt, so any pointers in how to improve the code quality there is much appreciated.
  • Also for the TFLM submodule, should I make a PX4 branch for it? With the dependencies already downloaded and the standard c++ header files extracted. Should it be on a fork or try to get it upstream?
  • Would also like it to have documentation on the main docs, where do I start writing that?
  • Should it be merged into main or as a separate branch upstream?

Other feature requests:

  • Put in different controller parameters with possibilities to replace different parts: Full e2e, only control, only allocation, Allocation and control as two separate networks.
  • Script to give thrust and delay parameters from a flight log

Info:
The networks I'm flying with right now is not mine and is being used for research, so they will be open sourced at some point, but can't give them away at the moment

Inference times on the pixracer pro:
control net: 70 microseconds
allocation net: 13 microseconds
Full NN controller: 141 microseconds

@SindreMHegre SindreMHegre changed the title Add tflm to px4 with module Neural Control Mode Feb 19, 2025
@DronecodeBot
Copy link

This pull request has been mentioned on Discussion Forum for PX4, Pixhawk, QGroundControl, MAVSDK, MAVLink. There might be relevant details there:

https://discuss.px4.io/t/px4-sync-q-a-feb-19-2025/43827/2

@DronecodeBot
Copy link

This pull request has been mentioned on Discussion Forum for PX4, Pixhawk, QGroundControl, MAVSDK, MAVLink. There might be relevant details there:

https://discuss.px4.io/t/px4-sync-q-a-feb-19-2025/43827/1

@SindreMHegre SindreMHegre force-pushed the neural_control_with_tflm branch from f5cefaa to 33189a3 Compare February 19, 2025 17:26
@Jaeyoung-Lim Jaeyoung-Lim marked this pull request as draft February 19, 2025 18:18
@SindreMHegre SindreMHegre force-pushed the neural_control_with_tflm branch 2 times, most recently from 600755d to 2eb97f2 Compare February 20, 2025 14:40
 - Add TensorFlow Lite Micro(TFLM) as a library in px4
 - Make a module that uses neural network inference for control, which uses TFLM for inference
 - Make board config files for PX4 with neural module
@SindreMHegre SindreMHegre force-pushed the neural_control_with_tflm branch from 2eb97f2 to ff4254a Compare February 20, 2025 14:50
@Jaeyoung-Lim Jaeyoung-Lim requested a review from dagar February 21, 2025 10:06
 - Add TensorFlow Lite Micro(TFLM) as a library in px4
 - Make a module that uses neural network inference for control, which uses TFLM for inference
 - Make board config files for PX4 with neural module
@mrpollo mrpollo force-pushed the neural_control_with_tflm branch from 51ae6ae to 4ceaaa9 Compare February 21, 2025 23:21
# if(NOT ${TOOL})
# message(FATAL_ERROR "could not find ${tool}")
# endif()
# endforeach()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed?

set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}-g++)
set(CMAKE_CXX_COMPILER_TARGET ${triple})
# Define the path to the new toolchain
set(NEW_TOOLCHAIN_PATH "/home/sindre/Dropbox/Studier/2025_Var/dev/PX4-Autopilot-public/src/lib/tflm/tflite_micro/tensorflow/lite/micro/tools/make/downloads/gcc_embedded/bin")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has your local user name in the path

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I change it to use a variable like ${PX4_SOURCE_DIR}, I get errors when building uxrce_dds_client. Not sure how to fix unless the standard toolchain downloaded when calling
bash ./Tools/setup/ubuntu.sh
is upgraded.

- Switch ssh link to https link in submodule
- Remove mc_nn_control from startup
…4-Autopilot-public into neural_control_with_tflm
@Pedro-Roque Pedro-Roque self-requested a review February 24, 2025 15:54
@@ -0,0 +1,2 @@
control_net.cpp
allocation_net.cpp
Copy link
Member

@Jaeyoung-Lim Jaeyoung-Lim Feb 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be no gitignore here

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's there temporarily as I don't own the network I'm currently flying it, so I can't publish it in a public repo yet. But I'm in the process of fixing this

@@ -0,0 +1,138 @@
/****************************************************************************
*
* Copyright (c) 2013-2025 PX4 Development Team. All rights reserved.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Copyright (c) 2013-2025 PX4 Development Team. All rights reserved.
* Copyright (c) 2025 PX4 Development Team. All rights reserved.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@@ -89,3 +89,7 @@
[submodule "src/drivers/uavcan/libdronecan/libuavcan/dsdl_compiler/pydronecan"]
path = src/drivers/uavcan/libdronecan/libuavcan/dsdl_compiler/pydronecan
url = https://github.com/dronecan/pydronecan
[submodule "src/lib/tflm/tflite_micro"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice if this wasn't pulled for all builds.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how do I fix this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SindreMHegre Why do you even need the submodule?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Jaeyoung-Lim I have no idea, it just seemed like the standard way to include a big library. What other options do I have? Just adding the needed parts of the lib directly in the code?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SindreMHegre Can we install it on the system? submodules only make sense if you need to compile the library with the source

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the part of this PR where I'm the least experienced. What do you mean installing it on the system? I'm unsure if the library needs to compile on it's own first

@SindreMHegre SindreMHegre force-pushed the neural_control_with_tflm branch 2 times, most recently from fe9ca99 to aa83e4e Compare March 3, 2025 13:04
@DronecodeBot
Copy link

This pull request has been mentioned on Discussion Forum for PX4, Pixhawk, QGroundControl, MAVSDK, MAVLink. There might be relevant details there:

https://discuss.px4.io/t/how-to-integrate-eigen-library-with-px4-firmware/44065/2

@DronecodeBot
Copy link

This pull request has been mentioned on Discussion Forum for PX4, Pixhawk, QGroundControl, MAVSDK, MAVLink. There might be relevant details there:

https://discuss.px4.io/t/adding-an-external-flight-mode-from-the-fcu/44137/1

@SindreMHegre SindreMHegre force-pushed the neural_control_with_tflm branch from b08dba2 to 2243d9b Compare March 7, 2025 08:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants