-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServoPidModel.cs
More file actions
236 lines (206 loc) · 6.35 KB
/
Copy pathServoPidModel.cs
File metadata and controls
236 lines (206 loc) · 6.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using JetBrains.Annotations;
namespace ServoPIDControl.Model
{
public class ServoPidModel : INotifyPropertyChanged
{
public const float TimeSeriesLengthSec = 10.0f;
private float _p;
private float _i;
private float _d;
private float _dLambda = 1;
private float _setPoint;
private float _InputMin = 1;
private float _InputMax;
private float _input;
private float _output;
private float _integrator;
private float _dFiltered;
private readonly object _timeSeriesLock = new object();
public ServoPidModel(int id)
{
Id = id;
#if DEBUG && false
lock (_timeSeriesLock)
{
const int ts = 500;
Times = Enumerable.Range(0, ts)
.Select(i => i / 100.0f).ToList();
SetPoints = Enumerable.Range(id * 100, ts)
.Select(i => i / 100 % 2 == 0 ? 80.0f : 100.0f).ToList();
Inputs = Enumerable.Range(id * 100, ts)
.Select(i => 90 + (float) Math.Cos(i / 20.0f) * 9).ToList();
Outputs = Enumerable.Range(id * 100, ts)
.Select(i => 90 + (float) Math.Sin(i / 20.0f) * 5).ToList();
}
#endif
}
public int Id { get; }
public float P
{
get => _p;
set
{
if (value.Equals(_p)) return;
_p = value;
OnPropertyChanged();
}
}
public float I
{
get => _i;
set
{
if (value.Equals(_i)) return;
_i = value;
OnPropertyChanged();
}
}
public float D
{
get => _d;
set
{
if (value.Equals(_d)) return;
_d = value;
OnPropertyChanged();
}
}
public float DLambda
{
get => _dLambda;
set
{
if (value.Equals(_dLambda)) return;
_dLambda = value;
OnPropertyChanged();
}
}
public float SetPoint
{
get => _setPoint;
set
{
if (value.Equals(_setPoint)) return;
_setPoint = value;
OnPropertyChanged();
}
}
public float InputMin
{
get => _InputMin;
set
{
if (value.Equals(_InputMin)) return;
_InputMin = value;
OnPropertyChanged();
}
}
public float InputMax
{
get => _InputMax;
set
{
if (value.Equals(_InputMax)) return;
_InputMax = value;
OnPropertyChanged();
}
}
public float Input
{
get => _input;
internal set
{
if (value.Equals(_input)) return;
_input = value;
OnPropertyChanged();
}
}
public float Output
{
get => _output;
internal set
{
if (value.Equals(_output)) return;
_output = value;
OnPropertyChanged();
}
}
public float Integrator
{
get => _integrator;
internal set
{
if (value.Equals(_integrator)) return;
_integrator = value;
OnPropertyChanged();
}
}
public float DFiltered
{
get => _dFiltered;
internal set
{
if (value.Equals(_dFiltered)) return;
_dFiltered = value;
OnPropertyChanged();
}
}
// ReSharper disable MemberInitializerValueIgnored
public List<float> Times { get; } = new List<float>();
public List<float> SetPoints { get; } = new List<float>();
public List<float> Inputs { get; } = new List<float>();
public List<float> Outputs { get; } = new List<float>();
// ReSharper restore MemberInitializerValueIgnored
internal class TimeSeries
{
public float[] X { get; set; }
public float[] Y { get; set; }
public string Name { get; set; }
}
internal IEnumerable<TimeSeries> AllTimeSeries
{
get
{
lock (_timeSeriesLock)
{
if (!Times.Any())
yield break;
var timeArray = Times.ToArray();
yield return new TimeSeries {X = timeArray, Y = SetPoints.ToArray(), Name = "SetPoint"};
yield return new TimeSeries {X = timeArray, Y = Inputs.ToArray(), Name = "Input"};
yield return new TimeSeries {X = timeArray, Y = Outputs.ToArray(), Name = "Output"};
}
}
}
public event EventHandler TimePointRecorded;
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public void RecordTimePoint(float elapsedSeconds)
{
lock (_timeSeriesLock)
{
Times.Add(elapsedSeconds);
SetPoints.Add(SetPoint);
Inputs.Add(Input);
Outputs.Add(Output);
var lastTime = Times.Last();
var removeCount = Times.TakeWhile(t => lastTime - t > TimeSeriesLengthSec).Count();
Times.RemoveRange(0, removeCount);
SetPoints.RemoveRange(0, removeCount);
Inputs.RemoveRange(0, removeCount);
Outputs.RemoveRange(0, removeCount);
}
TimePointRecorded?.Invoke(this, EventArgs.Empty);
}
}
}