|
1 | 1 | import math |
| 2 | +import warnings |
| 3 | +from typing import Iterable |
2 | 4 |
|
3 | 5 | import numpy as np |
4 | 6 |
|
|
21 | 23 | from rocketpy.rocket.aero_surface.generic_surface import GenericSurface |
22 | 24 | from rocketpy.rocket.components import Components |
23 | 25 | from rocketpy.rocket.parachute import Parachute |
24 | | -from rocketpy.tools import deprecated, parallel_axis_theorem_from_com |
| 26 | +from rocketpy.tools import ( |
| 27 | + deprecated, |
| 28 | + find_obj_from_hash, |
| 29 | + parallel_axis_theorem_from_com, |
| 30 | +) |
25 | 31 |
|
26 | 32 |
|
27 | 33 | # pylint: disable=too-many-instance-attributes, too-many-public-methods, too-many-instance-attributes |
@@ -2070,17 +2076,29 @@ def from_dict(cls, data): |
2070 | 2076 | for parachute in data["parachutes"]: |
2071 | 2077 | rocket.parachutes.append(parachute) |
2072 | 2078 |
|
2073 | | - for air_brakes in data["air_brakes"]: |
2074 | | - rocket.add_air_brakes( |
2075 | | - drag_coefficient_curve=air_brakes["drag_coefficient_curve"], |
2076 | | - controller_function=air_brakes["controller_function"], |
2077 | | - sampling_rate=air_brakes["sampling_rate"], |
2078 | | - clamp=air_brakes["clamp"], |
2079 | | - reference_area=air_brakes["reference_area"], |
2080 | | - initial_observed_variables=air_brakes["initial_observed_variables"], |
2081 | | - override_rocket_drag=air_brakes["override_rocket_drag"], |
2082 | | - name=air_brakes["name"], |
2083 | | - controller_name=air_brakes["controller_name"], |
2084 | | - ) |
| 2079 | + for sensor, position in data["sensors"]: |
| 2080 | + rocket.add_sensor(sensor, position) |
| 2081 | + |
| 2082 | + for air_brake in data["air_brakes"]: |
| 2083 | + rocket.air_brakes.append(air_brake) |
| 2084 | + |
| 2085 | + for controller in data["_controllers"]: |
| 2086 | + interactive_objects_hash = getattr(controller, "_interactive_objects_hash") |
| 2087 | + if interactive_objects_hash is not None: |
| 2088 | + is_iterable = isinstance(interactive_objects_hash, Iterable) |
| 2089 | + if not is_iterable: |
| 2090 | + interactive_objects_hash = [interactive_objects_hash] |
| 2091 | + for hash_ in interactive_objects_hash: |
| 2092 | + if (hashed_obj := find_obj_from_hash(data, hash_)) is not None: |
| 2093 | + if not is_iterable: |
| 2094 | + controller.interactive_objects = hashed_obj |
| 2095 | + else: |
| 2096 | + controller.interactive_objects.append(hashed_obj) |
| 2097 | + else: |
| 2098 | + warnings.warn( |
| 2099 | + "Could not find controller interactive objects." |
| 2100 | + "Deserialization will proceed, results may not be accurate." |
| 2101 | + ) |
| 2102 | + rocket._add_controllers(controller) |
2085 | 2103 |
|
2086 | 2104 | return rocket |
0 commit comments