Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- clear all;
- close all;
- %% Record the video
- VIDEO = 1;
- %% Initial conditions
- theta = deg2rad(20); % ramp angle (rad)
- R = 0.1; % disk radius (m)
- g = -9.81; % acceleration from gravity (m/s^2)
- m = 1; % disk mass (kg)
- thickness = 0.01;
- x_init = 0;
- y_init = 0;
- %z_init = R;
- a_init = 0;
- b_init = deg2rad(90);
- p_init = 0;
- xd_init = 0;
- yd_init = 0;
- zd_init = 0;
- ad_init = 0;
- bd_init = 0;
- pd_init = 0;
- z_init = y_init * sin(theta) + R;
- X_init = [x_init; y_init; z_init; a_init; b_init; p_init; xd_init; yd_init; zd_init; ad_init; bd_init; pd_init];
- % Start and finish times
- tspan = [0 10];
- options = odeset('RelTol',1e-7,'AbsTol',1e-7');
- % Solve the EOMs
- sol = ode45(@diskEOM, tspan, X_init, R, theta, g, m, options);
- % Evaluate the solution
- dt = 0.03;
- t = tspan(1):dt:tspan(2);
- X = deval(sol, t);
- % %% SETUP VIDEO IF REQUIRED
- % if VIDEO
- % fps = 1/dt;
- % MyVideo = VideoWriter('gyro_animation','MPEG-4');
- % MyVideo.FrameRate = fps;
- % open(MyVideo);
- % end
- %
- %
- % %% PLOT THE STATES
- % plot(t,X)
- % xlabel('time')
- % ylabel('states')
- % h = legend('$x$', '$y$', '$z$', '$\alpha$', '$\beta$', '$\phi$',...
- % '$\dot{x}$', '$\dot{y}$', '$\dot{z}$',...
- % '$\dot{\alpha}$', '$\dot{\beta}$', '$\dot{\phi}$');
- % set(h,'Interpreter','latex')
- %
- %
- %
- % %% Draw Disk
- %
- % [xcyl, ycyl, zcyl] = cylinder(R);
- % zcyl = thickness*zcyl - thickness/2;
- %
- % r_OC = [xcyl; ycyl; zcyl];
- % r_OC = r_OC + [1; 1; 0; 0; 0; 0];
- %
- %
- % %% Create Animation
- % handle = figure;
- % hold on;
- %
- % for i = 1:length(t)
- % cla
- %
- % x = X(1, i);
- % y = X(2, i);
- % z = X(3, i);
- % a = X(4, i);
- % b = X(5, i);
- % p = X(6, i);
- %
- % R10 = [1 0 0; 0 cos(theta) -sin(theta); 0 sin(theta) cos(theta)]; % Pos x rotation (slope)
- % R21 = [cos(a) -sin(a) 0; sin(a) cos(a) 0; 0 0 1]; % Pos z rotation (yaw)
- % R32 = [cos(b) 0 sin(b); 0 1 0; -sin(b) 0 cos(b)]; % Pos y rotation
- % R43 = [cos(p) -sin(p) 0; sin(p) cos(p) 0; 0 0 1]; % Pos z rotation
- %
- % % Rotate disk
- % [xcyl_r, ycyl_r, zcyl_r] = rotation(xcyl, ycyl, zcyl, R10*R21*R32*R43);
- %
- % % Disk movement
- % xcyl_r = xcyl_r + x;
- % ycyl_r = ycyl_r + y;
- % zcyl_r = zcyl_r + z;
- %
- % % For surf method (unused)
- % % xcyl_top = [xcyl(2,:); zeros(1, length(xcyl))];
- % % ycyl_top = [ycyl(2,:); zeros(1, length(ycyl))];
- % % zcyl_top = [zcyl(2,:); zcyl(2,:)];
- % %
- % % xcyl_bot = [xcyl(1,:); zeros(1, length(xcyl))];
- % % ycyl_bot = [ycyl(1,:); zeros(1, length(ycyl))];
- % % zcyl_bot = [zcyl(1,:); zcyl(1,:)];
- %
- % surf(xcyl_r, ycyl_r, zcyl_r, 'edgeColor', 'none', 'faceColor', 'black')
- % hold on
- % patch(xcyl_r(1,:), ycyl_r(1,:), zcyl_r(1,:), 4*zcyl_r(1,:));
- % patch(xcyl_r(2,:), ycyl_r(2,:), zcyl_r(2,:), 4*zcyl_r(2,:));
- %
- % % Surf method (unused)
- % %surf(xcyl_top, ycyl_top, zcyl_top, 'edgeColor', 'none')
- % %surf(xcyl_bot, ycyl_bot, zcyl_bot, 'edgeColor', 'none')
- %
- % view(3)
- % axis(1*[-1 1 -1 1 -1 1])
- %
- % % Plot plane
- % [xgrid, ygrid] = meshgrid(-1:0.05:1, -1:0.05:1);
- % zgrid = ygrid*sin(-theta);
- % surf(xgrid, ygrid, zgrid, 'edgeColor', 'none', 'faceColor', 'blue');
- %
- % xlabel('X (Meters)')
- % ylabel('Y (Meters)')
- % zlabel('Z (Meters)')
- % if VIDEO
- % writeVideo(MyVideo,getframe(handle));
- % else
- % pause(dt);
- % end
- % end
- %
- %
- % %% Video setup
- % if VIDEO
- % fps = 1/dt;
- % MyVideo = VideoWriter('disk_animation','MPEG-4');
- % MyVideo.FrameRate = fps;
- % open(MyVideo);
- % end
- %
- %
- % %%
- % function [Xf,Yf,Zf] = rotation(Xi,Yi,Zi,R)
- %
- % I=size(Xi,1);
- % J=size(Xi,2);
- %
- % Xf=zeros(I,J);
- % Yf=zeros(I,J);
- % Zf=zeros(I,J);
- %
- % for ii=1:I
- % for jj=1:J
- % vector=[Xi(ii,jj);Yi(ii,jj);Zi(ii,jj)];
- % vector=R*vector;
- % Xf(ii,jj)=vector(1);
- % Yf(ii,jj)=vector(2);
- % Zf(ii,jj)=vector(3);
- % end
- % end
- %
- % end
- %
- %% Solve the EOMs
- function X_d = diskEOM(t, X, R, theta, g, m)
- % x, y, z, alpha, beta and phi and their derivatives are parsed from the
- % state vector X
- x1 = X(1); x2 = X(2); x3 = X(3); x4 = X(4); x5 = X(5); x6 = X(6);
- x7 = X(7); x8 = X(8); x9 = X(9); x10 = X(10); x11 = X(11); x12 = X(12);
- % Assign derivatives in the state vector
- x1_d = x7; x2_d = x8; x3_d = x9; x4_d = x10; x5_d = x11; x6_d = x12;
- % Add the equations from Y for the double derivative terms
- x7_d = R*x11^2*cos(x4)*cos(x5) - (2*g*cos(x4)*sin(theta)*sin(x4))/15 - (R*x10*x12*cos(x4))/5 + R*x10^2*cos(x4)*cos(x5)^3 - (4*g*cos(theta)*cos(x4)*cos(x5)*sin(x5))/5 - (R*x10*x11*sin(x4)*sin(x5))/3 + (4*g*cos(x4)*cos(x5)^2*sin(theta)*sin(x4))/5 + (6*R*x10*x12*cos(x4)*cos(x5)^2)/5;
- x8_d = R*x11^2*sin(theta)*sin(x5) - (2*g*sin(2*theta))/5 + (4*g*cos(x5)*sin(x4)*sin(x5))/5 + (2*g*cos(theta)*cos(x4)^2*sin(theta))/15 + (8*g*cos(theta)*cos(x5)^2*sin(theta))/5 - R*x10^2*sin(theta)*sin(x5)*(sin(x5)^2 - 1) + R*x11^2*cos(theta)*cos(x5)*sin(x4) - (R*x10*x12*cos(theta)*sin(x4))/5 + R*x10^2*cos(theta)*cos(x5)^3*sin(x4) - (8*g*cos(theta)^2*cos(x5)*sin(x4)*sin(x5))/5 - (4*g*cos(theta)*cos(x4)^2*cos(x5)^2*sin(theta))/5 + (R*x10*x11*cos(theta)*cos(x4)*sin(x5))/3 + (6*R*x10*x12*cos(x5)*sin(theta)*sin(x5))/5 + (6*R*x10*x12*cos(theta)*cos(x5)^2*sin(x4))/5;
- x9_d = (2*g*cos(x4)^2)/15 - (4*g)/5 + (4*g*cos(x5)^2)/5 + (4*g*cos(theta)^2)/5 - (2*g*cos(theta)^2*cos(x4)^2)/15 - (8*g*cos(theta)^2*cos(x5)^2)/5 - (4*g*cos(x4)^2*cos(x5)^2)/5 - R*x11^2*cos(theta)*sin(x5) + (4*g*cos(theta)^2*cos(x4)^2*cos(x5)^2)/5 + R*x11^2*cos(x5)*sin(theta)*sin(x4) - (R*x10*x12*sin(theta)*sin(x4))/5 - R*x10^2*cos(theta)*cos(x5)^2*sin(x5) + R*x10^2*cos(x5)^3*sin(theta)*sin(x4) - (8*g*cos(theta)*cos(x5)*sin(theta)*sin(x4)*sin(x5))/5 - (6*R*x10*x12*cos(theta)*cos(x5)*sin(x5))/5 + (R*x10*x11*cos(x4)*sin(theta)*sin(x5))/3 + (6*R*x10*x12*cos(x5)^2*sin(theta)*sin(x4))/5;
- x10_d = (2*x11*x12)/sin(x5);
- x11_d = -(5*R*cos(x5)*sin(x5)*x10^2 + 6*R*x12*sin(x5)*x10 + 4*g*cos(theta)*cos(x5) + 4*g*sin(theta)*sin(x4)*sin(x5))/(5*R);
- x12_d = (2*g*cos(x4)*cos(x5)^2*sin(theta) - 5*R*x10*x11*sin(x5) - 2*g*cos(x4)*sin(theta) + 6*R*x11*x12*cos(x5)*sin(x5) + 5*R*x10*x11*cos(x5)^2*sin(x5))/(3*R*(cos(x5)^2 - 1));
- X_d = [x1_d; x2_d; x3_d; x4_d; x5_d; x6_d; x7_d; x8_d; x9_d; x10_d; x11_d; x12_d];
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement