|
|
|
@ -20,13 +20,16 @@ namespace Simulator.ViewModels |
|
|
|
|
private MovingPIDSetpointModel _setpointModel; |
|
|
|
|
|
|
|
|
|
[ObservableProperty] |
|
|
|
|
private ObservableCollection<double> _processHistory; |
|
|
|
|
private ObservableCollection<double> _processHistoryCollection; |
|
|
|
|
[ObservableProperty] |
|
|
|
|
private ObservableCollection<double> _setpointHistory; |
|
|
|
|
private ObservableCollection<double> _setpointHistoryCollection; |
|
|
|
|
|
|
|
|
|
[ObservableProperty] |
|
|
|
|
private SeriesCollection _series; |
|
|
|
|
|
|
|
|
|
private List<double> _processHistoryList; |
|
|
|
|
private List<double> _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<double>(); |
|
|
|
|
SetpointHistory = new ObservableCollection<double>(); |
|
|
|
|
_processHistoryList = new List<double>(); |
|
|
|
|
_setpointHistoryList = new List<double>(); |
|
|
|
|
|
|
|
|
|
ProcessHistoryCollection = new ObservableCollection<double>(); |
|
|
|
|
SetpointHistoryCollection = new ObservableCollection<double>(); |
|
|
|
|
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<double>(_processHistoryList); |
|
|
|
|
Series[1].Values = new ChartValues<double>(_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); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|