Skip to content

Commit 1a4df44

Browse files
committed
[LANDING]: The landing module now works as intended.
1 parent 559ee39 commit 1a4df44

5 files changed

Lines changed: 225 additions & 313 deletions

File tree

src/com/pesterenan/controllers/LandingController.java

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,12 @@
1212

1313
public class LandingController extends FlightController implements Runnable {
1414

15-
private static final int ALTITUDE_POUSO_AUTOMATICO = 8000;
1615
private static double velP = 0.05, velI = 0.001, velD = 0.01;
1716
private static boolean landFromHovering = false;
1817
private ControlePID altitudeCtrl = new ControlePID();
1918
private ControlePID velocityCtrl = new ControlePID();
2019
private Navigation navigation = new Navigation();
2120
private double hoverAltitude = 100;
22-
private double distanciaDaQueima = 0, velocidadeTotal = 0;
2321
private Map<String, String> commands;
2422
private boolean autoLandingEngaged = false;
2523
private boolean hoveringMode = false;
@@ -40,7 +38,7 @@ public void run() {
4038
if (commands.get(Modulos.MODULO.get()).equals(Modulos.MODULO_POUSO_SOBREVOAR.get())) {
4139
this.hoverAltitude = Double.parseDouble(commands.get(Modulos.ALTITUDE_SOBREVOO.get()));
4240
hoveringMode = true;
43-
altitudeCtrl.limitarSaida(- 0.5, 1);
41+
altitudeCtrl.limitarSaida(-0.5, 1);
4442
hoverArea();
4543
}
4644
if (commands.get(Modulos.MODULO.get()).equals(Modulos.MODULO_POUSO.get())) {
@@ -54,10 +52,11 @@ private void hoverArea() {
5452
naveAtual.getAutoPilot().engage();
5553
while (hoveringMode) {
5654
try {
57-
if (velHorizontal.get() > 15) {
58-
navigation.mirarRetrogrado();
59-
} else {
60-
navigation.mirarRadialDeFora();
55+
if (velHorizontal.get() > 10) {
56+
navigation.targetLanding();
57+
}
58+
else {
59+
navigation.targetRadialOut();
6160
}
6261
adjustPIDCtrls();
6362
double altPID = altitudeCtrl.computarPID(altitudeSup.get(), hoverAltitude);
@@ -80,48 +79,44 @@ private void hoverArea() {
8079
}
8180

8281
private void autoLanding() {
82+
StatusJPanel.setStatus(Bundle.getString("status_starting_landing_at") + " " + celestialBody);
8383
try {
8484
throttle(0.0f);
85-
naveAtual.getAutoPilot().engage();
86-
StatusJPanel.setStatus(Bundle.getString("status_starting_landing_at") + celestialBody);
8785
deOrbitShip();
88-
checkAltitudeForLanding();
8986
beginAutoLanding();
9087
} catch (RPCException | StreamException | InterruptedException e) {
9188
disengageAfterException(Bundle.getString("status_couldnt_land"));
9289
}
9390
}
9491

9592
private void deOrbitShip() throws RPCException, InterruptedException, StreamException {
93+
naveAtual.getAutoPilot().engage();
9694
if (naveAtual.getSituation().equals(VesselSituation.ORBITING) || naveAtual.getSituation().equals(VesselSituation.SUB_ORBITAL)) {
9795
StatusJPanel.setStatus(Bundle.getString("status_going_suborbital"));
9896
Thread.sleep(1000);
9997
while (naveAtual.getAutoPilot().getHeadingError() > 5) {
100-
StatusJPanel.setStatus(Bundle.getString("status_orienting_ship"));
101-
navigation.mirarRetrogrado();
98+
navigation.targetLanding();
10299
Thread.sleep(250);
100+
StatusJPanel.setStatus(Bundle.getString("status_orienting_ship"));
103101
}
104102
while (periastro.get() > 0) {
105-
StatusJPanel.setStatus(Bundle.getString("status_lowering_periapsis"));
106-
throttle(altitudeCtrl.computarPID(- periastro.get(), 0));
103+
throttle(altitudeCtrl.computarPID(-periastro.get(), 0));
107104
Thread.sleep(100);
105+
StatusJPanel.setStatus(Bundle.getString("status_lowering_periapsis"));
108106
}
109107
throttle(0);
110108
}
111109
}
112110

113-
private void checkAltitudeForLanding() throws RPCException, StreamException, InterruptedException {
114-
while (! autoLandingEngaged) {
111+
private void beginAutoLanding() throws InterruptedException, RPCException, StreamException {
112+
while (!autoLandingEngaged) {
115113
naveAtual.getControl().setBrakes(true);
116-
navigation.mirarRetrogrado();
117-
if (calculateCurrentVelocityMagnitude() < calculateZeroVelocityMagnitude() && velVertical.get() < - 1) {
114+
navigation.targetLanding();
115+
if (calculateCurrentVelocityMagnitude() < calculateZeroVelocityMagnitude() && velVertical.get() < -1) {
118116
autoLandingEngaged = true;
119117
}
120118
Thread.sleep(50);
121119
}
122-
}
123-
124-
private void beginAutoLanding() throws InterruptedException, RPCException, StreamException {
125120
StatusJPanel.setStatus(Bundle.getString("status_starting_landing"));
126121
while (autoLandingEngaged) {
127122
checkAltitude();
@@ -143,9 +138,10 @@ private void checkAltitude() throws RPCException, StreamException {
143138

144139
// Mirar no retrogrado ou radial dependendo da velocidade
145140
if (velHorizontal.get() > 0.5) {
146-
navigation.mirarRetrogrado();
147-
} else {
148-
navigation.mirarRadialDeFora();
141+
navigation.targetLanding();
142+
}
143+
else {
144+
navigation.targetRadialOut();
149145
}
150146

151147
double landingDistanceThreshold = 0;
@@ -155,26 +151,22 @@ private void checkAltitude() throws RPCException, StreamException {
155151
}
156152

157153
double acelPIDValue = altitudeCtrl.computarPID(currentVelocityMagnitude, zeroVelocityMagnitude);
158-
double velPIDValue = velocityCtrl.computarPID(velVertical.get(), - Utilities.clamp(altitudeSup.get() * 0.1, 2, 20));
154+
double velPIDValue = velocityCtrl.computarPID(velVertical.get(), -Utilities.clamp(altitudeSup.get() * 0.1, 2, 20));
159155
double threshold = Utilities.clamp(((currentVelocityMagnitude + zeroVelocityMagnitude) - landingDistanceThreshold) / landingDistanceThreshold, 0, 1);
160156
throttle(Utilities.linearInterpolation(velPIDValue, acelPIDValue, threshold));
161157
}
162158

163159
private void checkForLanding() throws RPCException {
164-
switch (naveAtual.getSituation()) {
165-
case LANDED:
166-
case SPLASHED:
167-
StatusJPanel.setStatus(Bundle.getString("status_landed"));
168-
autoLandingEngaged = false;
169-
hoveringMode = false;
170-
landFromHovering = false;
171-
throttle(0.0f);
172-
naveAtual.getControl().setSAS(true);
173-
naveAtual.getControl().setRCS(true);
174-
naveAtual.getControl().setBrakes(false);
175-
naveAtual.getAutoPilot().disengage();
176-
default:
177-
break;
160+
if (naveAtual.getSituation().equals(VesselSituation.LANDED) || naveAtual.getSituation().equals(VesselSituation.SPLASHED)) {
161+
StatusJPanel.setStatus(Bundle.getString("status_landed"));
162+
autoLandingEngaged = false;
163+
hoveringMode = false;
164+
landFromHovering = false;
165+
throttle(0.0f);
166+
naveAtual.getControl().setSAS(true);
167+
naveAtual.getControl().setRCS(true);
168+
naveAtual.getControl().setBrakes(false);
169+
naveAtual.getAutoPilot().disengage();
178170
}
179171
}
180172

src/com/pesterenan/controllers/ManeuverController.java

Lines changed: 27 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,14 @@ public void run() {
4747

4848
public void calculateManeuver() {
4949
try {
50-
if (naveAtual.getSituation() == VesselSituation.LANDED
51-
|| naveAtual.getSituation() == VesselSituation.SPLASHED) {
50+
if (naveAtual.getSituation() == VesselSituation.LANDED || naveAtual.getSituation() == VesselSituation.SPLASHED) {
5251
throw new InterruptedException();
5352
}
5453
if (this.function.equals(Modulos.AJUSTAR.get())) {
5554
this.alignPlanes();
5655
return;
5756
}
58-
if (this.function.equals(Modulos.EXECUTAR.get()))
59-
return;
57+
if (this.function.equals(Modulos.EXECUTAR.get())) return;
6058
double parametroGravitacional = naveAtual.getOrbit().getBody().getGravitationalParameter();
6159
double altitudeInicial = 0, tempoAteAltitude = 0;
6260
if (this.function.equals(Modulos.APOASTRO.get())) {
@@ -69,12 +67,10 @@ public void calculateManeuver() {
6967
}
7068

7169
double semiEixoMaior = naveAtual.getOrbit().getSemiMajorAxis();
72-
double velOrbitalAtual = Math
73-
.sqrt(parametroGravitacional * ((2.0 / altitudeInicial) - (1.0 / semiEixoMaior)));
74-
double velOrbitalAlvo = Math
75-
.sqrt(parametroGravitacional * ((2.0 / altitudeInicial) - (1.0 / altitudeInicial)));
70+
double velOrbitalAtual = Math.sqrt(parametroGravitacional * ((2.0 / altitudeInicial) - (1.0 / semiEixoMaior)));
71+
double velOrbitalAlvo = Math.sqrt(parametroGravitacional * ((2.0 / altitudeInicial) - (1.0 / altitudeInicial)));
7672
double deltaVdaManobra = velOrbitalAlvo - velOrbitalAtual;
77-
double[] deltaV = {deltaVdaManobra, 0, 0};
73+
double[] deltaV = { deltaVdaManobra, 0, 0 };
7874
createManeuver(tempoAteAltitude, deltaV);
7975
} catch (RPCException | InterruptedException e) {
8076
disengageAfterException(Bundle.getString("status_maneuver_not_possible"));
@@ -85,8 +81,7 @@ public void matchOrbitApoapsis() {
8581
try {
8682
Orbit targetOrbit = getTargetOrbit();
8783
System.out.println(targetOrbit.getApoapsis() + "-- APO");
88-
Node maneuver = hohmannTransferToOrbit(targetOrbit,
89-
naveAtual.getOrbit().getTimeToPeriapsis());
84+
Node maneuver = hohmannTransferToOrbit(targetOrbit, naveAtual.getOrbit().getTimeToPeriapsis());
9085
while (true) {
9186
double currentDeltaApo = compareOrbitParameter(maneuver.getOrbit(), targetOrbit, Compare.AP);
9287
String deltaApoFormatted = String.format("%.2f", currentDeltaApo);
@@ -106,15 +101,14 @@ public void matchOrbitApoapsis() {
106101
}
107102

108103
private Node hohmannTransferToOrbit(Orbit targetOrbit, double timeToStart) {
109-
double[] totalDv = {0, 0, 0};
104+
double[] totalDv = { 0, 0, 0 };
110105
try {
111106
double startingRadius = naveAtual.getOrbit().getPeriapsis();
112107
double finalRadius = targetOrbit.getApoapsis();
113108
System.out.println(startingRadius + " --- " + finalRadius);
114109
double gravitationalParameter = naveAtual.getOrbit().getBody().getGravitationalParameter();
115110
// DeltaV used to get to the second Node
116-
double firstNodeDv = Math.sqrt(gravitationalParameter
117-
* ((2.0 / startingRadius) - (1.0 / naveAtual.getOrbit().getSemiMajorAxis())));
111+
double firstNodeDv = Math.sqrt(gravitationalParameter * ((2.0 / startingRadius) - (1.0 / naveAtual.getOrbit().getSemiMajorAxis())));
118112
// DeltaV used to orbit in the second Node
119113
double secondNodeDv = Math.sqrt(gravitationalParameter * ((2.0 / startingRadius) - (1.0 / finalRadius)));
120114
// Time taken between the two points
@@ -127,7 +121,7 @@ private Node hohmannTransferToOrbit(Orbit targetOrbit, double timeToStart) {
127121

128122
private Node createManeuverAtClosestIncNode(Orbit targetOrbit) {
129123
double uTatClosestNode = 1;
130-
double[] dv = {0, 0, 0};
124+
double[] dv = { 0, 0, 0 };
131125
try {
132126
double[] incNodesUt = getTimeToIncNodes(targetOrbit);
133127
uTatClosestNode = Math.min(incNodesUt[0], incNodesUt[1]) - centroEspacial.getUT();
@@ -137,7 +131,7 @@ private Node createManeuverAtClosestIncNode(Orbit targetOrbit) {
137131
}
138132

139133
private double[] getTimeToIncNodes(Orbit targetOrbit) {
140-
double[] incNodesUt = {0, 0};
134+
double[] incNodesUt = { 0, 0 };
141135
try {
142136
Orbit vesselOrbit = naveAtual.getOrbit();
143137
double ascendingNode = vesselOrbit.trueAnomalyAtAN(targetOrbit);
@@ -163,11 +157,11 @@ public void alignPlanes() {
163157
break;
164158
}
165159
double dvNormal = maneuver.getNormal();
166-
double ctrlOutput = ctrlManeuver.computarPID(currentDeltaInc, 0.0)
167-
* limitPIDOutput(Math.abs(currentDeltaInc));
168-
if ((closestIsAN ? currentDeltaInc : - currentDeltaInc) > 0.0) {
160+
double ctrlOutput = ctrlManeuver.computarPID(currentDeltaInc, 0.0) * limitPIDOutput(Math.abs(currentDeltaInc));
161+
if ((closestIsAN ? currentDeltaInc : -currentDeltaInc) > 0.0) {
169162
maneuver.setNormal(dvNormal + (ctrlOutput));
170-
} else {
163+
}
164+
else {
171165
maneuver.setNormal(dvNormal - (ctrlOutput));
172166
}
173167
Thread.sleep(25);
@@ -178,10 +172,8 @@ public void alignPlanes() {
178172
}
179173

180174
private double limitPIDOutput(double absDeltaInc) {
181-
if (absDeltaInc < 0.05)
182-
return 0.1;
183-
if (absDeltaInc < 0.5)
184-
return 5;
175+
if (absDeltaInc < 0.05) return 0.1;
176+
if (absDeltaInc < 0.5) return 5;
185177
return 10;
186178
}
187179

@@ -229,8 +221,7 @@ private Orbit getTargetOrbit() throws RPCException {
229221
private Node createManeuver(double laterTime, double[] deltaV) {
230222
Node maneuverNode = null;
231223
try {
232-
naveAtual.getControl().addNode(centroEspacial.getUT() + laterTime, (float) deltaV[0], (float) deltaV[1],
233-
(float) deltaV[2]);
224+
naveAtual.getControl().addNode(centroEspacial.getUT() + laterTime, (float) deltaV[0], (float) deltaV[1], (float) deltaV[2]);
234225
List<Node> currentNodes = naveAtual.getControl().getNodes();
235226
maneuverNode = currentNodes.get(currentNodes.size() - 1);
236227
} catch (UnsupportedOperationException | RPCException e) {
@@ -260,7 +251,7 @@ public void orientToManeuverNode(Node maneuverNode) {
260251
float roll = ap.getTargetRoll();
261252
roll = Float.isNaN(roll) ? 0 : roll;
262253
ap.setTargetRoll(roll);
263-
nav.mirarNaManobra(maneuverNode);
254+
nav.targetManeuver(maneuverNode);
264255
ap.engage();
265256
while (ap.getError() > 1) {
266257
StatusJPanel.setStatus(Bundle.getString("status_orienting_ship"));
@@ -275,7 +266,7 @@ public double calculateBurnTime(Node noDeManobra) throws RPCException {
275266

276267
List<Engine> motores = naveAtual.getParts().getEngines();
277268
for (Engine motor : motores) {
278-
if (motor.getPart().getStage() == naveAtual.getControl().getCurrentStage() && ! motor.getActive()) {
269+
if (motor.getPart().getStage() == naveAtual.getControl().getCurrentStage() && !motor.getActive()) {
279270
motor.setActive(true);
280271
}
281272
}
@@ -302,26 +293,22 @@ public void executeBurn(Node noDeManobra, double duracaoDaQueima) {
302293
while (inicioDaQueima > 0) {
303294
inicioDaQueima = noDeManobra.getTimeTo() - (duracaoDaQueima / 2.0);
304295
inicioDaQueima = Math.max(inicioDaQueima, 0.0);
305-
nav.mirarNaManobra(noDeManobra);
296+
nav.targetManeuver(noDeManobra);
306297
StatusJPanel.setStatus(String.format(Bundle.getString("status_maneuver_ignition_in"), inicioDaQueima));
307298
Thread.sleep(100);
308299
}
309300
// Executar a manobra:
310-
Stream<Triplet<Double, Double, Double>> queimaRestante = getConexao().addStream(noDeManobra,
311-
"remainingBurnVector", noDeManobra.getReferenceFrame());
301+
Stream<Triplet<Double, Double, Double>> queimaRestante = getConexao().addStream(noDeManobra, "remainingBurnVector", noDeManobra.getReferenceFrame());
312302
StatusJPanel.setStatus(Bundle.getString("status_maneuver_executing"));
313-
double limiteParaDesacelerar = noDeManobra.getDeltaV() > 1000 ? 0.025
314-
: noDeManobra.getDeltaV() > 250 ? 0.10 : 0.25;
303+
double limiteParaDesacelerar = noDeManobra.getDeltaV() > 1000 ? 0.025 : noDeManobra.getDeltaV() > 250 ? 0.10 : 0.25;
315304

316305
while (noDeManobra != null) {
317306
if (queimaRestante.get().getValue1() < (fineAdjustment ? 3 : 0.5)) {
318307
break;
319308
}
320-
nav.mirarNaManobra(noDeManobra);
321-
throttle(Utilities.remap(noDeManobra.getDeltaV() * limiteParaDesacelerar, 0, 1, 0.1,
322-
queimaRestante.get().getValue1()));
323-
MainGui.getParametros().getComponent(0).firePropertyChange("distancia", 0,
324-
queimaRestante.get().getValue1());
309+
nav.targetManeuver(noDeManobra);
310+
throttle(Utilities.remap(noDeManobra.getDeltaV() * limiteParaDesacelerar, 0, 1, 0.1, queimaRestante.get().getValue1()));
311+
MainGui.getParametros().getComponent(0).firePropertyChange("distancia", 0, queimaRestante.get().getValue1());
325312
Thread.sleep(25);
326313
}
327314
throttle(0.0f);
@@ -342,11 +329,10 @@ public void executeBurn(Node noDeManobra, double duracaoDaQueima) {
342329
}
343330
}
344331

345-
private void adjustManeuverWithRCS(Stream<Triplet<Double, Double, Double>> queimaRestante)
346-
throws RPCException, StreamException, InterruptedException {
332+
private void adjustManeuverWithRCS(Stream<Triplet<Double, Double, Double>> queimaRestante) throws RPCException, StreamException, InterruptedException {
347333
naveAtual.getControl().setRCS(true);
348334
while (queimaRestante.get().getValue1() >= 0.1) {
349-
naveAtual.getControl().setForward((float) ctrlRCS.computarPID(- queimaRestante.get().getValue1() * 10, 0));
335+
naveAtual.getControl().setForward((float) ctrlRCS.computarPID(-queimaRestante.get().getValue1() * 10, 0));
350336
Thread.sleep(25);
351337
}
352338
naveAtual.getControl().setForward(0);

0 commit comments

Comments
 (0)