Mellinger¶
drone_controllers.mellinger.control
¶
Mellinger controller reimplementation based on the Crazyflie firmware.
The controller is split into three pure functions that form a pipeline:
state2attitude → attitude2force_torque → force_torque2rotor_vel.
Each stage can be used independently or chained together to produce per-motor
RPM commands from a full-state setpoint.
Reference: D. Mellinger and V. Kumar, "Minimum snap trajectory generation and control for quadrotors", ICRA 2011.
state2attitude(pos, quat, vel, cmd, ctrl_errors=None, ctrl_freq=100, *, mass, kp, kd, ki, gravity_vec, mass_thrust, int_err_max, thrust_max, pwm_max)
¶
Compute the positional part of the mellinger controller.
All controllers are implemented as pure functions. Therefore, integral errors have to be passed as an argument and returned as well.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pos
|
Array
|
Drone position with shape (..., 3). |
required |
quat
|
Array
|
Drone orientation as xyzw quaternion with shape (..., 4). |
required |
vel
|
Array
|
Drone velocity with shape (..., 3). |
required |
cmd
|
Array
|
Full state command in SI units and rad with shape (..., 13). The entries are [x, y, z, vx, vy, vz, ax, ay, az, yaw, roll_rate, pitch_rate, yaw_rate]. |
required |
ctrl_errors
|
tuple[Array, ...] | None
|
Tuple of integral errors. For state2attitude, the tuple contains a single array (..., 3) for the position integral error or is None. |
None
|
ctrl_freq
|
float
|
Control frequency in Hz |
100
|
mass
|
float
|
Drone mass used for calculations in the controller in kg. |
required |
kp
|
Array
|
Proportional gain for the position controller with shape (3,). |
required |
kd
|
Array
|
Derivative gain for the position controller with shape (3,). |
required |
ki
|
Array
|
Integral gain for the position controller with shape (3,). |
required |
gravity_vec
|
Array
|
Gravity vector with shape (3,). We assume gravity to be in the negative z direction. E.g., [0, 0, -9.81]. |
required |
mass_thrust
|
float
|
Conversion factor from thrust to PWM. |
required |
int_err_max
|
Array
|
Range of the integral error with shape (3,). i_range in the firmware. |
required |
thrust_max
|
float
|
Maximum thrust in N. |
required |
pwm_max
|
float
|
Maximum PWM value. |
required |
Returns:
| Type | Description |
|---|---|
Array
|
The RPY collective thrust command [rad, rad, rad, N], and the integral error of the position |
Array
|
controller. |
attitude2force_torque(quat, ang_vel, cmd, prev_ang_vel=None, ctrl_errors=None, ctrl_freq=500, *, kR, kw, ki_m, kd_omega, int_err_max, torque_pwm_max, thrust_max, pwm_min, pwm_max, L, thrust2torque, mixing_matrix)
¶
Compute the attitude to desired force-torque part of the Mellinger controller.
Note
We omit the axis flip in the firmware as it has only been introduced to make the controller compatible with the new frame of the Crazyflie 2.1.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
quat
|
Array
|
Drone orientation as xyzw quaternion with shape (..., 4). |
required |
ang_vel
|
Array
|
Drone angular drone velocity in rad/s with shape (..., 3). |
required |
cmd
|
Array
|
Commanded attitude (roll, pitch, yaw) and total thrust [rad, rad, rad, N]. |
required |
ctrl_errors
|
tuple[Array, ...] | None
|
Tuple of integral errors. For attitude2force_torque, the tuple contains a single array (..., 3) for the angular velocity integral error or is None. |
None
|
ctrl_freq
|
int
|
Control frequency in Hz |
500
|
kR
|
Array
|
Proportional gain for the rotation error with shape (3,). |
required |
kw
|
Array
|
Proportional gain for the angular velocity error with shape (3,). |
required |
ki_m
|
Array
|
Integral gain for the rotation error with shape (3,). |
required |
kd_omega
|
Array
|
Derivative gain for the angular velocity error with shape (3,). |
required |
int_err_max
|
Array
|
Range of the integral error with shape (3,). i_range in the firmware. |
required |
torque_pwm_max
|
Array
|
Maximum torque in PWM. |
required |
thrust_max
|
float
|
Maximum thrust in N. |
required |
pwm_min
|
float
|
Minimum PWM value. |
required |
pwm_max
|
float
|
Maximum PWM value. |
required |
prev_ang_vel
|
Array | None
|
Previous angular velocity in rad/s. |
None
|
L
|
float
|
Distance from the center of the quadrotor to the center of the rotor in m. |
required |
thrust2torque
|
float
|
Conversion factor (m). |
required |
mixing_matrix
|
Array
|
Mixing matrix for the motor forces with shape (4, 3). |
required |
Returns:
| Type | Description |
|---|---|
tuple[Array, Array, Array]
|
4 Motor forces [N], i_error_m |
force_torque_pwms2pwms(force_pwm, torque_pwm, mixing_matrix)
¶
Convert desired collective thrust and torques to rotor speeds using legacy behavior.
force_torque2rotor_vel(force, torque, *, thrust_min, thrust_max, L, rpm2thrust, thrust2torque, mixing_matrix)
¶
Convert desired collective thrust and torques to rotor speeds.
The firmware calculates PWMs for each motor, compensates for the battery voltage, and then applies the modified PWMs to the motors. We assume perfect battery compensation here, skip the PWM interface except for clipping, and instead return desired motor forces.
Note
The equivalent function in the crazyflie firmware is power_distribution from power_distribution_quadrotor.c.
Warning
This function assumes an X rotor configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
force
|
Array
|
Desired thrust in SI units with shape (...,). |
required |
torque
|
Array
|
Desired torque in SI units with shape (..., 3). |
required |
thrust_min
|
float
|
Minimum thrust in N. |
required |
thrust_max
|
float
|
Maximum thrust in N. |
required |
L
|
float
|
Distance from the center of the quadrotor to the center of the rotor in m. |
required |
rpm2thrust
|
Array
|
Force constants (N/RPM, N/RPM**2). |
required |
thrust2torque
|
float
|
Conversion factor (m). |
required |
mixing_matrix
|
Array
|
Mixing matrix for the motor forces with shape (4, 3). |
required |
Returns:
| Type | Description |
|---|---|
Array
|
The desired motor forces in SI units with shape (..., 4). |
See the Mellinger user guide for input/output tables, worked examples, and guidance on chaining all three stages.