Skip to content

Commit 6d44683

Browse files
committed
Fix force_torque2rotor_vel conversion
1 parent 9cf5306 commit 6d44683

2 files changed

Lines changed: 11 additions & 12 deletions

File tree

drone_controllers/mellinger/control.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,8 @@ def attitude2force_torque(
186186
prev_ang_vel: Previous angular velocity in rad/s.
187187
prev_ang_vel_des: Previous angular velocity command in rad/s.
188188
L: Distance from the center of the quadrotor to the center of the rotor in m.
189-
KM: Torque constant in Nm/rad/s^2.
190-
KF: Force constant in N/rad/s^2.
189+
KM: Torque constant (Nm/RPM).
190+
KF: Force constant (N/RPM).
191191
mixing_matrix: Mixing matrix for the motor forces with shape (4, 3).
192192
193193
Returns:
@@ -252,14 +252,14 @@ def attitude2force_torque(
252252
# controllers within drone_models, we still want to return SI forces and torques. We thus need
253253
# to convert the legacy output to SI units.
254254
# l. 310 ff
255-
torque_des = motor_forces @ mixing_matrix * xp.stack([L, L, KM / KF])
255+
torque_des = (mixing_matrix @ motor_forces[..., None])[..., 0] * xp.stack([L, L, KM / KF])
256256
force_des = xp.sum(motor_forces, axis=-1)[..., None]
257257
return force_des, torque_des, r_int_error
258258

259259

260260
def force_torque_pwms2pwms(force_pwm: Array, torque_pwm: Array, mixing_matrix: Array) -> Array:
261261
"""Convert desired collective thrust and torques to rotor speeds using legacy behavior."""
262-
return force_pwm[..., None] + (mixing_matrix @ torque_pwm[..., None])[..., 0]
262+
return force_pwm[..., None] + (torque_pwm @ mixing_matrix)
263263

264264

265265
@register_controller_parameters(ForceTorqueParams)
@@ -293,8 +293,8 @@ def force_torque2rotor_vel(
293293
thrust_min: Minimum thrust in N.
294294
thrust_max: Maximum thrust in N.
295295
L: Distance from the center of the quadrotor to the center of the rotor in m.
296-
KM: Torque constant in Nm/rad/s^2.
297-
KF: Force constant in N/rad/s^2.
296+
KM: Torque constant (Nm/RPM).
297+
KF: Force constant (N/RPM).
298298
mixing_matrix: Mixing matrix for the motor forces with shape (4, 3).
299299
300300
Returns:
@@ -303,7 +303,7 @@ def force_torque2rotor_vel(
303303
xp = array_namespace(torque)
304304
assert torque.shape[-1] == 3, f"Torque must have shape (..., 3), but has {torque.shape}"
305305
assert force.shape[-1] == 1, f"Force must have shape (..., 1), but has {force.shape}"
306-
torque_forces = (mixing_matrix @ (torque * xp.asarray([L, L, KM / KF]))[..., None])[..., 0]
306+
torque_forces = (torque * xp.asarray([1 / L, 1 / L, KF / KM])) @ mixing_matrix
307307
motor_forces = (torque_forces + force) / 4
308308
# Clip motor forces on the thrust instead of PWM level.
309309
motor_forces = xp.where(xp.all(force == 0), 0.0, xp.clip(motor_forces, thrust_min, thrust_max))

drone_controllers/mellinger/params.toml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ pwm_min = 20000.0
77
pwm_max = 65535.0
88
torque_pwm_max = [32000.0, 32000.0, 32000.0]
99
L = 0.03253
10-
KF = 8.7e-10
10+
KF = 3.16e-10 # TODO: Check what the firmware uses
1111
KM = 7.94e-12
1212
mixing_matrix = [
13-
[-1.0, -1.0, -1.0],
14-
[-1.0, 1.0, 1.0],
15-
[ 1.0, 1.0, -1.0],
16-
[ 1.0, -1.0, 1.0]
13+
[-1.0, -1.0, 1.0, 1.0],
14+
[-1.0, 1.0, 1.0, -1.0],
15+
[-1.0, 1.0, -1.0, 1.0]
1716
]
1817
gravity_vec = [0.0, 0.0, -9.81]
1918

0 commit comments

Comments
 (0)