示例#1
0
        static HiResTimer()                                                            //costruttore static, viene chiamato prima del primo utilizzo della classe
        {
            mPerfCounterSupported = QueryPerformanceFrequency(ref mOriginalFrequency); //verifica se è disponibile l'HiResTimer (true a partire da WinXP)
            if (!mPerfCounterSupported)                                                //se non è supportato lo emuliamo con il LowRes
            {
                mOriginalFrequency = MILLI_IN_SECOND;
            }

#if timedebug
            mCurrentFrequency = (long)Math.Round(mOriginalFrequency * testmultiplier);
#else
            mCurrentFrequency = mOriginalFrequency;
#endif

            // per testare il caso più complicato di allinemento a cambio lowres
            //long old = GetTickCount64();
            //while (old == GetTickCount64()) // allineati all'inizio della variazione
            //	;

            TimeReference now = TimeReference.Now;

#if timedebug
            startQPC = startEPC = now.HiRes;
#endif

            LastReset   = MobileWindow = now; //avvia le finestre fissa e mobile a partire da adesso
            cambioNano  = 0;                  // assegna il cambioNano iniziale - nessun cambio
            cambioCount = 0;                  // assegna il cambioCount iniziale - nessun cambio
        }
示例#2
0
        private static void ApplicaNuovaFrequenza(TimeReference delta)
        {
            long mNewFrequency = delta.CalculateHiResFrequency();

            //applica la media geometrica tra vecchia e nuova frequenza
            //compensa l'oscillazione del valore dovuta al fatto che la mNewFrequency una volta viene in eccesso e una volta viene in difetto
            //ed è ugualmente veloce verso numeri grossi e verso numeri piccoli (a differenza della media aritmetica)
            long mCompFrequency = mCurrentFrequency == 0 ? mNewFrequency : (long)(Math.Sign(mNewFrequency) * Math.Sqrt(Math.Abs((double)mCurrentFrequency * mNewFrequency)));

#if timedebug
            double TimeError       = (delta.HiResNano - delta.LowResNano) / (double)NANO_IN_MILLI;
            long   TargetFrequency = (long)Math.Round(mOriginalFrequency * testmultiplier);
            long   anew            = Math.Abs(mCompFrequency);
            long   atar            = Math.Abs(TargetFrequency);
            double PercError       = (anew > atar) ? (anew - atar) * 100.0 / anew : -(atar - anew) * 100.0 / atar;

            if (mCorrectionCount == 0)
            {
                System.Diagnostics.Debug.WriteLine(" TIME \t  ERROR  \t   OLD   \t   NEW   \t   COM   \t  TARGET  \t DELAY \tWINDOW SIZE");
            }

            System.Diagnostics.Debug.WriteLine(string.Format(System.Globalization.CultureInfo.InvariantCulture,
                                                             "{0:00.000}\t{1:+00.00000;-00.00000}\t{2:000000000}\t{3:000000000}\t{4:000000000}\t{5:000000000}\t{6:+00.0;-00.0}ms\t[LR {7}ms HR {8:0.00}ms REF {9:0.00}ms]", DateTime.Now.Subtract(startDT).TotalSeconds, PercError, mCurrentFrequency, mNewFrequency, mCompFrequency, TargetFrequency, TimeError, delta.LowRes, delta.HiResNano / (double)NANO_IN_MILLI, delta.ReferenceNano / (double)NANO_IN_MILLI));
#else
            double TimeError = (delta.HiResNano - delta.LowResNano) / (double)NANO_IN_MILLI;
            System.Diagnostics.Debug.WriteLine($"CLOCK CORRECTION:\t{mCurrentFrequency:000000000}\t{mNewFrequency:000000000}\t{mCompFrequency:000000000}\t{TimeError:0.00}ms\tΔHiRes\t{delta.HiRes}\tΔLowRes\t{delta.LowRes}");
#endif
            mCurrentFrequency = mCompFrequency;
            mCorrectionCount++;
        }
示例#3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TimeOperand" /> class.
 /// </summary>
 /// <param name="timeZoneId">A java time zone ID reference.</param>
 /// <param name="daysOfWeek">daysOfWeek.</param>
 /// <param name="reference">reference (required).</param>
 /// <param name="offset">offset.</param>
 public TimeOperand(string timeZoneId = default(string), List <DayOfWeek> daysOfWeek = default(List <DayOfWeek>), TimeReference reference = default(TimeReference), Interval offset = default(Interval))
 {
     this.Reference  = reference;
     this.TimeZoneId = timeZoneId;
     this.DaysOfWeek = daysOfWeek;
     this.Offset     = offset;
 }
