Esempio n. 1
0
        private void showPlaneStats(TelemetryUpdate telemetryUpdate)
        {
            if (this.InvokeRequired)//to check if it is on the same thread still
            {
                this.Invoke(new ReceivedDataDelegate(showPlaneStats), new object[] { telemetryUpdate });
            }
            else
            {
                //keeping the textboxes up to date with the stats of the plane
                txtAltitude.Text      = Math.Round(telemetryUpdate.Altitude).ToString() + " ft";
                txtAirSpeed.Text      = Math.Round(telemetryUpdate.Speed).ToString() + " knots";
                txtVerticleSpeed.Text = Math.Round(telemetryUpdate.VerticalSpeed).ToString() + " fpm";
                txtThrottle.Text      = Math.Round(telemetryUpdate.Throttle).ToString() + " %";
                txtPitchAngle.Text    = Math.Round(telemetryUpdate.Pitch).ToString() + "°";
                txtElevatorPitch.Text = Math.Round(telemetryUpdate.ElevatorPitch).ToString() + "°";

                //showing it in the table
                DataGridViewRow row = (DataGridViewRow)dgvreceiverDataTable.Rows[0].Clone();
                row.Cells[0].Value = telemetryUpdate.Speed.ToString();
                row.Cells[1].Value = telemetryUpdate.VerticalSpeed.ToString();
                row.Cells[2].Value = telemetryUpdate.Pitch.ToString();
                row.Cells[3].Value = telemetryUpdate.Altitude.ToString();
                row.Cells[4].Value = telemetryUpdate.Throttle.ToString();
                row.Cells[5].Value = telemetryUpdate.ElevatorPitch.ToString();
                row.Cells[6].Value = telemetryUpdate.WarningCode.ToString();

                dgvreceiverDataTable.Rows.Insert(0, row);//inserting to what we have set above
            }
        }
Esempio n. 2
0
            public void ReceiveData()
            {
                TelemetryUpdate telemetryUpdate = new TelemetryUpdate();//this makes sure that the data is cleared from the structs

                bool Retrieve = true;

                while (Retrieve)//endless loop so we always try to retrieve data
                {
                    byte[] data          = new byte[256];
                    int    numberOfBytes = networkStream.Read(data, 0, 256);                                                      //the data variable is an array to store the data that is being received

                    JavaScriptSerializer serializer = new JavaScriptSerializer();                                                 //this is the way we start serializer
                    telemetryUpdate = serializer.Deserialize <TelemetryUpdate>(Encoding.ASCII.GetString(data, 0, numberOfBytes)); //telemetryUpdate will be populated by decoding the numberOfByte

                    if (telemetryUpdate.WarningCode == 1)
                    {
                        warningEvent?.Invoke("Too low terrain (lower than 1000 ft)!");//passing on the proper message to the warning label
                    }
                    else if (telemetryUpdate.WarningCode == 2)
                    {
                        warningEvent?.Invoke("Stalling!");//passing on the proper message to the warning label
                    }
                    else if (telemetryUpdate.WarningCode == 0)
                    {
                        warningEvent?.Invoke("No warnings");//passing on the proper message to the warning label
                    }

                    receivingEvent?.Invoke(telemetryUpdate); //so we see what we are receiving
                    AutoPilotEvent?.Invoke(telemetryUpdate); //this is placed in here so we can always update the throttle and elevator pitch angle when activated by the button
                }
            }
        /// <summary>
        /// Will take data coming in from the simulator and will append it to dgvTelemetryUpdates.
        /// </summary>
        /// <param name="telemetryData">TelemetryUpdate struct that gets passed through RecieveDataFromSimulator()</param>
        void OnDataRecieveHandler(TelemetryUpdate telemetryData)
        {
            // Check if the delegate was invoked, and then invoke it.
            if (InvokeRequired)
            {
                Invoke(new DataRecieve(OnDataRecieveHandler), new object[] { telemetryData });
            }
            else
            {
                // Check if writing is enabled and start writing to a file.
                if (chkEnableWriting.Checked)
                {
                    string telemetryUpdateString = "Altidude: " + telemetryData.altitude +
                                                   " | Speed: " + telemetryData.speed +
                                                   " | Pitch: " + telemetryData.pitch +
                                                   " | Vertical Speed: " + telemetryData.verticalSpeed +
                                                   " | Throttle: " + telemetryData.throttle +
                                                   " | Elevator Pitch: " + telemetryData.elevatorPitch + Environment.NewLine;

                    WriteDataToFile(telemetryUpdateString);
                }

                // Get the DataGridView.
                DataGridViewRow dgvRow = (DataGridViewRow)dgvTelemetryUpdates.Rows[0].Clone();

                // Append each row with data whilst rounding values and formatting data with units.
                dgvRow.Cells[0].Value = Math.Round(telemetryData.altitude, 2).ToString() + " ft";
                dgvRow.Cells[1].Value = Math.Round(telemetryData.speed, 2).ToString() + " kt";
                dgvRow.Cells[2].Value = Math.Round(telemetryData.pitch, 2).ToString() + "°";
                dgvRow.Cells[3].Value = Math.Round(telemetryData.verticalSpeed, 2).ToString() + " fpm";
                dgvRow.Cells[4].Value = Math.Round(telemetryData.throttle).ToString() + "%";
                dgvRow.Cells[5].Value = Math.Round(telemetryData.elevatorPitch, 1).ToString() + "°";
                dgvRow.Cells[6].Value = telemetryData.warningCode.ToString();

                // Insert the row into the grid.
                dgvTelemetryUpdates.Rows.Insert(0, dgvRow);

                // Replace the last row if the grid is getting too large.
                if (dgvTelemetryUpdates.Rows.Count > 9)
                {
                    dgvTelemetryUpdates.Rows.RemoveAt(8);
                }
            }
        }
        /// <summary>
        /// Will open a NetworkStream and read data coming in. This method will then
        /// deserialize the incoming JSON data and Invoke the DataRecieve event which
        /// will append the now populated TelemetryUpdate data to dgvTelemetryUpdates.
        ///
        /// Additionally, it will also Invoke the WarningRecieve event which will only
        /// occur when the WarningCode recieved from the simulator is greater than 0.
        /// </summary>
        public void RecieveDataFromSimulator()
        {
            Thread.Sleep(100);
            // Create an empty TelemetryUpdate struct.
            TelemetryUpdate telemetryData = new TelemetryUpdate();
            // Get the incoming stream from the NetworkClient.
            NetworkStream        networkStream = networkClient.GetStream();
            JavaScriptSerializer serializer    = new JavaScriptSerializer();

            // Continously recieve data.
            while (true)
            {
                // Read the incoming data from NetworkStream into a byte buffer, then encode it as a string.
                byte[] incomingDataBuffer = new byte[256];
                int    incomingLength     = networkStream.Read(incomingDataBuffer, 0, 256);
                string recievedData       = Encoding.ASCII.GetString(incomingDataBuffer, 0, incomingLength);

                // Deserialize the JSON data and then, using reflection, assign each value in the TelemetryUpdate struct
                // to the values recieved from the simulator.
                telemetryData = serializer.Deserialize <TelemetryUpdate>(recievedData);

                // Check if warning codes are greater than 0 and update the warning controls accordingly.
                switch (telemetryData.warningCode)
                {
                case 1:
                    OnWarningRecieve?.Invoke("Warning: Flying too low.");
                    break;

                case 2:
                    OnWarningRecieve?.Invoke("Warning: You are at stall risk.");
                    break;

                default:
                    OnWarningRecieve?.Invoke("No Warning Recieved.");
                    break;
                }

                // Invoke the data recieve event.
                OnDataRecieve?.Invoke(telemetryData);
            }
        }
