Exemple #1
0
        public Form1()
        {
            InitializeComponent();
            int    a = (40 + (104 * trackBar1.Value));
            string b = a.ToString() + Deg;

            textBox1.Text = b;
            int    c = 10 * trackBar2.Value;
            string d = c.ToString() + perc;

            textBox2.Text         = d;
            _serialPort.RtsEnable = true;
            string[] ports = SerialPort.GetPortNames();


            // _serialPort.PortName = ports[1];
            //_serialPort.BaudRate = Convert.ToInt32(9600 );
            // _serialPort.Parity = Parity.None;
            // _serialPort.DataBits = Convert.ToInt32( 8);
            // _serialPort.StopBits = StopBits.One;
            _serialPort.Handshake = Handshake.None;

            // Set the read/write timeouts
            _serialPort.ReadTimeout  = 4000;
            _serialPort.WriteTimeout = 5000;
            lapData       = new PacketLapData(0);
            motionData    = new PacketMotionData(0);
            sessionData   = new PacketSessionData(0);
            telemetryData = new PacketCarTelemetryData(0);
        }
        private void HandlePacket(PacketHeader packet, byte[] bytes)
        {
            _telemetryRecorder.RecordPacket(packet, bytes);

            switch (packet.PacketType)
            {
            case PacketType.Motion:
                var motionPacket = StructUtility.ConvertToPacket <PacketMotionData>(bytes);
                OnMotionPacketReceived(new PacketReceivedEventArgs <PacketMotionData>(_oldMotionData, motionPacket));
                _oldMotionData = motionPacket;
                break;

            case PacketType.Session:
                var sessionPacket = StructUtility.ConvertToPacket <PacketSessionData>(bytes);
                OnSessionPacketReceived(new PacketReceivedEventArgs <PacketSessionData>(_oldSessionData, sessionPacket));
                _oldSessionData = sessionPacket;
                break;

            case PacketType.LapData:
                var lapPacket = StructUtility.ConvertToPacket <PacketLapData>(bytes);
                OnLapPacketReceivedReceived(new PacketReceivedEventArgs <PacketLapData>(_oldLapData, lapPacket));
                _oldLapData = lapPacket;
                break;

            case PacketType.Event:
                var eventPacket = StructUtility.ConvertToPacket <EventPacket>(bytes);
                OnEventPacketReceived(new PacketReceivedEventArgs <EventPacket>(_oldEventPacket, eventPacket));
                _oldEventPacket = eventPacket;
                break;

            case PacketType.Participants:
                var participantPacket = StructUtility.ConvertToPacket <PacketParticipantsData>(bytes);
                OnParticipantsPacketReceived(new PacketReceivedEventArgs <PacketParticipantsData>(_oldParticipantsData, participantPacket));
                _oldParticipantsData = participantPacket;
                break;

            case PacketType.CarSetups:
                var carSetupsPacket = StructUtility.ConvertToPacket <PacketCarSetupData>(bytes);
                OnCarSetupPacketReceived(new PacketReceivedEventArgs <PacketCarSetupData>(_oldCarSetupData, carSetupsPacket));
                _oldCarSetupData = carSetupsPacket;
                break;

            case PacketType.CarTelemetry:
                var carTelemetryPacket = StructUtility.ConvertToPacket <PacketCarTelemetryData>(bytes);
                OnCarTelemetryPacketReceived(new PacketReceivedEventArgs <PacketCarTelemetryData>(_oldCarTelemetryData, carTelemetryPacket));
                _oldCarTelemetryData = carTelemetryPacket;
                break;

            case PacketType.CarStatus:
                var carStatusPacket = StructUtility.ConvertToPacket <PacketCarStatusData>(bytes);
                OnCarStatusPacketReceived(new PacketReceivedEventArgs <PacketCarStatusData>(_oldCarStatusData, carStatusPacket));
                _oldCarStatusData = carStatusPacket;
                break;
            }

            _oldSessionId       = packet.SessionUId;
            _oldFrameIdentifier = packet.FrameIdentifier;
        }
