From a1e7772b114c147c0cf701a1cd2ccd22484a8035 Mon Sep 17 00:00:00 2001 From: syneffort Date: Tue, 14 May 2024 16:54:11 +0900 Subject: [PATCH] moving pid sim --- AutomaticControl/Simulator/MainWindow.xaml | 2 +- .../Models/MovingPIDSetpointModel.cs | 16 +++ .../ViewModels/FixedSetpointPIDViewModel.cs | 8 +- .../ViewModels/MovingSetpointPIDViewModel.cs | 115 ++++++++++++++++++ .../Views/MovingSetpointPIDView.xaml | 43 +++++++ .../Views/MovingSetpointPIDView.xaml.cs | 28 +++++ 6 files changed, 208 insertions(+), 4 deletions(-) create mode 100644 AutomaticControl/Simulator/Models/MovingPIDSetpointModel.cs create mode 100644 AutomaticControl/Simulator/ViewModels/MovingSetpointPIDViewModel.cs create mode 100644 AutomaticControl/Simulator/Views/MovingSetpointPIDView.xaml create mode 100644 AutomaticControl/Simulator/Views/MovingSetpointPIDView.xaml.cs diff --git a/AutomaticControl/Simulator/MainWindow.xaml b/AutomaticControl/Simulator/MainWindow.xaml index b70ccdd..b894f9c 100644 --- a/AutomaticControl/Simulator/MainWindow.xaml +++ b/AutomaticControl/Simulator/MainWindow.xaml @@ -9,6 +9,6 @@ Height="450" mc:Ignorable="d"> + Source="/Views/MovingSetpointPIDView.xaml"> diff --git a/AutomaticControl/Simulator/Models/MovingPIDSetpointModel.cs b/AutomaticControl/Simulator/Models/MovingPIDSetpointModel.cs new file mode 100644 index 0000000..e43f653 --- /dev/null +++ b/AutomaticControl/Simulator/Models/MovingPIDSetpointModel.cs @@ -0,0 +1,16 @@ +using AutomaticController; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Simulator.Models +{ + internal class MovingPIDSetpointModel + { + public List Setpoints { get; set; } + + public PIDController Controller { get; set; } + } +} diff --git a/AutomaticControl/Simulator/ViewModels/FixedSetpointPIDViewModel.cs b/AutomaticControl/Simulator/ViewModels/FixedSetpointPIDViewModel.cs index 61dbd94..aacff0f 100644 --- a/AutomaticControl/Simulator/ViewModels/FixedSetpointPIDViewModel.cs +++ b/AutomaticControl/Simulator/ViewModels/FixedSetpointPIDViewModel.cs @@ -55,12 +55,14 @@ namespace Simulator.ViewModels new LineSeries() { Title = "Actual", - Values = new ChartValues() + Values = new ChartValues(), + PointGeometrySize = 5 }, new LineSeries() { Title = "Setpoint", - Values = new ChartValues() + Values = new ChartValues(), + PointGeometrySize = 5 }, }; #if DEBUG @@ -97,7 +99,7 @@ namespace Simulator.ViewModels Series[1].Values.Add(setpoint); Series[0].Values.Add(actual); - SetpointHistory.Add(SetpointModel.Setpoint); + SetpointHistory.Add(setpoint); ProcessHistory.Add(actual); } } diff --git a/AutomaticControl/Simulator/ViewModels/MovingSetpointPIDViewModel.cs b/AutomaticControl/Simulator/ViewModels/MovingSetpointPIDViewModel.cs new file mode 100644 index 0000000..3bf8ca1 --- /dev/null +++ b/AutomaticControl/Simulator/ViewModels/MovingSetpointPIDViewModel.cs @@ -0,0 +1,115 @@ +using AutomaticController; +using CommunityToolkit.Mvvm.ComponentModel; +using CommunityToolkit.Mvvm.Input; +using LiveCharts; +using LiveCharts.Wpf; +using Simulator.Models; +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Linq; +using System.Runtime.Intrinsics.X86; +using System.Text; +using System.Threading.Tasks; + +namespace Simulator.ViewModels +{ + internal partial class MovingSetpointPIDViewModel : ObservableObject + { + [ObservableProperty] + private MovingPIDSetpointModel _setpointModel; + + [ObservableProperty] + private ObservableCollection _processHistory; + [ObservableProperty] + private ObservableCollection _setpointHistory; + + [ObservableProperty] + private SeriesCollection _series; + + + [ObservableProperty] + private double _kp; + [ObservableProperty] + private double _ki; + [ObservableProperty] + private double _kd; + + private readonly int SIMULATION_COUNT = 200; + + + public MovingSetpointPIDViewModel() + { + Kp = 0.5; + Ki = 0.1; + Kd = 0.2; + + SetpointModel = new MovingPIDSetpointModel() + { + Controller = new PIDController(Kp, Kd, Ki), + Setpoints = new List() + }; + + // Generate setpoints + for (int i = 0; i < SIMULATION_COUNT; i++) + { + double cos = Math.Cos(0.5 / (2 * Math.PI) * i); + SetpointModel.Setpoints.Add(cos); + } + + ProcessHistory = new ObservableCollection(); + SetpointHistory = new ObservableCollection(); + Series = new SeriesCollection() + { + new LineSeries() + { + Title = "Actual", + Values = new ChartValues(), + PointGeometrySize = 5 + }, + new LineSeries() + { + Title = "Setpoint", + Values = new ChartValues(), + PointGeometrySize = 5 + }, + }; +#if DEBUG + Simulate(); +#endif + } + + [RelayCommand] + public void Simulate() + { + SetpointModel.Controller.Kp = Kp; + SetpointModel.Controller.Ki = Ki; + SetpointModel.Controller.Kd = Kd; + + ProcessHistory.Clear(); + SetpointHistory.Clear(); + Series[0].Values.Clear(); + Series[1].Values.Clear(); + + InsertData(SetpointModel.Setpoints[0], 0d); + + for (int i = 1; i < SIMULATION_COUNT; i++) + { + double current = ProcessHistory.LastOrDefault(); + double output = SetpointModel.Controller.Compute(SetpointModel.Setpoints[i], current); + double actual = current + output; + + InsertData(SetpointModel.Setpoints[i], actual); + } + } + + private void InsertData(double setpoint, double actual) + { + Series[1].Values.Add(setpoint); + Series[0].Values.Add(actual); + + SetpointHistory.Add(setpoint); + ProcessHistory.Add(actual); + } + } +} diff --git a/AutomaticControl/Simulator/Views/MovingSetpointPIDView.xaml b/AutomaticControl/Simulator/Views/MovingSetpointPIDView.xaml new file mode 100644 index 0000000..49ddd2a --- /dev/null +++ b/AutomaticControl/Simulator/Views/MovingSetpointPIDView.xaml @@ -0,0 +1,43 @@ + + + + + + + + + + + + +