Esempio n. 5
0
        private void RetrieveNewMessage(TelemetryUpdate telemetryUpdate) // Used to retrieve the messages and store them in the below locations (telemetryUpdate is also passed in here).
        {
            // Checks if the thread is on the correct thread, if not and invoke is require, invoke this method is called on correct thread.
            if (this.InvokeRequired)
            {
                this.Invoke(new IncomingMessage(RetrieveNewMessage), new object[] { telemetryUpdate }); // Calls invoke on the current function. Delegate targets method to invoke, taking one arguemnt telemetryUpdate.
            }
            else
            {
                // The following takes the contents of telemetryUpdate and stores them in to the relvant text boxes for display.
                //extract each field from telemetry update into spearate text boxes.
                txtAirSpeed.Text      = telemetryUpdate.Speed.ToString();
                txtVerticalSpeed.Text = telemetryUpdate.VerticalSpeed.ToString();
                txtPitchAngle.Text    = telemetryUpdate.Pitch.ToString();
                txtAltitude.Text      = telemetryUpdate.Altitude.ToString();
                txtThrottle.Text      = telemetryUpdate.Throttle.ToString();
                txtElevatorPitch.Text = telemetryUpdate.ElevatorPitch.ToString();


                int             rowId = dgvTelemetryInfo.Rows.Add(); // Adds a row everytime a new entry is retrieved.
                DataGridViewRow row   = dgvTelemetryInfo.Rows[rowId];
                // Each Field of telemetryUpdate is extracted and stored in the following cells.
                row.Cells[0].Value = telemetryUpdate.Altitude.ToString();
                row.Cells[1].Value = telemetryUpdate.Speed.ToString();
                row.Cells[2].Value = telemetryUpdate.Pitch.ToString();
                row.Cells[3].Value = telemetryUpdate.VerticalSpeed.ToString();
                row.Cells[4].Value = telemetryUpdate.Throttle.ToString();
                row.Cells[5].Value = telemetryUpdate.ElevatorPitch.ToString();
                row.Cells[6].Value = telemetryUpdate.WarningCode.ToString();
                // The follow condition allows the data grid view to update its field respectively to the currents status of the telemtry data and the fixed length of the data grid view.
                if (dgvTelemetryInfo.Rows[dgvTelemetryInfo.Rows.Count - 1].Displayed == true)
                {
                    dgvTelemetryInfo.FirstDisplayedScrollingRowIndex = dgvTelemetryInfo.Rows.Count - 1; // Displays the first row of the data grid view permantly. However, if the rows go beyond the length of the data grid view
                                                                                                        // That data won't be avaliable to view.
                    dgvTelemetryInfo.SelectionMode = DataGridViewSelectionMode.FullRowSelect;           // Fixes the above problem, the rows are limited to the length of the data grid view.
                    // Both first row and last row are within the length of the data grid view and any updates to the field are viewable.
                }
            }
        }
