void ReadTelemetry()
        {
            UdpClient socket = new UdpClient();

            socket.ExclusiveAddressUse = false;
            socket.Client.Bind(new IPEndPoint(IPAddress.Any, readPort));

            Stopwatch sw = new Stopwatch();

            sw.Start();

            StartSending();


            while (!IsStopped)
            {
                try
                {
                    //wait for telemetry
                    if (socket.Available == 0)
                    {
                        if (sw.ElapsedMilliseconds > 500)
                        {
                            Thread.Sleep(1000);
                        }
                        continue;
                    }

                    Byte[] received = socket.Receive(ref senderIP);

                    if (socket.Available != 0)
                    {
                        continue;
                    }

                    var alloc = GCHandle.Alloc(received, GCHandleType.Pinned);
                    telemetryData = (BNGAPI)Marshal.PtrToStructure(alloc.AddrOfPinnedObject(), typeof(BNGAPI));
                    alloc.Free();

                    if (telemetryData.magic[0] == 'B' &&
                        telemetryData.magic[1] == 'N' &&
                        telemetryData.magic[2] == 'G' &&
                        telemetryData.magic[3] == '2')
                    {
                        dt = (float)sw.Elapsed.TotalSeconds;
                        sw.Restart();
                        ProcessBNGAPI(dt);
                    }
                }
                catch (Exception e)
                {
                    Thread.Sleep(1000);
                }
            }

            StopSending();
            socket.Close();

            Thread.CurrentThread.Join();
        }
示例#2
0
        public void CopyFields(BNGAPI other)
        {
            posX = other.posX;
            posY = other.posY;
            posZ = other.posZ;

            velX = other.velX;
            velY = other.velY;
            velZ = other.velZ;

            accX = other.accX;
            accY = other.accY;
            accZ = other.accZ;

            upVecX = other.upVecX;
            upVecY = other.upVecY;
            upVecZ = other.upVecZ;

            rollPos  = other.rollPos;
            pitchPos = other.pitchPos;
            yawPos   = other.yawPos;

            rollRate  = other.rollRate;
            pitchRate = other.pitchRate;
            yawRate   = other.yawRate;

            rollAcc  = other.rollAcc;
            pitchAcc = other.pitchAcc;
            yawAcc   = other.yawAcc;


            engine_rate = other.engine_rate;
            idle_rpm    = other.idle_rpm;
            max_rpm     = other.max_rpm;

            gear      = other.gear;
            max_gears = other.max_gears;

            suspension_position_bl = other.suspension_position_bl;
            suspension_position_br = other.suspension_position_br;
            suspension_position_fl = other.suspension_position_fl;
            suspension_position_fr = other.suspension_position_fr;

            suspension_velocity_bl = other.suspension_velocity_bl;
            suspension_velocity_br = other.suspension_velocity_br;
            suspension_velocity_fl = other.suspension_velocity_fl;
            suspension_velocity_fr = other.suspension_velocity_fr;

            suspension_acceleration_bl = other.suspension_acceleration_bl;
            suspension_acceleration_br = other.suspension_acceleration_br;
            suspension_acceleration_fl = other.suspension_acceleration_fl;
            suspension_acceleration_fr = other.suspension_acceleration_fr;
        }
示例#3
0
        public byte[] ToByteArray()
        {
            BNGAPI packet = this;
            int    num    = Marshal.SizeOf <BNGAPI>(packet);

            byte[] array  = new byte[num];
            IntPtr intPtr = Marshal.AllocHGlobal(num);

            Marshal.StructureToPtr <BNGAPI>(packet, intPtr, false);
            Marshal.Copy(intPtr, array, 0, num);
            Marshal.FreeHGlobal(intPtr);
            return(array);
        }