-
Notifications
You must be signed in to change notification settings - Fork 0
/
balln.ks
287 lines (265 loc) · 8.17 KB
/
balln.ks
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
function GETDENSITY {
parameter altitude.
set LOWDENS TO 0.
set HIGHDENS TO 0.
SET MIX TO 1.
if (altitude < 2500)
{
SET LOWDENS TO 1.225.
SET HIGHDENS TO 0.898.
SET MIX TO altitude/2500.
} else if (altitude < 5000)
{
SET LOWDENS TO 0.898.
SET HIGHDENS TO 0.642 .
SET MIX TO (altitude-2500)/2500.
} else if (altitude < 7500)
{
SET LOWDENS TO 0.642 .
SET HIGHDENS TO 0.446.
SET MIX TO (altitude-5000)/2500.
} else if (altitude < 10000)
{
SET LOWDENS TO 0.446.
SET HIGHDENS TO 0.288.
SET MIX TO (altitude-7500)/2500.
} else if (altitude < 15000)
{
SET LOWDENS TO 0.288.
SET HIGHDENS TO 0.108 .
SET MIX TO (altitude-10000)/5000.
} else if (altitude < 20000)
{
SET LOWDENS TO 0.108.
SET HIGHDENS TO 0.040 .
SET MIX TO (altitude-15000)/5000.
} else if (altitude < 25000)
{
SET LOWDENS TO 0.040 .
SET HIGHDENS TO 0.015 .
SET MIX TO (altitude-20000)/5000.
} else if (altitude < 30000)
{
SET LOWDENS TO 0.015 .
SET HIGHDENS TO 0.006 .
SET MIX TO (altitude-25000)/5000.
} else if (altitude < 40000)
{
SET LOWDENS TO 0.006 .
SET HIGHDENS TO 0.001.
SET MIX TO (altitude-30000)/10000.
} else if (altitude < 70000)
{
SET LOWDENS TO 0.001.
SET HIGHDENS TO 0.0001.
SET MIX TO (altitude-40000)/30000.
} else
{
RETURN 0.
}
RETURN (LOWDENS*(1-MIX))+(HIGHDENS*(MIX)).
}.
function GETGATALT {
parameter ALTITUDETOCHECK.
return body:mu / (ALTITUDETOCHECK + body:radius)^2.
}.
function GETBALLISTICX {
parameter STARTINGALT.
parameter Uo.
parameter Vo.
parameter Cd.
SET g to GETGATALT(STARTINGALT).
SET Vt TO SQRT((2*SHIP:MASS*g)/(Cd*Density)).
SET ESTFALLTIME TO (Vo/g)*2.
SET ymax TO (Vt^2 / (2 * g)) * ln ((Vo^2 + Vt^2)/Vt^2).
SET ITERATIONS TO 5.
UNTIL (ITERATIONS = 0)
{
SET NEWG TO GETGATALT(STARTINGALT+ymax).
SET Vt TO SQRT((2*SHIP:MASS*NEWG)/(Cd*GETDENSITY((STARTINGALT+ymax)/2))).
SET ymax TO (Vt^2 / (2 * NEWG)) * ln ((Vo^2 + Vt^2)/Vt^2).
SET AVGG TO (g+NEWG)/2.
SET FALLTIMENEW TO (Vo/AVGG)*2.
SET X TO ((Vt*Vt)/AVGG)*(LN(((Vt*Vt)+AVGG*UoHMS*FALLTIMENEW)/(Vt*Vt))).
SET ITERATIONS TO ITERATIONS - 1.
}.
RETURN X.
}
function GETAEROFORCE {
parameter ALTITUDETOCHECK.
parameter VEL.
parameter CD.
SET Q TO (GETDENSITY(ALTITUDETOCHECK) * 0.5 * VEL * VEL).
RETURN Q * CD.
}.
function GETITERATEBALLX {
// Iterative method.
parameter STARTINGALT.
parameter ENDALT.
parameter Uo.
parameter Vo.
parameter Cd.
SET DT to 1.
SET ITERATIONS TO 1000.
SET CURRENTALT TO STARTINGALT.
SET CURRENTHORV TO Uo.
SET CURRENTVERV TO Vo.
SET DISTANCEX TO 0.
SET SMASS TO SHIP:MASS * 1000.
PRINT "CURRENTALT: " + CURRENTALT.
PRINT "CURRENTHORV: " + CURRENTHORV.
PRINT "CURRENTVERV: " + CURRENTVERV.
PRINT "DISTANCEX: " + DISTANCEX.
PRINT "ITERATION: " + ITERATIONS.
PRINt "SHIP MASS: " + SMASS.
UNTIL (ITERATIONS = 0)
{
SET AERODYNAMICFORCEHOR TO GETAEROFORCE(CURRENTALT, CURRENTHORV, Cd).
SET AERODYNAMICFORCEVER TO GETAEROFORCE(CURRENTALT, CURRENTVERV, Cd).
PRINT "AEROHOR: " + AERODYNAMICFORCEHOR.
PRINT "AEROVER: " + AERODYNAMICFORCEVER.
// TODO: Do RK4 integration instead.
SET NEWHORV TO CURRENTHORV - (AERODYNAMICFORCEHOR / SMASS).
SET NEWVERV TO 0.
IF (CURRENTVERV > 0)
{
SET NEWVERV TO CURRENTVERV - (AERODYNAMICFORCEVER / SMASS).
} ELSE {
// I guess if falling the vertical drag is applied upwards.
SET NEWVERV TO CURRENTVERV + (AERODYNAMICFORCEVER / SMASS).
}
// Apply G.
SET NEWVERV TO NEWVERV - GETGATALT(CURRENTALT).
// Prepare for next iteration.
SET CURRENTHORV TO NEWHORV.
SET CURRENTVERV TO NEWVERV.
SET CURRENTALT TO CURRENTALT + CURRENTVERV.
SET DISTANCEX TO DISTANCEX + CURRENTHORV.
PRINT "CURRENTALT: " + CURRENTALT.
PRINT "CURRENTHORV: " + CURRENTHORV.
PRINT "CURRENTVERV: " + CURRENTVERV.
PRINT "DISTANCEX: " + DISTANCEX.
PRINT "ITERATION: " + ITERATIONS.
IF (CURRENTALT < ENDALT)
{
// End iterations.
SET ITERATIONS TO 0.
} ELSE {
// Continue iterations.
SET ITERATIONS TO ITERATIONS - 1.
}
}.
}.
function PRINTDATA {
SET Cd TO 0.007.
LOCK CALCHORV TO COS((ARCSIN(SHIP:VERTICALSPEED/SHIP:AIRSPEED)))*SHIP:AIRSPEED.
PRINT "CURRENT X:" + GETBALLISTICX(ALTITUDE, SHIP:VERTICALSPEED, CALCHORV, Cd) at (20,0).
}
CLEARSCREEN.
PRINT "*** BALLISTIC BURN TEST ***" AT (0,0).
STAGE.
SET Cd TO 0.0003.
SET Vo TO 250.
SET Uo TO 20.
SET TRAJECTORYANGLE TO (ARCTAN(Vo/Uo)).//* constant:RadToDeg.
PRINT "TRAJECTORYANGLE: " + TRAJECTORYANGLE.
SET Density TO 1.225.
//lock g to constant:g * body:mass / body:radius^2. // Gravity (m/s^2)
lock g to GETGATALT(altitude).
SET Vt TO SQRT((2*SHIP:MASS*g)/(Cd*Density)).
SET FALLTIMEHMS TO (Vo/g)*2.
SET UoHMS TO Uo.
LOCK CALCHORV TO COS((ARCSIN(SHIP:VERTICALSPEED/SHIP:AIRSPEED)))*SHIP:AIRSPEED.
SET ymax TO (Vt^2 / (2 * g)) * ln ((Vo^2 + Vt^2)/Vt^2).
SET NEWG TO body:mu / (ymax + body:radius)^2.
SET Vt TO SQRT((2*SHIP:MASS*NEWG)/(Cd*GETDENSITY((ALTITUDE+ymax)/2))).
SET ymax TO (Vt^2 / (2 * NEWG)) * ln ((Vo^2 + Vt^2)/Vt^2).
SET AVGG TO (g+NEWG)/2.
SET FALLTIMENEW TO (Vo/AVGG)*2.
PRINT "YMAX: " + Ymax AT (0,9).
PRINT "EST FALLTIME: " + FALLTIMENEW AT (0,1).
SET X TO ((Vt*Vt)/AVGG)*(LN(((Vt*Vt)+AVGG*UoHMS*FALLTIMENEW)/(Vt*Vt))).
PRINT "Calculated X: " + X AT (0,3).
LOCK STEERING TO HEADING(90,90).
LOCK THROTTLE TO 1.0.
SET DELTATIME TO 0.1.
SET LASTTIME TO TIME:SECONDS.
SET VVACC TO 1.
SET LASTVV TO 0.
SET SUMCD TO 0.
SET SUMCDI TO 0.
UNTIL (SHIP:VERTICALSPEED > Vo*0.5)
{
PRINTDATA().
// Measure Cd...
WAIT 0.01.
SET DELTATIME TO TIME:SECONDS - LASTTIME.
SET LASTTIME TO TIME:SECONDS.
SET VVACC TO (SHIP:VERTICALSPEED - LASTVV)/DELTATIME.
SET LASTVV TO SHIP:VERTICALSPEED.
IF (SHIP:VERTICALSPEED > 0.1)
{
PRINT "VVACC: " + VVACC AT (0,23).
GEAR OFF.
}.
SET ENGINEPUSH TO ((SHIP:MAXTHRUST*1000)/(SHIP:MASS*1000)) - g.
PRINT "ENGINEPUSH: " + ENGINEPUSH AT (0,24).
SET REMAININGACC TO VVACC - ENGINEPUSH.
//SET REMAININGACC TO REMAININGACC - g.
PRINT "REMAINING ACC: " + REMAININGACC AT (0,18).
SET AEROFORCE TO REMAININGACC*(SHIP:MASS*1000).
PRINT "AEROFORCE: " + AEROFORCE AT (0,19).
SET CALCQ TO (GETDENSITY(ALTITUDE) * 0.5 * SHIP:AIRSPEED * SHIP:AIRSPEED).
PRINT "CALC QV: " + CALCQ AT (0,22).
PRINT "Cd: " + AEROFORCE/CALCQ AT (0,20).
PRINT "Cd: " + AEROFORCE/SHIP:Q AT (0,21).
IF (SHIP:VERTICALSPEED > 10.0)
{
SET SUMCD TO (SUMCD - (AEROFORCE/CALCQ)).
SET SUMCDI TO (SUMCDI + 1).
PRINT "Sumcd: " + SUMCD AT (0,25).
print "Sumcdi: " + SUMCDI AT (0,26).
PRINT "Av Cd: " + (SUMCD/SUMCDI) AT (0,27).
}
}.
//SET Cd TO (SUMCD/SUMCDI).
PRINT "Av Cd: " + (SUMCD/SUMCDI) AT (0,22).
UNTIL CALCHORV > Uo {
PRINTDATA().
LOCK THROTTLE TO 1.0.
IF (APOAPSIS > 500)
{
LOCK STEERING TO HEADING(90,TRAJECTORYANGLE).
}
SET TRAJECTORYANGLE TO (ARCTAN(CALCHORV/SHIP:VERTICALSPEED)).
PRINT "G: " + g AT (0,30).
PRINT "EST DENSITY: " + GETDENSITY(ALTITUDE) AT (0,31).
PRINT "CALC Q: " + (GETDENSITY(ALTITUDE) * 0.5 * SHIP:AIRSPEED * SHIP:AIRSPEED) AT (0,32).
PRINT "GIVEN Qkpa: " + (SHIP:Q*constant:AtmToKpa) AT (0,33).
PRINT "GIVEN Q: " + (SHIP:Q * 101325) AT (0,17).
SET CALCDENS TO ((SHIP:Q*101325)/(0.5*SHIP:AIRSPEED*SHIP:AIRSPEED)).
PRINT "CALC DENS: " + CALCDENS AT (0,34).
SET CALCDENS2 TO ((SHIP:Q)/(0.5*SHIP:AIRSPEED*SHIP:AIRSPEED)).
PRINT "CALC DENS2: " + CALCDENS2 AT (0,15).
SET CALCDENS3 TO (((GETDENSITY(ALTITUDE) * 0.5 * SHIP:AIRSPEED * SHIP:AIRSPEED))/(0.5*SHIP:AIRSPEED*SHIP:AIRSPEED)).
PRINT "CALC DENS3: " + CALCDENS3 AT (0,16).
PRINT "AIRSPEED: " + SHIP:AIRSPEED AT (0,37).
RCS ON.
}
LOCK THROTTLE TO 0.
LOCK STEERING TO PROGRADE.
wait 1.
PRINT "ACTAUL Uo: " + CALCHORV AT (0,13).
PRINT "ACTUAL Vo: " + SHIP:VERTICALSPEED AT (0,14).
SET NEWDENSITY TO Density.
//(0.898+0.642)/2.
SET NEWVt TO SQRT((2*SHIP:MASS*g)/(Cd*GETDENSITY((ALTITUDE+(ALTITUDE+ymax))/2))).
SET FALLTIMENEW TO ((SHIP:VERTICALSPEED)/g)*2.
SET XNEW TO ((NEWVt*NEWVt)/g)*(LN(((NEWVt*NEWVt)+g*CALCHORV*FALLTIMENEW)/(NEWVt*NEWVt))).
SET XNEW TO GETBALLISTICX(ALTITUDE,CALCHORV, SHIP:VERTICALSPEED, Cd).
//PRINT "NEW ESTIMATE: " + XNEW AT (0,4).
PRINT "NEW FALLTIME: " + FALLTIMENEW AT (0,5).
PRINT "NEWER ESTIMATE: " + GETITERATEBALLX(ALTITUDE,ALTITUDE, CALCHORV, SHIP:VERTICALSPEED, Cd) AT (0,4).
SET YMAXNEW TO (NEWVt^2 / (2 * g)) * ln ((SHIP:VERTICALSPEED^2 + NEWVt^2)/NEWVt^2).
PRINT "NEW YMAX: " + (YMAXNEW + ALTITUDE) AT (0,10).
// End here.