示例#4
0
        public TimeSelector(TimeReference[] allowedTimeRef)
        {
            this.allowedTimeRef = allowedTimeRef;
            universalTime = 0;
            timeRefNames = new string[allowedTimeRef.Length];
            for (int i = 0 ; i < allowedTimeRef.Length ; ++i)
            {
              switch (allowedTimeRef[i])
              {
                case TimeReference.APOAPSIS: timeRefNames[i] = "at the next apoapsis"; break;
                case TimeReference.CLOSEST_APPROACH: timeRefNames[i] = "at closest approach to target"; break;
                case TimeReference.EQ_ASCENDING: timeRefNames[i] = "at the equatorial AN"; break;
                case TimeReference.EQ_DESCENDING: timeRefNames[i] = "at the equatorial DN"; break;
                case TimeReference.PERIAPSIS: timeRefNames[i] = "at the next periapsis"; break;
                case TimeReference.REL_ASCENDING: timeRefNames[i] = "at the next AN with the target."; break;
                case TimeReference.REL_DESCENDING: timeRefNames[i] = "at the next DN with the target."; break;

                case TimeReference.X_FROM_NOW: timeRefNames[i] = "after a fixed time"; break;

                case TimeReference.ALTITUDE: timeRefNames[i] = "at an altitude"; break;

                case TimeReference.EQ_NEAREST_AD: timeRefNames[i] = "at the nearest equatorial AN/DN"; break;
                case TimeReference.EQ_HIGHEST_AD: timeRefNames[i] = "at the highest equatorial AN/DN"; break;
                case TimeReference.REL_NEAREST_AD: timeRefNames[i] = "at the nearest AN/DN with the target"; break;
                case TimeReference.REL_HIGHEST_AD: timeRefNames[i] = "at the highest AN/DN with the target"; break;
              }
            }
        }
示例#5
0
    static void Main(string[] args)
    {
        var           timeReference1 = new TimeReference();
        TimeReference timeReference2 = timeReference1;

        // Modify only one instance
        timeReference1.Minutes = 15;
        timeReference1.Hours   = 12;

        Console.WriteLine("Reference types:");
        Console.WriteLine("timeReference1:\tHours: " + timeReference1.Hours + "\tMinutes: " + timeReference1.Minutes);
        Console.WriteLine("timeReference2:\tHours: " + timeReference2.Hours + "\tMinutes: " + timeReference2.Minutes);

        var       timeValue1 = new TimeValue();
        TimeValue timeValue2 = timeValue1;

        // Modify only one instance
        timeValue1.Minutes = 15;
        timeValue1.Hours   = 12;

        Console.WriteLine("Value types:");
        Console.WriteLine("timeValue1:\tHours: " + timeValue1.Hours + "\tMinutes: " + timeValue1.Minutes);
        Console.WriteLine("timeValue2:\tHours: " + timeValue2.Hours + "\tMinutes: " + timeValue2.Minutes);


        //// Uncomment if running in Visual Studio:
        //Console.ReadKey();
    }
示例#6
0
 private static void CalcolaFrequenzaDebug(TimeReference elapsed, TimeReference now)
 {
     if (!elapsed.IsLowResZero)
     {
         mDebugFrequency = elapsed.CalculateHiResFrequency();
     }
 }
示例#7
0
            internal TimeReference Subtract(TimeReference t)
            {
#if timedebug
                return(new TimeReference(mLowRes - t.mLowRes, mHiRes - t.mHiRes, mReference - t.mReference));
#else
                return(new TimeReference(mLowRes - t.mLowRes, mHiRes - t.mHiRes));
#endif
            }
示例#8
0
    /**
     * Constructor with specified TimeReference.
     */
    public CountdownTimer(float countdownTime, TimeReference timeReference)
    {
        this.timeReference = timeReference;

        Assertion.Assert(countdownTime > 0, "The specified time must be greater than zero.");

        Reset(countdownTime);
    }
示例#9
0
 private void init(int hour, int minute, int second, bool isUtc)
 {
     this.hour      = hour;
     this.minute    = minute;
     this.second    = second;
     this.reference = isUtc ? TimeReference.UTC : TimeReference.undefined;
     this.validate();
 }
示例#10
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DateTimeOperand" /> class.
 /// </summary>
 /// <param name="timeZoneId">A java time zone ID reference.</param>
 /// <param name="daysOfWeek">daysOfWeek.</param>
 /// <param name="year">year.</param>
 /// <param name="month">month.</param>
 /// <param name="day">day.</param>
 /// <param name="reference">reference (required).</param>
 /// <param name="offset">offset.</param>
 public DateTimeOperand(string timeZoneId = default(string), List <DayOfWeek> daysOfWeek = default(List <DayOfWeek>), int year = default(int), int month = default(int), int day = default(int), TimeReference reference = default(TimeReference), Interval offset = default(Interval))
 {
     this.Reference  = reference;
     this.TimeZoneId = timeZoneId;
     this.DaysOfWeek = daysOfWeek;
     this.Year       = year;
     this.Month      = month;
     this.Day        = day;
     this.Offset     = offset;
 }
示例#11
0
        private static void ComputeFrequency(TimeReference now)
        {
            TimeReference longWindow = now.Subtract(LongWindow);

            if (longWindow.ShouldAdjustFreqency())             //verifica la bontà dell' ActualFrequency sull'intervallo lungo (orologio derivante)
            {
                ApplicaNuovaFrequenza(longWindow);
                LongWindow = now;
            }
        }
