Skip to content

Commit

Permalink
feat: firework-efficient autopilot pitch controller
Browse files Browse the repository at this point in the history
  • Loading branch information
Octol1ttle committed Apr 18, 2024
1 parent ec860e0 commit 3b7c9ed
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import ru.octol1ttle.flightassistant.computers.impl.autoflight.FireworkController;
import ru.octol1ttle.flightassistant.computers.impl.autoflight.PitchController;
import ru.octol1ttle.flightassistant.computers.impl.autoflight.YawController;
import ru.octol1ttle.flightassistant.computers.impl.autoflight.pitch.AutopilotPitchController;
import ru.octol1ttle.flightassistant.computers.impl.autoflight.pitch.AutopilotPitchComputer;
import ru.octol1ttle.flightassistant.computers.impl.autoflight.pitch.ProtectionsPitchController;
import ru.octol1ttle.flightassistant.computers.impl.navigation.FlightPlanner;
import ru.octol1ttle.flightassistant.computers.impl.safety.AlertController;
Expand Down Expand Up @@ -46,7 +46,7 @@ public ComputerHost(@NotNull MinecraftClient mc) {
ComputerRegistry.register(new YawController());
ComputerRegistry.register(new AutoFlightComputer());
ComputerRegistry.register(new FlightPhaseComputer());
ComputerRegistry.register(new AutopilotPitchController());
ComputerRegistry.register(new AutopilotPitchComputer());
ComputerRegistry.register(new AlertController(mc.getSoundManager()));

CustomComputerRegistrationCallback.EVENT.invoker().registerCustomComputers();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ public void tick() {
float heightAboveDestination = data.altitude() - plan.landAltitude;
minimumHeight = MathHelper.clamp(heightAboveDestination, 0.0f, minimumHeight);

if (heightAboveDestination - minimumHeight >= 5.0f || (wasAutolandAllowed && !plan.autolandAllowed)) {
if (heightAboveDestination - minimumHeight >= 5.0f
|| wasAutolandAllowed && !plan.autolandAllowed && plan.getDistanceToWaypoint() > 10.0f && heightAboveDestination > 2.0f) {
phase = FlightPhase.GO_AROUND;
}
}
Expand All @@ -87,7 +88,7 @@ private boolean isAboutToLand() {
return phase == FlightPhase.APPROACH || phase == FlightPhase.LAND;
}

private boolean isNearDestination() {
public boolean isNearDestination() {
return isAboutToLand() || phase == FlightPhase.GO_AROUND;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package ru.octol1ttle.flightassistant.computers.impl.autoflight;

import net.minecraft.util.math.MathHelper;
import org.jetbrains.annotations.Nullable;
import org.joml.Vector2d;
import ru.octol1ttle.flightassistant.FAMathHelper;
import ru.octol1ttle.flightassistant.computers.impl.AirDataComputer;
import ru.octol1ttle.flightassistant.computers.api.ITickableComputer;
import ru.octol1ttle.flightassistant.computers.impl.AirDataComputer;
import ru.octol1ttle.flightassistant.computers.impl.navigation.FlightPlanner;
import ru.octol1ttle.flightassistant.computers.impl.safety.GroundProximityComputer;
import ru.octol1ttle.flightassistant.config.FAConfig;
Expand Down Expand Up @@ -58,26 +55,6 @@ public void tick() {
return selectedAltitude != null ? selectedAltitude : plan.getManagedAltitude();
}

public @Nullable Float getTargetPitch() {
if (getTargetAltitude() == null) {
return null;
}

double altitudeDelta = getTargetAltitude() - data.altitude();

double distance;
Vector2d planPos = plan.getTargetPosition();

boolean isTargetingApproachAltitude = plan.isOnApproach() && !plan.autolandAllowed;
if (planPos != null && selectedAltitude == null && !isTargetingApproachAltitude) {
distance = Vector2d.distance(planPos.x, planPos.y, data.position().x, data.position().z);
} else {
distance = Math.max(15.0f, data.speed() * 2.0f);
}

return FAMathHelper.toDegrees(MathHelper.atan2(altitudeDelta, distance));
}

public @Nullable Float getTargetHeading() {
return selectedHeading != null ? Float.valueOf(selectedHeading) : plan.getManagedHeading();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package ru.octol1ttle.flightassistant.computers.impl.autoflight.pitch;

import net.minecraft.util.Pair;
import net.minecraft.util.math.MathHelper;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.joml.Vector2d;
import ru.octol1ttle.flightassistant.FAMathHelper;
import ru.octol1ttle.flightassistant.computers.api.IPitchController;
import ru.octol1ttle.flightassistant.computers.api.ITickableComputer;
import ru.octol1ttle.flightassistant.computers.impl.AirDataComputer;
import ru.octol1ttle.flightassistant.computers.impl.FlightPhaseComputer;
import ru.octol1ttle.flightassistant.computers.impl.autoflight.AutoFlightComputer;
import ru.octol1ttle.flightassistant.computers.impl.autoflight.PitchController;
import ru.octol1ttle.flightassistant.computers.impl.navigation.FlightPlanner;
import ru.octol1ttle.flightassistant.registries.ComputerRegistry;

public class AutopilotPitchComputer implements ITickableComputer, IPitchController {
private final AirDataComputer data = ComputerRegistry.resolve(AirDataComputer.class);
private final AutoFlightComputer autoflight = ComputerRegistry.resolve(AutoFlightComputer.class);
private final FlightPhaseComputer phase = ComputerRegistry.resolve(FlightPhaseComputer.class);
private final FlightPlanner plan = ComputerRegistry.resolve(FlightPlanner.class);
public Float targetPitch;

@Override
public void tick() {
targetPitch = computeTargetPitch();
}

private Float computeTargetPitch() {
if (phase.phase == FlightPhaseComputer.FlightPhase.TAKEOFF
|| phase.phase == FlightPhaseComputer.FlightPhase.GO_AROUND && data.heightAboveGround() < 15.0f) {
return PitchController.CLIMB_PITCH;
}

Integer targetAltitude = autoflight.getTargetAltitude();
if (targetAltitude == null) {
return null;
}
Vector2d planPos = plan.getTargetPosition();

float diff = targetAltitude - data.altitude();
boolean landing = phase.phase == FlightPhaseComputer.FlightPhase.LAND;
if (!landing && diff > -10.0f && diff < 5.0f) {
return (PitchController.GLIDE_PITCH + PitchController.ALTITUDE_PRESERVE_PITCH) * 0.5f;
}

if (data.altitude() < targetAltitude) {
return computeClimbPitch(diff, planPos);
}

if (planPos == null || autoflight.selectedAltitude != null) {
if (diff > -15.0f) {
return PitchController.GLIDE_PITCH;
}

return (PitchController.GLIDE_PITCH + PitchController.DESCEND_PITCH) * 0.5f;
}

float degrees = FAMathHelper.toDegrees(
MathHelper.atan2(
diff,
Vector2d.distance(data.position().x, data.position().z, planPos.x, planPos.y)
)
);
if (!landing && diff > -15.0f) {
return Math.max(PitchController.GLIDE_PITCH, degrees);
}

return degrees;
}

private float computeClimbPitch(float diff, Vector2d target) {
if (target == null || autoflight.selectedAltitude != null) {
if (diff <= 15.0f) {
return PitchController.ALTITUDE_PRESERVE_PITCH;
}

return (PitchController.ALTITUDE_PRESERVE_PITCH + PitchController.CLIMB_PITCH) * 0.5f;
}

float degrees = Math.max(5.0f, FAMathHelper.toDegrees(
MathHelper.atan2(
diff,
Vector2d.distance(data.position().x, data.position().z, target.x, target.y)
)
));
if (diff <= 15.0f) {
return Math.min(PitchController.ALTITUDE_PRESERVE_PITCH, degrees);
}

return degrees;
}

@Override
public @Nullable Pair<@NotNull Float, @NotNull Float> getTargetPitch() {
if (!autoflight.autoPilotEnabled || targetPitch == null) {
return null;
}

return new Pair<>(targetPitch, 1.0f);
}

@Override
public Priority getPriority() {
return Priority.NORMAL;
}

@Override
public String getId() {
return "autopilot_pitch_ctl";
}

@Override
public void reset() {
autoflight.disconnectAutopilot(true);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import ru.octol1ttle.flightassistant.DrawHelper;
import ru.octol1ttle.flightassistant.computers.impl.AirDataComputer;
import ru.octol1ttle.flightassistant.computers.impl.autoflight.AutoFlightComputer;
import ru.octol1ttle.flightassistant.computers.impl.autoflight.pitch.AutopilotPitchComputer;
import ru.octol1ttle.flightassistant.config.FAConfig;
import ru.octol1ttle.flightassistant.hud.api.IHudDisplay;
import ru.octol1ttle.flightassistant.registries.ComputerRegistry;
Expand All @@ -16,6 +17,7 @@ public class FlightDirectorsDisplay implements IHudDisplay {
private final Dimensions dim;
private final AirDataComputer data = ComputerRegistry.resolve(AirDataComputer.class);
private final AutoFlightComputer autoflight = ComputerRegistry.resolve(AutoFlightComputer.class);
private final AutopilotPitchComputer autoPitch = ComputerRegistry.resolve(AutopilotPitchComputer.class);

public FlightDirectorsDisplay(Dimensions dim) {
this.dim = dim;
Expand All @@ -27,8 +29,8 @@ public void render(DrawContext context, TextRenderer textRenderer) {
return;
}

if (autoflight.getTargetPitch() != null) {
float deltaPitch = autoflight.getTargetPitch() - data.pitch();
if (autoPitch.targetPitch != null) {
float deltaPitch = autoPitch.targetPitch - data.pitch();
int fdY = MathHelper.clamp(Math.round(dim.yMid - deltaPitch * dim.degreesPerPixel), dim.tFrame + 10, dim.bFrame - 20);
DrawHelper.drawHorizontalLine(context, dim.xMid - dim.wFrame / 10, dim.xMid + dim.wFrame / 10, fdY, FAConfig.indicator().advisoryColor);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,8 @@ private void renderVerticalMode(DrawContext context, TextRenderer textRenderer)

if (plan.landAltitude != null) {
verticalMode.update(Text.translatable("mode.flightassistant.vert.land", plan.landAltitude));
} else if (!autoflight.autoPilotEnabled || diff <= 5) {
} else if (!autoflight.autoPilotEnabled || diff <= 10) {
verticalMode.update(Text.translatable("mode.flightassistant.vert.alt_hold" + type, targetAltitude));
} else if (diff <= 10) {
verticalMode.update(Text.translatable("mode.flightassistant.vert.alt_approaching" + type, targetAltitude));
} else if (targetAltitude > data.altitude()) {
verticalMode.update(Text.translatable("mode.flightassistant.vert.climb" + type, targetAltitude));
} else {
Expand Down
2 changes: 0 additions & 2 deletions src/main/resources/assets/flightassistant/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,9 @@
"mode.flightassistant.firework.speed.managed": "P. SPD %s",
"mode.flightassistant.vert.climb.selected": "CLB %s",
"mode.flightassistant.vert.descend.selected": "DES %s",
"mode.flightassistant.vert.alt_approaching.selected": "ALT* %s",
"mode.flightassistant.vert.alt_hold.selected": "ALT %s",
"mode.flightassistant.vert.climb.managed": "P. CLB %s",
"mode.flightassistant.vert.descend.managed": "P. DES %s",
"mode.flightassistant.vert.alt_approaching.managed": "P. ALT* %s",
"mode.flightassistant.vert.alt_hold.managed": "P. ALT %s",
"mode.flightassistant.vert.land": "LAND %s",
"mode.flightassistant.lat.heading": "HDG %s",
Expand Down
2 changes: 0 additions & 2 deletions src/main/resources/assets/flightassistant/lang/ru_ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,10 @@
"mode.flightassistant.firework.speed.managed": "П. СКОР %s",
"mode.flightassistant.vert.climb.selected": "НАБОР %s",
"mode.flightassistant.vert.descend.selected": "СНИЖ %s",
"mode.flightassistant.vert.alt_approaching.selected": "ВЫС* %s",
"mode.flightassistant.vert.alt_hold.selected": "ВЫС %s",
"mode.flightassistant.vert.land": "ПОСАДКА %s",
"mode.flightassistant.vert.climb.managed": "П. НАБОР %s",
"mode.flightassistant.vert.descend.managed": "П. СНИЖ %s",
"mode.flightassistant.vert.alt_approaching.managed": "П. ВЫС* %s",
"mode.flightassistant.vert.alt_hold.managed": "П. ВЫС %s",
"mode.flightassistant.lat.heading": "КУРС %s",
"mode.flightassistant.lat.position": "ПОЗ %s %s",
Expand Down

0 comments on commit 3b7c9ed

Please sign in to comment.