|
|
|
@ -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<double> _processHistory; |
|
|
|
|
[ObservableProperty] |
|
|
|
|
private ObservableCollection<double> _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<double>() |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
// 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<double>(); |
|
|
|
|
SetpointHistory = new ObservableCollection<double>(); |
|
|
|
|
Series = new SeriesCollection() |
|
|
|
|
{ |
|
|
|
|
new LineSeries() |
|
|
|
|
{ |
|
|
|
|
Title = "Actual", |
|
|
|
|
Values = new ChartValues<double>(), |
|
|
|
|
PointGeometrySize = 5 |
|
|
|
|
}, |
|
|
|
|
new LineSeries() |
|
|
|
|
{ |
|
|
|
|
Title = "Setpoint", |
|
|
|
|
Values = new ChartValues<double>(), |
|
|
|
|
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); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |