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 237 238 239 240
| clear
close all
% Definindo os parâmetros que serão exportadas para a função auxiliar
global M_v g mu r L W Jr Jw L_m R_m K_bm K_t reduction_m C_m U_motor_1 ...
U_motor_2 U_motor_3 U_motor_4
% Dados de entrada*********************************************************
% Integrador numérico
passo=0.00001;
% Massa veículo com as 4 rodas (kg)
M_v=2;
% Gravidade (m/s^2)
g=9.81;
% Coeficiente de atrito seco
mu=0.7;
% raio da roda (m)
r=0.06;
% dimençoes robot (m)
L=0.15;
W=0.16;
% Momento de inércia de cada roda (kg*m^2)
Jw=0.01;
% Momento de inércia do robot (kg*m^2)
Jr=0.01;
% CC Motor specifications
L_m=0.001; % (Henry)
R_m=40; % (Ohm)
K_t=0.05; % (N.m/A)
K_bm=0.05; % (V/rad/s)
reduction_m=30/1; % (-)
C_m=0; % dry friction
% Tempo de simulação (s)
t_final=5;
% Condições iniciais
x_1_0=0;
y_1_0=0;
dx_1_0=0;
dy_1_0=0;
teta_1_0=0;
teta_2_0=0;
teta_3_0=0;
teta_4_0=0;
dteta_1_0=0;
dteta_2_0=0;
dteta_3_0=0;
dteta_4_0=0;
phi_0=0;
dphi_0=0;
i_m1_0=0;
i_m2_0=0;
i_m3_0=0;
i_m4_0=0;
%**************************************************************************
% CORPO DO PROGRAMA
%**************************************************************************
% Inciando contador
contador=1;
% Vetor com as condições iniciais
vetor_q=[x_1_0; y_1_0; dx_1_0; dy_1_0; teta_1_0; teta_2_0; teta_3_0;
teta_4_0; dteta_1_0; dteta_2_0; dteta_3_0; dteta_4_0; phi_0; dphi_0;
i_m1_0; i_m2_0; i_m3_0; i_m4_0];
% LOOP --------------------------------------------------------------------
% Rotation Matrix at t=0
T2_1=[cos(phi_0) sin(phi_0) 0 ; -sin(phi_0) cos(phi_0) 0 ; 0 0 1]; % from frame F1 to frame F2
T1_2=[cos(phi_0) -sin(phi_0) 0 ; sin(phi_0) cos(phi_0) 0 ; 0 0 1]; % from frame F2 to frame F1
% Speed of the center of mass in F2 at t=0
Vcm_2=T2_1*[dx_1_0; dy_1_0; 0];
% Speed of each hub wheel in the base F2 at t=0
VH1_2=Vcm_2+cross([0; 0; dphi_0],[L; -W; 0]); % Hub1 speed in F2
VH2_2=Vcm_2+cross([0; 0; dphi_0],[L; W; 0]); % Hub2 speed in F2
VH3_2=Vcm_2+cross([0; 0; dphi_0],[-L; W; 0]); % Hub3 speed in F2
VH4_2=Vcm_2+cross([0; 0; dphi_0],[-L; -W; 0]); % Hub4 speed in F2
for T=0:passo:t_final
%Tensao do motor 1
if (T>0)&(T<=1)
U_motor_1=12*T;
elseif (T>1)&(T<=4)
U_motor_1=0;
elseif (T>4)&(T<=5)
U_motor_1=-48+12*T;
end
%Tensao do motor 2
if (T>0)&(T<=1)
U_motor_2=12*T;
elseif (T>1)&(T<=4)
U_motor_2=0;
elseif (T>4)&(T<=5)
U_motor_2=-48+12*T;
end
%Tensao do motor 3
if (T>0)&(T<=1)
U_motor_3=12*T;
elseif (T>1)&(T<=4)
U_motor_3=0;
elseif (T>4)&(T<=5)
U_motor_3=-48+12*T;
end
%Tensao do motor 4
if (T>0)&(T<=1)
U_motor_4=12*T;
elseif (T>1)&(T<=2)
U_motor_4=12;
elseif (T>2)&(T<=3)
U_motor_4=36-12*T;
elseif (T>3)&(T<=4)
U_motor_4=0;
elseif (T>4)&(T<=5)
U_motor_4=-48+12*T;
end
% Vetores para plotagens posteriores (inicia em T=0 e termina em T=t_final)
vetor_T(contador)=T;
vetor_x_1(contador)=vetor_q(1);
vetor_y_1(contador)=vetor_q(2);
vetor_dx_1(contador)=vetor_q(3);
vetor_dy_1(contador)=vetor_q(4);
vetor_teta_1(contador)=vetor_q(5);
vetor_teta_2(contador)=vetor_q(6);
vetor_teta_3(contador)=vetor_q(7);
vetor_teta_4(contador)=vetor_q(8);
vetor_dteta_1(contador)=vetor_q(9);
vetor_dteta_2(contador)=vetor_q(10);
vetor_dteta_3(contador)=vetor_q(11);
vetor_dteta_4(contador)=vetor_q(12);
vetor_phi(contador)=vetor_q(13);
vetor_dphi(contador)=vetor_q(14);
vetor_i_m1(contador)=vetor_q(15);
vetor_i_m2(contador)=vetor_q(16);
vetor_i_m3(contador)=vetor_q(17);
vetor_i_m4(contador)=vetor_q(18);
% % Rotation Matrix
% T2_1=[cos(vetor_q(13)) sin(vetor_q(13)) 0 ; -sin(vetor_q(13)) cos(vetor_q(13)) 0 ; 0 0 1]; % from frame F1 to frame F2
% T1_2=[cos(vetor_q(13)) -sin(vetor_q(13)) 0 ; sin(vetor_q(13)) cos(vetor_q(13)) 0 ; 0 0 1]; % from frame F2 to frame F1
%
% % Speed of the center of mass in F2
% Vcm_2=T2_1*[vetor_q(3); vetor_q(4); 0];
%
% % Speed of each hub wheel in the base F2
% VH1_2=Vcm_2+cross([0; 0; vetor_q(14)],[L; -W; 0]); % Hub1 speed in F2
% VH2_2=Vcm_2+cross([0; 0; vetor_q(14)],[L; W; 0]); % Hub2 speed in F2
% VH3_2=Vcm_2+cross([0; 0; vetor_q(14)],[-L; W; 0]); % Hub3 speed in F2
% VH4_2=Vcm_2+cross([0; 0; vetor_q(14)],[-L; -W; 0]); % Hub4 speed in F2
%
% VH1_x=VH1_2(1,1);
% VH1_y=VH1_2(2,1);
% VH2_x=VH2_2(1,1);
% VH2_y=VH2_2(2,1);
% VH3_x=VH3_2(1,1);
% VH3_y=VH3_2(2,1);
% VH4_x=VH4_2(1,1);
% VH4_y=VH4_2(2,1);
% vetor_VH1_x(contador)=VH1_x;
% vetor_VH1_y(contador)=VH1_y;
% vetor_VH2_x(contador)=VH2_x;
% vetor_VH2_y(contador)=VH2_y;
% vetor_VH3_x(contador)=VH3_x;
% vetor_VH3_y(contador)=VH3_y;
% vetor_VH4_x(contador)=VH4_x;
% vetor_VH4_y(contador)=VH4_y;
% Vetores com as constantes k1, k2, k3 e k4 de Runge-Kutta relativas à cada equacão
vetor_k1_1_RK=fun_aux3(vetor_q);
vetor_k2_1_RK=fun_aux3(vetor_q+0.5*vetor_k1_1_RK*passo);
vetor_k3_1_RK=fun_aux3(vetor_q+0.5*vetor_k2_1_RK*passo);
vetor_k4_1_RK=fun_aux3(vetor_q+vetor_k3_1_RK*passo);
% Vetor q no instante T+passo
vetor_q=vetor_q+(passo/6)*(vetor_k1_1_RK+2*vetor_k2_1_RK+2*vetor_k3_1_RK+vetor_k4_1_RK);
% Atualizando o contador
contador=contador+1;
end
% *************************************************************************
% Plotagens dominio do tempo
% *************************************************************************
figure(1); plot(vetor_T,vetor_x_1,'r-'); hold on
plot(vetor_T,vetor_dx_1,'g-');
title('Chassis')
xlabel ('Time (s)')
legend ('Robot position (m)','Robot speed (m/s)')
figure(2); plot(vetor_T,vetor_teta_1,'r-'); hold on
plot(vetor_T,vetor_dteta_1,'g-');
title ('Rear Wheel')
xlabel ('Time (s)')
legend('Angular position (rad)','Angular speed (rad/s)')
figure(3); plot(vetor_T,vetor_teta_2,'r-'); hold on
plot(vetor_T,vetor_dteta_2,'g-');
title ('Front wheel')
xlabel ('Time (s)')
legend('Angular position (rad)','Angular speed (rad/s)')
figure(4); plot(vetor_x_1,vetor_y_1,'r-');hold on
title ('Trajectory of the chassis in the plan X,Y ')
xlabel ('x(m)')
ylabel ('y(m)')
% for contador=1:1:(contador-1);
% plot([vetor_VH4_x(contador);vetor_VH1_x(contador)],[vetor_VH4_y(contador);vetor_VH1_y(contador)]);grid on; hold on;
% plot([vetor_VH1_x(contador);vetor_VH2_x(contador)],[vetor_VH1_y(contador);vetor_VH2_y(contador)]);grid on;
% plot([vetor_VH3_x(contador);vetor_VH2_x(contador)],[vetor_VH3_y(contador);vetor_VH2_y(contador)]);grid on;
% plot([vetro_VH3_x(contador);vetor_VH4_x(contador)],[vetor_VH3_y(contador);vetor_VH4_y(contador)]);grid on;hold off;
% M(contador)= getframe;
% end
figure(5); plot(vetor_T,vetor_i_m1,'r-'); hold on
plot(vetor_T,vetor_i_m2,'b-'); hold on
plot(vetor_T,vetor_i_m3,'y-'); hold on
plot(vetor_T,vetor_i_m4,'g-'); hold on
title ('Current Motor')
xlabel ('Time (s)')
ylabel ('Current (A)')
legend('i(1)','i(2)','i(3)','i(4)') |
Partager