Exemple #3
0
        private static IPacket Lap(PacketHeader packetHeader, BinaryReader reader)
        {
            var packetLapData = new PacketLapData(packetHeader);

            for (int i = 0; i < MaxNumberOfCarsOnTrack; i++)
            {
                var lapData = new LapData();
                lapData.SessionUID  = packetHeader.SessionUID;
                lapData.SessionTime = packetHeader.SessionTime;

                lapData.LastLapTime    = reader.ReadSingle();
                lapData.CurrentLapTime = reader.ReadSingle();

                lapData.Sector1TimeInMS = reader.ReadUInt16();
                lapData.Sector2TimeInMS = reader.ReadUInt16();

                lapData.BestLapTime = reader.ReadSingle();
                lapData.BestLapNum  = reader.ReadByte();

                lapData.BestLapSector1TimeInMS = reader.ReadUInt16();
                lapData.BestLapSector2TimeInMS = reader.ReadUInt16();
                lapData.BestLapSector3TimeInMS = reader.ReadUInt16();

                lapData.BestOverallSector1TimeInMS = reader.ReadUInt16();
                lapData.BestOverallSector1LapNum   = reader.ReadByte();

                lapData.BestOverallSector2TimeInMS = reader.ReadUInt16();
                lapData.BestOverallSector2LapNum   = reader.ReadByte();

                lapData.BestOverallSector3TimeInMS = reader.ReadUInt16();
                lapData.BestOverallSector3LapNum   = reader.ReadByte();

                lapData.LapDistance   = reader.ReadSingle();
                lapData.TotalDistance = reader.ReadSingle();

                lapData.SafetyCarDelta = reader.ReadSingle();
                lapData.CarPosition    = reader.ReadByte();

                lapData.CurrentLapNum     = reader.ReadByte();
                lapData.PitStatus         = (PitStatus)reader.ReadByte();
                lapData.Sector            = reader.ReadByte();
                lapData.CurrentLapInvalid = reader.ReadByte();
                lapData.Penalties         = reader.ReadByte();
                lapData.GridPosition      = reader.ReadByte();
                lapData.DriverStatus      = (DriverStatus)reader.ReadByte();
                lapData.ResultStatus      = (ResultStatus)reader.ReadByte();

                packetLapData.LapData[i] = lapData;
            }

            return(packetLapData);
        }
        public void OnDebugDataSelected(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count == 0)
            {
                return;
            }

            var selectedDebugData = ((DebugModel)e.AddedItems[0]);

            switch (selectedDebugData.PacketType)
            {
            case PacketType.Motion:
                PacketMotionData motionData = StructUtility.ConvertToPacket <PacketMotionData>(selectedDebugData.Data);
                DebugDetails = new ObjectViewModelHierarchy(motionData);
                return;

            case PacketType.Session:
                PacketSessionData sessionData = StructUtility.ConvertToPacket <PacketSessionData>(selectedDebugData.Data);
                DebugDetails = new ObjectViewModelHierarchy(sessionData);
                return;

            case PacketType.LapData:
                PacketLapData lapData = StructUtility.ConvertToPacket <PacketLapData>(selectedDebugData.Data);
                DebugDetails = new ObjectViewModelHierarchy(lapData);
                return;

            case PacketType.Event:
                EventPacket eventData = StructUtility.ConvertToPacket <EventPacket>(selectedDebugData.Data);
                DebugDetails = new ObjectViewModelHierarchy(eventData);
                return;

            case PacketType.Participants:
                PacketParticipantsData participantsData = StructUtility.ConvertToPacket <PacketParticipantsData>(selectedDebugData.Data);
                DebugDetails = new ObjectViewModelHierarchy(participantsData);
                return;

            case PacketType.CarSetups:
                PacketCarSetupData carSetupsData = StructUtility.ConvertToPacket <PacketCarSetupData>(selectedDebugData.Data);
                DebugDetails = new ObjectViewModelHierarchy(carSetupsData);
                return;

            case PacketType.CarTelemetry:
                PacketCarTelemetryData carTelemetryData = StructUtility.ConvertToPacket <PacketCarTelemetryData>(selectedDebugData.Data);
                DebugDetails = new ObjectViewModelHierarchy(carTelemetryData);
                return;

            case PacketType.CarStatus:
                PacketCarStatusData carStatusData = StructUtility.ConvertToPacket <PacketCarStatusData>(selectedDebugData.Data);
                DebugDetails = new ObjectViewModelHierarchy(carStatusData);
                return;
            }
        }
