Main Robot
Main Robot
clc
clear
close
tic
%% Donn�es du probleme
% Parametres Inertielles
M_Body = 7.50;
M_Wheel = 2;
M_Leg = 1;
M_Bar_1 = 1;
M_Bar_2 = 1;
I_Body = 2;
I_Wheel = 0.1;
I_Leg = 0.06;
I_Bar_1 = 0.06;
I_Bar_2 = 0.06;
% Parametres Geometriques
global L_1 L_2 L_3 R_W l_B h_B hh L_B L_C L_E L_D phi_E phi_D
L_1 = 0.527;
L_2 = 0.409;
L_3 = 0.368;
R_W = 0.2;
hh = 0.05;
l_B = 0.580;
h_B = 0.378;
g = -9.81;
mu1 = 0.9;
mu2 = 0.9;
e = 0.00002;
treshold = 0.0001;
Poly_Ground = Poly_Ground1;
%
% % Terrain : route plane puis descente
% Poly_Ground = [
% -10 0; % D�but du terrain (x = -10, y = 0)
% 10 0; % Fin de la route plane (x = 10, y = 0)
% 20 -5; % Descente (x = 20, y = -5)
% 30 -5; % Route plate apr�s descente (x = 30, y = -5)
% 40 0; % Remont�e (optionnelle, x = 40, y = 0)
% 50 0; % Continuit� de la route plane (x = 50, y = 0)
% 60 -2; % Nouvelle descente optionnelle
% 70 -2; % Continuit� en bas
% 80 0; % transition
% ]
ti = 0;
tf = 3;
delta = 0.001;
T_simul = ti:delta:tf;
dt=0.001;
% Vitesse roues
%tau1 = -400;
%tau2 = -400;
V_Car_km_h = 10;
V_Car = V_Car_km_h/3.6;
K_Car = 2;
previous_error = 0;
%% Conditions initiales
% q = [ x_Body y_Body theta_Body
% x_Wheel y_Wheel theta_Wheel
% x_Leg y_Leg theta_Leg
% x_Bar_1 y_Bar_1 theta_Bar_1
% x_Bar_2 y_Bar_2 theta_Bar_2]
% Positions
theta1(1) = 0.0 ;
theta2(1) = 0.0 ;
theta3(1) = deg2rad(48.54);
theta4(1) = - deg2rad(180 - 141.71);
theta5(1) = - deg2rad(180 - 140.31);
% Vitesse
x1_p(1) = 0.0 ; y1_p(1) = 0.0; theta1_p(1) = 0.0;
x2_p(1) = 0.0 ; y2_p(1) = 0.0; theta2_p(1) = 0.0;
x3_p(1) = 0.0 ; y3_p(1) = 0.0; theta3_p(1) = 0.0;
x4_p(1) = 0.0 ; y4_p(1) = 0.0; theta4_p(1) = 0.0;
x5_p(1) = 0.0 ; y5_p(1) = 0.0; theta5_p(1) = 0.0;
%% MAIN LOOP
M1=[];
M2=[];
M3=[];
M4=[];
M5=[];
% Initialisation des figures et des courbes AVANT la boucle
figure;
% subplot(5,1,1); p1 = plot(nan, 'r', 'LineWidth', 1.5); title('�volution de
Theta1'); grid on;
% subplot(5,1,2); p2 = plot(nan, 'b', 'LineWidth', 1.5); title('�volution de
Theta2'); grid on;
% subplot(5,1,3); p3 = plot(nan, 'g', 'LineWidth', 1.5); title('�volution de
Theta3'); grid on;
% subplot(5,1,4); p4 = plot(nan, 'm', 'LineWidth', 1.5); title('�volution de
Theta4'); grid on;
% subplot(5,1,5); p5 = plot(nan, 'k', 'LineWidth', 1.5); title('�volution de
Theta5'); grid on;
subplot(2,1,1); p1 = plot(nan, 'r', 'LineWidth', 1.5); title('�volution ERP');
grid on;
subplot(2,1,2); p2 = plot(nan, 'b', 'LineWidth', 1.5); title('�volution ERV');
grid on;
ERV=[];
ERP=[];
for i = 1 : length(T_simul)
[J,phi] = constraints_Jacobian(q);
phi1(i) = phi(1) ;
phi2(i) = phi(2) ;
phi3(i) = phi(3) ;
phi4(i) = phi(4) ;
phi5(i) = phi(5) ;
phi6(i) = phi(6) ;
phi7(i) = phi(7) ;
phi8(i) = phi(8) ;
phi9(i) = phi(9) ;
phi10(i) = phi(10) ;
%%
target_v=20;
error = target_v - theta1_p(i);
ERV= [ERV error];
Fg = [F_Body ;F_Wheel;F_Leg;F_Bar_1;F_Bar_2];
% Detection de contact
======================================================================
[yes_nott,ncc,nc,n,nbc,rG,err,Cnn] = Contact_Detection(q,Poly_Ground);
if yes_nott == 1
[Wn,D,E,b] = Wrenches_Matrix_2D(nb,ncc,nc,n,rG,nbc,err,e,treshold,delta);
mu = mu1 * eye(ncc);
H = [Wn D zeros(3*nb,ncc)];
N = [zeros(ncc,4*ncc);zeros(2*ncc,ncc*(1+2)) E;mu -E' zeros(ncc,ncc)];
invM_H=M\H;
invM_Jt = M\J';
invM_h=M\h;
A = G + N;
C = s;
xx = pathlcp(A,C);
end
if yes_nott == 0
K = [M -J';J zeros(10,10)];
B = [h;zeros(10,1)];
q_p = K\B;
end
x1_p(i+1) = q_p(1);
y1_p(i+1) = q_p(2);
theta1_p(i+1) = q_p(3);
x2_p(i+1) = q_p(4);
y2_p(i+1) = q_p(5);
theta2_p(i+1) = q_p(6);
x3_p(i+1) = q_p(7);
y3_p(i+1) = q_p(8);
theta3_p(i+1) = q_p(9);
x4_p(i+1) = q_p(10);
y4_p(i+1) = q_p(11);
theta4_p(i+1) = q_p(12);
x5_p(i+1) = q_p(13);
y5_p(i+1) = q_p(14);
theta5_p(i+1) = q_p(15);
q_new(1) = x1(i) + delta * x1_p(i+1);
q_new(2) = y1(i) + delta * y1_p(i+1);
q_new(3) = theta1(i) + delta * theta1_p(i+1);
[J_new,Phi_new] = constraints_Jacobian(q_new);
dq_new = -(J_new'*inv(J_new*J_new'))*Phi_new;
% Mettre � jour les valeurs des angles dans les vecteurs de stockage
M1 = [M1 theta1(i+1)];
M2 = [M2 theta2(i+1)];
M3 = [M3 theta3(i+1)];
M4 = [M4 theta4(i+1)];
M5 = [M5 theta5(i+1)];
end
toc
%% Animation
===================================================================================
=
hold on
grid on
axis([-4 4 -4 4]);
v = VideoWriter('V30_T625_A020_W.avi');
open(v);
cla
axis([x1(i)-1 x1(i)+1 -0.5 2]);
hold on
% Appliquer la saturation sur les angles utilis�s pour l'animation
theta1_anim = max(-pi/12, min(pi/12, theta1(i)));
theta3_anim = max(-pi/12, min(pi/12, theta3(i)));
theta4_anim = max(-pi/12, min(pi/12, theta4(i)));
theta5_anim = max(-pi/12, min(pi/12, theta5(i)));
G1 = [x1(i) y1(i)]';
b11 = G1 + RR1*[0.5 * l_B 0.5 * h_B]';
b12 = G1 + RR1*[-0.5 * l_B 0.5 * h_B]';
b13 = G1 + RR1*[-0.5 * l_B -0.5 * h_B]';
b14 = G1 + RR1*[0.5 * l_B -0.5 * h_B]';
G3 = [x3(i) y3(i)]';
b31 = G3 + RR3*[0.5 * L_1 0.5 * hh]';
b32 = G3 + RR3*[-0.5 * L_1 0.5 * hh]';
b33 = G3 + RR3*[-0.5 * L_1 -0.5 * hh]';
b34 = G3 + RR3*[0.5 * L_1 -0.5 * hh]';
G4 = [x4(i) y4(i)]';
b41 = G4 + RR4*[0.5 * L_2 0.5 * hh]';
b42 = G4 + RR4*[-0.5 * L_2 0.5 * hh]';
b43 = G4 + RR4*[-0.5 * L_2 -0.5 * hh]';
b44 = G4 + RR4*[0.5 * L_2 -0.5 * hh]';
G5 = [x5(i) y5(i)]';
b51 = G5 + RR5*[0.5 * L_3 0.5 * hh]';
b52 = G5 + RR5*[-0.5 * L_3 0.5 * hh]';
b53 = G5 + RR5*[-0.5 * L_3 -0.5 * hh]';
b54 = G5 + RR5*[0.5 * L_3 -0.5 * hh]';
Poly_Body = [b11';b12';b13';b14';b11'];
Poly_Leg = [b31';b32';b33';b34';b31'];
Poly_Bar_1 = [b41';b42';b43';b44';b41'];
Poly_Bar_2 = [b51';b52';b53';b54';b51'];
%drawCircle(x2(i),y2(i),R_W,4,6);
drawCircle(x2(i),y2(i),R_W);
drawPoint(x2(i),y2(i));
plot([x2(i) x2(i)+R_W*cos(theta2(i))],[y2(i) y2(i)
+R_W*sin(theta2(i))],'r','Linewidth',3);
frame = getframe(gcf);
writeVideo(v,frame);
pause(0.01)
end
close(v);
% % Tracer les forces
% figure;
% plot(T_simul, forces(:, 1), 'r', 'LineWidth', 1.5);
% hold on;
% plot(T_simul, forces(:, 2), 'b', 'LineWidth', 1.5);
% xlabel('Temps (s)');
% ylabel('Forces (N)');
% legend('Force x1', 'Force y1');
% title('Forces g�n�ralis�es');
% grid on;
%
% % Tracer les vitesses
% figure;
% plot(T_simul, velocities(:, 1), 'r', 'LineWidth', 1.5);
% hold on;
% plot(T_simul, velocities(:, 2), 'b', 'LineWidth', 1.5);
% xlabel('Temps (s)');
% ylabel('Vitesses (m/s)');
% legend('Vitesse x1', 'Vitesse y1');
% title('Vitesses g�n�ralis�es');
% grid on;
%
% % Tracer les acc�l�rations
% figure;
% plot(T_simul, accelerations(:, 1), 'r', 'LineWidth', 1.5);
% hold on;
% plot(T_simul, accelerations(:, 2), 'b', 'LineWidth', 1.5);
% xlabel('Temps (s)');
% ylabel('Acc�l�rations (m/s�)');
% legend('Acc�l�ration x1', 'Acc�l�ration y1');
% title('Acc�l�rations g�n�ralis�es');
% grid on;
%
%