|
| 1 | +# Mellinger Controller |
| 2 | + |
| 3 | +The Mellinger controller is a geometric tracking controller originally developed for aggressive quadrotor maneuvers [[1]](#references). This implementation is based on the Crazyflie firmware version. |
| 4 | + |
| 5 | +## Controller Functions |
| 6 | + |
| 7 | +### state2attitude |
| 8 | + |
| 9 | +::: drone_controllers.mellinger.control.state2attitude |
| 10 | + |
| 11 | +The position control component of the Mellinger controller. Converts desired position, velocity, and acceleration into attitude commands. |
| 12 | + |
| 13 | +**Example:** |
| 14 | +```python |
| 15 | +from drone_controllers import parametrize |
| 16 | +from drone_controllers.mellinger import state2attitude |
| 17 | + |
| 18 | +controller = parametrize(state2attitude, "cf2x_L250") |
| 19 | + |
| 20 | +rpyt, pos_err_i = controller(pos, quat, vel, ang_vel, cmd) |
| 21 | +``` |
| 22 | + |
| 23 | +### attitude2force_torque |
| 24 | + |
| 25 | +::: drone_controllers.mellinger.control.attitude2force_torque |
| 26 | + |
| 27 | +The attitude control component that converts attitude commands into desired forces and torques. |
| 28 | + |
| 29 | +**Example:** |
| 30 | +```python |
| 31 | +from drone_controllers import parametrize |
| 32 | +from drone_controllers.mellinger import attitude2force_torque |
| 33 | + |
| 34 | +controller = parametrize(attitude2force_torque, "cf2x_L250") |
| 35 | + |
| 36 | +force, torque, att_err_i = controller(pos, quat, vel, ang_vel, rpyt_cmd) |
| 37 | +``` |
| 38 | + |
| 39 | +### force_torque2rotor_vel |
| 40 | + |
| 41 | +::: drone_controllers.mellinger.control.force_torque2rotor_vel |
| 42 | + |
| 43 | +Converts desired forces and torques into individual rotor velocities using the quadrotor mixing matrix. |
| 44 | + |
| 45 | +**Example:** |
| 46 | +```python |
| 47 | +from drone_controllers import parametrize |
| 48 | +from drone_controllers.mellinger import force_torque2rotor_vel |
| 49 | + |
| 50 | +controller = parametrize(force_torque2rotor_vel, "cf2x_L250") |
| 51 | + |
| 52 | +rotor_speeds = controller(force, torque) |
| 53 | +``` |
| 54 | + |
| 55 | +## Parameter Classes |
| 56 | + |
| 57 | +### StateParams |
| 58 | + |
| 59 | +::: drone_controllers.mellinger.params.StateParams |
| 60 | + |
| 61 | +Parameters for the position control loop. |
| 62 | + |
| 63 | +### AttitudeParams |
| 64 | + |
| 65 | +::: drone_controllers.mellinger.params.AttitudeParams |
| 66 | + |
| 67 | +Parameters for the attitude control loop. |
| 68 | + |
| 69 | +### ForceTorqueParams |
| 70 | + |
| 71 | +::: drone_controllers.mellinger.params.ForceTorqueParams |
| 72 | + |
| 73 | +Parameters for the force/torque to rotor speed conversion. |
| 74 | + |
| 75 | +## Complete Controller Pipeline |
| 76 | + |
| 77 | +Here's how to use all three components together: |
| 78 | + |
| 79 | +```python |
| 80 | +import numpy as np |
| 81 | +from drone_controllers import parametrize |
| 82 | +from drone_controllers.mellinger import ( |
| 83 | + state2attitude, |
| 84 | + attitude2force_torque, |
| 85 | + force_torque2rotor_vel |
| 86 | +) |
| 87 | + |
| 88 | +# Parametrize all three controller components |
| 89 | +state_ctrl = parametrize(state2attitude, "cf2x_L250") |
| 90 | +attitude_ctrl = parametrize(attitude2force_torque, "cf2x_L250") |
| 91 | +rotor_ctrl = parametrize(force_torque2rotor_vel, "cf2x_L250") |
| 92 | + |
| 93 | +# Define state |
| 94 | +pos = np.array([0.0, 0.0, 1.0]) |
| 95 | +quat = np.array([0.0, 0.0, 0.0, 1.0]) # [x, y, z, w] |
| 96 | +vel = np.array([0.0, 0.0, 0.0]) |
| 97 | +ang_vel = np.array([0.0, 0.0, 0.0]) |
| 98 | + |
| 99 | +# Define command: [x, y, z, vx, vy, vz, ax, ay, az, yaw, r_rate, p_rate, y_rate] |
| 100 | +cmd = np.array([1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]) |
| 101 | + |
| 102 | +# Run the complete pipeline |
| 103 | +rpyt, pos_err_i = state_ctrl(pos, quat, vel, ang_vel, cmd) |
| 104 | +force, torque, att_err_i = attitude_ctrl(pos, quat, vel, ang_vel, rpyt) |
| 105 | +rotor_speeds = rotor_ctrl(force, torque) |
| 106 | + |
| 107 | +print(f"Final rotor speeds: {rotor_speeds} rad/s") |
| 108 | +``` |
| 109 | + |
| 110 | +## Integral Error Handling |
| 111 | + |
| 112 | +The Mellinger controller uses integral terms for robustness. You must pass integral errors between calls: |
| 113 | + |
| 114 | +```python |
| 115 | +# Initialize |
| 116 | +pos_err_i = None |
| 117 | +att_err_i = None |
| 118 | + |
| 119 | +for step in range(100): |
| 120 | + # Update state and command... |
| 121 | + |
| 122 | + # Pass previous integral errors |
| 123 | + ctrl_errors = (pos_err_i,) if pos_err_i is not None else None |
| 124 | + rpyt, pos_err_i = state_ctrl(pos, quat, vel, ang_vel, cmd, ctrl_errors=ctrl_errors) |
| 125 | + |
| 126 | + ctrl_errors = (att_err_i,) if att_err_i is not None else None |
| 127 | + force, torque, att_err_i = attitude_ctrl(pos, quat, vel, ang_vel, rpyt, ctrl_errors=ctrl_errors) |
| 128 | + |
| 129 | + rotor_speeds = rotor_ctrl(force, torque) |
| 130 | +``` |
| 131 | + |
| 132 | +## Array API Compatibility |
| 133 | + |
| 134 | +All Mellinger functions support the Python Array API and can be used with JAX, PyTorch, etc.: |
| 135 | + |
| 136 | +```python |
| 137 | +import jax.numpy as jnp |
| 138 | +from jax import jit |
| 139 | + |
| 140 | +# Convert to JAX arrays |
| 141 | +pos_jax = jnp.array([0.0, 0.0, 1.0]) |
| 142 | +quat_jax = jnp.array([0.0, 0.0, 0.0, 1.0]) |
| 143 | +# ... other arrays |
| 144 | + |
| 145 | +# JIT compile the controller |
| 146 | +jit_controller = jit(parametrize(state2attitude, "cf2x_L250")) |
| 147 | + |
| 148 | +rpyt, pos_err_i = jit_controller(pos_jax, quat_jax, vel_jax, ang_vel_jax, cmd_jax) |
| 149 | +``` |
| 150 | + |
| 151 | +# References |
| 152 | + |
| 153 | +[1] D. Mellinger and V. Kumar, "Minimum snap trajectory generation and control for quadrotors," 2011 IEEE International Conference on Robotics and Automation, Shanghai, China, 2011, pp. 2520-2525, doi: 10.1109/ICRA.2011.5980409. |
0 commit comments