示例#12
0
        /// <summary>
        /// Add offset to an unspecified local time
        /// </summary>
        /// <param name="offsetHour"></param>
        /// <param name="offsetMinute"></param>
        /// <param name="offsetSign"></param>
        public void addOffset(int offsetHour, int offsetMinute, OffsetSign offsetSign)
        {
            if (this.reference != TimeReference.undefined)
            {
                throw new TimeHandlerException("Time offset can only be added to undefined local time!");
            }

            this.offsetHour   = offsetHour;
            this.offsetMinute = offsetMinute;
            this.offsetSign   = offsetSign;

            this.reference = TimeReference.offset;
        }
示例#13
0
        /// <summary>
        /// Create a Local Time with a given offset to UTC
        /// </summary>
        /// <param name="hour"></param>
        /// <param name="minute"></param>
        /// <param name="second"></param>
        /// <param name="offsetHour"></param>
        /// <param name="offsetMinute"></param>
        /// <param name="offsetSign"></param>
        public CompleteTime(int hour, int minute, int second, int offsetHour, int offsetMinute, OffsetSign offsetSign)
        {
            this.hour   = hour;
            this.minute = minute;
            this.second = second;

            this.offsetHour   = offsetHour;
            this.offsetMinute = offsetMinute;
            this.offsetSign   = offsetSign;

            this.reference = TimeReference.offset;

            this.validate();
        }
示例#14
0
        private static void ApplicaNuovaFrequenza(TimeReference elapsed, TimeReference now)
        {
            long newfreq = elapsed.CalculateHiResFrequency();

#if timedebug
            long target = (long)Math.Round(mOriginalFrequency * testmultiplier);
            System.Diagnostics.Debug.WriteLine(String.Format("{0} -> {1} target {2} err {3:+0.00000;-0.00000}% trigger with {4:+00.000;-00.000}", mCurrentFrequency, newfreq, target, (newfreq - target) * 100.0 / target, elapsed.Error));
#else
            System.Diagnostics.Debug.WriteLine(String.Format("{0} -> {1}", mCurrentFrequency, newfreq));
#endif

            AssignFrequency(newfreq, now.HiRes);

#if timedebug
            AlignmentCount++;
#endif
        }
示例#15
0
        public NodePlanningResult PlanNode(Operation op, TimeReference timeRef, double leadingTime, double newPeA, double newApA, double newInc, double courseCorrectFinalPeA, double moonReturnAltitude, double interceptInterval, bool planLast = false)
        {
            NodePlanningResult result = new MechJebModuleManeuverPlanner.NodePlanningResult();

            result.Success = true;
            var UT = DoChooseTimeGUI(op, timeRef, out result.TimeError, false, leadingTime);

            if (result.TimeError == "" && CheckPreconditions(vessel.GetPatchAtUT(UT), UT, op, newPeA, newApA, newInc))
            {
                MakeNodeForOperation(vessel.GetPatchAtUT(UT), UT, op, newPeA, newApA, newInc, courseCorrectFinalPeA, moonReturnAltitude, interceptInterval);
            }
            else
            {
                result.Success = false;
            }
            result.Error   = errorMessage;
            errorMessage   = "";
            result.Success = (result.Success == true && result.Error == "" && result.TimeError == "");
            return(result);
        }
示例#16
0
        /// <summary>
        /// Create using ISO 8601- string
        /// </summary>
        /// <param name="ISO8601"></param>
        public CompleteTime(string ISO8601)
        {
            ISO8601 = ISO8601.Replace(separator, String.Empty);

            this.hour   = Convert.ToInt32(ISO8601.Substring(0, 2));
            this.minute = Convert.ToInt32(ISO8601.Substring(2, 2));
            this.second = Convert.ToInt32(ISO8601.Substring(4, 2));

            if (ISO8601.Length > 6 && (ISO8601.Substring(6, 1).Equals("Z") || ISO8601.Substring(6, 1).Equals("z")))
            {
                this.reference = TimeReference.UTC;
            }
            else if (ISO8601.Length > 6)
            {
                this.reference    = TimeReference.offset;
                this.offsetSign   = ISO8601.Substring(6, 1).Equals("+") ? OffsetSign.positive : OffsetSign.negative;
                this.offsetHour   = Convert.ToInt32(ISO8601.Substring(7, 2));
                this.offsetMinute = Convert.ToInt32(ISO8601.Substring(9, 2));
            }

            this.validate();
        }
示例#17
0
        private static void ComputeFrequency(TimeReference now)
        {
            TimeReference longWindow  = now.Subtract(LastReset);
            TimeReference smallWindow = now.Subtract(MobileWindow);

#if timedebug
            MaxCycleError = 0;
#endif

#if timedebug
            CalcolaFrequenzaDebug(longWindow, now);
#endif

            if (longWindow.InError())             //verifica la bontà dell' ActualFrequency sull'intervallo lungo (orologio derivante)
            {
                ApplicaNuovaFrequenza(longWindow, now);
            }

            if (smallWindow.LowRes >= 2000)             //verifica la bontà dell' ActualFrequency sull'intervallo piccolo (orologio impazzito) - tolleranza 24ms/2000ms = 1.2%
            {
                if (smallWindow.InError())
                {
#if timedebug
                    ShortWTrigger++;
#endif

                    System.Diagnostics.Debug.Write("SWC: ");
                    ApplicaNuovaFrequenza(smallWindow, now);
                    LastReset = now;
                }
                MobileWindow = now;
            }

#if timedebug
            TotalError += MaxCycleError;
            Samples++;
#endif
        }
