Пример #1
0
        public void SendFlightPlanPacket(RawOrbitPlanData rawData)
        {
            if (!Connected)
            {
                return;
            }
            int numOrbits        = rawData.CurrentOrbitPatches.Count;
            int numPlannedOrbits = rawData.PlannedOrbitPatches.Count;
            int numMans          = rawData.Mans.Count;
            int targetNameLength = rawData.TargetName.Length;

            sendODPHeader.length = (UInt16)(6 + Marshal.SizeOf(typeof(OrbitData)) * (numOrbits + numPlannedOrbits + 1)
                                            + Marshal.SizeOf(typeof(ManData)) * numMans
                                            + targetNameLength + Marshal.SizeOf(typeof(ClosestAprouchData)));
            int fullPayloadLength = sendODPHeader.length + Marshal.SizeOf(typeof(Header));

            byte[] payload = new byte[fullPayloadLength]; //copy to here

            int copyOffset = Marshal.SizeOf(typeof(Header));

            payload[copyOffset++] = (byte)numOrbits; //1

            IntPtr ptr;

            ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(OrbitData)));
            for (int i = 0; i < numOrbits; i++)
            {
                Marshal.StructureToPtr(rawData.CurrentOrbitPatches[i], ptr, true);
                Marshal.Copy(ptr, payload, copyOffset, Marshal.SizeOf(typeof(OrbitData)));
                copyOffset += Marshal.SizeOf(typeof(OrbitData));
            }

            payload[copyOffset++] = (byte)rawData.ManPatchNum; //2
            payload[copyOffset++] = (byte)numPlannedOrbits;    //3

            for (int i = 0; i < numPlannedOrbits; i++)
            {
                Marshal.StructureToPtr(rawData.PlannedOrbitPatches[i], ptr, true);
                Marshal.Copy(ptr, payload, copyOffset, Marshal.SizeOf(typeof(OrbitData)));
                copyOffset += Marshal.SizeOf(typeof(OrbitData));
            }

            //copy target orbit
            Marshal.StructureToPtr(rawData.TargetOrbit, ptr, true);
            Marshal.Copy(ptr, payload, copyOffset, Marshal.SizeOf(typeof(OrbitData)));
            copyOffset += Marshal.SizeOf(typeof(OrbitData));

            Marshal.FreeHGlobal(ptr);

            payload[copyOffset++] = (byte)targetNameLength; //copy target name //4
            for (int i = 0; i < targetNameLength; i++)
            {
                payload[copyOffset++] = (byte)rawData.TargetName[i];
            }
            payload[copyOffset++] = 0x00; //5

            ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(ClosestAprouchData)));
            Marshal.StructureToPtr(rawData.Rendezvous, ptr, true);
            Marshal.Copy(ptr, payload, copyOffset, Marshal.SizeOf(typeof(ClosestAprouchData)));
            Marshal.FreeHGlobal(ptr);
            copyOffset += Marshal.SizeOf(typeof(ClosestAprouchData));

            payload[copyOffset++] = (byte)numMans; //menuever nodes  //6

            ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(ManData)));
            for (int i = 0; i < numMans; i++)
            {
                Marshal.StructureToPtr(rawData.Mans[i], ptr, true);
                Marshal.Copy(ptr, payload, copyOffset, Marshal.SizeOf(typeof(ManData)));
                copyOffset += Marshal.SizeOf(typeof(ManData));
            }
            Marshal.FreeHGlobal(ptr);

            sendODPHeader.checksum = Checksum(payload, Marshal.SizeOf(typeof(Header)), sendODPHeader.length); //calc checksum

            ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Header)));                                       //copy header to array
            Marshal.StructureToPtr(sendODPHeader, ptr, true);
            Marshal.Copy(ptr, payload, 0, Marshal.SizeOf(typeof(Header)));
            Marshal.FreeHGlobal(ptr);

            sendQueue.Enqueue(payload); //enqueue data
        }
