|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
| 5 | +import tomllib |
5 | 6 | import warnings |
6 | 7 | from functools import partial, wraps |
| 8 | +from pathlib import Path |
7 | 9 | from typing import TYPE_CHECKING, Any, Callable, ParamSpec, Protocol, TypeVar, runtime_checkable |
8 | 10 |
|
| 11 | +import numpy as np |
| 12 | + |
9 | 13 | if TYPE_CHECKING: |
10 | 14 | from types import ModuleType |
11 | 15 |
|
@@ -138,18 +142,27 @@ def decorator(fn: Callable[P, R]) -> Callable[P, R]: |
138 | 142 | return decorator |
139 | 143 |
|
140 | 144 |
|
141 | | -# def load_xml_params(drone_model: str) -> dict[str, Array]: |
142 | | -# """TODO. |
| 145 | +def load_params(physics: str, drone_model: str, xp: ModuleType | None = None) -> dict: |
| 146 | + """TODO. |
143 | 147 |
|
144 | | -# Args: |
145 | | -# model (str): _description_ |
| 148 | + Args: |
| 149 | + physics: _description_ |
| 150 | + drone_model: _description_ |
| 151 | + xp: The array API module to use. If not provided, numpy is used. |
146 | 152 |
|
147 | | -# Returns: |
148 | | -# dict[str, Array]: _description_ |
149 | | -# """ |
150 | | -# drone_path = Path(__file__).parents[1] / path |
151 | | -# # read in all parameters from xml |
152 | | -# params = ET.parse(drone_path).findall(".//custom/numeric") |
153 | | -# # create a dict from parameters containing array of floats |
154 | | -# params = {p.get("name"): np.array(list(map(float, p.get("data").split()))) for p in params} |
155 | | -# return params |
| 153 | + Returns: |
| 154 | + dict[str, Array]: _description_ |
| 155 | + """ |
| 156 | + xp = np if xp is None else xp |
| 157 | + with open(Path(__file__).parent / "data/params.toml", "rb") as f: |
| 158 | + physical_params = tomllib.load(f) |
| 159 | + if drone_model not in physical_params: |
| 160 | + raise KeyError(f"Drone model `{drone_model}` not found in data/params.toml") |
| 161 | + with open(Path(__file__).parent / f"{physics}/params.toml", "rb") as f: |
| 162 | + model_params = tomllib.load(f) |
| 163 | + if drone_model not in model_params: |
| 164 | + raise KeyError(f"Drone model `{drone_model}` not found in model params.toml") |
| 165 | + params = physical_params[drone_model] | model_params[drone_model] |
| 166 | + params["J_inv"] = np.linalg.inv(params["J"]) |
| 167 | + params = {k: xp.asarray(v) for k, v in params.items()} # if k in fields |
| 168 | + return params |
0 commit comments