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.

106 lines
2.9 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;
private readonly int SIMULATION_STEP = 200;
public FixedSetpointPIDViewModel()
{
Kp = 0.1;
Ki = 0.0;
Kd = 0.0;
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>(),
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.Setpoint, 0d);
for (int i = 0; i < SIMULATION_STEP; 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(setpoint);
ProcessHistory.Add(actual);
}
}
}