示例#18
0
        static HiResTimer_PerfCounter()         //costruttore static, viene chiamato prima del primo utilizzo della classe
        {
            //verifica se è disponibile l'HiResTimer (true a partire da WinXP) e si fa restituire l' original frequency
            mPerfCounterSupported = WinAPI.QueryPerformanceFrequency(ref mOriginalFrequency);
            //se non è supportato lo emuliamo con il LowRes
            if (!mPerfCounterSupported)
            {
                mOriginalFrequency = MILLI_IN_SECOND;
            }
            //assegna la frequenza corrente
            mCurrentFrequency = mOriginalFrequency;


            TimeReference now = TimeReference.Now;

#if timedebug
            startQPC = startEPC = now.HiRes;
#endif

            mLastReference = now;
            mTotalNano     = now.LowResNano;

            LongWindow = now;                           //inizia a monitorare la frequenza da adesso
        }
示例#19
0
        double DoChooseTimeGUI(Operation op, TimeReference timeRef, out string timeErrorMessage, bool InvolveGUI = true, double leadingTime = 0)
        {
            if (InvolveGUI)
            {
                TimeReference[] allowedReferences = references[op];

                int referenceIndex = 0;
                if (allowedReferences.Contains(timeReference))
                {
                    referenceIndex = Array.IndexOf(allowedReferences, timeReference);
                }

                referenceIndex = GuiUtils.ArrowSelector(referenceIndex, allowedReferences.Length, () =>
                {
                    switch (timeReference)
                    {
                    case TimeReference.APOAPSIS: GUILayout.Label("at the next apoapsis"); break;

                    case TimeReference.CLOSEST_APPROACH: GUILayout.Label("at closest approach to target"); break;

                    case TimeReference.EQ_ASCENDING: GUILayout.Label("at the equatorial AN"); break;

                    case TimeReference.EQ_DESCENDING: GUILayout.Label("at the equatorial DN"); break;

                    case TimeReference.PERIAPSIS: GUILayout.Label("at the next periapsis"); break;

                    case TimeReference.REL_ASCENDING: GUILayout.Label("at the next AN with the target."); break;

                    case TimeReference.REL_DESCENDING: GUILayout.Label("at the next DN with the target."); break;

                    case TimeReference.X_FROM_NOW:
                        leadTime.text = GUILayout.TextField(leadTime.text, GUILayout.Width(50));
                        GUILayout.Label(" from now");
                        break;

                    case TimeReference.ALTITUDE:
                        GuiUtils.SimpleTextBox("at an altitude of", circularizeAltitude, "km");
                        break;
                    }
                });

                timeReference = allowedReferences[referenceIndex];
            }

            timeErrorMessage = "";

            bool error = false;

            double UT = vesselState.time;

            Orbit o = orbit;

            List <ManeuverNode> maneuverNodes = GetManeuverNodes();

            if (maneuverNodes.Any())
            {
                if (InvolveGUI)
                {
                    GUILayout.Label("after the last maneuver node.");
                }
                ManeuverNode last = maneuverNodes.Last();
                UT = last.UT;
                o  = last.nextPatch;
            }

            switch (InvolveGUI ? timeReference : timeRef)
            {
            case TimeReference.X_FROM_NOW:
                UT += (InvolveGUI ? leadTime.val : leadingTime);
                break;

            case TimeReference.APOAPSIS:
                if (o.eccentricity < 1)
                {
                    UT = o.NextApoapsisTime(UT);
                }
                else
                {
                    error            = true;
                    timeErrorMessage = "Warning: orbit is hyperbolic, so apoapsis doesn't exist.";
                }
                break;

            case TimeReference.PERIAPSIS:
                UT = o.NextPeriapsisTime(UT);
                break;

            case TimeReference.CLOSEST_APPROACH:
                if (core.target.NormalTargetExists)
                {
                    UT = o.NextClosestApproachTime(core.target.TargetOrbit, UT);
                }
                else
                {
                    error            = true;
                    timeErrorMessage = "Warning: no target selected.";
                }
                break;

            case TimeReference.ALTITUDE:
                if (circularizeAltitude > o.PeA && (circularizeAltitude < o.ApA || o.eccentricity >= 1))
                {
                    UT = o.NextTimeOfRadius(UT, o.referenceBody.Radius + circularizeAltitude);
                }
                else
                {
                    error            = true;
                    timeErrorMessage = "Warning: can't circularize at this altitude, since current orbit does not reach it.";
                }
                break;

            case TimeReference.EQ_ASCENDING:
                if (o.AscendingNodeEquatorialExists())
                {
                    UT = o.TimeOfAscendingNodeEquatorial(UT);
                }
                else
                {
                    error            = true;
                    timeErrorMessage = "Warning: equatorial ascending node doesn't exist.";
                }
                break;

            case TimeReference.EQ_DESCENDING:
                if (o.DescendingNodeEquatorialExists())
                {
                    UT = o.TimeOfDescendingNodeEquatorial(UT);
                }
                else
                {
                    error            = true;
                    timeErrorMessage = "Warning: equatorial descending node doesn't exist.";
                }
                break;
            }

            if (op == Operation.COURSE_CORRECTION && core.target.NormalTargetExists)
            {
                Orbit correctionPatch = o;
                while (correctionPatch != null)
                {
                    if (correctionPatch.referenceBody == core.target.TargetOrbit.referenceBody)
                    {
                        o  = correctionPatch;
                        UT = correctionPatch.StartUT;
                        break;
                    }
                    correctionPatch = vessel.GetNextPatch(correctionPatch);
                }
            }

            if (error && InvolveGUI)
            {
                GUIStyle s = new GUIStyle(GUI.skin.label);
                s.normal.textColor = Color.yellow;
                GUILayout.Label(timeErrorMessage, s);
            }

            return(UT);
        }
