private async void PrintData() { // Set True _isPrinting = true; try { // Create CSV String of Yaw Readings string yawReadingResults = string.Join(System.Environment.NewLine, ObservedYawDataPoints.Select(s => $"{s.X},{s.Y}")?.ToList() ?? new List <string>()); string rollReadingResults = string.Join(System.Environment.NewLine, ObservedRollDataPoints.Select(s => $"{s.X},{s.Y}")?.ToList() ?? new List <string>()); string pitchReadingResults = string.Join(System.Environment.NewLine, ObservedPitchDataPoints.Select(s => $"{s.X},{s.Y}")?.ToList() ?? new List <string>()); // Get File Path StorageFolder storageFolder = ApplicationData.Current.LocalFolder; StorageFile yawFilePath = await storageFolder.CreateFileAsync($"yaw_{DateTime.Now.ToString("yyyyMMddHHmmss")}.csv", CreationCollisionOption.ReplaceExisting); StorageFile rollFilePath = await storageFolder.CreateFileAsync($"roll_{DateTime.Now.ToString("yyyyMMddHHmmss")}.csv", CreationCollisionOption.ReplaceExisting); StorageFile pitchFilePath = await storageFolder.CreateFileAsync($"pitch_{DateTime.Now.ToString("yyyyMMddHHmmss")}.csv", CreationCollisionOption.ReplaceExisting); // Write all Data to CSV File await FileIO.WriteTextAsync(yawFilePath, yawReadingResults); await FileIO.WriteTextAsync(rollFilePath, rollReadingResults); await FileIO.WriteTextAsync(pitchFilePath, pitchReadingResults); } catch (Exception ex) { Console.WriteLine($"Failed to Print beause of '{ex.Message}'"); } // Clear Printing Data Flag _isPrinting = false; }
private void ProcessCommand(GamepadReading reading) { // Check if Exit has been Demanded CommandedData.Exit = (reading.Buttons.HasFlag(GamepadButtons.Y) || (CommandedData.Exit)); // Get Left Thumb Stick Values LeftThumbStickX = reading.LeftThumbstickX; LeftThumbStickY = reading.LeftThumbstickY; // Get Right Thumb Stick Values RightThumbStickX = reading.RightThumbstickX; RightThumbStickY = reading.RightThumbstickY; // Get Trigger Values LeftTriggerValue = reading.LeftTrigger; RightTriggerValue = reading.RightTrigger; // Update Commanded Flight Data CommandedData.Yaw = LeftThumbStickX; CommandedData.Throttle = LeftThumbStickY; CommandedData.Roll = RightThumbStickX; CommandedData.Pitch = RightThumbStickY; // Disarm is B is Pressed and it is currently Armed if (reading.Buttons.HasFlag(GamepadButtons.B) && CommandedArmed) { CommandedArmed = false; } // Otherwise arm if A is Pressed and it is not currently armed else if (reading.Buttons.HasFlag(GamepadButtons.A) && !CommandedArmed) { CommandedArmed = true; } // Perform error reset if X is Pressed CommandedData.ResetError = (reading.Buttons.HasFlag(GamepadButtons.X) || (CommandedData.ResetError)); // Check if D-PAD Up / Down / Left / Right Selected if (reading.Buttons.HasFlag(GamepadButtons.DPadUp)) { CommandedData.ThrottleTrim += 0.01; } else if (reading.Buttons.HasFlag(GamepadButtons.DPadDown)) { CommandedData.ThrottleTrim -= 0.01; } else if (reading.Buttons.HasFlag(GamepadButtons.DPadLeft)) { CommandedData.YawTrim -= 0.01; } else if (reading.Buttons.HasFlag(GamepadButtons.DPadRight)) { CommandedData.YawTrim += 0.01; } if (reading.Buttons.HasFlag(GamepadButtons.LeftShoulder)) { CommandedData.RollTrim -= 0.01; } else if (reading.Buttons.HasFlag(GamepadButtons.RightShoulder)) { CommandedData.RollTrim += 0.01; } else if (LeftTriggerValue > 0) { CommandedData.PitchTrim -= 0.01; } else if (RightTriggerValue > 0) { CommandedData.PitchTrim += 0.01; } // Accumulate time _timeSinceLastChartUpdateMs += _gatherMessageFrequencyMs; _timeSinceLastServerUpdateMs += _gatherMessageFrequencyMs; // Check if enough time has accumulated to post new data to Data Server if (_timeSinceLastServerUpdateMs > _frequencyToUpdateServerWithNewFlighDataMs) { // Reset Gather Time _timeSinceLastServerUpdateMs = 0; // Update Flight Data ObservedData = _myRestClient.Update(_observedData, _commandedData).Result; // Check if Observed Error Reset if (ObservedData.ResetError) { // Clear Command for Reset Error CommandedData.ResetError = false; // Clear Observed for Reset Error ObservedData.ResetError = false; } } // Notify Update for (int i = 0; i < FlightStateDataViewModels.Count; i++) { // Update Reference FlightStateDataViewModels[i].ObservedData = ObservedData; // Notify of Update FlightStateDataViewModels[i].NotifyValuesChanged(); } // Check if enough time has accumlated to post a new data point to chart if (_timeSinceLastChartUpdateMs > _frequencyOfChartUpdateMs) { // Reset Gather Time _timeSinceLastChartUpdateMs = 0; // Add Data Points if (_numberOfPointsAddedToChart > _maxNumberOfPoints) { // Remove First Commanded CommandedYawDataPoints.RemoveAt(0); CommandedThrottleDataPoints.RemoveAt(0); CommandedRollDataPoints.RemoveAt(0); CommandedPitchDataPoints.RemoveAt(0); // Remove First Observed ObservedYawDataPoints.RemoveAt(0); ObservedThrottleDataPoints.RemoveAt(0); ObservedRollDataPoints.RemoveAt(0); ObservedPitchDataPoints.RemoveAt(0); } // Add New Commanded CommandedYawDataPoints.Add(new DataPoint() { Y = CommandedData.Yaw }); CommandedThrottleDataPoints.Add(new DataPoint() { Y = CommandedData.Throttle }); CommandedRollDataPoints.Add(new DataPoint() { Y = CommandedData.Roll }); CommandedPitchDataPoints.Add(new DataPoint() { Y = CommandedData.Pitch }); // Add New Observed ObservedYawDataPoints.Add(new DataPoint() { Y = ObservedData.Yaw }); ObservedThrottleDataPoints.Add(new DataPoint() { Y = ObservedData.Throttle }); ObservedRollDataPoints.Add(new DataPoint() { Y = ObservedData.Roll }); ObservedPitchDataPoints.Add(new DataPoint() { Y = ObservedData.Pitch }); // Increment the number of Points added to Chart _numberOfPointsAddedToChart++; } // Use Start Button to Reset Chart if (reading.Buttons.HasFlag(GamepadButtons.Menu)) { // Reset Number of Points Added _numberOfPointsAddedToChart = 0; // Reset All Commanded CommandedYawDataPoints.Clear(); CommandedRollDataPoints.Clear(); CommandedPitchDataPoints.Clear(); CommandedThrottleDataPoints.Clear(); // Reset All Observed ObservedYawDataPoints.Clear(); ObservedRollDataPoints.Clear(); ObservedPitchDataPoints.Clear(); ObservedThrottleDataPoints.Clear(); } // Use View Button Print all Recorded values to CSV File if (reading.Buttons.HasFlag(GamepadButtons.View) && (!_isPrinting)) { // Print data PrintData(); } }