STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
config_loader.py
Go to the documentation of this file.
1"""Configuration loader for robust DDP controller."""
2
3from pathlib import Path
4from typing import Optional
5from .data_models import ControllerConfig
6
7
8def get_default_config() -> ControllerConfig:
9 """Get default controller configuration.
10
11 Returns:
12 -------
13 config : ControllerConfig
14 Default configuration with reasonable values for LOX/RP-1 engine.
15 """
16 return ControllerConfig(
17 # Prediction horizon (0.5 s at 100 Hz)
18 N=50,
19 dt=0.01,
20
21 # Dwell time (50 ms = 20 Hz max control rate)
22 dwell_time=0.05,
23
24 # Duty quantization (1% steps for PWM)
25 duty_quantization=0.01,
26
27 # Cost weights - tuned for good thrust tracking without objective blowup
28 qF=100.0, # Thrust tracking (high priority, normalized by reference to prevent blowup)
29 qMR=10.0, # Mixture ratio (high priority, normalized by reference)
30 qGas=0.001, # Gas consumption (minimal - allow controller to use gas freely)
31 qSwitch=0.0001, # Control switching (minimal - allow very aggressive control changes)
32
33 # Constraints
34 MR_min=1.5, # Minimum O/F
35 MR_max=3.0, # Maximum O/F
36 injector_dp_frac=0.1, # 10% minimum pressure drop
37 eps_i=1e-3, # Constraint tolerance
38 P_u_max=10e6, # 1450 psi max upstream pressure
39 P_copv_min=1e6, # 145 psi min COPV pressure
40 headroom_dp_min=0.05e6, # 7.25 psi min headroom
41
42 # Robustness
43 rho=0.1, # Disturbance bound
44 eta=0.01, # Robustness margin
45
46 # Solver
47 max_iterations=20, # Increased for better convergence
48 convergence_tol=1e-3, # Relaxed to allow more exploration
49
50 # Dynamics parameters
51 copv_cF=1e5, # COPV consumption coefficient for fuel [Pa/s per unit u_F]
52 copv_cO=1e5, # COPV consumption coefficient for oxidizer [Pa/s per unit u_O]
53 copv_loss=1e3, # COPV leakage/heat loss [Pa/s]
54 reg_setpoint=6.89476e6, # Regulator setpoint [Pa] = 1000 psi (fixed setpoint regulator)
55 reg_ratio=0.8, # P_reg / P_copv ratio if setpoint not specified (fallback)
56 alpha_F=10.0, # Fuel pressurization flow coefficient [1/s]
57 alpha_O=10.0, # Oxidizer pressurization flow coefficient [1/s]
58 rho_F=800.0, # Fuel density (RP-1) [kg/m³]
59 rho_O=1140.0, # Oxidizer density (LOX) [kg/m³]
60 tau_line_F=0.05, # Fuel feed line time constant [s] (increased for hardware-matching damping)
61 tau_line_O=0.05, # Oxidizer feed line time constant [s] (increased for hardware-matching damping)
62 )
63
64
65def load_config(filepath: Optional[str] = None) -> ControllerConfig:
66 """Load controller configuration from file.
67
68 Parameters:
69 -----------
70 filepath : str, optional
71 Path to config file. If None, returns default config.
72 Supports .json and .yaml extensions.
73
74 Returns:
75 -------
76 config : ControllerConfig
77 Loaded configuration.
78 """
79 if filepath is None:
80 return get_default_config()
81
82 path = Path(filepath)
83 if not path.exists():
84 raise FileNotFoundError(f"Config file not found: {filepath}")
85
86 suffix = path.suffix.lower()
87 if suffix == '.json':
88 return ControllerConfig.from_json(str(path))
89 elif suffix in ['.yaml', '.yml']:
90 return ControllerConfig.from_yaml(str(path))
91 else:
92 raise ValueError(f"Unsupported config file format: {suffix}. Use .json or .yaml")
93
94
95def save_config(config: ControllerConfig, filepath: str) -> None:
96 """Save controller configuration to file.
97
98 Parameters:
99 -----------
100 config : ControllerConfig
101 Configuration to save.
102 filepath : str
103 Path to save config file. Extension determines format (.json or .yaml).
104 """
105 path = Path(filepath)
106 suffix = path.suffix.lower()
107
108 if suffix == '.json':
109 config.to_json(str(path))
110 elif suffix in ['.yaml', '.yml']:
111 config.to_yaml(str(path))
112 else:
113 raise ValueError(f"Unsupported config file format: {suffix}. Use .json or .yaml")
114
None save_config(ControllerConfig config, str filepath)
ControllerConfig load_config(Optional[str] filepath=None)