Skip to content

Commit 818ddb3

Browse files
committed
Add drone models to envs. Fix so_rpy_rotor_drag physics
1 parent 8005408 commit 818ddb3

6 files changed

Lines changed: 21 additions & 17 deletions

File tree

crazyflow/envs/drone_env.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ def __init__(
6363
num_envs: int = 1,
6464
max_episode_time: float = 10.0,
6565
physics: Literal["so_rpy", "first_principles"] | Physics = Physics.so_rpy,
66+
drone_model: str = "cf2x_L250",
6667
freq: int = 500,
6768
device: str = "cpu",
6869
reset_randomization: Callable[[SimData, Array], SimData] | None = None,
@@ -85,7 +86,9 @@ def __init__(
8586
assert Physics(physics) in Physics, f"Invalid physics type {physics}"
8687

8788
# Initialize the simulation
88-
self.sim = Sim(n_worlds=num_envs, n_drones=1, device=device, physics=physics)
89+
self.sim = Sim(
90+
n_worlds=num_envs, n_drones=1, drone_model=drone_model, device=device, physics=physics
91+
)
8992
assert self.sim.freq >= self.sim.control_freq, "Sim freq must be higher than control freq"
9093
if not self.sim.freq % self.freq == 0:
9194
# We can handle other cases, but it's not recommended

crazyflow/envs/figure_8_env.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def __init__(
3131
num_envs: int = 1,
3232
max_episode_time: float = 10.0,
3333
physics: Literal["so_rpy", "first_principles"] | Physics = Physics.so_rpy,
34+
drone_model: str = "cf2x_L250",
3435
freq: int = 500,
3536
device: str = "cpu",
3637
):
@@ -43,13 +44,15 @@ def __init__(
4344
num_envs: Number of environments to run in parallel.
4445
max_episode_time: Maximum episode time in seconds.
4546
physics: Physics backend to use.
47+
drone_model: Drone model of the environment.
4648
freq: Frequency of the simulation.
4749
device: Device to use for the simulation.
4850
"""
4951
super().__init__(
5052
num_envs=num_envs,
5153
max_episode_time=max_episode_time,
5254
physics=physics,
55+
drone_model=drone_model,
5356
freq=freq,
5457
device=device,
5558
)
@@ -60,9 +63,9 @@ def __init__(
6063
n_steps = int(np.ceil(trajectory_time * self.freq))
6164
t = np.linspace(0, 2 * np.pi, n_steps)
6265
radius = 1 # Radius for the circles
63-
y = np.zeros_like(t) # x is 0 everywhere
6466
x = radius * np.sin(t) # Scale amplitude for 1-meter diameter
65-
z = radius * np.sin(2 * t) + 1.2 # Scale amplitude for 1-meter diameter
67+
y = np.zeros_like(t) # x is 0 everywhere
68+
z = radius / 2 * np.sin(2 * t) + 1 # Scale amplitude for 1-meter diameter
6669
self.trajectory = np.array([x, y, z]).T
6770

6871
# Define trajectory sampling parameters

crazyflow/sim/physics.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ class SoRpyRotorDragData:
263263
"""Force constant of the drone."""
264264
KM: Array # (N, M, 1)
265265
"""Torque constant of the drone."""
266-
rotor_coef: Array # (N, M, 1)
266+
thrust_time_coef: Array # (N, M, 1)
267267
"""Rotor coefficient of the drone."""
268268
acc_coef: Array # (N, M, 1)
269269
"""Acceleration coefficient of the drone."""
@@ -277,8 +277,8 @@ class SoRpyRotorDragData:
277277
"""Roll pitch yaw command coefficient of the drone."""
278278
drag_linear_coef: Array # (N, M, 1)
279279
"""Linear drag coefficient of the drone."""
280-
drag_abs_linear_coef: Array # (N, M, 1)
281-
"""Absolute linear drag coefficient of the drone."""
280+
drag_square_coef: Array # (N, M, 1)
281+
"""Square drag coefficient of the drone."""
282282

283283
def create(
284284
n_worlds: int, n_drones: int, drone_model: str, device: Device
@@ -293,27 +293,27 @@ def create(
293293
J_inv=jnp.linalg.inv(J),
294294
KF=jnp.asarray(p.KF, device=device),
295295
KM=jnp.asarray(p.KM, device=device),
296-
rotor_coef=jnp.asarray(p.rotor_coef, device=device),
296+
thrust_time_coef=jnp.asarray(p.thrust_time_coef, device=device),
297297
acc_coef=jnp.asarray(p.acc_coef, device=device),
298298
cmd_f_coef=jnp.asarray(p.cmd_f_coef, device=device),
299299
rpy_coef=jnp.asarray(p.rpy_coef, device=device),
300300
rpy_rates_coef=jnp.asarray(p.rpy_rates_coef, device=device),
301301
cmd_rpy_coef=jnp.asarray(p.cmd_rpy_coef, device=device),
302302
drag_linear_coef=jnp.asarray(p.drag_linear_coef, device=device),
303-
drag_abs_linear_coef=jnp.asarray(p.drag_abs_linear_coef, device=device),
303+
drag_square_coef=jnp.asarray(p.drag_square_coef, device=device),
304304
)
305305

306306

307307
def so_rpy_rotor_drag_physics(data: SimData) -> SimData:
308308
"""Compute the forces and torques from the so_rpy_rotor_drag physics model."""
309309
params: SoRpyRotorDragData = data.params
310-
vel, _, acc, ang_acc, _ = so_rpy_rotor_drag_dynamics(
310+
vel, _, acc, ang_acc, rotor_acc = so_rpy_rotor_drag_dynamics(
311311
pos=data.states.pos,
312312
quat=data.states.quat,
313313
vel=data.states.vel,
314314
ang_vel=data.states.ang_vel,
315315
cmd=data.controls.attitude.cmd,
316-
rotor_vel=None, # TODO: Add rotor_vel once the parameters are available
316+
rotor_vel=data.states.rotor_vel,
317317
dist_f=data.states.force,
318318
dist_t=data.states.torque,
319319
mass=params.mass,
@@ -322,17 +322,16 @@ def so_rpy_rotor_drag_physics(data: SimData) -> SimData:
322322
J_inv=params.J_inv,
323323
KF=params.KF,
324324
KM=params.KM,
325-
rotor_coef=params.rotor_coef,
325+
thrust_time_coef=params.thrust_time_coef,
326326
acc_coef=params.acc_coef,
327327
cmd_f_coef=params.cmd_f_coef,
328328
rpy_coef=params.rpy_coef,
329329
rpy_rates_coef=params.rpy_rates_coef,
330330
cmd_rpy_coef=params.cmd_rpy_coef,
331331
drag_linear_coef=params.drag_linear_coef,
332-
drag_abs_linear_coef=params.drag_abs_linear_coef,
332+
drag_square_coef=params.drag_square_coef,
333333
)
334-
# TODO: Add rotor_vel to the states_deriv
335334
states_deriv = data.states_deriv.replace(
336-
vel=vel, ang_vel=data.states.ang_vel, acc=acc, ang_acc=ang_acc
335+
vel=vel, ang_vel=data.states.ang_vel, acc=acc, ang_acc=ang_acc, rotor_acc=rotor_acc
337336
)
338337
return data.replace(states_deriv=states_deriv)

crazyflow/sim/sim.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
from einops import rearrange
1717
from gymnasium.envs.mujoco.mujoco_rendering import MujocoRenderer
1818
from jax import Array, Device
19-
from jax.scipy.spatial.transform import Rotation as R
2019

2120
from crazyflow.control.control import Control, controllable
2221
from crazyflow.exception import ConfigError, NotInitializedError

submodules/drone-controllers

submodules/drone-models

0 commit comments

Comments
 (0)