示例#20
0
 public IEnumerable <double> GetAllScaledTimes(OpticalProperties op)
 {
     return(TimeReference.Select(time => time * muspReference / op.Musp).ToArray());
 }
示例#21
0
 internal TimeReference Subtract(TimeReference t)
 {
     return(new TimeReference(mLowRes - t.mLowRes, mHiRes - t.mHiRes));
 }
示例#22
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TimeOperand" /> class.
 /// </summary>
 /// <param name="timeZoneId">A java time zone ID reference.</param>
 /// <param name="reference">reference (required).</param>
 /// <param name="offset">offset.</param>
 public TimeOperand(string timeZoneId = default(string), TimeReference reference = default(TimeReference), Interval offset = default(Interval))
 {
     this.Reference  = reference;
     this.TimeZoneId = timeZoneId;
     this.Offset     = offset;
 }
 /**
  * Constructor
  */
 public MoveAlongDirection(FsmState owner, string timeReferenceName) : base(owner)
 {
     this.timeReference = TimeReferencePool.GetInstance().Get(timeReferenceName);
 }
        double DoChooseTimeGUI(Operation op, TimeReference timeRef, out string timeErrorMessage, bool InvolveGUI = true, double leadingTime = 0)
        {
            if (InvolveGUI) {
                TimeReference[] allowedReferences = references[op];

                int referenceIndex = 0;
                if (allowedReferences.Contains(timeReference)) referenceIndex = Array.IndexOf(allowedReferences, timeReference);

                referenceIndex = GuiUtils.ArrowSelector(referenceIndex, allowedReferences.Length, () =>
                    {
                        switch (timeReference)
                        {
                            case TimeReference.APOAPSIS: GUILayout.Label("at the next apoapsis"); break;
                            case TimeReference.CLOSEST_APPROACH: GUILayout.Label("at closest approach to target"); break;
                            case TimeReference.EQ_ASCENDING: GUILayout.Label("at the equatorial AN"); break;
                            case TimeReference.EQ_DESCENDING: GUILayout.Label("at the equatorial DN"); break;
                            case TimeReference.PERIAPSIS: GUILayout.Label("at the next periapsis"); break;
                            case TimeReference.REL_ASCENDING: GUILayout.Label("at the next AN with the target."); break;
                            case TimeReference.REL_DESCENDING: GUILayout.Label("at the next DN with the target."); break;

                            case TimeReference.X_FROM_NOW:
                                leadTime.text = GUILayout.TextField(leadTime.text, GUILayout.Width(50));
                                GUILayout.Label(" from now");
                                break;

                            case TimeReference.ALTITUDE:
                                GuiUtils.SimpleTextBox("at an altitude of", circularizeAltitude, "km");
                                break;
                        }
                    });

                timeReference = allowedReferences[referenceIndex];
            }

            timeErrorMessage = "";

            bool error = false;

            double UT = vesselState.time;

            Orbit o = orbit;

            List<ManeuverNode> maneuverNodes = GetManeuverNodes();
            if (maneuverNodes.Count() > 0)
            {
                if (InvolveGUI) { GUILayout.Label("after the last maneuver node."); }
                ManeuverNode last = maneuverNodes.Last();
                UT = last.UT;
                o = last.nextPatch;
            }

            switch (InvolveGUI ? timeReference : timeRef)
            {
                case TimeReference.X_FROM_NOW:
                    UT += (InvolveGUI ? leadTime.val : leadingTime);
                    break;

                case TimeReference.APOAPSIS:
                    if (o.eccentricity < 1)
                    {
                        UT = o.NextApoapsisTime(UT);
                    }
                    else
                    {
                        error = true;
                        timeErrorMessage = "Warning: orbit is hyperbolic, so apoapsis doesn't exist.";
                    }
                    break;

                case TimeReference.PERIAPSIS:
                    UT = o.NextPeriapsisTime(UT);
                    break;

                case TimeReference.CLOSEST_APPROACH:
                    if (core.target.NormalTargetExists)
                    {
                        UT = o.NextClosestApproachTime(core.target.Orbit, UT);
                    }
                    else
                    {
                        error = true;
                        timeErrorMessage = "Warning: no target selected.";
                    }
                    break;

                case TimeReference.ALTITUDE:
                    if (circularizeAltitude > o.PeA && (circularizeAltitude < o.ApA || o.eccentricity >= 1))
                    {
                        UT = o.NextTimeOfRadius(UT, o.referenceBody.Radius + circularizeAltitude);
                    }
                    else
                    {
                        error = true;
                        timeErrorMessage = "Warning: can't circularize at this altitude, since current orbit does not reach it.";
                    }
                    break;

                case TimeReference.EQ_ASCENDING:
                    if (o.AscendingNodeEquatorialExists())
                    {
                        UT = o.TimeOfAscendingNodeEquatorial(UT);
                    }
                    else
                    {
                        error = true;
                        timeErrorMessage = "Warning: equatorial ascending node doesn't exist.";
                    }
                    break;

                case TimeReference.EQ_DESCENDING:
                    if (o.DescendingNodeEquatorialExists())
                    {
                        UT = o.TimeOfDescendingNodeEquatorial(UT);
                    }
                    else
                    {
                        error = true;
                        timeErrorMessage = "Warning: equatorial descending node doesn't exist.";
                    }
                    break;

            }

            if (op == Operation.COURSE_CORRECTION && core.target.NormalTargetExists)
            {
                Orbit correctionPatch = o;
                while (correctionPatch != null)
                {
                    if (correctionPatch.referenceBody == core.target.Orbit.referenceBody)
                    {
                        o = correctionPatch;
                        UT = correctionPatch.StartUT;
                        break;
                    }
                    correctionPatch = vessel.GetNextPatch(correctionPatch);
                }
            }

            if (error && InvolveGUI)
            {
                GUIStyle s = new GUIStyle(GUI.skin.label);
                s.normal.textColor = Color.yellow;
                GUILayout.Label(timeErrorMessage, s);
            }

            return UT;
        }