Пример #2
0
        private RawOrbitPlanData GenerateRawOrbitPlanData()
        {
            RawOrbitPlanData dataOut = new RawOrbitPlanData
            {
                CurrentOrbitPatches = new List <OrbitData>(),
                ManPatchNum         = -1,
                PlannedOrbitPatches = new List <OrbitData>(),
                TargetName          = "???",
                Mans = new List <ManData>(),
            };
            Orbit patch, lastPatch = null;

            patch = AV.orbit;
            while (patch != null && patch.activePatch)
            {
                dataOut.CurrentOrbitPatches.Add(OrbUtil.GetOrbitData(lastPatch = patch));
                patch = patch.nextPatch;
            }
            if (AV.patchedConicSolver != null)
            {
                if (AV.patchedConicSolver.maneuverNodes != null)
                {
                    if (AV.patchedConicSolver.maneuverNodes.Count > 0)
                    {
                        for (int i = 0; i < AV.patchedConicSolver.flightPlan.Count; i++)
                        {
                            if (AV.patchedConicSolver.flightPlan[i].patchStartTransition == Orbit.PatchTransitionType.MANEUVER)
                            {
                                dataOut.ManPatchNum = i;
                                break;
                            }
                        }
                        patch = AV.patchedConicSolver.maneuverNodes[0].nextPatch;
                        while (patch != null && patch.activePatch)
                        {
                            dataOut.PlannedOrbitPatches.Add(OrbUtil.GetOrbitData(lastPatch = patch));
                            patch = patch.nextPatch;
                        }
                        for (int i = 0; i < AV.patchedConicSolver.maneuverNodes.Count; i++)
                        {
                            ManeuverNode m = AV.patchedConicSolver.maneuverNodes[i];
                            dataOut.Mans.Add(new ManData
                            {
                                DV = (float)m.GetBurnVector(m.patch).magnitude,
                                UT = m.UT,
                                X  = (float)m.DeltaV.x,
                                Y  = (float)m.DeltaV.y,
                                Z  = (float)m.DeltaV.z,
                            });
                        }
                    }
                }
            }

            if (HasTarget())
            {
                dataOut.TargetName = AV.targetObject.GetName();
                OrbitData o = OrbUtil.GetOrbitData(AV.targetObject.GetOrbit());
                if (AV.targetObject is CelestialBody)
                {
                    o.transEnd   = (byte)PatchTransitionType.FINAL;
                    o.anomolyEnd = o.anomoly + Math.PI * 2;
                }
                dataOut.TargetOrbit = (o);
                //lastPatch.GetDTforTrueAnomaly
                dataOut.Rendezvous.ANAnom       = (float)(FinePrint.Utilities.OrbitUtilities.AngleOfAscendingNode(lastPatch, AV.targetObject.GetOrbit()) * Deg2Rad);
                dataOut.Rendezvous.TargetANAnom = (float)(FinePrint.Utilities.OrbitUtilities.AngleOfAscendingNode(AV.targetObject.GetOrbit(), lastPatch) * Deg2Rad);

                dataOut.Rendezvous.T2AN = (int)OrbUtil.T2TAnom(lastPatch, dataOut.Rendezvous.ANAnom) % o.period;
                dataOut.Rendezvous.T2DN = (int)OrbUtil.T2TAnom(lastPatch, dataOut.Rendezvous.ANAnom + Math.PI) % o.period;

                dataOut.Rendezvous.RelInc = (float)FinePrint.Utilities.OrbitUtilities.GetRelativeInclination(lastPatch, AV.targetObject.GetOrbit());

                double dist;
                double time = OrbUtil.CalcClosestAproach(lastPatch, AV.targetObject.GetOrbit(), lastPatch.StartUT, out dist);
                dataOut.Rendezvous.T2CA         = (int)(time - Planetarium.GetUniversalTime());
                dataOut.Rendezvous.SepAtCA      = (int)dist;
                dataOut.Rendezvous.CAAnom       = (float)lastPatch.TrueAnomalyAtUT(time);
                dataOut.Rendezvous.TargetCAAnom = (float)AV.targetObject.GetOrbit().TrueAnomalyAtUT(time);
            }

            return(dataOut);
        }