Esempio n. 6
0
            private bool generate_Message = false;                       // This flag is used to indicate if StartRetrievingMessages() has been invoked.

            public void StartRetrievingMessages()                        // This method starts reading messages as long as the condition of the while loop is met.
            {
                TelemetryUpdate telemetryUpdate = new TelemetryUpdate(); // New object of TelemetryUpdate is created.

                generate_Message = true;

                while (generate_Message)
                {
                    byte[] sendBuffer = new byte[256];                                   // Creates a buffer to write into.
                    int    num_bytes  = stream.Read(sendBuffer, 0, 256);                 // This handles the number of bytes recieved and the maximum number of bytes willing to be accepted.
                                                                                         // stream.Read will block the current thread until data is recieved.
                    string message = Encoding.ASCII.GetString(sendBuffer, 0, num_bytes); // Decodes a sequence of bytes from the specifed byte array into a string.
                    JavaScriptSerializer Serializer = new JavaScriptSerializer();        // Provides serialisation functionality and initalizes a new instance. Additonally, Allows for data to be Serialized and Deserialized.
                    telemetryUpdate = Serializer.Deserialize <TelemetryUpdate>(message); // Converts string message to a object of type T and is passed into telemetryUpdate. The data is Deserialized.

                    OnNewRetrievedMessage?.Invoke(telemetryUpdate);                      // The OnNewRetrievedMessage event is handled here. This will only invoke the event if the event(telemetryUpdate) is not null. ? = if not null then execute.


                    // The OnNewWarning event is handled in this next section. If the warning code is equal to any of the following the
                    // event will be invoked and the following message will be displayed to the respective labels/ text boxes in RetrieveNewMessage method.
                    if (telemetryUpdate.WarningCode == 0)
                    {
                        OnNewWarning?.Invoke("");
                    }
                    else if (telemetryUpdate.WarningCode == 1)
                    {
                        OnNewWarning?.Invoke("Warning Level: 1 - Plane too Low, increase Elevator Pitch! (less than 1000ft!)");
                    }
                    else if (telemetryUpdate.WarningCode == 2)
                    {
                        OnNewWarning?.Invoke("Warning Level: 2 - Stall Risk! lower elevator pitch and increase throttle!");
                    }
                    else //This else Conditon will never execute.
                    {
                        OnNewWarning?.Invoke("Plane Crash! fighlt simulator over!");
                    }
                }
            }
Esempio n. 7
0
        private void AutoPilotfunction(TelemetryUpdate telemetryUpdate)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new AutoPilotDelegate(AutoPilotfunction), new object[] { telemetryUpdate });
            }
            else
            {
                //--------------------------------
                //the throttle changes
                if (telemetryUpdate.Speed <= 250)
                {
                    if (trkbThrottle.Value != 100)
                    {
                        trkbThrottle.Value = 100;
                    }
                }
                else if (telemetryUpdate.Speed >= 300)
                {
                    if (trkbThrottle.Value != 30)
                    {
                        trkbThrottle.Value = 30;
                    }
                }
                else
                {
                    if (trkbThrottle.Value != 60)
                    {
                        trkbThrottle.Value = 60;
                    }
                }
                //--------------------------------

                //--------------------------------
                //the elevator pitch angle changes
                if (telemetryUpdate.Altitude < 1000)
                {
                    if (trkbElevatorLevel.Value != 50)
                    {
                        trkbElevatorLevel.Value = 50;
                    }
                }
                else if (telemetryUpdate.Altitude < 10000)
                {
                    if (trkbElevatorLevel.Value != 20)
                    {
                        trkbElevatorLevel.Value = 20;
                    }
                }
                else if (telemetryUpdate.Altitude >= 10000 && telemetryUpdate.Altitude <= 12000)
                {
                    if (trkbElevatorLevel.Value != 15)
                    {
                        trkbElevatorLevel.Value = 15;
                    }
                }
                if (telemetryUpdate.Altitude >= 13000 && telemetryUpdate.Altitude <= 15000)
                {
                    if (trkbElevatorLevel.Value != 10)
                    {
                        trkbElevatorLevel.Value = 10;
                    }
                }
                //--------------------------------
            }
        }