-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_flight.py
304 lines (254 loc) · 12.9 KB
/
test_flight.py
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import unittest
import math
import krpctest
from krpctest.geometry import rad2deg, norm, normalize, dot, cross, vector
class TestFlight(krpctest.TestCase):
@classmethod
def setUpClass(cls):
cls.new_save()
cls.remove_other_vessels()
cls.set_circular_orbit('Kerbin', 100000)
cls.space_center = cls.connect().space_center
cls.vessel = cls.space_center.active_vessel
cls.connect().testing_tools.clear_rotation()
cls.connect().testing_tools.apply_rotation(116, (0, 0, -1))
cls.connect().testing_tools.apply_rotation(27, (-1, 0, 0))
cls.connect().testing_tools.apply_rotation(40, (0, -1, 0))
cls.far = cls.space_center.far_available
def test_equality(self):
flight = self.vessel.flight(self.vessel.reference_frame)
self.assertEqual(
flight, self.vessel.flight(self.vessel.reference_frame))
def check_properties_not_affected_by_reference_frame(self, flight):
""" Verify flight properties that aren't
affected by reference frames """
self.assertAlmostEqual(100000, flight.mean_altitude, delta=10)
self.assertAlmostEqual(100000 - max(0, flight.elevation),
flight.surface_altitude, delta=10)
self.assertAlmostEqual(100000 - flight.elevation,
flight.bedrock_altitude, delta=10)
def check_directions(self, flight):
""" Check flight.direction against flight.heading and flight.pitch """
direction = vector(flight.direction)
up_direction = (1, 0, 0)
north_direction = (0, 1, 0)
self.assertAlmostEqual(1, norm(direction))
# Check vessel direction vector agrees with pitch angle
pitch = 90 - rad2deg(math.acos(dot(up_direction, direction)))
self.assertAlmostEqual(pitch, flight.pitch, delta=2)
# Check vessel direction vector agrees with heading angle
up_component = dot(direction, up_direction) * vector(up_direction)
north_component = normalize(vector(direction) - up_component)
self.assertDegreesAlmostEqual(
rad2deg(math.acos(dot(north_component, north_direction))),
flight.heading, delta=1)
def check_speeds(self, flight):
""" Check flight.velocity agrees with flight.*_speed """
up_direction = (0, 1, 0)
velocity = vector(flight.velocity)
vertical_speed = dot(velocity, up_direction)
horizontal_speed = norm(velocity) - vertical_speed
self.assertAlmostEqual(norm(velocity), flight.speed, delta=1)
self.assertAlmostEqual(horizontal_speed,
flight.horizontal_speed, delta=1)
self.assertAlmostEqual(vertical_speed,
flight.vertical_speed, delta=1)
def check_orbital_vectors(self, flight):
""" Check orbital direction vectors """
prograde = vector(flight.prograde)
retrograde = vector(flight.retrograde)
normal = vector(flight.normal)
anti_normal = vector(flight.anti_normal)
radial = vector(flight.radial)
anti_radial = vector(flight.anti_radial)
self.assertAlmostEqual(1, norm(prograde))
self.assertAlmostEqual(1, norm(retrograde))
self.assertAlmostEqual(1, norm(normal))
self.assertAlmostEqual(1, norm(anti_normal))
self.assertAlmostEqual(1, norm(radial))
self.assertAlmostEqual(1, norm(anti_radial))
self.assertAlmostEqual(
tuple(prograde), [-x for x in retrograde], places=2)
self.assertAlmostEqual(
tuple(radial), [-x for x in anti_radial], places=2)
self.assertAlmostEqual(
tuple(normal), [-x for x in anti_normal], places=2)
self.assertAlmostEqual(0, dot(prograde, radial), places=2)
self.assertAlmostEqual(0, dot(prograde, normal), places=2)
self.assertAlmostEqual(0, dot(radial, normal), places=2)
def test_flight_vessel_reference_frame(self):
flight = self.vessel.flight(self.vessel.reference_frame)
self.check_properties_not_affected_by_reference_frame(flight)
self.assertAlmostEqual((0, 0, 0), flight.velocity, delta=0.5)
self.assertAlmostEqual(0, flight.speed, delta=0.5)
self.assertAlmostEqual(0, flight.horizontal_speed, delta=0.5)
self.assertAlmostEqual(0, flight.vertical_speed, delta=0.5)
self.assertDegreesAlmostEqual(0, flight.pitch, delta=1)
self.assertDegreesAlmostEqual(0, flight.heading, delta=1)
self.assertDegreesAlmostEqual(-90, flight.roll, delta=1)
self.check_directions(flight)
self.check_speeds(flight)
self.check_orbital_vectors(flight)
def test_flight_vessel_orbital_reference_frame(self):
ref = self.vessel.orbital_reference_frame
flight = self.vessel.flight(ref)
self.check_properties_not_affected_by_reference_frame(flight)
self.assertAlmostEqual((0, 0, 0), flight.velocity, delta=0.5)
self.assertAlmostEqual(0, flight.speed, delta=0.5)
self.assertAlmostEqual(0, flight.horizontal_speed, delta=0.5)
self.assertAlmostEqual(0, flight.vertical_speed, delta=0.5)
self.check_speeds(flight)
self.check_orbital_vectors(flight)
def test_flight_vessel_surface_reference_frame(self):
ref = self.vessel.surface_reference_frame
flight = self.vessel.flight(ref)
self.check_properties_not_affected_by_reference_frame(flight)
self.assertAlmostEqual((0, 0, 0), flight.velocity, delta=0.5)
self.assertAlmostEqual(0, flight.speed, delta=0.5)
self.assertAlmostEqual(0, flight.horizontal_speed, delta=0.5)
self.assertAlmostEqual(0, flight.vertical_speed, delta=0.5)
self.assertAlmostEqual(27, flight.pitch, delta=1)
self.assertAlmostEqual(116, flight.heading, delta=1)
self.assertAlmostEqual(40, flight.roll, delta=1)
self.check_directions(flight)
self.check_speeds(flight)
self.check_orbital_vectors(flight)
def test_flight_orbit_body_reference_frame(self):
ref = self.vessel.orbit.body.reference_frame
flight = self.vessel.flight(ref)
self.check_properties_not_affected_by_reference_frame(flight)
speed = 2041.75
self.assertAlmostEqual(speed, norm(flight.velocity), delta=0.5)
position = self.vessel.position(ref)
direction = vector(cross(normalize(position), (0, 1, 0)))
velocity = tuple(direction * speed)
self.assertAlmostEqual(velocity, flight.velocity, delta=0.5)
self.assertAlmostEqual(speed, flight.speed, delta=0.5)
self.assertAlmostEqual(speed, flight.horizontal_speed, delta=0.5)
self.assertAlmostEqual(0, flight.vertical_speed, delta=0.5)
self.check_speeds(flight)
self.check_orbital_vectors(flight)
def test_flight_orbit_body_non_rotating_reference_frame(self):
ref = self.vessel.orbit.body.non_rotating_reference_frame
flight = self.vessel.flight(ref)
self.check_properties_not_affected_by_reference_frame(flight)
speed = 2245.75
self.assertAlmostEqual(speed, norm(flight.velocity), delta=0.5)
position = self.vessel.position(ref)
direction = vector(cross(normalize(position), (0, 1, 0)))
velocity = direction * speed
self.assertAlmostEqual(tuple(velocity), flight.velocity, delta=2)
self.assertAlmostEqual(speed, flight.speed, delta=0.5)
self.assertAlmostEqual(speed, flight.horizontal_speed, delta=0.5)
self.assertAlmostEqual(0, flight.vertical_speed, delta=0.5)
self.check_speeds(flight)
self.check_orbital_vectors(flight)
def test_latitude_and_longitude(self):
# In a circular orbit, in anti-clockwise direction looking down on the
# north pole of Kerbin.
# Latitude should be 0 (we're at the equator)
# Longitude should be gradually increasing
flight = self.vessel.flight()
longitude = flight.longitude
self.wait()
for _ in range(5):
self.assertAlmostEqual(0, flight.latitude, places=3)
self.assertLess(longitude, flight.longitude)
longitude = flight.longitude
self.wait()
class TestFlightVerticalSpeed(krpctest.TestCase):
@classmethod
def setUpClass(cls):
cls.new_save()
cls.remove_other_vessels()
cls.vessel = cls.connect().space_center.active_vessel
def check_speed(self, flight, ref):
up = normalize(vector(self.vessel.position(ref)) -
vector(self.vessel.orbit.body.position(ref)))
v = self.vessel.velocity(ref)
speed = norm(v)
vertical_speed = dot(v, up)
horizontal_speed = math.sqrt(
speed*speed - vertical_speed*vertical_speed)
self.assertAlmostEqual(speed, flight.speed, delta=0.5)
self.assertAlmostEqual(vertical_speed,
flight.vertical_speed, delta=0.5)
self.assertAlmostEqual(horizontal_speed,
flight.horizontal_speed, delta=0.5)
def test_vertical_speed_positive(self):
self.set_orbit('Kerbin', 2000000, 0.2, 0, 0, 0, 1, 0)
ref = self.vessel.orbit.body.reference_frame
flight = self.vessel.flight(ref)
self.assertGreater(flight.vertical_speed, 0)
self.check_speed(flight, ref)
def test_vertical_speed_negative(self):
self.set_orbit('Kerbin', 2000000, 0.2, 0, 0, 0, -2, 0)
ref = self.vessel.orbit.body.reference_frame
flight = self.vessel.flight(ref)
self.assertGreater(0, flight.vertical_speed)
self.check_speed(flight, ref)
def test_surface_speed(self):
self.set_circular_orbit('Kerbin', 100000)
ref = self.vessel.orbit.body.reference_frame
flight = self.vessel.flight(ref)
self.check_speed(flight, ref)
self.assertAlmostEqual(2042.04, flight.speed, places=1)
self.assertAlmostEqual(2042.04, flight.horizontal_speed, places=1)
self.assertAlmostEqual(0, flight.vertical_speed, delta=0.5)
def test_orbital_speed(self):
self.set_circular_orbit('Kerbin', 100000)
ref = self.vessel.orbit.body.non_rotating_reference_frame
flight = self.vessel.flight(ref)
self.check_speed(flight, ref)
self.assertAlmostEqual(2246.14, flight.speed, places=1)
self.assertAlmostEqual(2246.14, flight.horizontal_speed, places=1)
self.assertAlmostEqual(0, flight.vertical_speed, delta=0.5)
class TestFlightAtLaunchpad(krpctest.TestCase):
@classmethod
def setUpClass(cls):
cls.new_save()
cls.launch_vessel_from_vab('Basic')
cls.remove_other_vessels()
cls.vessel = cls.connect().space_center.active_vessel
cls.far = cls.connect().space_center.far_available
def test_latitude_and_longitude(self):
flight = self.vessel.flight()
self.assertAlmostEqual(-0.09694444, flight.latitude, places=3)
self.assertAlmostEqual(-74.5575, flight.longitude, places=3)
def test_ferram_aerospace_research(self):
if self.far:
flight = self.vessel.flight()
self.assertAlmostEqual(1.188, flight.atmosphere_density, places=3)
self.assertAlmostEqual(0, flight.drag, delta=0.5)
self.assertAlmostEqual(0, flight.dynamic_pressure)
self.assertAlmostEqual(0, flight.angle_of_attack, delta=2.5)
self.assertAlmostEqual(-27, flight.sideslip_angle, delta=2)
self.assertAlmostEqual(0, flight.stall_fraction)
self.assertAlmostEqual(0, flight.mach_number)
self.assertAlmostEqual(193, flight.terminal_velocity, delta=0.5)
self.assertAlmostEqual(0.103, flight.drag_coefficient, places=3)
self.assertAlmostEqual(0, flight.lift_coefficient, places=3)
self.assertAlmostEqual(
0, flight.pitching_moment_coefficient, places=3)
self.assertAlmostEqual(
2246.8, flight.ballistic_coefficient, delta=50)
self.assertAlmostEqual(0, flight.thrust_specific_fuel_consumption)
self.assertEqual('Nominal', flight.far_status)
# def test_drag_coefficient(self):
# if not self.far:
# # Using stock aerodynamic model
# parts = {
# 'mk1pod': {'n': 1, 'mass': 0.8, 'drag': 0.2},
# 'fuelTank': {'n': 1, 'mass': 0.125, 'drag': 0.2},
# 'batteryPack': {'n': 2, 'mass': 0.01, 'drag': 0.2},
# 'solarPanels1': {'n': 3, 'mass': 0.02, 'drag': 0.25},
# 'liquidEngine2': {'n': 1, 'mass': 1.5, 'drag': 0.2}
# }
# total_mass = sum(x['mass']*x['n'] for x in parts.values())
# mass_drag_products = sum(x['mass']*x['drag']*x['n']
# for x in parts.values())
# drag_coefficient = mass_drag_products / total_mass
# self.assertAlmostEqual(
# drag_coefficient, self.vessel.flight().drag_coefficient)
if __name__ == '__main__':
unittest.main()