-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathsaturateInputDerivative.m
73 lines (57 loc) · 2.01 KB
/
saturateInputDerivative.m
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
function uSat = saturateInputDerivative(u, u_0, tStep, uDeltaMax)
% SATURATEINPUTDERIVATIVE saturates the input u such that the absolute
% value of its numerical derivative:
%
% uDelta = (uPrev-u)/tStep
%
% cannot be greater than a predefined value.
%
% FORMAT: uSat = saturateInputDerivative(u, u_0, Config, Sat)
%
% INPUTS: u = input signal;
% u_0 = input at t = 0;
% tStep = time step for finite difference formula;
% uDeltaMax = max input derivative (absolute value).
%
% OUTPUTS: uSat = saturated input signal.
%
% Authors: Daniele Pucci, Marie Charbonneau, Gabriele Nava
%
% all authors are with the Italian Istitute of Technology (IIT)
% email: [email protected]
%
% Genoa, Dec 2017
%
%% --- Initialization ---
persistent uPrev;
% initialize the input value at the previous step
if isempty(uPrev)
uPrev = u_0;
end
% evaluate the max and min allowed input
delta_u_max = uDeltaMax * tStep;
delta_u_min = -uDeltaMax * tStep;
delta_u_Sat = saturateInput(u-uPrev, delta_u_min, delta_u_max);
% update u at previous time
uSat = uPrev + delta_u_Sat;
uPrev = uSat;
end
% utility function: saturates the input value
function y = saturateInput(u, min, max)
assert(isequal(size(min), size(max)), 'Min and max must be same size')
if length(min) == 1
y = u;
y(y > max) = max;
y(y < min) = min;
else
assert(length(min) == length(u), 'input and saturation must have same size');
y = u;
for i = 1:length(min)
if y(i) > max(i)
y(i) = max(i);
elseif y(i) < min(i)
y(i) = min(i);
end
end
end
end