Exemple #5
0
        public async Task ReceiveLoop(CancellationToken cancellationToken)
        {
            while (!cancellationToken.IsCancellationRequested)
            {
                UdpReceiveResult receiveResult = await _udpReceiver.Receive(cancellationToken).ConfigureAwait(false);

                byte[]       rawPacked = receiveResult.Buffer;
                PacketHeader header    = DeserializePackedHeader(rawPacked);
                switch (header.MPacketId)
                {
                case 0:
                    PacketMotionData packetMotionData = Deserialize <PacketMotionData>(rawPacked);
                    _dataSetCompositor.ProcessPacket(packetMotionData);
                    break;

                case 1:
                    PacketSessionData packetSessionData = Deserialize <PacketSessionData>(rawPacked);
                    _dataSetCompositor.ProcessPacket(packetSessionData);
                    break;

                case 2:
                    PacketLapData packetLapData = Deserialize <PacketLapData>(rawPacked);
                    _dataSetCompositor.ProcessPacket(packetLapData);
                    break;

                case 3:
                    PacketEventData packetEventData = Deserialize <PacketEventData>(rawPacked);
                    _dataSetCompositor.ProcessPacket(packetEventData);
                    break;

                case 4:
                    PacketParticipantsData packetParticipantsData = Deserialize <PacketParticipantsData>(rawPacked);
                    _dataSetCompositor.ProcessPacket(packetParticipantsData);
                    break;

                case 5:
                    PacketCarSetupData packetCarSetupData = Deserialize <PacketCarSetupData>(rawPacked);
                    _dataSetCompositor.ProcessPacket(packetCarSetupData);
                    break;

                case 6:
                    PacketCarTelemetryData packetCarTelemetryData = Deserialize <PacketCarTelemetryData>(rawPacked);
                    _dataSetCompositor.ProcessPacket(packetCarTelemetryData);
                    break;

                case 7:
                    PacketCarStatusData packetCarStatusData = Deserialize <PacketCarStatusData>(rawPacked);
                    _dataSetCompositor.ProcessPacket(packetCarStatusData);
                    break;
                }
            }
        }
Exemple #6
0
        private void CalculateCarSectors(PacketLapData packet)
        {
            if (LastLapPacketCarData == null && LastLapPacketCarData.Count == 0)
            {
                return;
            }

            if (_trackSectors.Count == 0)
            {
                return;
            }

            for (int i = 0; i < LastLapPacketCarData.Count; i++)
            {
                if (LastLapPacketCarData[i] == null)
                {
                    continue;
                }

                if (packet.LapData[i].Equals(default(LapData)))
                {
                    continue;
                }

                var sectorIndex = FindSectorIndexByLapDistance(packet.LapData[i].LapDistance);

                if (sectorIndex == -1)
                {
                    continue;
                }

                LastLapPacketCarData[i].CurrentDeltaSector = sectorIndex;

                if (!LastLapPacketCarData[i].DeltaSectorTimes.ContainsKey(packet.LapData[i].CurrentLapNum))
                {
                    LastLapPacketCarData[i]
                    .DeltaSectorTimes.Add(packet.LapData[i].CurrentLapNum, new Dictionary <int, TimeSpan>());
                }

                if (!LastLapPacketCarData[i].DeltaSectorTimes[packet.LapData[i].CurrentLapNum].ContainsKey(sectorIndex))
                {
                    LastLapPacketCarData[i]
                    .DeltaSectorTimes[packet.LapData[i].CurrentLapNum]
                    .Add(sectorIndex, TimeSpan.FromSeconds(packet.Header.SessionTime));
                }
                else
                {
                    LastLapPacketCarData[i]
                    .DeltaSectorTimes[packet.LapData[i].CurrentLapNum][sectorIndex] = TimeSpan.FromSeconds(packet.Header.SessionTime);
                }
            }
        }
Exemple #7
0
        private void DetectGridChanges(PacketLapData packetLapData)
        {
            int validCount = 0;

            foreach (LapData item in packetLapData.LapData)
            {
                if (!item.Equals(default(LapData)))
                {
                    validCount++;
                }
            }

            if (validCount != LastLapPacketCarData.Count)
            {
                LastLapPacketCarData.Clear();
            }
        }
Exemple #8
0
        private void UpdateCarGrid(PacketLapData packetLapData)
        {
            if (LastLapPacketCarData == null || LastLapPacketCarData.Count == 0)
            {
                return;
            }

            for (int i = 0; i < LastLapPacketCarData.Count; i++)
            {
                LastLapPacketCarData[i].CarPosition   = packetLapData.LapData[i].CarPosition;
                LastLapPacketCarData[i].TotalDistance = packetLapData.LapData[i].TotalDistance;
                LastLapPacketCarData[i].IsPlayer      =
                    packetLapData.GetPlayerLapData().CarPosition == packetLapData.LapData[i].CarPosition;
                LastLapPacketCarData[i].CurrentLap         = packetLapData.LapData[i].CurrentLapNum;
                LastLapPacketCarData[i].CurrentTrackSector = (int)packetLapData.LapData[i].Sector;
                LastLapPacketCarData[i].Pitting            = packetLapData.LapData[i].PitStatus != PitStatus.None;
            }

            CalculateCarDeltas();

            NotifyOfPropertyChange(() => CarData);
        }
