This project consists in training a feedfoward network using reinforcement learning to fly a drone through a set of gates. For more information, consult the Nano Drone Racing repository.
This is a repository to that create, trains and accelerates a nerual network, which handles the drone’s guidance and control functions. This network runs at an impressive rate of 167Hz on an STM32F405 processor, outputting attitude rates and thrust values for an attitude rate PID controller.
The steps that need to be carried out are:
- Carry out system identification of the drone
- Train the decided architerue and train NN
- Evaluate NN
- Convert it to C code via ONNX
- Deploy it on any drone autopilot for example bitcraze(this is not included in this repo)
-
Clone the repository:
git clone https://github.com/fed12345/controlnet cd controlnet
-
Set Up a Virtual Environment (recommended)
It’s a best practice to use a virtual environment to manage dependencies. To create a virtual environment, run the following command with conda installed:
conda create --name controlnet
conda activate controlnet
- Intall Dependencies
With the environment active, install all necessary packages by running:
pip install -r requirements.txt
- Create Docker Image
You can build the Docker image by running the following command in the terminal from the directory where the Dockerfile is located:
docker build -t onnx2c .
- Model Quadcopter
It is necessary to aquire some data on the dynamics on the drone, as we will model the droens response as a first over system in terms of roll, pitch, yaw and thrust. We desired states and measured stated. We can then use the python modelling script to model the drone, the output will be a series of marticies which have to be inputted in the simulator:
model_quadcopter.py
- Training the Model
The training stage will save model in the results folder as well as some timestamp videos to see how the drone is performing.
train_godzilla.py
- Evaluate the model (Optional)
Script to run a simulation of the drone controlled by the neural network, this is a quick way to visually evaluate it.
run_sim.py
- Convert to ONNX and Quantize Model
This step will convert the seleced model to ONNX format in order to facilitate quatization and conversion to C code. This step will also quatized the mode for faster inference.
quantization.py
- Convert to C code
This step leverages the docker images created before with the onnx2c library to convert the neural network into optimized c code.
docker run --rm -v $(pwd):/module onnx2c /bin/bash -c './onnx2c/build/onnx2c /module/results/quantized/my_model.onnx > /module/results/c_code/output.c'