private void btnSend_Click(object sender, EventArgs e) { ControlUpdate update = new ControlUpdate(); //Creates new instance of ControlUpdate update.Throttle = Convert.ToDouble(trkThrottle.Value); //Defines the Throttle of Control Update with the track bar value update.ElevatorPitch = Convert.ToDouble(trkPitch.Value); //Defines the pitch of control update with the track bar value eventControls?.Invoke(update); //Invokes the eventControls events using the assigned delegate sends the update instance to SendData }
private void SendData(ControlUpdate updates) { //Serialises the contents of updates to JSON format JavaScriptSerializer Serializer = new JavaScriptSerializer(); string jsonString = Serializer.Serialize(updates); NetworkStream stream = m_client.GetStream(); byte[] rawData = Encoding.ASCII.GetBytes(jsonString); lblJsonSent.Text = jsonString; //Redefines the text inside lblJsonSent with the new message stream.Write(rawData, 0, rawData.Length); //Writes this message to the stream for the server to pick up }
public Form1() { InitializeComponent(); //Assign events with their repective delegates and functions eventControls += new ControlsUpdated(SendData); eventTelemetry += new TelemetryUpdated(RecievedMessage); eventWarnings += new WarningUpdated(UpdateWarnings); TU = new TelemetryUpdate(); CU = new ControlUpdate(); }
private void updateFormControls(ControlUpdate CU) { if (this.InvokeRequired) //Ensure that the delegate has been invoked before use { this.Invoke(new ControlsUpdated(updateFormControls), new object[] { CU }); } else { trkThrottle.Value = Convert.ToInt32(CU.Throttle); //Update the status of the throttle track bar trkPitch.Value = Convert.ToInt32(CU.ElevatorPitch); //Update the status of the pitch track bar lblThrottleControl.Text = CU.Throttle.ToString(); //Update the throrttle label lblPitchControl.Text = CU.ElevatorPitch.ToString(); //Update the pitch label eventControls.Invoke(CU); //Invoek the eventControls event, this event ends with the new data being sent to the server } }
private void AutomaticControls() //the auto pilot operates in a different thread { double targetSpeed = 0; double targetAltitude = 0; if (txtTargetSpeeds.Text == "") { targetSpeed = Convert.ToDouble(lblSpeed.Text); //if no targer speed is entered the plane will keep at the same speed } else { targetSpeed = Convert.ToDouble(txtTargetSpeeds.Text); } //Else the speed in the correct text box is set to the new target speed if (txtTargetAltitude.Text == "") { targetAltitude = Convert.ToDouble(lblAltitude.Text);//if no targer altitude is entered the plane will keep at the same altitude } else { targetAltitude = Convert.ToDouble(txtTargetAltitude.Text); } //Else the altitude in the correct text box is set to the new target altitude while (true) //Infinite loop, this loop is killed when the thread is closed { //Defines each variable with the correct values double labelSpeed = Convert.ToDouble(lblSpeed.Text); double labelAltidude = Convert.ToDouble(lblAltitude.Text); double labelPitch = Convert.ToDouble(lblPitch.Text); double labelThrottle = Convert.ToDouble(lblThrottle.Text); double labelElevatorPitch = Convert.ToDouble(lblElevatorPitch.Text); ControlUpdate CU = new ControlUpdate(); //Defines a new instance of ControlUpdate //If the plane is going too slow if (targetSpeed > labelSpeed) { if ((labelThrottle + 10) <= 100) { CU.Throttle = labelThrottle + 10; //Increase throttle by 10 } else { CU.Throttle = 100; //Ensures the thottle cannot go over 100 } } //If the plane is going too fast if (targetSpeed < labelSpeed) { if ((labelThrottle - 10) >= 0) { CU.Throttle = labelThrottle - 10; //Decrease throttle by 10 } else { CU.Throttle = 0; //Ensures the throttle cannot go below 0 } } if (targetSpeed == labelSpeed) //If at desired speed throttle is 0 { CU.Throttle = 0; } //The next checks determine the plane altitude //The plane cannot exceed 20 degrees in pitch //The plane cannot go below -5 degrees in pitch //The elevatorsa cannot exceed setting 2 //The elevators cannot go lower than -1 //These protects are placed so the plane remains stable //Checks if plane is currently lower altitude that target altitude if (targetAltitude < labelAltidude) { if (labelPitch < -5) //If plane pitch degrees is higher than -5 degrees { if (labelElevatorPitch < 2) //If plane elevators are lower than 2 { CU.ElevatorPitch = labelElevatorPitch + 1; //Increase elevators CU.Throttle = 50; //Increase speed to pull out of nose dive } else { CU.ElevatorPitch = 2; //Else set elevators to 2 } } else { if (labelElevatorPitch > -1) //If elevators are not set below -1 { CU.ElevatorPitch = labelElevatorPitch - 1; // Lower elevators } else { CU.ElevatorPitch = -1; //Set elevators to -1 } } } //If plane is above target altitude if (targetAltitude > labelAltidude) { if (labelPitch > 20) //if angle of attack is greater than 20 { if (labelElevatorPitch > -1) //Ensuring no more than -1 elevator control { CU.ElevatorPitch = labelElevatorPitch - 1; //Decrease elevators } else { CU.ElevatorPitch = -1; } } else { if (labelElevatorPitch < 2) //If elevators are not above 2 { CU.ElevatorPitch = labelElevatorPitch + 1; //Increase elvators } else { CU.ElevatorPitch = 2; //Set elevators to 2 } } } //Send new ControlUpdate to updateformcontrols to be send the data to the server updateFormControls(CU); Thread.Sleep(100); //Sleeps the thread } }