Skip to content

Commit 5442991

Browse files
committed
Address comments
1 parent 14f3845 commit 5442991

30 files changed

Lines changed: 46 additions & 469 deletions

crazyflow/control/transform.py

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
"""Transformations between physical parameters of the quadrotors.
22
3-
Conversions such as from motor forces to rotor speeds, or from thrust to PWM, are bundled in this
4-
module.
3+
Bundles conversions between motor forces, rotor velocities, and PWM commands.
54
"""
65

76
from __future__ import annotations
87

98
from typing import TYPE_CHECKING
109

11-
import array_api_extra as xpx
1210
from array_api_compat import array_namespace
1311

1412
if TYPE_CHECKING:
@@ -32,38 +30,6 @@ def motor_force2rotor_vel(motor_forces: Array, rpm2thrust: Array) -> Array:
3230
) / (2 * rpm2thrust[2])
3331

3432

35-
def rotor_vel2body_force(rotor_vel: Array, rpm2thrust: Array) -> Array:
36-
"""Convert rotor velocities to motor forces."""
37-
xp = array_namespace(rotor_vel)
38-
body_force = xp.zeros(rotor_vel.shape[:-1] + (3,), dtype=rotor_vel.dtype)
39-
body_force = xpx.at(body_force)[..., 2].set(
40-
xp.sum(
41-
rpm2thrust[..., 0] + rpm2thrust[..., 1] * rotor_vel + rpm2thrust[..., 2] * rotor_vel**2,
42-
axis=-1,
43-
)
44-
)
45-
return body_force
46-
47-
48-
def rotor_vel2body_torque(
49-
rotor_vel: Array, rpm2thrust: Array, rpm2torque: Array, L: float | Array, mixing_matrix: Array
50-
) -> Array:
51-
"""Convert rotor velocities to motor torques."""
52-
xp = array_namespace(rotor_vel)
53-
forces = rpm2thrust[..., 0] + rpm2thrust[..., 1] * rotor_vel + rpm2thrust[..., 2] * rotor_vel**2
54-
torques_xy = (
55-
xp.stack([xp.zeros_like(forces), xp.zeros_like(forces), forces])
56-
@ mixing_matrix
57-
* xp.stack([L, L, 0])
58-
)
59-
torques = (
60-
rpm2torque[..., 0] + rpm2torque[..., 1] * rotor_vel + rpm2torque[..., 2] * rotor_vel**2
61-
)
62-
torques_z = xp.stack([xp.zeros_like(torques), xp.zeros_like(torques), torques])
63-
body_torque = torques_xy + torques_z
64-
return body_torque
65-
66-
6733
def force2pwm(thrust: Array | float, thrust_max: Array | float, pwm_max: Array | float) -> Array:
6834
"""Convert thrust in N to thrust in PWM.
6935

crazyflow/dynamics/first_principles/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@
3131
\begin{aligned}
3232
\dot{\mathbf{p}} &= \mathbf{v}, \\
3333
\dot{\mathbf{q}} &= \tfrac{1}{2}
34-
\begin{bmatrix}0 \\ {}^{\mathcal{B}}\boldsymbol{\omega}\end{bmatrix}
35-
\otimes \mathbf{q}, \\
34+
\mathbf{q} \otimes \begin{bmatrix} {}^{\mathcal{B}}\boldsymbol{\omega}\\0 \end{bmatrix}, \\
3635
m\dot{\mathbf{v}} &= m\mathbf{g}
3736
+ R\,{}^{\mathcal{B}}\mathbf{f}_t
3837
+ R\,{}^{\mathcal{B}}\mathbf{f}_a, \\

crazyflow/dynamics/so_rpy/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
\begin{aligned}
99
\dot{\mathbf{p}} &= \mathbf{v}, \\
1010
\dot{\mathbf{q}} &= \tfrac{1}{2}
11-
\begin{bmatrix}0 \\ {}^{\mathcal{B}}\boldsymbol{\omega}\end{bmatrix}
12-
\otimes \mathbf{q}, \\
11+
\mathbf{q} \otimes \begin{bmatrix} {}^{\mathcal{B}}\boldsymbol{\omega}\\0 \end{bmatrix}, \\
1312
m\dot{\mathbf{v}} &= m\mathbf{g}
1413
+ (c_{\mathrm{acc}} + c_f F_{\mathrm{cmd}})\,R\,\mathbf{e}_z, \\
1514
\ddot{\boldsymbol{\psi}} &=

crazyflow/dynamics/so_rpy_rotor/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
\dot{F} &= \frac{1}{\tau}(F_{\mathrm{cmd}} - F), \\
1111
\dot{\mathbf{p}} &= \mathbf{v}, \\
1212
\dot{\mathbf{q}} &= \tfrac{1}{2}
13-
\begin{bmatrix}0 \\ {}^{\mathcal{B}}\boldsymbol{\omega}\end{bmatrix}
14-
\otimes \mathbf{q}, \\
13+
\mathbf{q} \otimes \begin{bmatrix} {}^{\mathcal{B}}\boldsymbol{\omega}\\0 \end{bmatrix}, \\
1514
m\dot{\mathbf{v}} &= m\mathbf{g}
1615
+ (c_{\mathrm{acc}} + c_f F)\,R\,\mathbf{e}_z, \\
1716
\ddot{\boldsymbol{\psi}} &=

crazyflow/dynamics/so_rpy_rotor/dynamics.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def dynamics(
142142

143143

144144
def symbolic_dynamics(
145-
model_rotor_vel: bool = False,
145+
model_rotor_vel: bool = True,
146146
model_dist_f: bool = False,
147147
model_dist_t: bool = False,
148148
*,
@@ -166,7 +166,7 @@ def symbolic_dynamics(
166166
167167
Args:
168168
model_rotor_vel: If ``True``, the scalar thrust state is included in ``X`` and first-order
169-
thrust dynamics are modelled. Defaults to ``False``.
169+
thrust dynamics are modelled. Defaults to ``_True``.
170170
model_dist_f: If ``True``, a 3-D force disturbance is appended to ``X``.
171171
model_dist_t: If ``True``, a 3-D torque disturbance is appended to ``X``.
172172
mass: Drone mass in kg.

crazyflow/dynamics/so_rpy_rotor_drag/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
\dot{F} &= \frac{1}{\tau}(F_{\mathrm{cmd}} - F), \\
1111
\dot{\mathbf{p}} &= \mathbf{v}, \\
1212
\dot{\mathbf{q}} &= \tfrac{1}{2}
13-
\begin{bmatrix}0 \\ {}^{\mathcal{B}}\boldsymbol{\omega}\end{bmatrix}
14-
\otimes \mathbf{q}, \\
13+
\mathbf{q} \otimes \begin{bmatrix} {}^{\mathcal{B}}\boldsymbol{\omega}\\0 \end{bmatrix}, \\
1514
m\dot{\mathbf{v}} &= m\mathbf{g}
1615
+ (c_{\mathrm{acc}} + c_f F)\,R\,\mathbf{e}_z
1716
+ R\,D_b\,R^{\top}\mathbf{v}, \\

crazyflow/dynamics/symbols.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,36 @@
1-
"""Symbols used in the symbolic drone dynamics."""
1+
"""Symbols used in the symbolic drone dynamics.
2+
3+
Can be used to define symbolic CasADi expressions that are passed to model-based optimizers such as
4+
Acados.
5+
"""
26

37
import casadi as cs
48

59
from crazyflow.dynamics.utils import rotation
610

711
# States
812
px, py, pz = cs.MX.sym("px"), cs.MX.sym("py"), cs.MX.sym("pz")
9-
pos = cs.vertcat(px, py, pz) # Position
10-
"""Symbolic drone position.
11-
12-
Can be used to define symbolic CasADi expressions that are passed to model-based optimizers such as
13-
Acados.
14-
15-
:meta hide-value:
16-
"""
17-
# States
13+
pos = cs.vertcat(px, py, pz)
14+
"""Symbolic drone position."""
1815
qw, qx, qy, qz = cs.MX.sym("qw"), cs.MX.sym("qx"), cs.MX.sym("qy"), cs.MX.sym("qz")
19-
quat = cs.vertcat(qx, qy, qz, qw) # Quaternions
16+
quat = cs.vertcat(qx, qy, qz, qw)
17+
"""Symbolic drone orientation as xyzw quaternion."""
2018
rot = rotation.cs_quat2matrix(quat) # Rotation matrix from body to world frame
2119
vx, vy, vz = cs.MX.sym("vx"), cs.MX.sym("vy"), cs.MX.sym("vz")
22-
vel = cs.vertcat(vx, vy, vz) # Velocity
20+
vel = cs.vertcat(vx, vy, vz)
21+
"""Symbolic drone velocity."""
2322
wx, wy, wz = cs.MX.sym("wx"), cs.MX.sym("wy"), cs.MX.sym("wz")
24-
ang_vel = cs.vertcat(wx, wy, wz) # Angular velocity
23+
ang_vel = cs.vertcat(wx, wy, wz)
24+
"""Symbolic drone angular velocity."""
2525
w1, w2, w3, w4 = cs.MX.sym("w1"), cs.MX.sym("w2"), cs.MX.sym("w3"), cs.MX.sym("w4")
26-
rotor_vel = cs.vertcat(w1, w2, w3, w4) # Motor thrust
26+
rotor_vel = cs.vertcat(w1, w2, w3, w4)
27+
"""Symbolic rotor velocities."""
2728
dfx, dfy, dfz = cs.MX.sym("dfx"), cs.MX.sym("dfy"), cs.MX.sym("dfz")
28-
dist_f = cs.vertcat(dfx, dfy, dfz) # Disturbance forces
29+
dist_f = cs.vertcat(dfx, dfy, dfz)
30+
"""Symbolic disturbance forces."""
2931
dtx, dty, dtz = cs.MX.sym("dtx"), cs.MX.sym("dty"), cs.MX.sym("dtz")
30-
dist_t = cs.vertcat(dtx, dty, dtz) # Disturbance torques
32+
dist_t = cs.vertcat(dtx, dty, dtz)
33+
"""Symbolic disturbance torques."""
3134

3235
# Inputs
3336
cmd_w1, cmd_w2, cmd_w3, cmd_w4 = (
@@ -37,10 +40,11 @@
3740
cs.MX.sym("cmd_w4"),
3841
)
3942
cmd_rotor_vel = cs.vertcat(cmd_w1, cmd_w2, cmd_w3, cmd_w4)
40-
cmd_force = cs.vertcat(cmd_w1, cmd_w2, cmd_w3, cmd_w4)
43+
"""Symbolic rotor velocity commands."""
4144
cmd_roll, cmd_pitch, cmd_yaw = (cs.MX.sym("cmd_roll"), cs.MX.sym("cmd_pitch"), cs.MX.sym("cmd_yaw"))
4245
cmd_thrust = cs.MX.sym("cmd_thrust")
4346
cmd_rpyt = cs.vertcat(cmd_roll, cmd_pitch, cmd_yaw, cmd_thrust)
47+
"""Symbolic roll/pitch/yaw/thrust commands."""
4448

4549
# Special states for the so_rpy dynamics
4650
roll, pitch, yaw = cs.MX.sym("roll"), cs.MX.sym("pitch"), cs.MX.sym("yaw")

crazyflow/dynamics/transform.py

Lines changed: 0 additions & 132 deletions
This file was deleted.

crazyflow/envs/drone_env.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import warnings
22
from functools import partial
3-
from typing import Callable, Literal
3+
from typing import Callable
44

55
import jax
66
import jax.numpy as jnp
@@ -64,7 +64,7 @@ def __init__(
6464
*,
6565
num_envs: int = 1,
6666
max_episode_time: float = 10.0,
67-
dynamics: Literal["so_rpy", "first_principles"] | Dynamics = Dynamics.so_rpy,
67+
dynamics: Dynamics = Dynamics.so_rpy,
6868
drone: str = "cf2x_L250",
6969
freq: int = 500,
7070
device: str = "cpu",

crazyflow/envs/figure_8_env.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from typing import Literal
2-
31
import jax
42
import jax.numpy as jnp
53
import numpy as np
@@ -30,7 +28,7 @@ def __init__(
3028
*,
3129
num_envs: int = 1,
3230
max_episode_time: float = 10.0,
33-
dynamics: Literal["so_rpy", "first_principles"] | Dynamics = Dynamics.so_rpy,
31+
dynamics: Dynamics = Dynamics.so_rpy,
3432
drone: str = "cf2x_L250",
3533
freq: int = 500,
3634
device: str = "cpu",

0 commit comments

Comments
 (0)