I want to simulate a vehicle which tracking known path, what I'm working on is just an animated shape moving at the path, I wrote my code under matlab I get the square (represent the vehicle) moving over the path, but, I want to remove the last squares(memories), just want one square every time with right orientation. anyone please advice me what I can do to achieve that. my code illustrate what I explained previously:
r=0.25; t=0:0.1:10; A=100; yo=A*sin(t);%%desired output position on Y axe Voy=diff( yo); Vox=ones(1,101); Voy(length(Voy)+1)=Voy(length(Voy)) Vmx=sqrt(Voy.*Voy+Vox.*Vox); theta=atan(Voy./Vox); %%Speed's equations Voy=Vmx.*sin(theta); Vox=Vmx.*cos(theta); x=0; y=0; t=0; for i=1:1:length(Voy); x=Vox(i)*(t); y=Voy(i)*0.1; pause(0.05) t=t+0.1; plot(x,y,'--gs',... 'LineWidth',2,... 'MarkerSize',30,... 'MarkerEdgeColor','b',... 'MarkerFaceColor',[0.5,0.5,0.5]) hold on end
I think the following code does what you are looking for:
r=0.25;
t=0:0.1:10;
A=100;
yo=A*sin(t);%%desired output position on Y axe
Voy=diff( yo);
Vox=ones(1,101);
Voy(length(Voy)+1)=Voy(length(Voy))
Vmx=sqrt(Voy.*Voy+Vox.*Vox);
theta=atan(Voy./Vox);
%%Speed's equations
Voy=Vmx.*sin(theta);
Vox=Vmx.*cos(theta);
x=0;
y=0;
t=0;
figure;
ph = plot(x,y,'--gs',...
'LineWidth',2,...
'MarkerSize',30,...
'MarkerEdgeColor','b',...
'MarkerFaceColor',[0.5,0.5,0.5]);
set(gca,'XLim',[0 10]);
set(gca,'YLim',[-2 2]);
for i=1:1:length(Voy);
x=Vox(i)*(t);
y=Voy(i)*0.1;
set(ph,'XData',x);
set(ph,'YData',y);
pause(0.05)
t=t+0.1;
end
The trick is, using the handle of the plot for updating its data in the for-loop. This can be done by changing the XData
and YData
property of the plot-object:
set(ph,'XData',x);
set(ph,'YData',y);
Furthermore I would manually set the X- and y-Limits of the axes by
set(gca,'XLim',[0 10]);
set(gca,'YLim',[-2 2]);
Otherwise it is set dynamically according to the current data, set in the several steps.