moving pid sim

main
syneffort 12 months ago
parent c78e98e11d
commit a1e7772b11
  1. 2
      AutomaticControl/Simulator/MainWindow.xaml
  2. 16
      AutomaticControl/Simulator/Models/MovingPIDSetpointModel.cs
  3. 8
      AutomaticControl/Simulator/ViewModels/FixedSetpointPIDViewModel.cs
  4. 115
      AutomaticControl/Simulator/ViewModels/MovingSetpointPIDViewModel.cs
  5. 43
      AutomaticControl/Simulator/Views/MovingSetpointPIDView.xaml
  6. 28
      AutomaticControl/Simulator/Views/MovingSetpointPIDView.xaml.cs

@ -9,6 +9,6 @@
Height="450"
mc:Ignorable="d">
<Frame NavigationUIVisibility="Hidden"
Source="/Views/FixedSetpointPIDView.xaml">
Source="/Views/MovingSetpointPIDView.xaml">
</Frame>
</Window>

@ -0,0 +1,16 @@
using AutomaticController;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Simulator.Models
{
internal class MovingPIDSetpointModel
{
public List<double> Setpoints { get; set; }
public PIDController Controller { get; set; }
}
}

@ -55,12 +55,14 @@ namespace Simulator.ViewModels
new LineSeries()
{
Title = "Actual",
Values = new ChartValues<double>()
Values = new ChartValues<double>(),
PointGeometrySize = 5
},
new LineSeries()
{
Title = "Setpoint",
Values = new ChartValues<double>()
Values = new ChartValues<double>(),
PointGeometrySize = 5
},
};
#if DEBUG
@ -97,7 +99,7 @@ namespace Simulator.ViewModels
Series[1].Values.Add(setpoint);
Series[0].Values.Add(actual);
SetpointHistory.Add(SetpointModel.Setpoint);
SetpointHistory.Add(setpoint);
ProcessHistory.Add(actual);
}
}

@ -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);
}
}
}

@ -0,0 +1,43 @@
<Page x:Class="Simulator.Views.MovingSetpointPIDView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Simulator.Views"
xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:viewModels="clr-namespace:Simulator.ViewModels"
Title="MovingSetpointPIDView"
d:DesignHeight="450"
d:DesignWidth="800"
mc:Ignorable="d">
<Page.DataContext>
<viewModels:MovingSetpointPIDViewModel />
</Page.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<lvc:CartesianChart LegendLocation="Bottom" Series="{Binding Series}"/>
<StackPanel Grid.Row="1" Orientation="Horizontal">
<Label Content="Kp" />
<TextBox Width="50"
HorizontalContentAlignment="Center" VerticalContentAlignment="Center"
Text="{Binding Kp}" />
<Label Content="Ki" />
<TextBox Width="50"
HorizontalContentAlignment="Center" VerticalContentAlignment="Center"
Text="{Binding Ki}" />
<Label Content="Kd" />
<TextBox Width="50"
HorizontalContentAlignment="Center" VerticalContentAlignment="Center"
Text="{Binding Kd}" />
<Button Margin="20,0,0,0"
Command="{Binding SimulateCommand}"
Content="Simulate" />
</StackPanel>
</Grid>
</Page>

@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Simulator.Views
{
/// <summary>
/// MovingSetpointPIDView.xaml에 대한 상호 작용 논리
/// </summary>
public partial class MovingSetpointPIDView : Page
{
public MovingSetpointPIDView()
{
InitializeComponent();
}
}
}
Loading…
Cancel
Save