You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
104 lines
2.8 KiB
104 lines
2.8 KiB
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.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Simulator.ViewModels
|
|
{
|
|
internal partial class FixedSetpointPIDViewModel : ObservableObject
|
|
{
|
|
[ObservableProperty]
|
|
private FixedPIDSetpointModel _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;
|
|
|
|
|
|
|
|
public FixedSetpointPIDViewModel()
|
|
{
|
|
Kp = 0.5;
|
|
Ki = 0.1;
|
|
Kd = 0.2;
|
|
|
|
SetpointModel = new FixedPIDSetpointModel()
|
|
{
|
|
Setpoint = 50,
|
|
Controller = new PIDController(Kp, Kd, Ki)
|
|
};
|
|
|
|
ProcessHistory = new ObservableCollection<double>();
|
|
SetpointHistory = new ObservableCollection<double>();
|
|
Series = new SeriesCollection()
|
|
{
|
|
new LineSeries()
|
|
{
|
|
Title = "Actual",
|
|
Values = new ChartValues<double>()
|
|
},
|
|
new LineSeries()
|
|
{
|
|
Title = "Setpoint",
|
|
Values = new ChartValues<double>()
|
|
},
|
|
};
|
|
#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.Setpoint, 0d);
|
|
|
|
for (int i = 0; i < 100; i++)
|
|
{
|
|
double current = ProcessHistory.LastOrDefault();
|
|
double output = SetpointModel.Controller.Compute(SetpointModel.Setpoint, current);
|
|
double actual = current + output;
|
|
|
|
InsertData(SetpointModel.Setpoint, actual);
|
|
}
|
|
}
|
|
|
|
private void InsertData(double setpoint, double actual)
|
|
{
|
|
Series[1].Values.Add(setpoint);
|
|
Series[0].Values.Add(actual);
|
|
|
|
SetpointHistory.Add(SetpointModel.Setpoint);
|
|
ProcessHistory.Add(actual);
|
|
}
|
|
}
|
|
}
|
|
|