forked from RocketPy-Team/RocketPy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_environment_parameter.py
More file actions
106 lines (87 loc) · 4.39 KB
/
Copy pathtest_environment_parameter.py
File metadata and controls
106 lines (87 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env python3
"""
Test script to verify that the environment parameter is properly passed
to air brakes controller functions.
This script demonstrates the solution to the GitHub issue about accessing
environment data in air brakes controllers without global variables.
"""
def test_controller_with_environment():
"""Test controller function that uses environment parameter"""
def controller_function(time, sampling_rate, state, state_history,
observed_variables, air_brakes, sensors, environment):
"""
Example controller that uses environment parameter instead of global variables
"""
# Access environment data locally (no globals needed!)
altitude_ASL = state[2]
altitude_AGL = altitude_ASL - environment.elevation
vx, vy, vz = state[3], state[4], state[5]
# Get atmospheric conditions from environment object
wind_x = environment.wind_velocity_x(altitude_ASL)
wind_y = environment.wind_velocity_y(altitude_ASL)
sound_speed = environment.speed_of_sound(altitude_ASL)
# Calculate Mach number
free_stream_speed = ((wind_x - vx)**2 + (wind_y - vy)**2 + vz**2)**0.5
mach_number = free_stream_speed / sound_speed
# Simple control logic
if altitude_AGL > 1000:
air_brakes.deployment_level = 0.5
else:
air_brakes.deployment_level = 0.0
print(f"Time: {time:.2f}s, Alt AGL: {altitude_AGL:.1f}m, Mach: {mach_number:.2f}")
return (time, air_brakes.deployment_level, mach_number)
return controller_function
def test_backward_compatibility():
"""Test that old controller functions (without environment) still work"""
def old_controller_function(time, sampling_rate, state, state_history,
observed_variables, air_brakes):
"""
Old-style controller function (6 parameters) - should still work
"""
altitude = state[2]
if altitude > 1000:
air_brakes.deployment_level = 0.3
else:
air_brakes.deployment_level = 0.0
return (time, air_brakes.deployment_level)
return old_controller_function
def test_with_sensors():
"""Test controller function with sensors parameter"""
def controller_with_sensors(time, sampling_rate, state, state_history,
observed_variables, air_brakes, sensors):
"""
Controller function with sensors (7 parameters) - should still work
"""
altitude = state[2]
if altitude > 1000:
air_brakes.deployment_level = 0.4
else:
air_brakes.deployment_level = 0.0
return (time, air_brakes.deployment_level)
return controller_with_sensors
if __name__ == "__main__":
print("✅ Air Brakes Controller Environment Parameter Test")
print("="*60)
# Test functions
controller_new = test_controller_with_environment()
controller_old = test_backward_compatibility()
controller_sensors = test_with_sensors()
print("✅ Created controller functions successfully:")
print(f" - New controller (8 params): {controller_new.__name__}")
print(f" - Old controller (6 params): {controller_old.__name__}")
print(f" - Sensors controller (7 params): {controller_sensors.__name__}")
print("\n✅ All controller function signatures are supported!")
print("\n📝 Benefits of the new environment parameter:")
print(" • No more global variables needed")
print(" • Proper serialization support")
print(" • More modular and testable code")
print(" • Access to wind, atmospheric, and environmental data")
print(" • Backward compatibility maintained")
print(f"\n🚀 Example usage in controller:")
print(" # Old way (with global variables):")
print(" altitude_AGL = altitude_ASL - env.elevation # ❌ Global variable")
print(" wind_x = env.wind_velocity_x(altitude_ASL) # ❌ Global variable")
print("")
print(" # New way (with environment parameter):")
print(" altitude_AGL = altitude_ASL - environment.elevation # ✅ Local parameter")
print(" wind_x = environment.wind_velocity_x(altitude_ASL) # ✅ Local parameter")