-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_drawing_mass_spring_damer.m
162 lines (117 loc) · 3.23 KB
/
main_drawing_mass_spring_damer.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
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
% Creating animation of a mass-spring-damper system
clear;
close all;
%% Simulation of a mass-spring-damter system
% Paramters
m = 1;
k = 50;
wn = sqrt(k/m);
zeta = 0.05;
c = 2*zeta*wn*m;
% Input
u = 0;
% State equation
fcn_x_dot = @(t,x)[x(2);(-c*x(2) - k*x(1) + u)/m];
% Initial values
x_ini = [0.5;0];
% Time duration for Simulation
Tspan = [0, 10];
% Solving State equation by ODE45
opts = odeset('RelTol', 1e-3, 'AbsTol', 1e-3, 'MaxStep', 1e-2);
[tout, x] = ode45(@(t,x)fcn_x_dot(t,x), Tspan, x_ini, opts);
% Plot of Time response
figure('WindowStyle','docked', 'Name', 'Cart Response');
ylabel_tmp = {'x [m]', 'x dot [m/s]'};
for i = 1:2
subplot(2,1,i)
plot(tout, x(:,i))
ylabel(ylabel_tmp{i})
end
xlabel('time [sec]')
set_plot_style_v03(16, 16, 2)
%% Setting Parameters for drawing
% Length of Equilibrium
l_equi = 2;
% Cart
l_w_c = 0.8; % Width
l_h_c = 0.6; % Height
% Spring
l_hdl_s = l_equi/10; % Length of Sides of Spring
num_s = 5; % Number of turns
% Damper
l_box_d = l_equi/4; % Width of Dashpot
% Wall & Ground
l_wg_xy = [l_equi + l_w_c;1.5*l_h_c];
p_wg_org = [0;0];
% p_wg_org = [0.2;0.2];
% Struct variable for drawing
param_plot_msd.l_equi = l_equi;
param_plot_msd.l_w_c = l_w_c;
param_plot_msd.l_h_c = l_h_c;
param_plot_msd.l_hdl_s = l_hdl_s;
param_plot_msd.num_s = num_s;
param_plot_msd.l_box_d = l_box_d;
param_plot_msd.p_wg_org = p_wg_org;
% Data for drawing Wall & Ground
p_wg = zeros(2,3);
p_wg(:,1) = [0;l_wg_xy(2)];
p_wg(:,2) = [0;0];
p_wg(:,3) = [l_wg_xy(1);0];
p_wg = p_wg + p_wg_org;
%% Plot: Initial situation
figure('WindowStyle','docked', 'Name', 'Drawing');
plot(p_wg(1,:), p_wg(2,:))
hold on
h_plot_msd = func_plot_msd(l_equi, param_plot_msd);
axis equal
for i = 1:numel(h_plot_msd)
h_plot_msd(i).LineWidth = 2;
end
set_plot_style_v03(16, 16, 2)
%% Creating Animation
close all;
% Resampling for Animation
fs_ani = 20; % Sampling rate
[tout_ani, p_cart_x_ani] = func_resample_v02(tout, x(:,1), 1/fs_ani); % Resampling with Sampling rate
num_frame_ani = numel(tout_ani); % Number of Data in Animation
% Struct variable for Making a movie file
Mov(num_frame_ani) = struct('cdata',[],'colormap',[]);
% Ranges of Plot
range_x = [-0.25, 3];
range_y = [-0.25, 1];
% Plots at All Time steps
figure;
for i = 1:num_frame_ani
p_cart_x = l_equi + p_cart_x_ani(i);
h_plot_ref = plot(l_equi*[1, 1], range_y, '--', 'LineWidth', 2);
hold on
h_plot_wg = plot(p_wg(1,:), p_wg(2,:), 'LineWidth', 2);
h_plot_msd = func_plot_msd(p_cart_x, param_plot_msd);
hold off
exp_p_cart = exp(-10*abs(p_cart_x_ani(i))/l_equi);
cmap_tmp = [1-exp_p_cart, 0, exp_p_cart];
h_plot_wg.Color = h_plot_ref.Color;
h_plot_msd(1).Color = h_plot_ref.Color;
h_plot_msd(2).Color = cmap_tmp;
h_plot_msd(3).Color = cmap_tmp;
for j = 1:numel(h_plot_msd)
h_plot_msd(j).LineWidth = 2;
end
grid on
axis equal
xlim(range_x)
ylim(range_y)
drawnow;
Mov(i) = getframe(gcf);
pause(0);
end
% Setting Parameters for Creating a movie file
name_movie = 'test';
vidObj = VideoWriter([name_movie, '.avi']);
vidObj.FrameRate = fs_ani;
% Creating a movie file
open(vidObj)
for i = 1:num_frame_ani
writeVideo(vidObj , Mov(i).cdata);
end
close(vidObj)