-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsetup.m
More file actions
131 lines (106 loc) · 4.29 KB
/
setup.m
File metadata and controls
131 lines (106 loc) · 4.29 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
clear
close all
Ts = 1e-4;
Tsim = 1e-5;
%% Switch winding that you develop
load_system('speedControlSimulation') % load Simulink model
% The input of this function should be 'lowPassFilter', 'pllEncoder', 'observer'
switch_speed_calculation('observer')
Tend = 0.2;
p = 1; % number of pole
speed_cmd = 3000; % rotational speed (r/min)
% Parameters for plant
J_z = 29.54e-6;
b = 4.28e-6;
% Parameters for low pass filter
f_lpf = 100; % low pass fileter cut-off frequency (Hz)
omega_lpf = 2*pi*f_lpf; % low pass fileter cut-off frequency (rad/s)
% Parameters for PLL
pole_1_Hz = -10;
pole_2_Hz = -100;
w1 = 2*pi*pole_1_Hz;
w2 = 2*pi*pole_2_Hz;
f_sf = 10; % motion state fileter cut-off frequency (Hz)
wb_sf = 2*pi*f_sf;
b_o_sf = wb_sf*J_z;
K_io_sf = wb_sf*b;
% Parameters for chirp signal
f_init = 0.1; % initial frequency of chirp [Hz]
f_target = 1000; % chirp frequency at target time [Hz]
% Parameters for speed control
fb_speed = 100;
omega_b_speed = 2*pi*fb_speed;
Kp_speed = omega_b_speed*J_z;
Ki_speed = omega_b_speed*b;
%% Run simulation
% set_param('compute_speed/Speed Controller LPF', 'Commented', 'off');
% set_param('compute_speed/Speed Controller PLL', 'Commented', 'on');
% set_param('compute_speed/Speed Control Observer', 'Commented', 'on');
sim('speedControlSimulation.slx');
run1 = Simulink.sdi.Run.getLatest;
% set_param('compute_speed/Speed Controller LPF', 'Commented', 'on');
% set_param('compute_speed/Speed Controller PLL', 'Commented', 'off');
% set_param('compute_speed/Speed Control Observer', 'Commented', 'on');
% sim('compute_speed.slx');
% run2 = Simulink.sdi.Run.getLatest;
%
% set_param('compute_speed/Speed Controller LPF', 'Commented', 'on');
% set_param('compute_speed/Speed Controller PLL', 'Commented', 'on');
% set_param('compute_speed/Speed Control Observer', 'Commented', 'off');
% sim('compute_speed.slx');
% run3 = Simulink.sdi.Run.getLatest;
%% Post processing
% List of variables to extract
% obj2ext = {'time', 'omega_raw', 'omega_lpf', 'omega_pll', 'omega_sf'};
obj2ext = {'time', 'omega'};
% runs = {run1, run2, run3};
runs = {run1};
for r = 1:1
runObj = runs{r};
for i = 1:length(obj2ext)
sigID = getSignalIDsByName(runObj, obj2ext{i});
if ~isempty(sigID)
sig_obj{r}.(obj2ext{i}) = Simulink.sdi.getSignal(sigID);
sig_val{r}.(obj2ext{i}) = sig_obj{r}.(obj2ext{i}).Values.Data;
time{r} = sig_obj{r}.(obj2ext{i}).Values.Time;
end
end
end
%% Plot figure
width = 2*5.43;
height = 1.2*3*4.38/3/2;
set(0,'units','inches');
Inch_SS = get(0,'screensize');
lw = 1; % line width
figure1 = figure;
% Plot omega
hold on;
% plot(time{3}, squeeze(sig_val{3}.omega_raw), 'Color', 'k', 'LineWidth', lw);
plot(time{1}, squeeze(sig_val{1}.omega), 'Color', 'r', 'LineWidth', lw);
% plot(time{2}, squeeze(sig_val{2}.omega_pll), 'Color', 'b', 'LineWidth', lw);
% plot(time{3}, squeeze(sig_val{3}.omega_sf), '--', 'Color', 'g', 'LineWidth', lw);
xlabel('Time [s]','Interpreter','latex');
ylabel('$\Omega$ (rad/s)','Interpreter','latex');
xlim([0 Tend]);
% ylim([0 400]);
% legend('$\Omega_{\mathrm{raw}}$','$\Omega_{\mathrm{lpf}}$', '$\Omega_{\mathrm{pll}}$', '$\Omega_{\mathrm{sf}}$', 'Interpreter','latex','Location','east');
set(findall(gcf, '-property', 'FontName'), 'FontName', 'Times New Roman');
set(figure1,'Units','inches','Position',[(Inch_SS(3)-width)/2 (Inch_SS(4)-height)/2 width height]);
print(figure1, '-dsvg','-noui','plot_results');
print(figure1, '-dpng','-r300','plot_results');
function switch_speed_calculation(speed_calculation_type)
% Define maps for speed calculation models
speed_calculation_models = containers.Map( ...
{'lowPassFilter', 'pllEncoder', 'observer'}, ...
{'lowPassFilter', 'pllEncoder', 'observer'} ...
);
if ~isKey(speed_calculation_models, speed_calculation_type)
error('Unknown speed calculation type: %s. Choose from: %s', speed_calculation_type, strjoin(keys(speed_calculation_models), ', '));
end
speed_calculation_name = speed_calculation_models(speed_calculation_type);
% Load the referenced models
load_system(speed_calculation_name);
% Update the reference blocks
set_param('speedControlSimulation', 'ModelName', speed_calculation_name);
fprintf('Speed calculation block has been switched to referenced model of %s and %s\n', controller_name);
end