|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using UnityEngine; |
| 6 | + |
| 7 | +namespace RemoteTech |
| 8 | +{ |
| 9 | + public enum FlightMode |
| 10 | + { |
| 11 | + Off, |
| 12 | + KillRot, |
| 13 | + AttitudeHold, |
| 14 | + AltitudeHold, |
| 15 | + Rover, |
| 16 | + } |
| 17 | + |
| 18 | + public enum FlightAttitude |
| 19 | + { |
| 20 | + Prograde, |
| 21 | + Retrograde, |
| 22 | + NormalPlus, |
| 23 | + NormalMinus, |
| 24 | + RadialPlus, |
| 25 | + RadialMinus, |
| 26 | + Surface, |
| 27 | + } |
| 28 | + |
| 29 | + public enum ReferenceFrame |
| 30 | + { |
| 31 | + Orbit, |
| 32 | + Surface, |
| 33 | + TargetVelocity, |
| 34 | + TargetParallel, |
| 35 | + North, |
| 36 | + Maneuver, |
| 37 | + World, |
| 38 | + } |
| 39 | + |
| 40 | + public class AttitudeCommand : AbstractCommand |
| 41 | + { |
| 42 | + public static readonly Dictionary<FlightMode, String> FormatMode = new Dictionary<FlightMode, String>() |
| 43 | + { |
| 44 | + { FlightMode.Off, "Mode: Off," }, |
| 45 | + { FlightMode.KillRot, "Mode: Kill rotation" }, |
| 46 | + { FlightMode.AttitudeHold, "Mode: Hold {0} {1}" }, |
| 47 | + { FlightMode.AltitudeHold, "Mode: Hold {0}" }, |
| 48 | + { FlightMode.Rover, "" }, |
| 49 | + }; |
| 50 | + |
| 51 | + public static readonly Dictionary<FlightAttitude, String> FormatAttitude = new Dictionary<FlightAttitude, String>() |
| 52 | + { |
| 53 | + { FlightAttitude.Prograde, "Prograde" }, |
| 54 | + { FlightAttitude.Retrograde, "Retrograde" }, |
| 55 | + { FlightAttitude.RadialMinus, "Radial -" }, |
| 56 | + { FlightAttitude.RadialPlus, "Radial +" }, |
| 57 | + { FlightAttitude.NormalMinus, "Normal -" }, |
| 58 | + { FlightAttitude.NormalPlus, "Normal +" }, |
| 59 | + { FlightAttitude.Surface, "Direction" }, |
| 60 | + }; |
| 61 | + |
| 62 | + public static readonly Dictionary<ReferenceFrame, String> FormatReference = new Dictionary<ReferenceFrame, String>() |
| 63 | + { |
| 64 | + { ReferenceFrame.Orbit, "OBT" }, |
| 65 | + { ReferenceFrame.Surface, "SRF" }, |
| 66 | + { ReferenceFrame.TargetVelocity, "RVEL" }, |
| 67 | + { ReferenceFrame.TargetParallel, "TGT" }, |
| 68 | + { ReferenceFrame.North, "North" }, |
| 69 | + { ReferenceFrame.Maneuver, "Maneuver" }, |
| 70 | + { ReferenceFrame.World, "World" }, |
| 71 | + }; |
| 72 | + |
| 73 | + public FlightMode Mode { get; set; } |
| 74 | + public FlightAttitude Attitude { get; set; } |
| 75 | + public ReferenceFrame Frame { get; set; } |
| 76 | + public Quaternion Orientation { get; set; } |
| 77 | + public float Altitude { get; set; } |
| 78 | + public override int Priority { get { return 0; } } |
| 79 | + |
| 80 | + public override string Description |
| 81 | + { |
| 82 | + get |
| 83 | + { |
| 84 | + String res = ""; |
| 85 | + switch (Mode) |
| 86 | + { |
| 87 | + default: res = FormatMode[Mode]; break; |
| 88 | + case FlightMode.AltitudeHold: res = String.Format(FormatMode[Mode], RTUtil.FormatSI(Altitude, "m")); break; |
| 89 | + case FlightMode.AttitudeHold: |
| 90 | + if (Attitude == FlightAttitude.Surface) |
| 91 | + { |
| 92 | + res = String.Format(FormatMode[Mode], Orientation.eulerAngles.x.ToString("F1") + "°, " + |
| 93 | + (360 - Orientation.eulerAngles.y).ToString("F1") + "°, " + |
| 94 | + RTUtil.Format360To180(180 - Orientation.eulerAngles.z).ToString("F1") + "°", ""); |
| 95 | + break; |
| 96 | + } |
| 97 | + res = String.Format(FormatMode[Mode], FormatReference[Frame], FormatAttitude[Attitude]); |
| 98 | + break; |
| 99 | + } |
| 100 | + return res + Environment.NewLine + base.Description; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + public override bool Pop(FlightComputer f) |
| 105 | + { |
| 106 | + if (Mode == FlightMode.KillRot) |
| 107 | + { |
| 108 | + Orientation = f.Vessel.transform.rotation; |
| 109 | + } |
| 110 | + return true; |
| 111 | + } |
| 112 | + |
| 113 | + public override bool Execute(FlightComputer f, FlightCtrlState fcs) |
| 114 | + { |
| 115 | + switch (Mode) |
| 116 | + { |
| 117 | + case FlightMode.Off: |
| 118 | + break; |
| 119 | + case FlightMode.KillRot: |
| 120 | + FlightCore.HoldOrientation(fcs, f, Orientation * Quaternion.AngleAxis(90, Vector3.left)); |
| 121 | + break; |
| 122 | + case FlightMode.AttitudeHold: |
| 123 | + FlightCore.HoldAttitude(fcs, f, Frame, Attitude, Orientation); |
| 124 | + break; |
| 125 | + case FlightMode.AltitudeHold: |
| 126 | + break; |
| 127 | + } |
| 128 | + |
| 129 | + return false; |
| 130 | + } |
| 131 | + |
| 132 | + public static AttitudeCommand Off() |
| 133 | + { |
| 134 | + return new AttitudeCommand() |
| 135 | + { |
| 136 | + Mode = FlightMode.Off, |
| 137 | + Attitude = FlightAttitude.Prograde, |
| 138 | + Frame = ReferenceFrame.World, |
| 139 | + Orientation = Quaternion.identity, |
| 140 | + Altitude = Single.NaN, |
| 141 | + TimeStamp = RTUtil.GameTime, |
| 142 | + }; |
| 143 | + } |
| 144 | + |
| 145 | + public static AttitudeCommand KillRot() |
| 146 | + { |
| 147 | + return new AttitudeCommand() |
| 148 | + { |
| 149 | + Mode = FlightMode.KillRot, |
| 150 | + Attitude = FlightAttitude.Prograde, |
| 151 | + Frame = ReferenceFrame.World, |
| 152 | + Orientation = Quaternion.identity, |
| 153 | + Altitude = Single.NaN, |
| 154 | + TimeStamp = RTUtil.GameTime, |
| 155 | + }; |
| 156 | + } |
| 157 | + |
| 158 | + public static AttitudeCommand ManeuverNode() |
| 159 | + { |
| 160 | + return new AttitudeCommand() |
| 161 | + { |
| 162 | + Mode = FlightMode.AttitudeHold, |
| 163 | + Attitude = FlightAttitude.Prograde, |
| 164 | + Frame = ReferenceFrame.Maneuver, |
| 165 | + Orientation = Quaternion.identity, |
| 166 | + Altitude = Single.NaN, |
| 167 | + TimeStamp = RTUtil.GameTime, |
| 168 | + }; |
| 169 | + } |
| 170 | + |
| 171 | + public static AttitudeCommand WithAttitude(FlightAttitude att, ReferenceFrame frame) |
| 172 | + { |
| 173 | + return new AttitudeCommand() |
| 174 | + { |
| 175 | + Mode = FlightMode.AttitudeHold, |
| 176 | + Attitude = att, |
| 177 | + Frame = frame, |
| 178 | + Orientation = Quaternion.identity, |
| 179 | + Altitude = Single.NaN, |
| 180 | + TimeStamp = RTUtil.GameTime, |
| 181 | + }; |
| 182 | + } |
| 183 | + |
| 184 | + public static AttitudeCommand WithAltitude(float height) |
| 185 | + { |
| 186 | + return new AttitudeCommand() |
| 187 | + { |
| 188 | + Mode = FlightMode.AltitudeHold, |
| 189 | + Attitude = FlightAttitude.Prograde, |
| 190 | + Frame = ReferenceFrame.North, |
| 191 | + Orientation = Quaternion.identity, |
| 192 | + Altitude = height, |
| 193 | + TimeStamp = RTUtil.GameTime, |
| 194 | + }; |
| 195 | + } |
| 196 | + |
| 197 | + public static AttitudeCommand WithSurface(double pitch, double yaw, double roll) |
| 198 | + { |
| 199 | + Quaternion rotation = Quaternion.Euler(new Vector3d(Double.IsNaN(pitch) ? 0 : pitch, |
| 200 | + Double.IsNaN(yaw) ? 0 : -yaw, |
| 201 | + Double.IsNaN(roll) ? 0 : 180 - roll)); |
| 202 | + return new AttitudeCommand() |
| 203 | + { |
| 204 | + Mode = FlightMode.AttitudeHold, |
| 205 | + Attitude = FlightAttitude.Surface, |
| 206 | + Frame = ReferenceFrame.North, |
| 207 | + Orientation = rotation, |
| 208 | + Altitude = Single.NaN, |
| 209 | + TimeStamp = RTUtil.GameTime, |
| 210 | + }; |
| 211 | + } |
| 212 | + } |
| 213 | +} |
0 commit comments