Setting axes for ginput
18 次查看(过去 30 天)
显示 更早的评论
I am trying to set waypoints for a direct drive robot to go to. My code goes like this:
waypoints=[0,0;...
1,1;...
2,10;...
3,8;...
1,5;...
4,2;...
ginput];
But when the ginput comes up the axis is from 0 to 1 on both x and y, but I would like to set the axes to be bigger to match the scale of the other waypoints.
Could someone please advise me on how to do this? thank you
P.S. this is my whole code
close all
clear all
waypoints=[0,0;...
1,1;...
2,10;...
3,8;...
1,5;...
4,2;...
ginput];
speed_nominal=0.2;
t0=0;
v0=[1,0];%initial velocity
t1=t0;dt=0.01;
v1=v0;
x_store=waypoints(1,1);
y_store=waypoints(1,2);
vx_store=v0(1);
vy_store=v0(2);
t_store=t0;
[m,n]=size(waypoints);
for i=1:m-1
P1=waypoints(i,:)
P2=waypoints(i+1,:);
d=norm(P2-P1);
t2=t1+d/speed_nominal;
%d: the distance between two consecutive points;
%t2-t1=d/nominal speed;
if i==m-1
v2=[0,0];
else
P3=waypoints(i+2,:);
v2=speed_nominal*(P3-P2)/norm(P3-P2);
end
ax=trajgene(P1(1),v1(1),t1,P2(1),v2(1),t2);
ay=trajgene(P1(2),v1(2),t1,P2(2),v2(2),t2);
for t=t1:dt:t2
xt=traj(ax,t);
yt=traj(ay,t);
vxt=generated_speed(ax,t);
vyt=generated_speed(ay,t);
x_store=[x_store;xt];
y_store=[y_store;yt];
vx_store=[vx_store;vxt];
vy_store=[vy_store;vyt];
t_store=[t_store;t];
end
t1=t2;
v1=v2;
end
figure(1),clf(1),
subplot(2,1,1),plot(t_store,x_store),title('x(t) vs. t');
subplot(2,1,2),plot(t_store,y_store),title('y(t) vs. t');
figure(2),clf(2),
subplot(2,1,1),plot(t_store,vx_store),title('vx(t) vs. t');
subplot(2,1,2),plot(t_store,vy_store),title('vy(t) vs. t');
figure(3),clf(3),
plot(x_store,y_store);title('smooth trajectory: x--waypoints; solid line--generated traj')
hold on,
plot(waypoints(:,1),waypoints(:,2),'x')
axis equal
Other functions, if this helps:
function vt=generated_speed(a,t)
%Given a0,a1,a2,a3,a4 with specified time t, compute x(t)
a0=a(1);a1=a(2);a2=a(3);a3=a(4);a4=a(5);
vt=a1+2*a2*t+3*a3*t^2+4*a4*t^3;
function a=trajgene(x1,vx1,t1,x2,vx2,t2)
%compute a=[a0,a1,a2,a3,a4] from x1,vx1,t1,x2,vx2,t2
b=[x1;vx1;x2;vx2];
A=[1,t1,t1^2,t1^3,t1^4;...
0,1,2*t1,3*t1^2,4*t1^3;...
1,t2,t2^2,t2^3,t2^4;...
0,1,2*t2,3*t2^2,4*t2^3];
B=[0,0,0,0,0;...
0,0,0,0,0;...
0,0,t2-t1,3*(t2^2-t1^2)/2,2*(t2^3-t1^3);...
0,0,3*(t2^2-t1^2)/2,3*(t2^3-t1^3),9*(t2^4-t1^4)/2;...
0,0,2*(t2^3-t1^3),9*(t2^4-t1^4)/2,36*(t2^5-t1^5)/5];
C=zeros(5,9);C(1,1)=1;C(2,2)=1;C(3,3)=1;C(4,4)=1;C(5,5)=1;
M1=[B,A';A,zeros(4,4)];
V1=[zeros(5,1);b];
a=C*inv(M1)*V1;
function xt=traj(a,t)
%Given a0,a1,a2,a3,a4 with specified time t, compute x(t)
a0=a(1);a1=a(2);a2=a(3);a3=a(4);a4=a(5);
xt=a0+a1*t+a2*t^2+a3*t^3+a4*t^4;
0 个评论
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Exploration 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!