-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShuttleAscent2.java
502 lines (388 loc) · 11.7 KB
/
ShuttleAscent2.java
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
package shuttleGuidance;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.apache.commons.math3.util.FastMath;
import org.javatuples.Triplet;
import krpc.client.Connection;
import krpc.client.RPCException;
import krpc.client.Stream;
import krpc.client.StreamException;
import krpc.client.services.KRPC;
import krpc.client.services.MechJeb;
import krpc.client.services.MechJeb.SmartASS;
import krpc.client.services.MechJeb.SmartASSAutopilotMode;
import krpc.client.services.MechJeb.SmartASSInterfaceMode;
import krpc.client.services.SpaceCenter;
import krpc.client.services.SpaceCenter.CelestialBody;
import krpc.client.services.SpaceCenter.Control;
import krpc.client.services.SpaceCenter.Engine;
import krpc.client.services.SpaceCenter.ReferenceFrame;
import krpc.client.services.SpaceCenter.Vessel;
import shuttleGuidance.FlightUI.FlightMode;
public class ShuttleAscent2 {
private static double MINIMUMPITCH = 26d;
private static boolean debug = false;
private Control vesselControl;
private MechJeb mj;
private SmartASS smartASS;
private static float maxRollRate = 10f;
private static float maxPitchRate = 0.65f;
private SpaceCenter spaceCenter;
private Vessel vessel;
private double roll = 0, pitch = 90, heading = 90;
private double targApoM;
private double inclination;
private CelestialBody body;
private double SRBmaxThrust;
private ReferenceFrame refFrame;
private boolean keepRunning = true;
private int SRBINDEX = Integer.MAX_VALUE;
private Connection connection;
private boolean maxQ = true;
protected FlightUI flightUI;
public ShuttleAscent2() throws InterruptedException, IOException, RPCException, StreamException {
connection = Connection.newInstance("Launch");
KRPC.newInstance(connection);
spaceCenter = SpaceCenter.newInstance(connection);
vessel = spaceCenter.getActiveVessel();
mj = MechJeb.newInstance(connection);
smartASS = mj.getSmartASS();
flightUI = new FlightUI();
TimeUnit.SECONDS.sleep(2);
body = vessel.getOrbit().getBody();
vesselControl = vessel.getControl();
smartASS.setForcePitch(true);
smartASS.setForceYaw(true);
smartASS.setForceRoll(true);
smartASS.setInterfaceMode(SmartASSInterfaceMode.SURFACE);
smartASS.setAutopilotMode(SmartASSAutopilotMode.SURFACE);
smartASS.setSurfaceHeading(90);
smartASS.setSurfacePitch(90);
smartASS.setSurfaceRoll(0);
smartASS.update(false);
refFrame = ReferenceFrame.createHybrid(connection, vessel.getOrbit().getBody().getReferenceFrame(),
vessel.getSurfaceReferenceFrame(), vessel.getOrbit().getBody().getReferenceFrame(),
vessel.getOrbit().getBody().getReferenceFrame());
flightUI.start();
initializeTWR();
findLaunchCorridor(300, 240, 30, false);
countdown();
launch();
// vesselControl.setThrottle(0);
// connection.close();
}
public static void main(String[] args) throws IOException, RPCException, InterruptedException, StreamException
{
new ShuttleAscent2();
}
private void initializeTWR() throws RPCException
{
for (Engine iterable_element : vessel.getParts().getEngines())
{
float thrust = iterable_element.getMaxThrust();
if (thrust > 1.6e6)
{
if (thrust > SRBmaxThrust)
{
SRBmaxThrust = thrust;
}
}
}
}
private double provideCurrentSRBThrust() throws RPCException
{
List<Engine> engines = vessel.getParts().getEngines();
ArrayList<Float> thrusts = new ArrayList<Float>();
for (int i = 0; i < engines.size(); i++)
{
thrusts.add(engines.get(i).getThrust());
}
if (SRBINDEX == Integer.MAX_VALUE)
{
Float srbThrust = Collections.max(thrusts);
SRBINDEX = thrusts.indexOf(srbThrust);
return srbThrust;
} else
{
return thrusts.get(SRBINDEX);
}
}
private void stage() throws RPCException
{
vesselControl.activateNextStage();
}
private boolean findLaunchCorridor(final double targOrbitApoKM, final double targOrbitPerKM, final double orbitInclination, final boolean timedLaunch) throws RPCException
{
targApoM = targOrbitApoKM * 1000;
inclination = orbitInclination;
double tempAngle = 0;
if (timedLaunch)
{
Vessel targetVessel = spaceCenter.getTargetVessel();
CelestialBody targetBody = spaceCenter.getTargetBody();
if (targetVessel != null)
{
tempAngle = FastMath.cos(targetVessel.getOrbit().getInclination()
/ FastMath.cos(vessel.flight(vessel.getOrbitalReferenceFrame()).getLatitude()));
if (debug)
{
}
} else if (targetBody != null)
{
}
if (tempAngle > 1)
{
System.out.println("Direct Launch Impossible.");
System.out.println("Will Launch at closest point.");
} else
{
}
}
else
{
if (FastMath.abs(inclination) < vessel.flight(vessel.getOrbitalReferenceFrame()).getLatitude())
{
System.out.println("Error Inclination is below current");
return false;
}
tempAngle = FastMath.cos(
orbitInclination / FastMath.cos(vessel.flight(vessel.getOrbitalReferenceFrame()).getLatitude()));
if (tempAngle > 1)
{
System.out.println("Direct Launch Impossible");
} else
{
heading = FastMath.asin(tempAngle);
double veq = (2 * FastMath.PI * body.getEquatorialRadius()) / body.getRotationalPeriod();
// set ospeed to sqrt(constant:G*body:mass/(body:radius+tgtapo)).
double ospeed = FastMath.sqrt(6.6743e-11 * body.getMass() / (body.getEquatorialRadius() + targApoM));
double vx = ospeed * FastMath.sin(heading)
- veq * FastMath.cos(vessel.flight(vessel.getOrbitalReferenceFrame()).getLatitude());
double vy = ospeed * FastMath.cos(heading);
heading = FastMath.atan(vx / vy);
heading = FastMath.toDegrees(heading);
vesselControl.setThrottle(0.90f);
}
}
return true;
}
private void countdown() throws InterruptedException, RPCException
{
vesselControl.setLights(true);
vesselControl.setAbort(false);
vesselControl.setThrottle(0.90f);
smartASS.update(true);
for (int i = 10; i > 0; i--)
{
flightUI.updateGUI();
System.out.println("T-minus " + i);
vesselControl.setThrottle(0.90f);
if (i == 7)
{
System.out.println("Main Engine Start");
stage();
}
TimeUnit.SECONDS.sleep(1);
}
System.out.println("T " + 0);
stage();
}
private void launch() throws RPCException, InterruptedException, IOException, StreamException
{
vesselControl.setAbort(false);
vesselControl.setThrottle(1f);
roll = 0;
pitch = 90;
Stream<Double> metStream = connection.addStream(vessel, "getMET");
boolean headsDown = false;
double oldMET = 0;
double MET = metStream.get();
boolean SRB = true;
vesselState vs = vesselState.verticalAscent;
while (keepRunning)
{
MET = metStream.get();
if (vesselControl.getAbort())
{
break;
}
if (MET - oldMET >= 1)
{
flightUI.updateGUI();
// findFlightDirectionalAngles();
// shipStatus();
smartASS.setSurfaceRoll(roll);
switch (vs) {
case verticalAscent: {
if (MET > 6)
{
smartASS.setSurfaceHeading(heading);
vs = vesselState.rollOver;
}
break;
}
case rollOver: {
if (roll < 180)
{
// smartASS.setSurfaceRoll(roll);
roll += maxRollRate;
} else
{
smartASS.setSurfaceRoll(180);
smartASS.setSurfaceHeading(heading);
roll = 180;
}
if (pitch > 78)
{
pitch -= maxPitchRate;
} else
{
smartASS.setSurfacePitch(78);
pitch = 78;
}
if (roll >= 180 && pitch <= 78)
{
smartASS.setSurfaceHeading(heading);
vs = vesselState.pitchTo45;
headsDown = true;
}
break;
}
case pitchTo45: {
if (headsDown)
{
if (maxQ)
{
maxQThrustMod();
} else
{
thrustChange(1.0f);
}
if (pitch > MINIMUMPITCH)
{
pitch -= maxPitchRate;
} else
{
pitch = MINIMUMPITCH;
}
}
break;
}
default:
break;
}
if (MET > 7)
{
if (SRB)
{
if (provideCurrentSRBThrust() / SRBmaxThrust <= 0.03f)
{
stage();
SRB = false;
}
} else
{
// //TODO prepare for handover
// String[] str = { Double.toString(targApoM) };
// connection.close();
// PEG.main(str);
//flightUI.setFlightMode(FlightMode.upfg);
new PEG(targApoM, flightUI);
keepRunning = false;
break;
}
}
smartASS.setSurfacePitch(pitch);
smartASS.setSurfaceRoll(roll);
oldMET = MET;
smartASS.update(true);
}
TimeUnit.MILLISECONDS.sleep(125);
}
connection.close();
}
private enum vesselState {
verticalAscent, rollOver, pitchTo45, pitchTo30, terminalApoRaise
}
private void maxQThrustMod() throws RPCException, StreamException
{
double oldMET = 0;
Stream<Float> q = connection.addStream(vessel.flight(refFrame), "getDynamicPressure");
if(q.get() > 2.9e4 && maxQ)
{
flightUI.setFlightMode(FlightMode.maxQ);
maxQ = false;
thrustChange(0.1f);
maxPitchRate = 1f;
}
while (q.get() > 2.9e4)
{
Stream<Double> metStream = connection.addStream(vessel, "getMET");
double MET = metStream.get();
if (MET - oldMET >= 0.125)
{
try
{
flightUI.updateGUI();
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
smartASS.setSurfacePitch(pitch);
//thrustChange(0.1f);
smartASS.update(false);
pitch -= 0.1;
oldMET = MET;
}
}
flightUI.setFlightMode(FlightMode.preprogramed);
}
private void thrustChange(float throttle) throws RPCException
{
vesselControl.setThrottle(throttle);
}
private Triplet<Double, Double, Double> vectorScalarMultiplication(double scalar, Triplet<Double, Double, Double> vector)
{
Triplet<Double, Double, Double> toReturn = vector;
toReturn.setAt0(vector.getValue0() * scalar).setAt1(vector.getValue1() * scalar)
.setAt2(vector.getValue2() * scalar);
return toReturn;
}
@SuppressWarnings({ "unused", "null" })
private Triplet<Double, Double, Double> vectorSubtraction(Triplet<Double, Double, Double> v1, Triplet<Double, Double, Double> v2)
{
Triplet<Double, Double, Double> toReturn = null;
double x1 = v1.getValue0();
double y1 = v1.getValue1();
double z1 = v1.getValue2();
double x2 = v2.getValue0();
double y2 = v2.getValue1();
double z2 = v2.getValue2();
toReturn.setAt0(x1 - x2).setAt1(y1 - y2).setAt2(z1 - z2);
return toReturn;
}
@SuppressWarnings("unused")
private Triplet<Double, Double, Double> makeIntoUnit(Triplet<Double, Double, Double> nonNormalized)
{
Triplet<Double, Double, Double> normalized = null;
double vectorMagScaled = FastMath.sqrt(magnitude(nonNormalized));
normalized = vectorScalarMultiplication(1 / vectorMagScaled, nonNormalized);
return normalized;
}
private double magnitude(Triplet<Double, Double, Double> vector)
{
double magnitude = FastMath.sqrt(dotProduct(vector, vector));
return magnitude;
}
private double dotProduct(Triplet<Double, Double, Double> v1, Triplet<Double, Double, Double> v2)
{
double xdot = v1.getValue0() * v2.getValue0();
double ydot = v1.getValue1() * v2.getValue1();
double zdot = v1.getValue2() * v2.getValue2();
return xdot + ydot + zdot;
}
}