This directory contains tools for deploying trained models on physical drones and running real-world tests.
The deployment module provides:
- Model conversion: Export PyTorch models to C for embedded systems
- Validation: Verify model correctness after conversion
- Ivy senders: Stream evader trajectories to Paparazzi autopilot for testing
File: extract_validate.py
This script converts trained PPO models to C headers for embedded deployment:
- Extracts weights from PyTorch model state dict
- Generates C header (
ppo_weights.h) with weight arrays - Compiles shared library (
libppo_controller.soor.dylib) - Validates by comparing PyTorch and C outputs
Usage:
python deployment/extract_validate.py \
--model path/to/trained/model.zip \
--csrc deployment/ppo_controller.cNote, ensure that the input and output dimensions in the extract validate script match that of ppo_controller.c.
Configuration:
HEADER_NAME = "ppo_weights.h"
LIB_BASE = "libppo_controller"
TOLERANCE = 1e-5 # Validation tolerance
NUM_TESTS = 500 # Number of validation tests
OBS_RANGE = (-100.0, 100.0) # Observation range for testing
INPUT_DIM = 18 # Observation dimension
OUTPUT_DIM = 3 # Action dimensionOutput:
ppo_weights.h: C header with network weightslibppo_controller.{so,dylib}: Compiled shared library
Validation: The script runs random test observations through both PyTorch and C implementations and checks outputs match within tolerance. Exit code 0 = success, 1 = failure.
File: ppo_controller.c
Template C implementation of a PPO policy forward pass. You'll need to:
- Copy this file to your build directory
- Include the generated
ppo_weights.h - Implement the forward pass using the weight arrays
- Compile into a shared library
The template assumes:
- ReLU activations
- Linear layers
- Tanh output activation (for bounded actions)
Key Functions:
ppo_forward(): Main forward pass function- Input: observation array
- Output: action array
See ivy_senders/README.md for detailed documentation on streaming evader trajectories.