This program generates payload trajectories leveraging differential-flatness of quadrotors. The users need to provide algebraic equations of quadrotor flat outputs (px, py, pz, psi). Then the trajectories including pos, vel, acc, omega, alpha are generated and stored in a CSV file.
Note: This is a Python port of the original MATLAB implementation.
- Python 3.7+ (tested with Python 3.10)
- CasADi: Symbolic computation and automatic differentiation
- NumPy: Numerical computing
- Pandas: Data manipulation and CSV output
- Matplotlib: Plotting (optional)
- SciPy: For rotation matrix to quaternion conversion
- Clone or download this repository
- Create a virtual environment (recommended):
python -m venv .venv source .venv/bin/activate # On Windows: .venv\Scripts\activate
- Install dependencies:
pip install casadi numpy pandas matplotlib scipy
- Define parameters in
main.py - Define algebraic equations of trajectories in
main.py - Run the main script:
python main.py
You can also use the trajectory generator directly in your own scripts:
import casadi as ca
from traj_generator_casadi import traj_generator_casadi
# Define symbolic time variable
t = ca.SX.sym('t')
# Define trajectory equations
px = 2.0 * ca.cos(0.5 * t) # x position
py = 2.0 * ca.sin(0.5 * t) # y position
pz = 1.0 + 0.0 * t # z position (constant height)
psi = 0.0 * t # heading angle
# Set options
opt = {
'zero_lateral_overload': True, # No lateral overload (quadrotor-like)
'reset_terminal_att': False # Don't reset terminal attitude
}
# Generate trajectory
data = traj_generator_casadi(t, px, py, pz, psi, dt=0.1, tend=20.0, opt=opt)
# Save to CSV
data.to_csv('my_trajectory.csv', index=False)The opt dictionary supports the following parameters:
zero_lateral_overload(bool):True: No lateral overload, similar to quadrotorsFalse: Zero pitch and roll
reset_terminal_att(bool):True: Regulate terminal quaternion to identityFalse: Let terminal attitude evolve naturally
The generated CSV file contains the following columns:
| Column | Description | Units |
|---|---|---|
| t | Time | s |
| p_x, p_y, p_z | Position | m |
| q_w, q_x, q_y, q_z | Quaternion (w,x,y,z) | - |
| v_x, v_y, v_z | Velocity | m/s |
| w_x, w_y, w_z | Angular velocity | rad/s |
| a_lin_x, a_lin_y, a_lin_z | Linear acceleration | m/s² |
| a_rot_x, a_rot_y, a_rot_z | Angular acceleration | rad/s² |
The default configuration generates a figure-eight trajectory:
# Time distortion for smooth start/stop
k = 1
t0 = 5
t1 = tend - 5.0
ts = ca.log(1 + ca.exp(k * (t - t0))) - ca.log(1 + ca.exp(k * (t - t1)))
# Figure-eight equations
px = 2.5 * ca.cos(0.25 * ts) + 0.2
py = 2.0 * ca.sin(0.5 * ts)
pz = 1.0 # constant height
psi = 0.25 * ts # gradually changing headingpx = 2.0 * ca.cos(0.5 * t)
py = 2.0 * ca.sin(0.5 * t)
pz = 1.0 + 0.0 * t
psi = 0.0 * tpx = 2.0 * ca.cos(0.5 * t)
py = 2.0 * ca.sin(0.5 * t)
pz = 1.0 + 0.1 * t
psi = 0.2 * tRun the test suite to validate the implementation:
python test_trajectory.pyThe test script validates:
- Basic trajectory generation functionality
- Figure-eight trajectory generation
- Different option configurations
- Output format and data integrity
This Python implementation maintains full compatibility with the MATLAB version while offering these improvements:
- Cross-platform compatibility: No MATLAB license required
- Modern Python ecosystem: Easy integration with other Python tools
- Virtual environment support: Clean dependency management
- Automated testing: Built-in test suite for validation
- Type hints and documentation: Better code maintainability
- Quaternion generation is the most computationally expensive part
- Progress indicators are shown during quaternion calculation
- For large trajectories (high resolution or long duration), consider reducing the time resolution or using smaller time windows
- Import errors: Make sure all dependencies are installed in your Python environment
- CasADi installation: On some systems, you might need to install CasADi from source
- Plotting issues: If running in a headless environment, set
make_plot = Falseinmain.py - Memory issues: For very long trajectories, consider processing in smaller chunks
If you encounter issues:
- Check that your trajectory equations are valid CasADi expressions
- Verify that your time variable
tis properly defined asca.SX.sym('t') - Ensure all required dependencies are installed
- Run the test suite to validate your installation
Same as the original MATLAB implementation.