Exemple #9
0
        private void InitializeCarGrid(PacketLapData packetLapData)
        {
            DetectGridChanges(packetLapData);
            if (LastLapPacketCarData != null && LastLapPacketCarData.Count > 0)
            {
                return;
            }

            for (int i = 0; i < packetLapData.LapData.Length; i++)
            {
                if (packetLapData.LapData[i].Equals(default(LapData)))
                {
                    // don't add cars that don't have a value
                    continue;
                }

                LastLapPacketCarData.Add(new CarTimingViewModel
                {
                    CarIndex = i,
                });
            }

            CarData = new SortedObservableCollection <CarTimingViewModel>(LastLapPacketCarData);
        }
 public void OnPacketLapData(ref PacketLapData pLap, PacketSource packetSource)
 {
     WritePacket(pLap.Header.SessionUID, ref pLap, packetSource);
 }
 public void OnPacketLapData(ref PacketLapData pLap, PacketSource packetSource)
 {
     HandlePacket(ph => ph.OnPacketLapData, ref pLap, packetSource);
 }
Exemple #12
0
 internal void ProcessPacket(PacketLapData packetLapData)
 {
     AllPacketsComposition.PacketLapData = packetLapData;
     _isPacketLapDataFilled = true;
     HandleCompletePacket();
 }
        void OnUdpData(IAsyncResult result)
        {
            UdpClient  socket = result.AsyncState as UdpClient;
            IPEndPoint source = new IPEndPoint(0, 0);

            byte[] message = socket.EndReceive(result, ref source);

            var ph = new PacketHeader(message);

            switch (ph.m_packetId)
            {
            //case 1:
            //    var sessionData = new PacketSessionData();
            //    sessionData.RawData = ph.RawData;
            //    sessionData.LoadData();
            //    break;
            case 2:
                var lapData = new PacketLapData();
                lapData.RawData = ph.RawData;
                lapData.LoadData();

                currentLap = lapData.m_lapData[lapData.m_header.m_playerCarIndex].m_currentLapNum.ToString();
                var lastLapTS = TimeSpan.FromSeconds(lapData.m_lapData[lapData.m_header.m_playerCarIndex].m_lastLapTime);
                lastLap = $"{ lastLapTS.Minutes }:{ lastLapTS.Seconds }.{ lastLapTS.Milliseconds }";

                if (lapData.m_lapData[lapData.m_header.m_playerCarIndex].m_lapDistance > 0)
                {
                    if (lastLapDistance < 0)
                    {
                        //NEW LAP
                        MediaPlayer.Play(larry);
                        StartNewLap();
                    }
                    else
                    {
                        var diff = lastLapDistance - lapData.m_lapData[lapData.m_header.m_playerCarIndex].m_lapDistance;
                        if (diff > 1000)
                        {
                            //NEW LAP

                            MediaPlayer.Play(larry);
                            StartNewLap();
                        }
                    }
                }

                if (currentLapTiming == null)
                {
                    currentLapTiming = new List <Tuple <float, float> >();
                }

                currentLapTiming.Add(new Tuple <float, float>(lapData.m_lapData[lapData.m_header.m_playerCarIndex].m_lapDistance, lapData.m_lapData[lapData.m_header.m_playerCarIndex].m_currentLapTime));



                lastLapDistance = lapData.m_lapData[lapData.m_header.m_playerCarIndex].m_lapDistance;

                //lastLap = lapData.m_lapData[0].m_lapDistance
                //lastLap = lapData.m_lapData[0].m_lapDistance.ToString();
                //Task.Run(() => WriteTextAsync(@"C:\Users\Andrew\Desktop\f1\output.txt", lapData.m_lapData[0].m_lapDistance.ToString() + Environment.NewLine));

                packetCount++;
                break;

            case 6:
                var tele = new PacketCarTelemetryData();
                tele.RawData = ph.RawData;
                tele.LoadData();
                currentGear = tele.m_carTelemetryData[tele.m_header.m_playerCarIndex].m_gear.ToString();
                currentRPM  = tele.m_carTelemetryData[tele.m_header.m_playerCarIndex].m_engineRPM;
                var rpm = tele.m_carTelemetryData[tele.m_header.m_playerCarIndex].m_revLightsPercent;
                rpmPercent   = rpm.ToString();
                ledRPMStrip1 = false;
                ledRPMStrip2 = false;
                ledRPMStrip3 = false;

                if (rpm > 25)
                {
                    ledRPMStrip1 = true;
                }

                if (rpm > 50)
                {
                    ledRPMStrip2 = true;
                }

                if (rpm > 75)
                {
                    ledRPMStrip3 = true;
                }

                //lastTelemetry = tele;
                break;
            }

            socket.BeginReceive(new AsyncCallback(OnUdpData), socket);
        }
 public void OnPacketLapData(ref PacketLapData pLap, PacketSource packetSource)
 {
 }
Exemple #15
0
 public void OnPacketLapData(ref PacketLapData pLap, PacketSource packetSource)
 {
     Delay(pLap.Header.SessionUID, pLap.Header.SessionTime, packetSource);
 }