Advertisement
rikisu_

SMR LAB Matlab Code with explanation comments

Apr 11th, 2024 (edited)
465
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.38 KB | Source Code | 0 0
  1. % Clear workspace, command window, and close all figures
  2. clear; clc; close all;
  3.  
  4. % Define time parameters
  5. dt = 0.1; % Time step
  6. ts = 10;  % Total simulation time
  7. t = 0:dt:ts; % Time vector
  8.  
  9. % Initial robot state
  10. x = 0;    % Initial x-position
  11. y = 0;    % Initial y-position
  12. psi = 0;  % Initial orientation angle
  13. eta = [x; y; psi]; % State vector [x, y, psi]
  14.  
  15. % Robot dimensions
  16. a = 0.1; % Wheel radius
  17. d = 0.2; % Distance from the center to the wheel
  18. l = 0.5; % Length between wheels
  19.  
  20. % Define the relative positions of robot corners
  21. mr_co = [-l/2, l/2, l/2, -l/2;
  22.          -d,    -d,   d,   d];
  23. % MR_CO FOR OMNI WHEEL NOT NEEDED AS PER SIR
  24. %mr_co = [l, (-sqrt((l^2)-((w/2)^2))),(-sqrt((l^2)-((w/2)^2)));
  25. %        0,                      w/2,                   -w/2];
  26.  
  27. %For Kinematics use u v r
  28. %u = 0.4; v = 0; r = 0.2;
  29. %zeta=[u;v;r];
  30.  
  31. % For Kinematics of DIFFERENTIAL bot use this W and omega
  32. %omega = [0.4;0.4];
  33. %W = [a/2,a/2;            0,0;       -a/(2*d), a/(2*d)];
  34. %zeta=(W * omega);
  35.  
  36. % For Kinematics of OMNI bot use this W and omega
  37. %omega = [0; 0.2; -0.2;]; % angular velocities of three wheels
  38. %W = a/3*[0,-sqrt(3),sqrt(3);     2,-1,-1;      1/l,1/l,1/l]
  39. %zeta=(W * omega);
  40.  
  41. % For Kinematics of MECHANUM bot use this W and omega
  42. %omega = [0.5;0.5;0.5;0.5]; % angular velocities of four wheels
  43. %W = a/4*[1,1,1,1;       1,-1,1,-1;       -1/(d-l),-1/(d-l),1/(d-l),1/(d-l)];
  44. %zeta=(W * omega);
  45.  
  46. % Main simulation loop
  47. for i = 1:length(t)
  48.     % Calculate the Jacobian matrix
  49.     Jacobian = [cos(eta(3,i)), -sin(eta(3,i)), 0;
  50.              sin(eta(3,i)), cos(eta(3,i)),  0;
  51.              0,              0,             1];
  52.    
  53.     % Compute the time derivative of the robot state
  54.     eta_dot = Jacobian * zeta;
  55.    
  56.     % Update the robot state using Euler integration
  57.     eta(:,i+1) = eta(:,i) + dt * eta_dot;
  58. end
  59.  
  60. % Animate the robot's trajectory and orientation
  61. figure
  62. for i = 1:length(t)
  63.     % Calculate the positions of robot corners in global coordinates
  64.     pos = [cos(eta(3,i)), -sin(eta(3,i));
  65.            sin(eta(3,i)),  cos(eta(3,i))] * mr_co;
  66.    
  67.     % Plot the robot
  68.     fill(pos(1,:)+eta(1,i), pos(2,:)+eta(2,i), 'o')
  69.     hold on, grid on, axis equal;
  70.     plot(eta(1,1:i), eta(2,1:i), 'b-');
  71.     axis([-1 5 -1 5]);
  72.     xlabel('x[m]'); ylabel('y[m]');
  73.     legend('MR','Path'), set(gca,'fontsize',24)
  74.     pause(0.001);
  75.     hold off;
  76. end
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement