-
Notifications
You must be signed in to change notification settings - Fork 45
Automate availability and add docs #121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ratheron
wants to merge
5
commits into
main
Choose a base branch
from
feature.availability_methods
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2d4f39e
Automate available drones and add docs on how to add drones
ratheron 1cb2cbd
Rename available_drones into Drone StrEnum
ratheron 0bd3182
Hard code Drone enum to enable type hinting
ratheron 41e1ea1
Fix docstrings
ratheron 695fd9f
Fix tests
ratheron File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -9,6 +9,7 @@ | |||||||
|
|
||||||||
| import numpy as np | ||||||||
|
|
||||||||
| from crazyflow.drones import Drone | ||||||||
| from crazyflow.utils import filter_to_signature, to_xp | ||||||||
| from crazyflow.utils import parametrize as _parametrize | ||||||||
|
|
||||||||
|
|
@@ -53,7 +54,7 @@ def decorator(fn: F) -> F: | |||||||
|
|
||||||||
|
|
||||||||
| def parametrize( | ||||||||
| fn: Callable[P, R], drone: str, xp: ModuleType | None = None, device: str | None = None | ||||||||
| fn: Callable[P, R], drone: Drone, xp: ModuleType | None = None, device: str | None = None | ||||||||
| ) -> Callable[P, R]: | ||||||||
| """Parametrize a dynamics function with the default dynamics parameters for a drone. | ||||||||
|
|
||||||||
|
|
@@ -66,10 +67,11 @@ def parametrize( | |||||||
| Example: | ||||||||
| ```python | ||||||||
| import numpy as np | ||||||||
| from crazyflow.drones import Drone | ||||||||
| from crazyflow.dynamics.core import parametrize | ||||||||
| from crazyflow.dynamics.first_principles import dynamics | ||||||||
|
|
||||||||
| dynamics_fn = parametrize(dynamics, drone="cf2x_L250") | ||||||||
| dynamics_fn = parametrize(dynamics, Drone.cf2x_L250) | ||||||||
| pos, quat = np.zeros(3), np.array([0.0, 0.0, 0.0, 1.0]) | ||||||||
| vel, ang_vel = np.zeros(3), np.zeros(3) | ||||||||
| rotor_vel, cmd = np.zeros(4), np.zeros(4) | ||||||||
|
|
@@ -85,16 +87,16 @@ def parametrize( | |||||||
|
|
||||||||
|
|
||||||||
| def load_params( | ||||||||
| dynamics: Dynamics | str, drone: str, xp: ModuleType | None = None, device: str | None = None | ||||||||
| dynamics: Dynamics, drone: Drone, xp: ModuleType | None = None, device: str | None = None | ||||||||
| ) -> dict: | ||||||||
| """Load all parameters of a drone for a dynamics model. | ||||||||
|
|
||||||||
| Merges the global parameters in ``crazyflow/dynamics/params.toml`` with the drone's section in | ||||||||
| ``crazyflow/dynamics/<dynamics>/params.toml`` and adds ``J_inv``. | ||||||||
|
|
||||||||
| Args: | ||||||||
| dynamics: The dynamics model, e.g. ``Dynamics.so_rpy`` or ``"so_rpy"``. | ||||||||
| drone: Name of the drone configuration, e.g. ``"cf2x_L250"``. | ||||||||
| dynamics: The dynamics model, e.g. ``Dynamics.so_rpy``. | ||||||||
| drone: The drone configuration, e.g. ``Drone.cf2x_L250``. | ||||||||
| xp: Array API module used to convert parameter values. If ``None``, NumPy is used. | ||||||||
| device: The device to use for the arrays. If ``None``, the device is inferred from the xp | ||||||||
| module. | ||||||||
|
|
@@ -103,16 +105,16 @@ def load_params( | |||||||
| A flat dict mapping parameter names to arrays in the requested array namespace. | ||||||||
|
|
||||||||
| Raises: | ||||||||
| ValueError: If ``dynamics`` is not a known model. | ||||||||
| ValueError: If ``dynamics`` or ``drone`` is unknown. | ||||||||
| KeyError: If ``drone`` has no section for ``dynamics``. | ||||||||
| """ | ||||||||
| dynamics = Dynamics(dynamics) | ||||||||
| if dynamics not in supported_dynamics(drone): | ||||||||
| raise KeyError(f"Drone `{drone}` not found in {dynamics}/params.toml") | ||||||||
| with open(Path(__file__).parent / "params.toml", "rb") as f: | ||||||||
| global_params = tomllib.load(f) | ||||||||
| with open(Path(__file__).parent / f"{dynamics}/params.toml", "rb") as f: | ||||||||
| dynamics_params = tomllib.load(f) | ||||||||
| if drone not in dynamics_params: | ||||||||
| raise KeyError(f"Drone `{drone}` not found in {dynamics}/params.toml") | ||||||||
| params = global_params | dynamics_params[drone] | ||||||||
| # Make sure J_inv does not have a dtype fixed before conversion to xp arrays to avoid fixing it | ||||||||
| # to np.float64 when other frameworks might prefer a different dtype. | ||||||||
|
|
@@ -121,7 +123,7 @@ def load_params( | |||||||
|
|
||||||||
|
|
||||||||
| def load_fn_params( | ||||||||
| fn: Callable, drone: str, xp: ModuleType | None = None, device: str | None = None | ||||||||
| fn: Callable, drone: Drone, xp: ModuleType | None = None, device: str | None = None | ||||||||
| ) -> dict: | ||||||||
| """Load the parameters a dynamics function accepts. | ||||||||
|
|
||||||||
|
|
@@ -130,7 +132,7 @@ def load_fn_params( | |||||||
|
|
||||||||
| Args: | ||||||||
| fn: The dynamics function for which to load parameters. | ||||||||
| drone: Name of the drone configuration, e.g. ``"cf2x_L250"``. | ||||||||
| drone: The drone configuration, e.g. ``Drone.cf2x_L250``. | ||||||||
| xp: Array API module used to convert parameter values. If ``None``, NumPy is used. | ||||||||
| device: The device to use for the arrays. If ``None``, the device is inferred from the xp | ||||||||
| module. | ||||||||
|
|
@@ -141,3 +143,46 @@ def load_fn_params( | |||||||
| assert callable(fn), f"Expected a function, got {type(fn)}" | ||||||||
| dynamics = fn.__module__.split(".")[-2] | ||||||||
| return filter_to_signature(load_params(dynamics, drone, xp=xp, device=device), fn) | ||||||||
|
|
||||||||
|
|
||||||||
| def _param_sections(dynamics: Dynamics) -> set[str]: | ||||||||
| """Return the drone sections declared in a dynamics model's ``params.toml``.""" | ||||||||
| with open(Path(__file__).parent / f"{dynamics}/params.toml", "rb") as f: | ||||||||
| return set(tomllib.load(f)) | ||||||||
|
|
||||||||
|
|
||||||||
| def supported_drones(dynamics: Dynamics) -> tuple[Drone, ...]: | ||||||||
| """Return the drones that ``dynamics`` can be parametrized for. | ||||||||
|
|
||||||||
| A drone is supported when ``crazyflow/dynamics/<dynamics>/params.toml`` has a section for it. | ||||||||
|
|
||||||||
| Args: | ||||||||
| dynamics: The dynamics model, e.g. ``Dynamics.so_rpy``. | ||||||||
|
|
||||||||
| Returns: | ||||||||
| The supported drones in the order of [Drone][crazyflow.drones.Drone]. | ||||||||
|
|
||||||||
| Raises: | ||||||||
| ValueError: If ``dynamics`` is not a known model. | ||||||||
|
Comment on lines
+164
to
+166
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I think we don't have to mention that here, especially since we don't raise ourselves |
||||||||
| """ | ||||||||
| dynamics = Dynamics(dynamics) | ||||||||
| return tuple(drone for drone in Drone if drone in _param_sections(dynamics)) | ||||||||
|
|
||||||||
|
|
||||||||
| def supported_dynamics(drone: Drone) -> tuple[Dynamics, ...]: | ||||||||
| """Return the dynamics models that ``drone`` can be simulated with. | ||||||||
|
|
||||||||
| A model is supported when its ``crazyflow/dynamics/<dynamics>/params.toml`` has a section for | ||||||||
| ``drone``. | ||||||||
|
|
||||||||
| Args: | ||||||||
| drone: The drone configuration, e.g. ``Drone.cf2x_L250``. | ||||||||
|
|
||||||||
| Returns: | ||||||||
| The supported models in the order of [Dynamics][crazyflow.dynamics.Dynamics]. | ||||||||
|
|
||||||||
| Raises: | ||||||||
| ValueError: If ``drone`` is not a known drone. | ||||||||
|
Comment on lines
+183
to
+185
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| """ | ||||||||
| drone = Drone(drone) | ||||||||
| return tuple(d for d in Dynamics if drone in _param_sections(d)) | ||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.