|
4 | 4 |
|
5 | 5 | import xml.etree.ElementTree as ET |
6 | 6 | from pathlib import Path |
7 | | -from typing import TYPE_CHECKING, NamedTuple, Type |
| 7 | +from typing import TYPE_CHECKING, NamedTuple |
| 8 | + |
| 9 | +from array_api_compat import numpy as np |
8 | 10 |
|
9 | 11 | if TYPE_CHECKING: |
| 12 | + from types import ModuleType |
| 13 | + |
10 | 14 | from array_api_typing import Array |
11 | 15 |
|
12 | 16 | # Configs (used in testing) |
@@ -59,12 +63,13 @@ class Constants(NamedTuple): |
59 | 63 | DI_DD_PARAMS: Array |
60 | 64 | DI_DD_ACC: Array |
61 | 65 |
|
62 | | - @classmethod |
63 | | - def from_file(cls, path: str, xp: Type[Array]) -> Constants: |
| 66 | + @staticmethod |
| 67 | + def from_file(path: Path, xp: ModuleType | None = None) -> Constants: |
64 | 68 | """Creates constants based on the xml file at the given location. |
65 | 69 |
|
66 | 70 | The constants are supposed to be under the costum/numeric category. |
67 | 71 | """ |
| 72 | + xp = np if xp is None else xp |
68 | 73 | # Constants |
69 | 74 | drone_path = Path(__file__).parents[1] / path |
70 | 75 | # read in all parameters from xml |
@@ -116,7 +121,7 @@ def from_file(cls, path: str, xp: Type[Array]) -> Constants: |
116 | 121 | DI_DD_PARAMS = xp.stack((DI_DD_ROLL, DI_DD_PITCH, DI_DD_YAW), axis=0) |
117 | 122 | DI_DD_ACC = params["DI_DD_acc"] |
118 | 123 |
|
119 | | - return cls( |
| 124 | + return Constants( |
120 | 125 | GRAVITY, |
121 | 126 | GRAVITY_VEC, |
122 | 127 | MASS, |
@@ -153,12 +158,13 @@ def from_file(cls, path: str, xp: Type[Array]) -> Constants: |
153 | 158 | DI_DD_ACC, |
154 | 159 | ) |
155 | 160 |
|
156 | | - @classmethod |
157 | | - def from_config(cls, config: str, xp: Type[Array]) -> Constants: |
| 161 | + @staticmethod |
| 162 | + def from_config(config: str, xp: ModuleType | None = None) -> Constants: |
158 | 163 | """Creates constants based on the give configuration. |
159 | 164 |
|
160 | 165 | For available configs see Constants.available_configs |
161 | 166 | """ |
| 167 | + xp = np if xp is None else xp |
162 | 168 | match config: |
163 | 169 | case "cf2x_L250": |
164 | 170 | return Constants.from_file("models/data/cf2x_L250.xml", xp) |
|
0 commit comments