示例#25
0
        double DoChooseTimeGUI()
        {
            Dictionary <Operation, TimeReference[]> references = new Dictionary <Operation, TimeReference[]>();

            references[Operation.CIRCULARIZE]             = new TimeReference[] { TimeReference.APOAPSIS, TimeReference.PERIAPSIS, TimeReference.ALTITUDE, TimeReference.X_FROM_NOW };
            references[Operation.PERIAPSIS]               = new TimeReference[] { TimeReference.X_FROM_NOW, TimeReference.APOAPSIS, TimeReference.PERIAPSIS };
            references[Operation.APOAPSIS]                = new TimeReference[] { TimeReference.X_FROM_NOW, TimeReference.APOAPSIS, TimeReference.PERIAPSIS };
            references[Operation.ELLIPTICIZE]             = new TimeReference[] { TimeReference.X_FROM_NOW };
            references[Operation.INCLINATION]             = new TimeReference[] { TimeReference.EQ_ASCENDING, TimeReference.EQ_DESCENDING, TimeReference.X_FROM_NOW };
            references[Operation.PLANE]                   = new TimeReference[] { TimeReference.REL_ASCENDING, TimeReference.REL_DESCENDING };
            references[Operation.TRANSFER]                = new TimeReference[] { TimeReference.COMPUTED };
            references[Operation.MOON_RETURN]             = new TimeReference[] { TimeReference.COMPUTED };
            references[Operation.INTERPLANETARY_TRANSFER] = new TimeReference[] { TimeReference.COMPUTED };
            references[Operation.COURSE_CORRECTION]       = new TimeReference[] { TimeReference.COMPUTED };
            references[Operation.LAMBERT]                 = new TimeReference[] { TimeReference.X_FROM_NOW };
            references[Operation.KILL_RELVEL]             = new TimeReference[] { TimeReference.CLOSEST_APPROACH, TimeReference.X_FROM_NOW };

            TimeReference[] allowedReferences = references[operation];

            int referenceIndex = 0;

            if (allowedReferences.Contains(timeReference))
            {
                referenceIndex = Array.IndexOf(allowedReferences, timeReference);
            }

            referenceIndex = GuiUtils.ArrowSelector(referenceIndex, allowedReferences.Length, () =>
            {
                switch (timeReference)
                {
                case TimeReference.APOAPSIS: GUILayout.Label("at the next apoapsis"); break;

                case TimeReference.CLOSEST_APPROACH: GUILayout.Label("at closest approach to target"); break;

                case TimeReference.EQ_ASCENDING: GUILayout.Label("at the equatorial AN"); break;

                case TimeReference.EQ_DESCENDING: GUILayout.Label("at the equatorial DN"); break;

                case TimeReference.PERIAPSIS: GUILayout.Label("at the next periapsis"); break;

                case TimeReference.REL_ASCENDING: GUILayout.Label("at the next AN with the target."); break;

                case TimeReference.REL_DESCENDING: GUILayout.Label("at the next DN with the target."); break;

                case TimeReference.X_FROM_NOW:
                    leadTime.text = GUILayout.TextField(leadTime.text, GUILayout.Width(50));
                    GUILayout.Label(" from now");
                    break;

                case TimeReference.ALTITUDE:
                    GuiUtils.SimpleTextBox("at an altitude of", circularizeAltitude, "km");
                    break;
                }
            });

            timeReference = allowedReferences[referenceIndex];

            bool   error            = false;
            string timeErrorMessage = "";

            double UT = vesselState.time;

            Orbit o = orbit;

            List <ManeuverNode> maneuverNodes = GetManeuverNodes();

            if (maneuverNodes.Count() > 0)
            {
                GUILayout.Label("after the last maneuver node.");
                ManeuverNode last = maneuverNodes.Last();
                UT = last.UT;
                o  = last.nextPatch;
            }

            switch (timeReference)
            {
            case TimeReference.X_FROM_NOW:
                UT += leadTime;
                break;

            case TimeReference.APOAPSIS:
                if (o.eccentricity < 1)
                {
                    UT = o.NextApoapsisTime(UT);
                }
                else
                {
                    error            = true;
                    timeErrorMessage = "Warning: orbit is hyperbolic, so apoapsis doesn't exist.";
                }
                break;

            case TimeReference.PERIAPSIS:
                UT = o.NextPeriapsisTime(UT);
                break;

            case TimeReference.CLOSEST_APPROACH:
                if (core.target.NormalTargetExists)
                {
                    UT = o.NextClosestApproachTime(core.target.Orbit, UT);
                }
                else
                {
                    error            = true;
                    timeErrorMessage = "Warning: no target selected.";
                }
                break;

            case TimeReference.ALTITUDE:
                if (circularizeAltitude > o.PeA && circularizeAltitude < o.ApA)
                {
                    UT = o.NextTimeOfRadius(UT, o.referenceBody.Radius + circularizeAltitude);
                }
                else
                {
                    error            = true;
                    timeErrorMessage = "Warning: can't circularize at this altitude, since current orbit does not reach it.";
                }
                break;

            case TimeReference.EQ_ASCENDING:
                if (o.AscendingNodeEquatorialExists())
                {
                    UT = o.TimeOfAscendingNodeEquatorial(UT);
                }
                else
                {
                    error            = true;
                    timeErrorMessage = "Warning: equatorial ascending node doesn't exist.";
                }
                break;

            case TimeReference.EQ_DESCENDING:
                if (o.DescendingNodeEquatorialExists())
                {
                    UT = o.TimeOfDescendingNodeEquatorial(UT);
                }
                else
                {
                    error            = true;
                    timeErrorMessage = "Warning: equatorial descending node doesn't exist.";
                }
                break;
            }

            if (operation == Operation.COURSE_CORRECTION && core.target.NormalTargetExists)
            {
                Orbit correctionPatch = o;
                while (correctionPatch != null)
                {
                    if (correctionPatch.referenceBody == core.target.Orbit.referenceBody)
                    {
                        o  = correctionPatch;
                        UT = correctionPatch.StartUT;
                        break;
                    }
                    correctionPatch = vessel.GetNextPatch(correctionPatch);
                }
            }

            if (error)
            {
                GUIStyle s = new GUIStyle(GUI.skin.label);
                s.normal.textColor = Color.yellow;
                GUILayout.Label(timeErrorMessage, s);
            }

            return(UT);
        }
 public NodePlanningResult PlanNode(Operation op, TimeReference timeRef, double leadingTime, double newPeA, double newApA, double newInc, double courseCorrectFinalPeA, double moonReturnAltitude, double interceptInterval, bool planLast = false)
 {
     NodePlanningResult result = new MechJebModuleManeuverPlanner.NodePlanningResult();
     result.Success = true;
     var UT = DoChooseTimeGUI(op, timeRef, out result.TimeError, false, leadingTime);
     if (result.TimeError == "" && CheckPreconditions(vessel.GetPatchAtUT(UT), UT, op, newPeA, newApA, newInc)) {
        			MakeNodeForOperation(vessel.GetPatchAtUT(UT), UT, op, newPeA, newApA, newInc, courseCorrectFinalPeA, moonReturnAltitude, interceptInterval);
     }
     else {
         result.Success = false;
     }
        		result.Error = errorMessage;
        		errorMessage = "";
        		result.Success = (result.Success == true && result.Error == "" && result.TimeError == "");
     return result;
 }
