Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. function [y1, y2] = att_ctrl(gamma,u,quad)
  2. %#codegen
  3. %State Measurements
  4.  
  5.  
  6. xE = u(1); yE = u(2); zE = u(3);
  7. phi = u(4); the = u(5); psi = u(6);
  8. vx = u(7); vy = u(8); vz = u(9);
  9. p = u(10); q = u(11); r = u(12);
  10.  
  11. %Commands
  12. the_d = u(13); the_d_dot = u(14); the_d_ddot = u(15);
  13. phi_d = u(16); phi_d_dot = u(17); phi_d_ddot = u(18);
  14. z_d = u(19); z_d_dot = u(20); z_d_ddot = u(21);
  15. psi_d = u(22); psi_d_dot = u(23); psi_d_ddot = u(24);
  16.  
  17. % If there is a fault configured we will do different adaptive laws
  18. % has_fault=fault(1,2);
  19.  
  20. %LOE
  21. theta_hat=[0;0;0;0];
  22. theta_hat(1) = u(25);
  23. theta_hat(2) = u(26);
  24. theta_hat(3) = u(27);
  25. theta_hat(4) = u(28);
  26.  
  27. % This is used for I4-Lambda
  28. theta_I = diag([1-theta_hat(1) 1-theta_hat(2) 1-theta_hat(3) 1-theta_hat(4)]);
  29.  
  30. % The q1/alpha function derivitives
  31. q1_dot=[u(29); u(30); u(31); u(32)];
  32.  
  33. % Lambda matrices
  34. lam_1=diag([1;0;0;0]);
  35. lam_2=diag([0;1;0;0]);
  36. lam_3=diag([0;0;1;0]);
  37. lam_4=diag([0;0;0;1]);
  38.  
  39.  
  40. Cd = quad.Cd;
  41. m = quad.M; g = quad.g;
  42. Jx = quad.Ixx; Jy = quad.Iyy; Jz = quad.Izz;
  43. Jbarx = (Jy-Jz)/Jx; Jbary = (Jz-Jx)/Jy; Jbarz = (Jx-Jy)/Jz;
  44. d = quad.d; b = quad.b; k = quad.k;
  45. sq2=sqrt(2);
  46. Map = [b b b b;
  47. d*b/sq2 -d*b/sq2 -d*b/sq2 d*b/sq2;
  48. d*b/sq2 d*b/sq2 -d*b/sq2 -d*b/sq2;
  49. k -k k -k ];
  50.  
  51. % define g1
  52. g1 = [ 1 0 0 0 ;
  53. 0 1 sin(phi)*tan(the) cos(phi)*tan(the);
  54. 0 0 cos(phi) -sin(phi);
  55. 0 0 sin(phi)*sec(the) cos(phi)*sec(the)];
  56.  
  57. % define f2
  58. f2 = [ g-(Cd/m)*vz;
  59. Jbarx*q*r;
  60. Jbary*p*r;
  61. Jbarz*p*q];
  62.  
  63. % define g2
  64. g2 = [ -(cos(phi)*cos(the))/m 0 0 0;
  65. 0 1/Jx 0 0;
  66. 0 0 1/Jy 0;
  67. 0 0 0 1/Jz];
  68.  
  69. % set the variables for the backstepping controller here
  70. x1 = [zE; phi; the; psi];
  71. % these are the desired positions/angles
  72. x1_d = [z_d; phi_d; the_d; psi_d];
  73. % derivitives
  74. x1_d_dot = [z_d_dot; phi_d_dot; the_d_dot; psi_d_dot];
  75.  
  76.  
  77. % control variables we want to drive to 0
  78. x2 = [vz; p; q; r];
  79.  
  80. z1 = x1-x1_d;
  81. %q1
  82. k1=diag([1 1 1 1]);
  83. k2=diag([1 1 1 1]);
  84. c1=diag([1 1 1 1]);
  85. c2=diag([1 1 1 1]);
  86. c3=diag([10 10 10 10]);
  87.  
  88. %q1=inv(k1*g1)*(-k1*z1-k2*(x1-x1_d)+k1*x1_d_dot);
  89. q1 = (c1*g1)\(c1*x1_d_dot - c2*z1);
  90.  
  91. z2 = x2 - q1;
  92.  
  93. q1_dot = [0;0;0;0];
  94.  
  95. Omega_bar = (g2*Map*theta_I)\(q1_dot - f2 - (c1*g1)'*z1-c3*z2);
  96.  
  97. w_squared = Omega_bar;
  98.  
  99. % adaptive laws for each rotor
  100. THETA_h_dot = [0;0;0;0];
  101. % if has_fault>0
  102. THETA_h_dot(1) = -gamma*(z2'*g2*Map*lam_1*Omega_bar);
  103. THETA_h_dot(2) = -gamma*(z2'*g2*Map*lam_2*Omega_bar);
  104. THETA_h_dot(3) = -gamma*(z2'*g2*Map*lam_3*Omega_bar);
  105. THETA_h_dot(4) = -gamma*(z2'*g2*Map*lam_4*Omega_bar);
  106. % end
  107.  
  108. w1 = sqrt(abs(w_squared(1)));
  109. w2 = -sqrt(abs(w_squared(2)));
  110. w3 = sqrt(abs(w_squared(3)));
  111. w4 = -sqrt(abs(w_squared(4)));
  112. w = [w1;w2;w3;w4];
  113.  
  114. y1 = [w;theta_hat];
  115. y2 = [THETA_h_dot;q1];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement