diff --git a/AutomaticControl/AutomaticController/PIDController.cs b/AutomaticControl/AutomaticController/PIDController.cs index f0069d5..18c99da 100644 --- a/AutomaticControl/AutomaticController/PIDController.cs +++ b/AutomaticControl/AutomaticController/PIDController.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace AutomaticController { - public class PIDController : IAutomaticController + public class PIDController { public double Kp { get; set; } public double Ki { get; set; } @@ -25,11 +25,11 @@ namespace AutomaticController _integralError = 0; } - public double Compute(double setpoint, double processVariable) + public double Compute(double setpoint, double actualValue, double deltaTime = 0.01) { - double error = setpoint - processVariable; - _integralError += error; - double derivative = error - _prevError; + double error = setpoint - actualValue; + _integralError += error * deltaTime; + double derivative = (error - _prevError) / deltaTime; double output = Kp * error + Ki * _integralError + Kd * derivative; diff --git a/AutomaticControl/Simulator/ViewModels/FixedSetpointPIDViewModel.cs b/AutomaticControl/Simulator/ViewModels/FixedSetpointPIDViewModel.cs index aacff0f..eb1bf15 100644 --- a/AutomaticControl/Simulator/ViewModels/FixedSetpointPIDViewModel.cs +++ b/AutomaticControl/Simulator/ViewModels/FixedSetpointPIDViewModel.cs @@ -34,13 +34,13 @@ namespace Simulator.ViewModels [ObservableProperty] private double _kd; - + private readonly int SIMULATION_STEP = 200; public FixedSetpointPIDViewModel() { - Kp = 0.5; - Ki = 0.1; - Kd = 0.2; + Kp = 0.1; + Ki = 0.0; + Kd = 0.0; SetpointModel = new FixedPIDSetpointModel() { @@ -84,7 +84,7 @@ namespace Simulator.ViewModels InsertData(SetpointModel.Setpoint, 0d); - for (int i = 0; i < 100; i++) + for (int i = 0; i < SIMULATION_STEP; i++) { double current = ProcessHistory.LastOrDefault(); double output = SetpointModel.Controller.Compute(SetpointModel.Setpoint, current); diff --git a/AutomaticControl/Simulator/ViewModels/MovingSetpointPIDViewModel.cs b/AutomaticControl/Simulator/ViewModels/MovingSetpointPIDViewModel.cs index 3bf8ca1..3452f37 100644 --- a/AutomaticControl/Simulator/ViewModels/MovingSetpointPIDViewModel.cs +++ b/AutomaticControl/Simulator/ViewModels/MovingSetpointPIDViewModel.cs @@ -20,13 +20,16 @@ namespace Simulator.ViewModels private MovingPIDSetpointModel _setpointModel; [ObservableProperty] - private ObservableCollection _processHistory; + private ObservableCollection _processHistoryCollection; [ObservableProperty] - private ObservableCollection _setpointHistory; + private ObservableCollection _setpointHistoryCollection; [ObservableProperty] private SeriesCollection _series; + private List _processHistoryList; + private List _setpointHistoryList; + [ObservableProperty] private double _kp; @@ -35,14 +38,14 @@ namespace Simulator.ViewModels [ObservableProperty] private double _kd; - private readonly int SIMULATION_COUNT = 200; + private readonly int SIMULATION_STEP = 1000; public MovingSetpointPIDViewModel() { - Kp = 0.5; - Ki = 0.1; - Kd = 0.2; + Kp = 0.1; + Ki = 0.0; + Kd = 0.0; SetpointModel = new MovingPIDSetpointModel() { @@ -51,14 +54,17 @@ namespace Simulator.ViewModels }; // Generate setpoints - for (int i = 0; i < SIMULATION_COUNT; i++) + for (int i = 0; i < SIMULATION_STEP; i++) { double cos = Math.Cos(0.5 / (2 * Math.PI) * i); SetpointModel.Setpoints.Add(cos); } - ProcessHistory = new ObservableCollection(); - SetpointHistory = new ObservableCollection(); + _processHistoryList = new List(); + _setpointHistoryList = new List(); + + ProcessHistoryCollection = new ObservableCollection(); + SetpointHistoryCollection = new ObservableCollection(); Series = new SeriesCollection() { new LineSeries() @@ -86,30 +92,39 @@ namespace Simulator.ViewModels SetpointModel.Controller.Ki = Ki; SetpointModel.Controller.Kd = Kd; - ProcessHistory.Clear(); - SetpointHistory.Clear(); - Series[0].Values.Clear(); - Series[1].Values.Clear(); + _processHistoryList.Clear(); + _setpointHistoryList.Clear(); InsertData(SetpointModel.Setpoints[0], 0d); - for (int i = 1; i < SIMULATION_COUNT; i++) + for (int i = 1; i < SIMULATION_STEP; i++) { - double current = ProcessHistory.LastOrDefault(); + double current = _processHistoryList.LastOrDefault(); double output = SetpointModel.Controller.Compute(SetpointModel.Setpoints[i], current); double actual = current + output; + if (!double.IsNormal(SetpointModel.Setpoints[i]) || !double.IsNormal(actual)) + continue; + InsertData(SetpointModel.Setpoints[i], actual); } + + Series[0].Values.Clear(); + Series[1].Values.Clear(); + + Series[0].Values = new ChartValues(_processHistoryList); + Series[1].Values = new ChartValues(_setpointHistoryList); } private void InsertData(double setpoint, double actual) { - Series[1].Values.Add(setpoint); - Series[0].Values.Add(actual); + if (double.IsNaN(setpoint)) + return; + if (double.IsNaN(actual)) + return; - SetpointHistory.Add(setpoint); - ProcessHistory.Add(actual); + _setpointHistoryList.Add(setpoint); + _processHistoryList.Add(actual); } } } diff --git a/AutomaticControl/Simulator/Views/MovingSetpointPIDView.xaml b/AutomaticControl/Simulator/Views/MovingSetpointPIDView.xaml index 49ddd2a..825254e 100644 --- a/AutomaticControl/Simulator/Views/MovingSetpointPIDView.xaml +++ b/AutomaticControl/Simulator/Views/MovingSetpointPIDView.xaml @@ -19,7 +19,7 @@ - +