• Remix
  • Share
  • New Entry

on 18 Oct 2024
  • 13
  • 86
  • 0
  • 0
  • 1929
How can I not put a pumpkin on this cool walking sim?
for i=1:45; drawframe(i); end
Write your drawframe function below
function drawframe(f)
% Simulation of the "Simplest Walking Model"
%
% See:
% https://github.com/horchler/Passive-Dynamic-Walking
% https://www.mathworks.com/matlabcentral/fileexchange/56859-passive-dynamic-walking
%
% Based on:
%
% M. Garcia, A. Chatterjee, A. Ruina, and M. Coleman, "The Simplest
% Walking Model: Stability, Complexity, and Scaling," ASME Journal of
% Biomedical Engineering, Vol. 120, No. 2, pp. 281-288, 1998.
% http://dx.doi.org/10.1115/1.2798313
% Incline slope angle in radians
gam = 0.015;
% Use persisent variable to state of dynamic system between frames
persistent y0
if f==1
% Calculate stable initial conditions
y0(1) = 0.970956*gam^(1/3)-0.270837*gam;
y0(2) = -1.045203*y0(1)+1.062895*gam;
y0(3) = 2*y0(1);
y0(4) = y0(2)*(1-cos(y0(3)));
% Initialize drawing
plotwalker(y0, false, gam, f);
return;
end
% TFINAL integration time in seconds
tf = 0.3;
% Turn on collision detection
opts = odeset('Events', @collision);
% Integrate to TFINAL or colleion condition
[t, y] = ode45(@(t, y)eom(t, y, gam), [0 tf], y0, opts);
% Determine if integration stopped due to collision
iscollision = (t(end) ~= tf);
% Update persistent y0 to last state of integration
y0 = y(end, :);
% Draw walker
plotwalker(y0, iscollision, gam, f);
% Mapping to calculate new initial conditions after collision
if iscollision
c2y1 = cos(2*y0(1));
y0 = [-y0(1); c2y1*y0(2); -2*y0(1); c2y1*(1-c2y1)*y0(2)];
end
end
function ydot = eom(~, y, gam)
% First order differential equations for Simplest Walking Model
% y(1): theta
% y(2): thetadot
% y(3): phi
% y(4): phidot
ydot2 = sin(y(1)-gam);
ydot = [y(2); ydot2; y(4); ydot2+sin(y(3))*(y(2)^2-cos(y(1)-gam))];
end
function [val, ist, dir] = collision(~, y)
% Check for heelstrike collision using zero-crossing detection
val = y(3)-2*y(1); % Geometric collision condition, when = 0
ist = 1; % Stop integrating if collision found
dir = 1; % Condition only true when passing from - to +
end
function plotwalker(y, iscollision, gam, f)
% Simple drawing routine to animate passive dynamic walker
% Leg length
L = 1.5;
persistent xst stleg swleg hip punk
if f==1
xst = [0; 0];
[xm, xsw] = kinematics(y, xst, gam, L);
% Set up plot
set(gcf, 'Color', 'w');
w = 4.221;
axis([0 w -0.5*xm(2) 1.5*xm(2)]);
axis off;
% Draw incline
line([xsw(1) w], [xsw(2) (xsw(1)-w)*tan(gam)], 'Color', [0.55 0.27 0.08], 'linewidth', 3);
% Set line and marker colors and styles
stleg = line('Color', [0 0 0.6], 'LineWidth', 3);
swleg = line('Color', [0 0 1], 'LineWidth', 3);
hip = line('Color', [0 0.2 0.6], 'LineStyle', 'none', 'Marker', '.', 'MarkerSize', 40);
punk = hgtransform;
% From pumpkin minihack
[X,Y,Z]=sphere(200);
R=1+(-(1-mod(0:.1:20,2)).^2)/20;
surface(punk,R.*X,R.*Y,(.8+(0-(1:-.01:-1)'.^4)*.2).*Z.*R,'FaceC','#ff7518','EdgeC','n');
surface(punk,X/12,Y/12,Z/2+.6,'FaceC', '#008000', 'EdgeC','n');
daspect([1 1 1]);
lighting g
material([.6 .9 .3 2 .5])
camlight
set(gca,'pos',[0 0 1 1]);
end
% Calculate hip and swing foot positions
[xm, xsw] = kinematics(y, xst, gam, L);
% Update locations of hip and swing foot
set(stleg, 'XData', [xst(1) xm(1)], 'YData', [xst(2) xm(2)]);
set(swleg, 'XData', [xsw(1) xm(1)], 'YData', [xsw(2) xm(2)]);
set(hip, 'XData', xm(1), 'YData', xm(2));
set(punk, 'Matrix', makehgtform('translate', [xm' 0],'scale',.35,'xrotate',-pi/2));
% On collision switch stance and swing legs
if iscollision
xst = xsw;
h = stleg;
stleg = swleg;
swleg = h;
end
end
function [xm, xsw] = kinematics(y, xst, gam, L)
% Calculate forward kinematics for hip (xm) and swing foot (xsw) positions
y1g = y(1)-gam;
xm = xst+L*[-sin(y1g); cos(y1g)];
xsw = xm-L*[sin(y(3)-y1g); cos(y(3)-y1g)];
end
Movie
Audio

This submission does not have audio.

Remix Tree