Example #1
0
 public bool getAltitude(out double alt)
 {
     alt = -1;
     if (_init())
     {
         return(true);
     }
     rc.TryGetPlanetElevation(MyPlanetElevation.Surface, out alt);
     return(false);
 }
        private void log()
        {
            sb.Clear();
            tick++;
            sb.Append("status: ").Append(status);
            sb.Append("\ntick: ").Append(tick);
            sb.Append("\nthrusters: ").Append(forwardList.Count());
            sb.Append("\ngyros: ").Append(targetCapture.getGyrosCount());
            sb.Append("\nRollInput: ").Append(targetCapture.RollInput);

            if (remoteControl != null)
            {
                remoteControl.TryGetPlanetElevation(MyPlanetElevation.Surface, out elevation);
                sb.Append(string.Format("\nelevation: {0:#.#}m", elevation));
            }

            Me.GetSurface(0).WriteText(sb.ToString());
        }
Example #3
0
        public void Main(string argument, UpdateType updateSource)
        {
            double levelElevation = 10000.00;
            double slowElevation  = 8000;
            double stopVelocity   = 1000;


            IMyRemoteControl     remoteControl = GridTerminalSystem.GetBlockWithName("Remote Control") as IMyRemoteControl;
            IMyGyro              gyroscope     = GridTerminalSystem.GetBlockWithName("Gyroscope") as IMyGyro;
            IMyCockpit           cockpit       = GridTerminalSystem.GetBlockWithName("Control Seat") as IMyCockpit;
            IMyProgrammableBlock autoLevel     = (IMyProgrammableBlock)GridTerminalSystem.GetBlockWithName("Prog Block Auto Level");

            Echo(remoteControl.CustomName);
            Echo(gyroscope.CustomName);
            StringBuilder sb = new StringBuilder();

            double basicVel = remoteControl.GetShipSpeed();
            double elevation;

            remoteControl.TryGetPlanetElevation(MyPlanetElevation.Surface, out elevation);

            sb.AppendLine($"Vel: {basicVel}\n Elev: {elevation.ToString("0.00")}");

            if (stopVelocity <= elevation)
            {
                sb.AppendLine($"Level Limit Reached");
            }
            else if (slowElevation <= elevation)
            {
                sb.AppendLine($"Level Limit Reached");
            }
            else if (levelElevation <= elevation)
            {
                sb.AppendLine($"Level Limit Reached");
            }

            Write(sb.ToString(), cockpit);
        }
        public void Main(string argument, UpdateType updateSource)//main is called once per pysics update, ideally 60Hz
        {
            if (argument.ToLower() == "reset")
            {
                stage = 0;
            }
            bool valid = controller.TryGetPlanetElevation(MyPlanetElevation.Surface, out currentAltitude);

            var parts = Storage.Split(';');

            stage = int.Parse(parts[0]);

            maxTotalEffectiveThrust = 0.0f;//have to recalculate every time because it increases with alt
            foreach (var thruster in thrusters)
            {
                maxTotalEffectiveThrust += thruster.MaxEffectiveThrust;
            }
            physicalMass          = controller.CalculateShipMass().PhysicalMass;
            currentNaturalGravity = controller.GetNaturalGravity();   //
            double gravityMagnitude = currentNaturalGravity.Length(); //what a weird thing to call magnitude of a vector

            Echo($"STAGE: {stage}\nSPEED: {controller.GetShipVelocities().LinearVelocity.Length()}\nALT: {currentAltitude}");
            antenna.HudText = stage.ToString();

            if (stage == 0)
            {
                if (Freefall()) //if we are in freefall. Freefall is defined as accelerating at a rate ~= gravity, this implies thrusters are not acive. Also should check if it's heading towards or away from the gravity well. Linear alg time yay
                {
                    foreach (var thruster in thrusters)
                    {
                        thruster.ThrustOverride = 0;
                    }
                    stage++;
                    Storage = stage + ";";
                }
            }

            else if (stage == 1)
            {
                if (!Freefall())
                {
                    stage = 0;
                }
                double a = maxTotalEffectiveThrust / physicalMass - currentNaturalGravity.Length(); //acceleration with full retro burn
                double b = -controller.GetShipVelocities().LinearVelocity.Length();                 //velocity
                double c = currentAltitude;
                double thrustStopTime = -b / (2 * a);                                               //y=ax^2+bx ->  dy = 2ax+b | 0 = 2*a*x+b -> -b/2a=x

                Func <double, double> altitudeFunc = delegate(double x) {                           //x=time, y=altitude assuming full retro thrust
                    return(a * x * x + b * x + c);
                };

                double minHeight = altitudeFunc(thrustStopTime);                  //the lowest point of the trajectory IF thrust is engaged instantly
                double calculatedStoppingDistance = currentAltitude - minHeight;
                double safetyBuffer           = Math.Abs(b) * 1.25 + craftHeight; //a coefficient in front of the safety buffer feels wrong
                double safetyStoppingDistance = calculatedStoppingDistance + safetyBuffer;

                if (!controller.DampenersOverride && currentAltitude <= safetyStoppingDistance)
                {
                    stage++;
                    Storage = stage + ";";
                    controller.DampenersOverride = true;
                }
            }

            else if (stage == 2)//descent burn has been initiated, goto stg 3 when a safe speed has been reached
            {
                antenna.HudText = stage.ToString();

                if (controller.GetShipVelocities().LinearVelocity.Length() < safeVelocity)
                {
                    stage++;
                    Storage = stage + ";";
                }
            }

            else if (stage == 3)//target safe descent speed has been reached, maintain low speed until touchdown to planet
            {
                float totalThrustNeededToHover = physicalMass * (float)currentNaturalGravity.Length();
                float idealThrustPerThruster   = (float)totalThrustNeededToHover / (float)numThrusters;
                float thrustRatio = thrusters[0].MaxThrust / thrusters[0].MaxEffectiveThrust;//actual output of thrust is similar to but different from the input set by code/user.
                float adjustedThrustPerThruster = idealThrustPerThruster * thrustRatio;

                foreach (var thruster in thrusters)
                {
                    thruster.ThrustOverride = adjustedThrustPerThruster;
                }

                if (currentAltitude < touchdownHeight)
                {
                    stage++;
                    Storage = stage + ";";

                    foreach (var thruster in thrusters)
                    {
                        controller.DampenersOverride = false;
                        thruster.ThrustOverride      = 0.0f;
                    }
                }
            }

            else if (stage == 4)//touchdown, turn stuff off pls
            {
                antenna.HudText = stage.ToString();
                Echo($"TOUCHDOWN");
                stage   = 0;
                Storage = stage + ";";
                Runtime.UpdateFrequency = UpdateFrequency.None;//stop running the script
            }
            lastVelocities = controller.GetShipVelocities();
        }