Python API
This section is generated with mkdocstrings (Python handler). It documents the public API of the crazyflow package.
crazyflow
Classes
Control
Bases: str, Enum
Control type of the simulated onboard controller.
Attributes
attitude = 'attitude'
class-attribute
instance-attribute
Attitude control takes [roll, pitch, yaw, collective thrust].
Note
Recommended frequency is >=100 Hz.
force_torque = 'force_torque'
class-attribute
instance-attribute
Force and torque control takes [fx, fy, fz, tx, ty, tz].
Note
Recommended frequency is >=500 Hz.
state = 'state'
class-attribute
instance-attribute
State control takes [x, y, z, vx, vy, vz, ax, ay, az, yaw, roll_rate, pitch_rate, yaw_rate].
Note
Recommended frequency is >=20 Hz.
Warning
Currently, we only use positions, velocities, and yaw. The rest of the state is ignored. This is subject to change in the future.
Physics
Bases: str, Enum
Physics mode for the simulation.
Sim(n_worlds=1, n_drones=1, drone_model='cf2x_L250', physics=Physics.default, control=Control.default, integrator=Integrator.default, freq=500, state_freq=100, attitude_freq=500, force_torque_freq=500, device='cpu', xml_path=None, rng_key=0)
Source code in crazyflow/sim/sim.py
Attributes
controllable
property
Boolean array of shape (n_worlds,) that indicates which worlds are controllable.
A world is controllable if the last control step was more than 1/control_freq seconds ago.
Desired controls get stashed in the staged control buffers and are applied in step
as soon as the controller frequency allows for an update. Successive control updates that
happen before the staged buffers are applied overwrite the desired values.
Functions
attitude_control(controls)
Set the desired attitude for all drones in all worlds.
We need to stage the attitude controls because the sys_id physics mode operates directly on the attitude controls. If we were to directly update the controls, this would effectively bypass the control frequency and run the attitude controller at the physics update rate. By staging the controls, we ensure that the physics module sees the old controls until the controller updates at its correct frequency.
Source code in crazyflow/sim/sim.py
build_data()
Build the simulation data for the current configuration.
Note
This function re-initializes the simulation data according to the current configuration. It also returns the constructed data for use with pure functions.
Returns:
| Type | Description |
|---|---|
SimData
|
The simulation data as a single PyTree that can be passed to the pure simulation |
SimData
|
functions for stepping and resetting. |
Source code in crazyflow/sim/sim.py
build_default_data()
Initialize the default data for the simulation.
Note
This function initializes the default data used as a reference in the reset function to reset the simulation to. It also returns the constructed data for use with pure functions.
Returns:
| Type | Description |
|---|---|
SimData
|
The default simulation data used as a reference in the reset function to reset the |
SimData
|
simulation to. |
Source code in crazyflow/sim/sim.py
build_mjx_model(spec)
Build the MuJoCo model and data structures for the simulation.
Source code in crazyflow/sim/sim.py
build_mjx_spec()
Build the MuJoCo model specification for the simulation.
Source code in crazyflow/sim/sim.py
build_reset_fn()
Build the reset function for the current simulation configuration.
Note
This function both changes the underlying implementation of Sim.reset() in-place to the current pipeline and returns the function for pure functional style programming.
Returns:
| Type | Description |
|---|---|
Callable[[SimData, SimData, Array | None], SimData]
|
The pure JAX function that resets simulation data. It takes the current SimData, default |
Callable[[SimData, SimData, Array | None], SimData]
|
SimData, and an optional mask for worlds to reset, returning the updated SimData. |
Source code in crazyflow/sim/sim.py
build_step_fn()
Setup the chain of functions that are called in Sim.step().
We know all the functions that are called in succession since the simulation is configured at initialization time. Instead of branching through options at runtime, we construct a step function at initialization that selects the correct functions based on the settings.
Note
This function both changes the underlying implementation of Sim.step() in-place to the current pipeline and returns the function for pure functional style programming.
Warning
If any settings change, the pipeline of functions needs to be reconstructed.
Returns:
| Type | Description |
|---|---|
Callable[[SimData, int], SimData]
|
The pure JAX function that steps through the simulation. It takes the current SimData |
Callable[[SimData, int], SimData]
|
and the number of steps to simulate, and returns the updated SimData. |
Source code in crazyflow/sim/sim.py
contacts(body=None)
Get contact information from the simulation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
body
|
str | None
|
Optional body name to filter contacts for. If None, returns flags for all bodies. |
None
|
Returns:
| Type | Description |
|---|---|
Array
|
An boolean array of shape (n_worlds,) that is True if any contact is present. |
Source code in crazyflow/sim/sim.py
force_torque_control(cmd)
Set the desired force and torque for all drones in all worlds.
Source code in crazyflow/sim/sim.py
init_data(state_freq, attitude_freq, force_torque_freq, rng_key)
Initialize the simulation data.
Source code in crazyflow/sim/sim.py
reset(mask=None)
Reset the simulation to the initial state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mask
|
Array | None
|
Boolean array of shape (n_worlds, ) that indicates which worlds to reset. If None, all worlds are reset. |
None
|
Source code in crazyflow/sim/sim.py
seed(seed)
Set the JAX rng key for the simulation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seed
|
int
|
The seed for the JAX rng. |
required |
state_control(controls)
Set the desired state for all drones in all worlds.
Source code in crazyflow/sim/sim.py
step(n_steps=1)
Simulate all drones in all worlds for n time steps.