Update repo
This commit is contained in:
1 parent
749dab5721
commit
ce65a0f59a
14950 files changed
+4408250
-1
No files matched your search
Binary file not shown.
@@ -0,0 +1,88 @@
|
||||
% GSM_example.m - example script for the Gain Scheduler Module
|
||||
close all; clc; clear all;
|
||||
|
||||
% plot target function
|
||||
dx = 0.01;
|
||||
x = 0:dx:1;
|
||||
y = zeros(1, size(x,2));
|
||||
k = 1;
|
||||
for i = 0:dx:1
|
||||
y(k) = target(x(k));
|
||||
k = k+1;
|
||||
end
|
||||
plot(x,y)
|
||||
|
||||
%% find sector index
|
||||
N = 8;
|
||||
n = zeros(1,N);
|
||||
I = 1/N;
|
||||
i = 0;
|
||||
for k = 1:N
|
||||
i = i + I;
|
||||
n(k) = i;
|
||||
end
|
||||
|
||||
%% load LUT offsets
|
||||
c = zeros(1,N);
|
||||
for k = 1:N
|
||||
c(k) = target(n(k));
|
||||
end
|
||||
|
||||
%% load LUT slopes
|
||||
m = zeros(1,N);
|
||||
m(1) = c(1) / I;
|
||||
for k = 2:N
|
||||
m(k) = (c(k) - c(k-1)) / I;
|
||||
end
|
||||
|
||||
%% plot GSM segments
|
||||
L = fix(size(x,2)/N);
|
||||
y1 = zeros(1,L);
|
||||
y2 = zeros(1,L);
|
||||
y3 = zeros(1,L);
|
||||
y4 = zeros(1,L);
|
||||
y5 = zeros(1,L);
|
||||
y6 = zeros(1,L);
|
||||
y7 = zeros(1,L);
|
||||
y8 = zeros(1,L);
|
||||
x1 = 0:dx:I;
|
||||
x2 = I:dx:2*I;
|
||||
x3 = 2*I:dx:3*I;
|
||||
x4 = 3*I:dx:4*I;
|
||||
x5 = 4*I:dx:5*I;
|
||||
x6 = 5*I:dx:6*I;
|
||||
x7 = 6*I:dx:7*I;
|
||||
x8 = 7*I:dx:8*I;
|
||||
|
||||
k = 1;
|
||||
for xi = 0:dx:I
|
||||
y1(k) = m(1)*xi;
|
||||
y2(k) = m(2)*xi + c(1);
|
||||
y3(k) = m(3)*xi + c(2);
|
||||
y4(k) = m(4)*xi + c(3);
|
||||
y5(k) = m(5)*xi + c(4);
|
||||
y6(k) = m(6)*xi + c(5);
|
||||
y7(k) = m(7)*xi + c(6);
|
||||
y8(k) = m(8)*xi + c(7);
|
||||
k = k + 1;
|
||||
end
|
||||
hold all
|
||||
plot(x1,y1)
|
||||
plot(x2,y2)
|
||||
plot(x3,y3)
|
||||
plot(x4,y4)
|
||||
plot(x5,y5)
|
||||
plot(x6,y6)
|
||||
plot(x7,y7)
|
||||
plot(x8,y8)
|
||||
|
||||
% target function
|
||||
function y = target(x)
|
||||
% y = sin(2*pi*x);
|
||||
% y = x*x;
|
||||
y = x*(3*x - 2);
|
||||
% y = exp(2*x) - 1;
|
||||
% y = -tan(pi*x/3);
|
||||
end
|
||||
|
||||
% end of file
|
||||
@@ -0,0 +1,73 @@
|
||||
% PID_config.m - 3rd order linear plant
|
||||
|
||||
endtime = 16; % test duration (sec)
|
||||
fs = 400; % sample rate in Hz
|
||||
T = 1 / fs; % sample period (sec)
|
||||
|
||||
%- Controller ------------------------------------------------------------
|
||||
|
||||
% controller parameters
|
||||
Kp = 7.5; % proportional loop gain
|
||||
Ki = 0.005; % integral gain
|
||||
Kd = 0.35; % differential gain
|
||||
Kr = 1; % set-point weight
|
||||
fc = 100; % cut-off frequency (Hz)
|
||||
|
||||
% controller output saturation
|
||||
Umax = inf;
|
||||
Umin = -inf;
|
||||
|
||||
%- Plant model -----------------------------------------------------------
|
||||
|
||||
% plant transfer function:
|
||||
%
|
||||
% s + 1
|
||||
% G(s) = ---------------------
|
||||
% s^3 + 3 s^2 + 5 s + 1
|
||||
|
||||
G = tf([1 1], [1 3 5 1]);
|
||||
[num_G,den_G] = tfdata(G,'v');
|
||||
G = tf(num_G,den_G); % save TF in workspace
|
||||
|
||||
% plant saturation limits
|
||||
Psatmax = inf;
|
||||
Psatmin = -inf;
|
||||
|
||||
% time delay
|
||||
Tdly = T/10;
|
||||
|
||||
% feedback path dynamics
|
||||
num_H = 1;
|
||||
den_H = 1;
|
||||
H = tf(num_H,den_H);
|
||||
|
||||
%- ADC model ------------------------------------------------------------
|
||||
|
||||
% ADC parameters
|
||||
Kn = 0; % feedback noise gain
|
||||
Vadc_max = inf; % positive input limit
|
||||
Vadc_min = -inf; % negative input limit
|
||||
Nadc = 16; % resolution in bits
|
||||
|
||||
% ADC quantizer interval
|
||||
if isfinite(Vadc_max) & isfinite(Vadc_min)
|
||||
Qadc = (Vadc_max - Vadc_min)/2^Nadc;
|
||||
else
|
||||
Qadc = 1/2^Nadc;
|
||||
end
|
||||
|
||||
%- DAC model ------------------------------------------------------------
|
||||
|
||||
% DAC parameters
|
||||
Umax = inf; % upper output saturation
|
||||
Umin = -inf; % lower output saturation
|
||||
Ndac = 16; % resolution in bits
|
||||
|
||||
% DAC quantizer interval
|
||||
if isfinite(Umax) & isfinite(Umin)
|
||||
Qdac = (Umax - Umin)/2^Ndac;
|
||||
else
|
||||
Qdac = 1/2^Ndac;
|
||||
end
|
||||
|
||||
% end of file
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,96 @@
|
||||
% PID_example.m - C2000 Digital Control Library - PID control example
|
||||
clc; close all; clear;
|
||||
|
||||
%- Configuration ---------------------------------------------------------
|
||||
|
||||
% input stimulus
|
||||
% 0 : step
|
||||
% 1 : pulse (return step)
|
||||
% 2 : bi-directional pulse
|
||||
% 3 : reverse bi-directional pulse
|
||||
% 4 : chirp (oscillation burst)
|
||||
% 5 : unit pulse
|
||||
% 6 : static offset
|
||||
% 7 : triangle ramp
|
||||
input_config = 1;
|
||||
|
||||
%- Plots -----------------------------------------------------------------
|
||||
|
||||
% system plots: set to 1 to plot
|
||||
data.inp.plot = 0; % input stimulus
|
||||
data.rsp.plot = 1; % servo response
|
||||
data.act.plot = 0; % control effort
|
||||
data.fbk.plot = 0; % feedback
|
||||
data.err.plot = 0; % servo error
|
||||
data.sat.plot = 0; % saturation
|
||||
|
||||
% system data: set to 1 to save to file
|
||||
data.inp.save = 1; % input stimulus
|
||||
data.fbk.save = 1; % feedback
|
||||
data.sat.save = 1; % saturation limit
|
||||
data.act.save = 1; % control effort
|
||||
|
||||
%- Read configuration ----------------------------------------------------
|
||||
|
||||
filename = 'PID_config.m';
|
||||
open(filename);
|
||||
run(filename);
|
||||
run('PID_inputs.m')
|
||||
|
||||
%- Simulation ------------------------------------------------------------
|
||||
|
||||
open 'PID_sim.slx';
|
||||
sim('PID_sim', timespan, [], []);
|
||||
% note: inspect command window for assertions
|
||||
fprintf('Simulation complete.\n');
|
||||
|
||||
%- Results ---------------------------------------------------------------
|
||||
|
||||
% display selected plots
|
||||
run('PID_plots.m')
|
||||
|
||||
% compute performance indices & display in command window
|
||||
% IES = sum(ferror.^2)
|
||||
% IAE = norm(ferror,1)
|
||||
ITAE = T * sum(time.*abs(ferror))
|
||||
|
||||
%- Data export -----------------------------------------------------------
|
||||
|
||||
% write test data to text files for CCS access
|
||||
|
||||
% 1. input stimulus
|
||||
if data.inp.save == 1
|
||||
filename = strcat('PID_input.dat');
|
||||
File1 = fopen(filename,'w');
|
||||
fprintf(File1,'1651 9 c000 1 641 10\r\n'); % TI header
|
||||
fprintf(File1,'%12.8e\r\n',finput');
|
||||
fclose(File1);
|
||||
end
|
||||
|
||||
% 2. feedback data
|
||||
if data.fbk.save == 1
|
||||
filename = strcat('PID_fbk.dat');
|
||||
File2 = fopen(filename,'w');
|
||||
fprintf(File2,'1651 9 e000 1 641 10\r\n'); % TI header
|
||||
fprintf(File2,'%12.8e\r\n',ffeedback');
|
||||
fclose(File2);
|
||||
end
|
||||
|
||||
% 3. saturation data
|
||||
if data.sat.save == 1
|
||||
filename = strcat('PID_sat.dat');
|
||||
File3 = fopen(filename,'w');
|
||||
fprintf(File3,'1651 9 10000 1 641 10\r\n'); % TI header
|
||||
fprintf(File3,'%12.8e\r\n',fsat');
|
||||
fclose(File3);
|
||||
end
|
||||
|
||||
% 4. control data
|
||||
if data.act.save == 1
|
||||
filename = strcat('PID_ctl.dat');
|
||||
File4 = fopen(filename,'w');
|
||||
fprintf(File4,'%12.8e\r\n',fcontrol');
|
||||
fclose(File4);
|
||||
end
|
||||
|
||||
% end of file
|
||||
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,169 @@
|
||||
% PID_inputs : configure test input stimulus
|
||||
|
||||
stepamp = 0.1; % input step size
|
||||
|
||||
%--------------------------------------------------------------------------
|
||||
|
||||
% single unit step 0 -> 1
|
||||
if input_config == 0
|
||||
% create time vector
|
||||
time = (0:T:endtime)';
|
||||
timespan = [0,endtime];
|
||||
|
||||
% define times (seconds)
|
||||
t0 = 0;
|
||||
t1 = 0.1 * endtime; % step up
|
||||
|
||||
% construct input stimulus
|
||||
input = stepamp * ones(size(time));
|
||||
input(1:ceil(t1/T)) = 0; % step at t1
|
||||
end
|
||||
|
||||
%--------------------------------------------------------------------------
|
||||
|
||||
% pulse (return unit step) 0 -> 1 -> 0
|
||||
if input_config == 1
|
||||
% create time vector
|
||||
time = (0:T:endtime)';
|
||||
timespan = [0,endtime];
|
||||
|
||||
% define times (seconds)
|
||||
t0 = 0;
|
||||
t1 = 0.1 * endtime; % step up
|
||||
t2 = 0.5 * endtime; % step down
|
||||
|
||||
% construct input stimulus
|
||||
input = stepamp * ones(size(time));
|
||||
input(1:ceil(t1/T)) = 0; % step at t1
|
||||
input(ceil(t2/T):size(time)) = 0; % recover at t2
|
||||
end
|
||||
|
||||
%--------------------------------------------------------------------------
|
||||
|
||||
% wiggle 0 -> 1 -> -1 -> 0
|
||||
if input_config == 2
|
||||
% create time vector
|
||||
time = (0:T:endtime)';
|
||||
timespan = [0,endtime];
|
||||
|
||||
% define times (seconds)
|
||||
t0 = 0;
|
||||
t1 = 0.1 * endtime; % step up
|
||||
t2 = 0.4 * endtime; % step down
|
||||
t3 = 0.7 * endtime; % step up
|
||||
|
||||
% construct input stimulus
|
||||
input = zeros(size(time));
|
||||
input(ceil(t1/T):ceil(t2/T)) = stepamp;
|
||||
input(ceil(t2/T)+1:ceil(t3/T)) = -stepamp;
|
||||
input(ceil(t3/T)+1:size(time)) = 0;
|
||||
end
|
||||
|
||||
%--------------------------------------------------------------------------
|
||||
|
||||
% reverse wiggle 0 -> -1 -> 1 -> 0
|
||||
if input_config == 3
|
||||
% create time vector
|
||||
time = (0:T:endtime)';
|
||||
timespan = [0,endtime];
|
||||
|
||||
% define times (seconds)
|
||||
t0 = 0;
|
||||
t1 = 0.1 * endtime; % step up
|
||||
t2 = 0.4 * endtime; % step down
|
||||
t3 = 0.7 * endtime; % step up
|
||||
|
||||
% construct input stimulus
|
||||
input = zeros(size(time));
|
||||
input(ceil(t1/T):ceil(t2/T)) = -stepamp;
|
||||
input(ceil(t2/T)+1:ceil(t3/T)) = stepamp;
|
||||
input(ceil(t3/T)+1:size(time)) = 0;
|
||||
end
|
||||
|
||||
%--------------------------------------------------------------------------
|
||||
|
||||
% oscillation burst
|
||||
oscamp = 0.1; % oscillation amplitude
|
||||
k = 2; % number of cycles in burst
|
||||
if input_config == 4
|
||||
% create time vector
|
||||
time = (0:T:endtime)';
|
||||
timespan = [0,endtime];
|
||||
|
||||
% define times (seconds)
|
||||
% t0 = 0;
|
||||
t1 = 0.1 * endtime; % osc start
|
||||
t2 = 0.7 * endtime; % osc stop
|
||||
N = (t2 - t1) / T; % points in test phase
|
||||
|
||||
% construct input stimulus
|
||||
input = zeros(size(time));
|
||||
n1 = ceil(t1/T); % first point in test phase
|
||||
for i = n1:ceil(t2/T)
|
||||
input(i) = oscamp * sin(2*pi*(i-n1)*k/N);
|
||||
end
|
||||
% input(ceil(t2/T)+1:size(time)) = 0;
|
||||
end
|
||||
|
||||
%--------------------------------------------------------------------------
|
||||
|
||||
% unit pulse
|
||||
impulseamp = 0.1;
|
||||
if input_config == 5
|
||||
% create time vector
|
||||
time = (0:T:endtime)';
|
||||
timespan = [0,endtime];
|
||||
|
||||
% define times (seconds)
|
||||
t0 = 0.1 * endtime; % step up
|
||||
|
||||
% construct input stimulus
|
||||
input = zeros(size(time));
|
||||
input(ceil(t0/T)) = impulseamp; % impulse at t1
|
||||
end
|
||||
|
||||
%--------------------------------------------------------------------------
|
||||
|
||||
% static
|
||||
impulseamp = 0.01;
|
||||
if input_config == 6
|
||||
% create time vector
|
||||
time = (0:T:endtime)';
|
||||
timespan = [0,endtime];
|
||||
|
||||
% construct input stimulus
|
||||
input = impulseamp * ones(size(time));
|
||||
end
|
||||
|
||||
%--------------------------------------------------------------------------
|
||||
|
||||
% triangle : 0 -> 1 ramp up : 1 -> 0 ramp down
|
||||
if input_config == 7
|
||||
% create time vector
|
||||
time = (0:T:endtime)';
|
||||
timespan = [0,endtime];
|
||||
val = 0;
|
||||
|
||||
% define times (seconds)
|
||||
t0 = 0;
|
||||
t1 = 0.1 * endtime; % ramp up
|
||||
t2 = 0.5 * endtime; % ramp down
|
||||
t3 = 0.9 * endtime; % stop
|
||||
|
||||
% construct input stimulus
|
||||
input = zeros(size(time));
|
||||
delta_y = stepamp / (t2 - t1);
|
||||
|
||||
for index = ceil(t1/T):ceil(t2/T)
|
||||
val = val + delta_y;
|
||||
input(index) = val;
|
||||
end
|
||||
|
||||
for index = ceil(t2/T):ceil(t3/T)
|
||||
val = val - delta_y;
|
||||
input(index) = val;
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
% end of file
|
||||
@@ -0,0 +1,79 @@
|
||||
% PID_plots : plot selected results in separate graphs
|
||||
|
||||
% Figure handle index:
|
||||
% 1 = servo response
|
||||
% 2 = feedback data
|
||||
% 3 = control effort
|
||||
% 4 = input stimulus
|
||||
% 5 = servo error
|
||||
% 6 = saturation limit
|
||||
|
||||
% plot response
|
||||
if data.rsp.plot == 1
|
||||
figure(1);
|
||||
plot(fresponse)
|
||||
grid on
|
||||
hold all
|
||||
title('Servo Response Plot')
|
||||
xlabel('Time (s)')
|
||||
ylabel('Output y(t)')
|
||||
% overlay input data
|
||||
stairs(time,finput)
|
||||
end
|
||||
|
||||
% plot feedback
|
||||
if data.fbk.plot == 1
|
||||
figure(2);
|
||||
stairs(time,ffeedback)
|
||||
grid on
|
||||
hold all
|
||||
title('Feedback Plot')
|
||||
xlabel('Time (s)')
|
||||
ylabel('Feedback ym(k)')
|
||||
end
|
||||
|
||||
% plot control effort
|
||||
if data.act.plot == 1
|
||||
figure(3);
|
||||
stairs(time,fcontrol)
|
||||
grid on
|
||||
hold all
|
||||
title('Control Plot')
|
||||
xlabel('Time (s)')
|
||||
ylabel('Control Effort u(k)')
|
||||
end
|
||||
|
||||
% plot input stimulus
|
||||
if data.inp.plot == 1
|
||||
figure(4);
|
||||
stairs(time,finput)
|
||||
grid on
|
||||
hold all
|
||||
title('Input Plot')
|
||||
xlabel('Time (s)')
|
||||
ylabel('Input r(k)')
|
||||
end
|
||||
|
||||
% plot servo error
|
||||
if data.err.plot == 1
|
||||
figure(5);
|
||||
stairs(time,ferror)
|
||||
grid on
|
||||
hold all
|
||||
title('Servo Error Plot')
|
||||
xlabel('Time (s)')
|
||||
ylabel('Error e(k)')
|
||||
end
|
||||
|
||||
% plot saturation data
|
||||
if data.sat.plot == 1
|
||||
figure(6);
|
||||
stairs(time,fsat)
|
||||
grid on
|
||||
hold all
|
||||
title('Saturation Limit Data Plot')
|
||||
xlabel('Time (s)')
|
||||
ylabel('Data lim(k)')
|
||||
end
|
||||
|
||||
% end of file
|
||||
File diff suppressed because it is too large.
Load diff
Binary file not shown.
Reference in new issue
Block a user