示例#27
0
 /**
  * Constructor that uses a default TimeReference.
  */
 public CountdownTimer(float countdownTime) : this(countdownTime, TimeReference.GetDefaultInstance())
 {
 }
        double DoChooseTimeGUI()
        {
            Dictionary<Operation, TimeReference[]> references = new Dictionary<Operation, TimeReference[]>();
            references[Operation.CIRCULARIZE] = new TimeReference[] { TimeReference.APOAPSIS, TimeReference.PERIAPSIS, TimeReference.ALTITUDE, TimeReference.X_FROM_NOW };
            references[Operation.PERIAPSIS] = new TimeReference[] { TimeReference.X_FROM_NOW, TimeReference.APOAPSIS, TimeReference.PERIAPSIS };
            references[Operation.APOAPSIS] = new TimeReference[] { TimeReference.X_FROM_NOW, TimeReference.APOAPSIS, TimeReference.PERIAPSIS };
            references[Operation.ELLIPTICIZE] = new TimeReference[] { TimeReference.X_FROM_NOW };
            references[Operation.INCLINATION] = new TimeReference[] { TimeReference.EQ_ASCENDING, TimeReference.EQ_DESCENDING, TimeReference.X_FROM_NOW };
            references[Operation.PLANE] = new TimeReference[] { TimeReference.REL_ASCENDING, TimeReference.REL_DESCENDING };
            references[Operation.TRANSFER] = new TimeReference[] { TimeReference.COMPUTED };
            references[Operation.MOON_RETURN] = new TimeReference[] { TimeReference.COMPUTED };
            references[Operation.INTERPLANETARY_TRANSFER] = new TimeReference[] { TimeReference.COMPUTED };
            references[Operation.COURSE_CORRECTION] = new TimeReference[] { TimeReference.COMPUTED };
            references[Operation.LAMBERT] = new TimeReference[] { TimeReference.X_FROM_NOW };
            references[Operation.KILL_RELVEL] = new TimeReference[] { TimeReference.CLOSEST_APPROACH, TimeReference.X_FROM_NOW };

            TimeReference[] allowedReferences = references[operation];

            int referenceIndex = 0;
            if (allowedReferences.Contains(timeReference)) referenceIndex = Array.IndexOf(allowedReferences, timeReference);

            referenceIndex = GuiUtils.ArrowSelector(referenceIndex, allowedReferences.Length, () =>
                {
                    switch (timeReference)
                    {
                        case TimeReference.APOAPSIS: GUILayout.Label("at the next apoapsis"); break;
                        case TimeReference.CLOSEST_APPROACH: GUILayout.Label("at closest approach to target"); break;
                        case TimeReference.EQ_ASCENDING: GUILayout.Label("at the equatorial AN"); break;
                        case TimeReference.EQ_DESCENDING: GUILayout.Label("at the equatorial DN"); break;
                        case TimeReference.PERIAPSIS: GUILayout.Label("at the next periapsis"); break;
                        case TimeReference.REL_ASCENDING: GUILayout.Label("at the next AN with the target."); break;
                        case TimeReference.REL_DESCENDING: GUILayout.Label("at the next DN with the target."); break;

                        case TimeReference.X_FROM_NOW:
                            leadTime.text = GUILayout.TextField(leadTime.text, GUILayout.Width(50));
                            GUILayout.Label(" from now");
                            break;

                        case TimeReference.ALTITUDE:
                            GuiUtils.SimpleTextBox("at an altitude of", circularizeAltitude, "km");
                            break;
                    }
                });

            timeReference = allowedReferences[referenceIndex];

            bool error = false;
            string timeErrorMessage = "";

            double UT = vesselState.time;

            Orbit o = orbit;

            List<ManeuverNode> maneuverNodes = GetManeuverNodes();
            if (maneuverNodes.Count() > 0)
            {
                GUILayout.Label("after the last maneuver node.");
                ManeuverNode last = maneuverNodes.Last();
                UT = last.UT;
                o = last.nextPatch;
            }

            switch (timeReference)
            {
                case TimeReference.X_FROM_NOW:
                    UT += leadTime;
                    break;

                case TimeReference.APOAPSIS:
                    if (o.eccentricity < 1)
                    {
                        UT = o.NextApoapsisTime(UT);
                    }
                    else
                    {
                        error = true;
                        timeErrorMessage = "Warning: orbit is hyperbolic, so apoapsis doesn't exist.";
                    }
                    break;

                case TimeReference.PERIAPSIS:
                    UT = o.NextPeriapsisTime(UT);
                    break;

                case TimeReference.CLOSEST_APPROACH:
                    if (core.target.NormalTargetExists)
                    {
                        UT = o.NextClosestApproachTime(core.target.Orbit, UT);
                    }
                    else
                    {
                        error = true;
                        timeErrorMessage = "Warning: no target selected.";
                    }
                    break;

                case TimeReference.ALTITUDE:
                    if (circularizeAltitude > o.PeA && circularizeAltitude < o.ApA)
                    {
                        UT = o.NextTimeOfRadius(UT, o.referenceBody.Radius + circularizeAltitude);
                    }
                    else
                    {
                        error = true;
                        timeErrorMessage = "Warning: can't circularize at this altitude, since current orbit does not reach it.";
                    }
                    break;

                case TimeReference.EQ_ASCENDING:
                    if (o.AscendingNodeEquatorialExists())
                    {
                        UT = o.TimeOfAscendingNodeEquatorial(UT);
                    }
                    else
                    {
                        error = true;
                        timeErrorMessage = "Warning: equatorial ascending node doesn't exist.";
                    }
                    break;

                case TimeReference.EQ_DESCENDING:
                    if (o.DescendingNodeEquatorialExists())
                    {
                        UT = o.TimeOfDescendingNodeEquatorial(UT);
                    }
                    else
                    {
                        error = true;
                        timeErrorMessage = "Warning: equatorial descending node doesn't exist.";
                    }
                    break;

            }

            if (operation == Operation.COURSE_CORRECTION && core.target.NormalTargetExists)
            {
                Orbit correctionPatch = o;
                while (correctionPatch != null)
                {
                    if (correctionPatch.referenceBody == core.target.Orbit.referenceBody)
                    {
                        o = correctionPatch;
                        UT = correctionPatch.StartUT;
                        break;
                    }
                    correctionPatch = vessel.GetNextPatch(correctionPatch);
                }
            }

            if (error)
            {
                GUIStyle s = new GUIStyle(GUI.skin.label);
                s.normal.textColor = Color.yellow;
                GUILayout.Label(timeErrorMessage, s);
            }

            return UT;
        }