Skip to content

Commit d721745

Browse files
committed
Use more generic parameter loading
1 parent b74c06d commit d721745

3 files changed

Lines changed: 89 additions & 21 deletions

File tree

drone_models/core.py

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22

33
from __future__ import annotations
44

5+
import tomllib
56
import warnings
67
from functools import partial, wraps
8+
from pathlib import Path
79
from typing import TYPE_CHECKING, Any, Callable, ParamSpec, Protocol, TypeVar, runtime_checkable
810

11+
import numpy as np
12+
913
if TYPE_CHECKING:
1014
from types import ModuleType
1115

@@ -138,18 +142,27 @@ def decorator(fn: Callable[P, R]) -> Callable[P, R]:
138142
return decorator
139143

140144

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.
143147
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.
146152
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

drone_models/data/params.toml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# This file contains all the **physical** parameters for all models.
2+
3+
[cf2x_L250]
4+
mass = 0.033
5+
gravity_vec = [0.0, 0.0, -9.81]
6+
J = [
7+
[1.68e-5, 0.0, 0.0],
8+
[0.0, 1.68e-5, 0.0],
9+
[0.0, 0.0, 2.98e-5]
10+
]
11+
KF = 3.16e-10 # TODO: Update with new fit
12+
KM = 7.94e-12
13+
L = 0.03253
14+
mixing_matrix = [
15+
[-1.0, -1.0, 1.0, 1.0],
16+
[-1.0, 1.0, 1.0, -1.0],
17+
[-1.0, 1.0, -1.0, 1.0]
18+
]
19+
# TODO: This is an arbitrary number that leads to a working simulation. It MUST be fitted and
20+
# updated as soon as possible.
21+
thrust_tau = 0.03
22+
23+
[cf2x_P250]
24+
mass = 0.03454
25+
gravity_vec = [0.0, 0.0, -9.81]
26+
# J from IROS 2022
27+
J = [
28+
[1.4e-5, 0.0, 0.0],
29+
[0.0, 1.4e-5, 0.0],
30+
[0.0, 0.0, 2.17e-5]
31+
]
32+
KF = 5.792654323957372e-10
33+
KM = 7.94e-12
34+
L = 0.03253
35+
mixing_matrix = [
36+
[-1.0, -1.0, 1.0, 1.0],
37+
[-1.0, 1.0, 1.0, -1.0],
38+
[-1.0, 1.0, -1.0, 1.0]
39+
]
40+
thrust_tau = 7.2333515424212695
41+
42+
[cf2x_T350]
43+
mass = 0.03454
44+
gravity_vec = [0.0, 0.0, -9.81]
45+
J = [
46+
[1.4e-5, 0.0, 0.0],
47+
[0.0, 1.4e-5, 0.0],
48+
[0.0, 0.0, 2.17e-5]
49+
]
50+
KF = 1.068340683985525e-09
51+
KM = 7.94e-12
52+
L = 0.03253
53+
mixing_matrix = [
54+
[-1.0, -1.0, 1.0, 1.0],
55+
[-1.0, 1.0, 1.0, -1.0],
56+
[-1.0, 1.0, -1.0, 1.0]
57+
]
58+
thrust_tau = 8.719306881898989

drone_models/so_rpy/params.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
from __future__ import annotations
44

5-
import tomllib
6-
from pathlib import Path
75
from typing import TYPE_CHECKING, NamedTuple
86

97
import numpy as np
108

9+
from drone_models.core import load_params
10+
1111
if TYPE_CHECKING:
1212
from array_api_typing import Array
1313

@@ -28,10 +28,7 @@ class SoRpyParams(NamedTuple):
2828
@staticmethod
2929
def load(drone_model: str) -> SoRpyParams:
3030
"""Load the parameters for the drone model from the params.toml file."""
31-
with open(Path(__file__).parent / "params.toml", "rb") as f:
32-
params = tomllib.load(f)
33-
if drone_model not in params:
34-
raise KeyError(f"Drone model `{drone_model}` not found in params.toml")
35-
params = {k: np.asarray(v) for k, v in params[drone_model].items()}
36-
params["J_inv"] = np.linalg.inv(params["J"])
31+
params = load_params("so_rpy", drone_model)
32+
fields = SoRpyParams.__annotations__.keys()
33+
params = {k: np.asarray(v) for k, v in params.items() if k in fields}
3734
return SoRpyParams(**params)

0 commit comments

Comments
 (0)