Advertisement
zero_rock

revolute joint

Jul 28th, 2020 (edited)
3,586
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 5.09 KB | None | 0 0
  1.  clc;
  2.  clear all;
  3.  close all;
  4.  
  5.  m_i=1; %mass of rod i
  6.  m_j=1; %mass of rod j
  7.  l_i = 0.1; %length of rod i
  8.  l_j = 1; %length of rod j
  9.  g=9.81; %acceleraation due to gravity
  10.  T_i=0; %external torque on rod i
  11.  T_j=0; %external torque on rod j
  12.  anglei= -90; %initial angle between global coordinate frame and xi_i, eta_i coordinate frame in degrees
  13.  anglej= -87; %initial angle between global coordinate frame and xi_j, eta_j coordinate frame in degrees
  14.  thetai=anglei*pi/180; %initial angle between global coordinate frame and xi_i, eta_i coordinate frame
  15.  thetaj=anglej*pi/180; %initial angle between global coordinate frame and xi_j, eta_j coordinate frame
  16.  xi_eta_i_theta= pi; %constant angular position of joint in xi_i and eta_i coordinates
  17.  xi_eta_j_theta= pi; %constant angular position of joint in xi_j and eta_j coordinates
  18.  
  19.  
  20.  xi_p_i= l_i/2*cos(xi_eta_i_theta); %x position of joint in xi_i and eta_i coordinates; constant
  21.  eta_p_i=l_i/2*sin(xi_eta_i_theta); %y position of joint in xi_i and eta_i coordinates; constant
  22.  
  23.  xi_p_j= l_j/2*cos(xi_eta_j_theta); %x position of joint in xi_j and eta_j coordinates; constant
  24.  eta_p_j=l_j/2*sin(xi_eta_j_theta); %y position of joint in xi_j and eta_j coordinates; constant
  25.  
  26.  %%%coordinate positon of joint p
  27.  x_p = 0; y_p = 0;
  28.  
  29.  %Position of the local coordinate origin
  30.  x_i = x_p - xi_p_i*cos(thetai) + eta_p_i*sin(thetai) ;
  31.  y_i = y_p - xi_p_i*sin(thetai) - eta_p_i*cos(thetai) ; %Position of local coordinate origin of body i in global coordinates
  32.  
  33.  x_j = x_p - xi_p_j*cos(thetaj) + eta_p_j*sin(thetaj) ;
  34.  y_j = y_p - xi_p_j*sin(thetaj) - eta_p_j*cos(thetaj); %Position of local coordinate origin of body j in global coordinates
  35.  
  36.  I_i=1/3*m_i*l_i^2; %Inertia about coordinate position
  37.  I_j=1/3*m_j*l_j^2; %Inertia about coordinate position
  38.  
  39.  M_i=[m_i 0 0; 0 m_i 0; 0 0 I_i];
  40.  M_j=[m_j 0 0; 0 m_j 0; 0 0 I_j];
  41.  
  42.  M=[M_i, zeros(3); zeros(3),M_j]; %Mass Matrix
  43.  
  44.  %Define Numerical Parameters
  45.  t_lim=3; %Time limit for simulation
  46.  
  47.  n=500; %Number of discretization steps
  48.  
  49.  %Setup a time vector of length n+1 with time values from 0 to t_lim
  50.  t(n+1)=0; %Initial array
  51.  for k=1:n+1
  52.  t(k)=t_lim/n*(k-1);
  53.  end
  54.  dt=t(2)-t(1);
  55.  
  56.  %Inintial angular position at coordinate positon
  57.  theta_i(n+1)=0; theta_j(n+1)=0;
  58.  theta_i(1)=thetai; theta_j(1)=thetaj;
  59.  
  60.  %Initial angular velocity at coodinate position
  61.  omega_i(n+1)=0; omega_j(n+1)=0;
  62.  omega_i(1)=0; omega_j(1)= 0;
  63.  
  64.  %Initial angular acceleration at coordinate position
  65.  alpha_i(n+1)=0; alpha_j(n+1)=0;
  66.  alpha_i(1)=0; alpha_j(1)=0;
  67.  
  68.  %Initial linear velocity at coordinate position
  69.  vx_i(n+1)=0; vy_i(n+1)=0; vx_j(n+1)=0; vy_j(n+1)=0;
  70.  vx_i(1)=0; vy_i(1)=0; vx_j(1)=0; vy_j(1)=0;
  71.  
  72.  %Inintial linear acceleration at coordinate positon
  73.  ax_i(n)=0; ay_i(n)=0; ax_j(n)=0; ay_j(n)=0;
  74.  
  75.  %%%Arm coordinates for ploting graph
  76.  x_jplot(n+1)=0; x_jplot(1)=x_p+l_j*cos(theta_j(1));
  77.  y_jplot(n+1)=0; y_jplot(1)=y_p+l_j*sin(theta_j(1));
  78.  
  79.  
  80.  x_iplot(n+1)=0; x_iplot(1)=x_p+l_i*cos(theta_i(1));
  81.  y_iplot(n+1)=0; y_iplot(1)=y_p+l_i*sin(theta_i(1));
  82.  
  83.  D(1:2,1:6,n)=0; %Jacobian Matrix initialization
  84.  gamma(1:2,n)=0; %Gamma Vector Initialization
  85.  acc_lagrand(1:8,n)=0; %Acceleration vector and Langrange multipliers initialization
  86.  
  87.  for k=1:n
  88.  %Jacobian Matrix
  89.  D(1:2,1:6,k) =[ -1 0 xi_p_i*sin(theta_i(k))+eta_p_i*cos(theta_i(k)) 1 0 -xi_p_j*sin(theta_j(k))-eta_p_j*cos(theta_j(k));
  90.  0 -1 -xi_p_i*cos(theta_i(k))+eta_p_i*sin(theta_i(k)) 0 1 xi_p_j*cos(theta_j(k))-eta_p_j*sin(theta_j(k))];
  91.  
  92.  %Gamma vector
  93.  gamma(1:2,k)=-[xi_p_i*cos(theta_i(k))*(omega_i(k))-eta_p_i*sin(theta_i(k))*(omega_i(k)),-xi_p_j*cos(theta_j(k))*(omega_j(k))+eta_p_j*sin(theta_j(k))*(omega_j(k));
  94. xi_p_i*sin(theta_i(k))*(omega_i(k))+eta_p_i*cos(theta_i(k))*(omega_i(k)), -xi_p_j*sin(theta_j(k))*(omega_j(k))-eta_p_j*cos(theta_j(k))*(omega_j(k))]*[ omega_i(k); omega_j(k)];
  95.  
  96.  %Acceleration vector and Langrange multipliers vector
  97.  acc_lagrand(1:8,k) = inv([M, (-D(1:2,1:6,k)'); D(1:2,1:6,k), zeros(2)]) * [0; 0; T_i ; 0; -m_j*g;
  98. -m_j*g*l_j/2*cos(theta_j(k))+T_j; gamma(1:2,k)];
  99.  
  100.  %Linear Acceleration in x and y coordinate
  101.  ax_i(k)=acc_lagrand(1:1,k);
  102.  ay_i(k)=acc_lagrand(2:2,k);
  103.  ax_j(k)=acc_lagrand(4:4,k);
  104.  
  105.  ay_j(k)=acc_lagrand(5:5,k);
  106.  
  107.  %Angular acceleration
  108.  alpha_i(k)=acc_lagrand(3:3,k);
  109.  alpha_j(k)=acc_lagrand(6:6,k);
  110.  
  111.  %linear and angular velocity of arm i
  112.  vx_i(k+1)= vx_i(k)+ acc_lagrand(1:1,k)*dt;
  113.  vy_i(k+1)= vy_i(k)+acc_lagrand(2:2,k)*dt;
  114.  omega_i(k+1)=omega_i(k)+acc_lagrand(3:3,k)*dt;
  115.  omega_i(k+1) = 0;
  116.  
  117.  %Linear and angular velocity of arm j
  118.  vx_j(k+1)= vx_j(k)+ acc_lagrand(4:4,k)*dt;
  119.  vy_j(k+1)= vy_j(k)+acc_lagrand(5:5,k)*dt;
  120.  omega_j(k+1)=omega_j(k)+acc_lagrand(6:6,k)*dt;
  121.  
  122.  %omega_i(k+1)=omega_i(1);
  123.  theta_i(k+1)=theta_i(k)+ omega_i(k+1)*dt;
  124.  %theta_i(k+1)=theta_i(1);
  125.  theta_j(k+1)=theta_j(k)+ omega_j(k+1)*dt;
  126.  
  127.  %%%Arm coordinates for Animation
  128.  x_jplot(k+1)=x_p+l_j*cos(theta_j(k+1));
  129.  y_jplot(k+1)=y_p+l_j*sin(theta_j(k+1));
  130.  
  131.  x_iplot(k+1)=x_p+l_i*cos(theta_i(k+1));
  132.  y_iplot(k+1)=y_p+l_i*sin(theta_i(k+1));
  133.  
  134.  plot(x_iplot, y_iplot,'r', x_jplot, y_jplot,'g')
  135.     axis([-.1 .1 -1.5 .5]); drawnow;
  136.  
  137.  
  138.  
  139.  end
  140.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement