Index in position 1 is invalid. Array indices must be positive integers or logical values.

I am getting an error in line: while m(xo,yo)==0 %if the location not occupied
the error is '' Index in position 1 is invalid. Array indices must be positive integers or logical values''
and my code is:
xo=-13; yo=-13; th=pi/2;
x=-13; y=-13;
rr=1; %Robot radius
r=rr; %ray vector
u=Control(); % control function
Unrecognized function or variable 'Control'.
n=4; %Number of sensors
dT=0.4;
dbstop if error
for t=1:size(u,1)
v=u(1,t);
if u(2,t)~=0
w=u(2,t);
else
w=100*eps; % 100*eps because eps alone is too small, so causes division by zero problem
end
subplot(1,2,1);
m=Map(); %map function
x=x-v/w*sin(th)+v/w*sin(th+w*dT);%state update
y=y+v/w*cos(th)-v/w*cos(th+w*dT);%state update
th=th+w*dT;%state update
for k=0:n-1
th=th+2*pi*k/n;
rr=1; r=rr;
xo=xo+r*cos(th); %corridnates(x,y) for the robot location
yo=yo+r*sin(th); %r is length(r)
xo=ceil((xo*641*0.05)-1);
yo=ceil(yo);
while m(xo,yo)==0 %if the location not occupied %%% my error in this line
r=rr+0.05;
if m(xo,yo)==1 %if the location occupied
subplot(1,2,2);
mesh(xo,yo,m); view(0, 90); axis image; hold on;
plot(xo,yo,'b.');
end
end
end
end

Risposte (2)

Would be very useful if you are able to post a minimal program exhibiting the error that we could run. In the debugger, set the Run button to "Pause on Error". When the program breaks at the error, check that xo and yo are scalars, and check that they are both greater than 0.

16 Commenti

Now I fixed the error but I didn't get the right output till now. the output should have an empty white map with dimension (from 1 to 641 in both x & y axis) and once the m(xo, yo) ==1 then it should label the empty map with blue dot each time on every loop. I think my problem in the last 3 lines from the code which are:
if m(xo,yo) == 1 %if the location is occupied
subplot(1,2,2); plot(m(xo,yo) ,'b.'); hold on;
note: the attachment showing the required output
Glad that you were able to fix the indexing error. Very hard to debug the image error without running code. But I do have this question for you:
while m(xo,yo)==0 %if the location not occupied %%% my error in this line
% at this point, it must be true that the value in m(xo,yo) is 0
% (because of the while expression: m(xo,yo)==0)
r=rr+0.05;
% This this if expression must be false since m(xo,yo) does not equal 1
if m(xo,yo)==1 %if the location occupied
subplot(1,2,2);
mesh(xo,yo,m); view(0, 90); axis image; hold on;
plot(xo,yo,'b.');
end
% so the if statement is not executed, and you are back at the while
% loop. And this looks like an infinite loop since m(xo,yo) is not
% changing within the while-loop. This is about all I could analyze
% without a running program.
% What am I missing here, since you are not having an infinite loop?
end
Here are my both functions with the first code , I think you can debug the code now.
this is my control function:
function u= Control()
x=-13; y=-13; th=pi/2; % initial state
r=1; % r is robot radius
dT=0.4;
t1=[0:dT:3]; % NOISELESS
u1=[0*ones(size(t1)); -1*ones(size(t1))];
t2=[0:dT:0.8]; % NOISELESS
u2=[1*ones(size(t2)); 0*ones(size(t2))];
t3=[0:dT:1.5]; % NOISELESS
u3=[0*ones(size(t3)); 1*ones(size(t3))];
t4=[0:dT:6]; % NOISELESS
u4=[1*ones(size(t4)); 0*ones(size(t4))];
t5=[0:dT:1.5]; % NOISELESS
u5=[0*ones(size(t5)); 1*ones(size(t5))];
t6=[0:dT:1.6]; % NOISELESS
u6=[1*ones(size(t6)); 0*ones(size(t6))];
t7=[0:dT:1.5]; % NOISELESS
u7=[0*ones(size(t7)); -1*ones(size(t7))];
t8=[0:dT:19]; % NOISELESS
u8=[1*ones(size(t8)); 0*ones(size(t8))];
t9=[0:dT:1.55]; % NOISELESS
u9=[0*ones(size(t9)); 1*ones(size(t9))];
t10=[0:dT:25.6]; % NOISELESS
u10=[1*ones(size(t10)); 0*ones(size(t10))];
t11=[0:dT:1.5]; % NOISELESS
u11=[0*ones(size(t11)); 0.98*ones(size(t11))];
t12=[0:dT:24]; % NOISELESS
u12=[1*ones(size(t12)); 0*ones(size(t12))];
t13=[0:dT:1.5]; % NOISELESS
u13=[0*ones(size(t13)); 0.98*ones(size(t13))];
t14=[0:dT:13]; % NOISELESS
u14=[1*ones(size(t14)); 0*ones(size(t14))];
t15=[0:dT:1.5]; % NOISELESS
u15=[0*ones(size(t15)); 0.98*ones(size(t15))];
t16=[0:dT:3.5]; % NOISELESS
u16=[1*ones(size(t16)); 0*ones(size(t16))];
t17=[0:dT:1.2]; % NOISELESS
u17=[0*ones(size(t17)); -0.94*ones(size(t17))];
t18=[0:dT:24]; % NOISELESS
u18=[1*ones(size(t18)); 1/8.2*ones(size(t18))];
t19=[0:dT:1.59]; % NOISELESS
u19=[0*ones(size(t19)); 1.01*ones(size(t19))];
t20=[0:dT:7.9]; % NOISELESS
u20=[1*ones(size(t20)); 0*ones(size(t20))];
u=[u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20]; % concatenate all controls into one
end
%%%%%%%%%%%%%%%%%
And this is my map function:
function m= Map()
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Start @ location (-13,-13). Apply controls to reach (0,0) %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
xw=-16; xe=16; ys=-16; yn=16; %map dimensions, west, east, south, north
mrx=0.05; mry=0.05; % map resolution {x,y}
[xm ym]=meshgrid(xw:mrx:xe,ys:mry:yn);
m=0*xm.*ym; % initialize the map m
for i=1:size(xm,2)
for j=1:size(ym,1)
r=sqrt(xm(1,i)^2+ym(j,1)^2);
ang=atan2(ym(j,1),xm(1,i))+pi;
if 9<=r && r<=10 && (pi/15<=ang && ang<=29*pi/15)
m(j,i)=1;
elseif 6<=r && r<=7 && ~(pi-pi/6<=ang && ang<=pi+pi/6)
m(j,i)=1;
elseif -14<=xm(1,i) && xm(1,i)<-9 && -2.8<=ym(j,1) && ym(j,1)<=-1.8
m(j,i)=1;
elseif -11<=xm(1,i) && xm(1,i)<-9 && 1.8<=ym(j,1) && ym(j,1)<=2.8
m(j,i)=1;
elseif -14<=xm(1,i) && xm(1,i)<-13 && -2.8<=ym(j,1) && ym(j,1)<=14
m(j,i)=1;
elseif -11<=xm(1,i) && xm(1,i)<-10 && 2.8<=ym(j,1) && ym(j,1)<=11
m(j,i)=1;
elseif -14<=xm(1,i) && xm(1,i)<14 && 14<=ym(j,1) && ym(j,1)<=15
m(j,i)=1;
elseif -11<=xm(1,i) && xm(1,i)<11 && 11<=ym(j,1) && ym(j,1)<=12
m(j,i)=1;
elseif 14<=xm(1,i) && xm(1,i)<15 && -15<=ym(j,1) && ym(j,1)<=15
m(j,i)=1;
elseif -5<=xm(1,i) && xm(1,i)<14 && -15<=ym(j,1) && ym(j,1)<=-14
m(j,i)=1;
elseif 10<=r && r<=16 && 0.95*pi/3.5<=ang && ang<=1.05*pi/3.5
m(j,i)=1;
elseif xm(1,i)<0.98*xw || 0.98*xe<xm(1,i) || ym(j,1)<0.98*ys || 0.98*yn<ym(j,1) % Boundaries
m(j,i)=1;
end
end
end
mesh(xm,ym,m); axis image; view(0, 90); xlabel('x'); ylabel('y');
end
When you have a while loop, each time you have a test, in this case that a certain array element is 0. In order to exit the loop, at least one of the variables involved in the test must have a probability or certainty of changing, or else there has to be a "break" statement inside the code.
Look at your code and ask under what circumstances that array element can become non-zero.
And remember you just tested the element so you know it is 0, so comparing the exact same element to 1 in the if is going to be false.
Please put all your code in a code block.
I got an error at while m(xo,yo)==0:
Index in position 1 is invalid. Array indices must be positive integers or logical values.
Have a good night.
%%%% you can find all the code together below:
close all
clear all
clc
xo=-16; yo=-16; th=pi/2;
x=-13; y=-13;
rr=1; %Robot radius
r=rr; %ray vector
n=4; %Number of sensors
dT=0.4;
t1=[0:dT:3]; % NOISELESS
u1=[0*ones(size(t1)); -1*ones(size(t1))];
t2=[0:dT:0.8]; % NOISELESS
u2=[1*ones(size(t2)); 0*ones(size(t2))];
t3=[0:dT:1.5]; % NOISELESS
u3=[0*ones(size(t3)); 1*ones(size(t3))];
t4=[0:dT:6]; % NOISELESS
u4=[1*ones(size(t4)); 0*ones(size(t4))];
t5=[0:dT:1.5]; % NOISELESS
u5=[0*ones(size(t5)); 1*ones(size(t5))];
t6=[0:dT:1.6]; % NOISELESS
u6=[1*ones(size(t6)); 0*ones(size(t6))];
t7=[0:dT:1.5]; % NOISELESS
u7=[0*ones(size(t7)); -1*ones(size(t7))];
t8=[0:dT:19]; % NOISELESS
u8=[1*ones(size(t8)); 0*ones(size(t8))];
t9=[0:dT:1.55]; % NOISELESS
u9=[0*ones(size(t9)); 1*ones(size(t9))];
t10=[0:dT:25.6]; % NOISELESS
u10=[1*ones(size(t10)); 0*ones(size(t10))];
t11=[0:dT:1.5]; % NOISELESS
u11=[0*ones(size(t11)); 0.98*ones(size(t11))];
t12=[0:dT:24]; % NOISELESS
u12=[1*ones(size(t12)); 0*ones(size(t12))];
t13=[0:dT:1.5]; % NOISELESS
u13=[0*ones(size(t13)); 0.98*ones(size(t13))];
t14=[0:dT:13]; % NOISELESS
u14=[1*ones(size(t14)); 0*ones(size(t14))];
t15=[0:dT:1.5]; % NOISELESS
u15=[0*ones(size(t15)); 0.98*ones(size(t15))];
t16=[0:dT:3.5]; % NOISELESS
u16=[1*ones(size(t16)); 0*ones(size(t16))];
t17=[0:dT:1.2]; % NOISELESS
u17=[0*ones(size(t17)); -0.94*ones(size(t17))];
t18=[0:dT:24]; % NOISELESS
u18=[1*ones(size(t18)); 1/8.2*ones(size(t18))];
t19=[0:dT:1.59]; % NOISELESS
u19=[0*ones(size(t19)); 1.01*ones(size(t19))];
t20=[0:dT:7.9]; % NOISELESS
u20=[1*ones(size(t20)); 0*ones(size(t20))];
u=[u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20]; % concatenate all controls into one
for t=1:size(u,1)
v=u(1,t);
if u(2,t)~=0
w=u(2,t);
else
w=100*eps; % 100*eps because eps alone is too small, so causes division by zero problem
end
xw=-16; xe=16; ys=-16; yn=16; %map dimensions, west, east, south, north
mrx=0.05; mry=0.05; % map resolution {x,y}
[xm ym]=meshgrid(xw:mrx:xe,ys:mry:yn);
m=0*xm.*ym; % initialize the map m
for i=1:size(xm,2)
for j=1:size(ym,1)
r=sqrt(xm(1,i)^2+ym(j,1)^2);
ang=atan2(ym(j,1),xm(1,i))+pi;
if 9<=r && r<=10 && (pi/15<=ang && ang<=29*pi/15)
m(j,i)=1;
elseif 6<=r && r<=7 && ~(pi-pi/6<=ang && ang<=pi+pi/6)
m(j,i)=1;
elseif -14<=xm(1,i) && xm(1,i)<-9 && -2.8<=ym(j,1) && ym(j,1)<=-1.8
m(j,i)=1;
elseif -11<=xm(1,i) && xm(1,i)<-9 && 1.8<=ym(j,1) && ym(j,1)<=2.8
m(j,i)=1;
elseif -14<=xm(1,i) && xm(1,i)<-13 && -2.8<=ym(j,1) && ym(j,1)<=14
m(j,i)=1;
elseif -11<=xm(1,i) && xm(1,i)<-10 && 2.8<=ym(j,1) && ym(j,1)<=11
m(j,i)=1;
elseif -14<=xm(1,i) && xm(1,i)<14 && 14<=ym(j,1) && ym(j,1)<=15
m(j,i)=1;
elseif -11<=xm(1,i) && xm(1,i)<11 && 11<=ym(j,1) && ym(j,1)<=12
m(j,i)=1;
elseif 14<=xm(1,i) && xm(1,i)<15 && -15<=ym(j,1) && ym(j,1)<=15
m(j,i)=1;
elseif -5<=xm(1,i) && xm(1,i)<14 && -15<=ym(j,1) && ym(j,1)<=-14
m(j,i)=1;
elseif 10<=r && r<=16 && 0.95*pi/3.5<=ang && ang<=1.05*pi/3.5
m(j,i)=1;
elseif xm(1,i)<0.98*xw || 0.98*xe<xm(1,i) || ym(j,1)<0.98*ys || 0.98*yn<ym(j,1) % Boundaries
m(j,i)=1;
end
end
end
subplot(1,2,1);
mesh(xm,ym,m); axis image; view(0, 90); xlabel('x'); ylabel('y');
x=x-v/w*sin(th)+v/w*sin(th+w*dT);%state update
y=y+v/w*cos(th)-v/w*cos(th+w*dT);%state update
th=th+w*dT;%state update
for k=0:n-1
th=th+2*pi*k/n;
xo=xo+r*cos(th); %corridnates(x,y) for the robot location
xo=(xo*0.05/32)+1; xo=ceil(xo);
yo=yo+r*sin(th); %r is length(r)
yo=(yo*0.05/32)+1; yo=ceil(yo);
while m(xo,yo) ==0 %if the location is not occupied
r=rr+0.05;
if m(xo,yo) == 1 %if the location is occupied
subplot(1,2,2); plot(m(xo,yo) ,'b.'); hold on;
end
end
end
end
%%%% you can find all the code together below:
close all
clear all
clc
xo=-16; yo=-16; th=pi/2;
x=-13; y=-13;
rr=1; %Robot radius
r=rr; %ray vector
n=4; %Number of sensors
dT=0.4;
t1=[0:dT:3]; % NOISELESS
u1=[0*ones(size(t1)); -1*ones(size(t1))];
t2=[0:dT:0.8]; % NOISELESS
u2=[1*ones(size(t2)); 0*ones(size(t2))];
t3=[0:dT:1.5]; % NOISELESS
u3=[0*ones(size(t3)); 1*ones(size(t3))];
t4=[0:dT:6]; % NOISELESS
u4=[1*ones(size(t4)); 0*ones(size(t4))];
t5=[0:dT:1.5]; % NOISELESS
u5=[0*ones(size(t5)); 1*ones(size(t5))];
t6=[0:dT:1.6]; % NOISELESS
u6=[1*ones(size(t6)); 0*ones(size(t6))];
t7=[0:dT:1.5]; % NOISELESS
u7=[0*ones(size(t7)); -1*ones(size(t7))];
t8=[0:dT:19]; % NOISELESS
u8=[1*ones(size(t8)); 0*ones(size(t8))];
t9=[0:dT:1.55]; % NOISELESS
u9=[0*ones(size(t9)); 1*ones(size(t9))];
t10=[0:dT:25.6]; % NOISELESS
u10=[1*ones(size(t10)); 0*ones(size(t10))];
t11=[0:dT:1.5]; % NOISELESS
u11=[0*ones(size(t11)); 0.98*ones(size(t11))];
t12=[0:dT:24]; % NOISELESS
u12=[1*ones(size(t12)); 0*ones(size(t12))];
t13=[0:dT:1.5]; % NOISELESS
u13=[0*ones(size(t13)); 0.98*ones(size(t13))];
t14=[0:dT:13]; % NOISELESS
u14=[1*ones(size(t14)); 0*ones(size(t14))];
t15=[0:dT:1.5]; % NOISELESS
u15=[0*ones(size(t15)); 0.98*ones(size(t15))];
t16=[0:dT:3.5]; % NOISELESS
u16=[1*ones(size(t16)); 0*ones(size(t16))];
t17=[0:dT:1.2]; % NOISELESS
u17=[0*ones(size(t17)); -0.94*ones(size(t17))];
t18=[0:dT:24]; % NOISELESS
u18=[1*ones(size(t18)); 1/8.2*ones(size(t18))];
t19=[0:dT:1.59]; % NOISELESS
u19=[0*ones(size(t19)); 1.01*ones(size(t19))];
t20=[0:dT:7.9]; % NOISELESS
u20=[1*ones(size(t20)); 0*ones(size(t20))];
u=[u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20]; % concatenate all controls into one
for t=1:size(u,1)
v=u(1,t);
if u(2,t)~=0
w=u(2,t);
else
w=100*eps; % 100*eps because eps alone is too small, so causes division by zero problem
end
xw=-16; xe=16; ys=-16; yn=16; %map dimensions, west, east, south, north
mrx=0.05; mry=0.05; % map resolution {x,y}
[xm ym]=meshgrid(xw:mrx:xe,ys:mry:yn);
m=0*xm.*ym; % initialize the map m
for i=1:size(xm,2)
for j=1:size(ym,1)
r=sqrt(xm(1,i)^2+ym(j,1)^2);
ang=atan2(ym(j,1),xm(1,i))+pi;
if 9<=r && r<=10 && (pi/15<=ang && ang<=29*pi/15)
m(j,i)=1;
elseif 6<=r && r<=7 && ~(pi-pi/6<=ang && ang<=pi+pi/6)
m(j,i)=1;
elseif -14<=xm(1,i) && xm(1,i)<-9 && -2.8<=ym(j,1) && ym(j,1)<=-1.8
m(j,i)=1;
elseif -11<=xm(1,i) && xm(1,i)<-9 && 1.8<=ym(j,1) && ym(j,1)<=2.8
m(j,i)=1;
elseif -14<=xm(1,i) && xm(1,i)<-13 && -2.8<=ym(j,1) && ym(j,1)<=14
m(j,i)=1;
elseif -11<=xm(1,i) && xm(1,i)<-10 && 2.8<=ym(j,1) && ym(j,1)<=11
m(j,i)=1;
elseif -14<=xm(1,i) && xm(1,i)<14 && 14<=ym(j,1) && ym(j,1)<=15
m(j,i)=1;
elseif -11<=xm(1,i) && xm(1,i)<11 && 11<=ym(j,1) && ym(j,1)<=12
m(j,i)=1;
elseif 14<=xm(1,i) && xm(1,i)<15 && -15<=ym(j,1) && ym(j,1)<=15
m(j,i)=1;
elseif -5<=xm(1,i) && xm(1,i)<14 && -15<=ym(j,1) && ym(j,1)<=-14
m(j,i)=1;
elseif 10<=r && r<=16 && 0.95*pi/3.5<=ang && ang<=1.05*pi/3.5
m(j,i)=1;
elseif xm(1,i)<0.98*xw || 0.98*xe<xm(1,i) || ym(j,1)<0.98*ys || 0.98*yn<ym(j,1) % Boundaries
m(j,i)=1;
end
end
end
subplot(1,2,1);
mesh(xm,ym,m); axis image; view(0, 90); xlabel('x'); ylabel('y');
x=x-v/w*sin(th)+v/w*sin(th+w*dT);%state update
y=y+v/w*cos(th)-v/w*cos(th+w*dT);%state update
th=th+w*dT;%state update
zcount = 0;
for k=0:n-1
th=th+2*pi*k/n;
xo=xo+r*cos(th); %corridnates(x,y) for the robot location
xo=(xo*0.05/32)+1; xo=ceil(xo);
yo=yo+r*sin(th); %r is length(r)
yo=(yo*0.05/32)+1; yo=ceil(yo);
xo, yo
while m(xo,yo) ==0 %if the location is not occupied
fprintf('m was 0 at (%g,%g)', xo, yo);
zcount = zcount + 1;
r=rr+0.05;
if m(xo,yo) == 1 %if the location is occupied
subplot(1,2,2); plot(m(xo,yo) ,'b.'); hold on;
end
end
end
fprintf('Number of times m(xo,yo) was 0 was %d\n', zcount);
end
xo = 1
yo = 2
xo = 1
yo = 2
xo = 2
yo = 1
xo = 1
yo = 1
Number of times m(xo,yo) was 0 was 0
xo = 1
yo = 1
xo = 2
yo = 1
xo = 1
yo = 2
xo = 2
yo = 2
Number of times m(xo,yo) was 0 was 0
You escaped the infinite loop only because m(xo,yo) is never 0.
But I need to m(xo,yo)==0 when there is no obsticale which is the purpule area in the map and I need to make m(xo,yo)==1 when there is obsticale which is the yellow area, how can I do that?
As of this point, we helped get the indexing problem fixed and pointed out dead code (which fortunately did not execute, because if it did, you would have an infinite loop). I have to catch up on work this week. Maybe I can take a look more this weekend. (Or you can ask a separate image processing question now that you have running code.)
There is one more fix that I noticed - your two plots did not come out correctly. Add/modify your code to correct the subplots:
kk = 0;
for t=1:size(u,1)
kk = kk + 1;
Change subplots to:
subplot(1,size(u,1),kk);
Now, you will get two identical images side by side.
Note: You can indent your Matlab code using these two sequences: Ctrl/A (select all) followed by Ctrl/I (indent selected lines).
%%%% you can find all the code together below:
close all
clear all
clc
xo=-16; yo=-16; th=pi/2;
x=-13; y=-13;
rr=1; %Robot radius
r=rr; %ray vector
n=4; %Number of sensors
dT=0.4;
t1=[0:dT:3]; % NOISELESS
u1=[0*ones(size(t1)); -1*ones(size(t1))];
t2=[0:dT:0.8]; % NOISELESS
u2=[1*ones(size(t2)); 0*ones(size(t2))];
t3=[0:dT:1.5]; % NOISELESS
u3=[0*ones(size(t3)); 1*ones(size(t3))];
t4=[0:dT:6]; % NOISELESS
u4=[1*ones(size(t4)); 0*ones(size(t4))];
t5=[0:dT:1.5]; % NOISELESS
u5=[0*ones(size(t5)); 1*ones(size(t5))];
t6=[0:dT:1.6]; % NOISELESS
u6=[1*ones(size(t6)); 0*ones(size(t6))];
t7=[0:dT:1.5]; % NOISELESS
u7=[0*ones(size(t7)); -1*ones(size(t7))];
t8=[0:dT:19]; % NOISELESS
u8=[1*ones(size(t8)); 0*ones(size(t8))];
t9=[0:dT:1.55]; % NOISELESS
u9=[0*ones(size(t9)); 1*ones(size(t9))];
t10=[0:dT:25.6]; % NOISELESS
u10=[1*ones(size(t10)); 0*ones(size(t10))];
t11=[0:dT:1.5]; % NOISELESS
u11=[0*ones(size(t11)); 0.98*ones(size(t11))];
t12=[0:dT:24]; % NOISELESS
u12=[1*ones(size(t12)); 0*ones(size(t12))];
t13=[0:dT:1.5]; % NOISELESS
u13=[0*ones(size(t13)); 0.98*ones(size(t13))];
t14=[0:dT:13]; % NOISELESS
u14=[1*ones(size(t14)); 0*ones(size(t14))];
t15=[0:dT:1.5]; % NOISELESS
u15=[0*ones(size(t15)); 0.98*ones(size(t15))];
t16=[0:dT:3.5]; % NOISELESS
u16=[1*ones(size(t16)); 0*ones(size(t16))];
t17=[0:dT:1.2]; % NOISELESS
u17=[0*ones(size(t17)); -0.94*ones(size(t17))];
t18=[0:dT:24]; % NOISELESS
u18=[1*ones(size(t18)); 1/8.2*ones(size(t18))];
t19=[0:dT:1.59]; % NOISELESS
u19=[0*ones(size(t19)); 1.01*ones(size(t19))];
t20=[0:dT:7.9]; % NOISELESS
u20=[1*ones(size(t20)); 0*ones(size(t20))];
u=[u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20]; % concatenate all controls into one
kk = 0;
for t=1:size(u,1)
kk = kk + 1;
v=u(1,t);
if u(2,t)~=0
w=u(2,t);
else
w=100*eps; % 100*eps because eps alone is too small, so causes division by zero problem
end
xw=-16; xe=16; ys=-16; yn=16; %map dimensions, west, east, south, north
mrx=0.05; mry=0.05; % map resolution {x,y}
[xm ym]=meshgrid(xw:mrx:xe,ys:mry:yn);
m=0*xm.*ym; % initialize the map m
for i=1:size(xm,2)
for j=1:size(ym,1)
r=sqrt(xm(1,i)^2+ym(j,1)^2);
ang=atan2(ym(j,1),xm(1,i))+pi;
if 9<=r && r<=10 && (pi/15<=ang && ang<=29*pi/15)
m(j,i)=1;
elseif 6<=r && r<=7 && ~(pi-pi/6<=ang && ang<=pi+pi/6)
m(j,i)=1;
elseif -14<=xm(1,i) && xm(1,i)<-9 && -2.8<=ym(j,1) && ym(j,1)<=-1.8
m(j,i)=1;
elseif -11<=xm(1,i) && xm(1,i)<-9 && 1.8<=ym(j,1) && ym(j,1)<=2.8
m(j,i)=1;
elseif -14<=xm(1,i) && xm(1,i)<-13 && -2.8<=ym(j,1) && ym(j,1)<=14
m(j,i)=1;
elseif -11<=xm(1,i) && xm(1,i)<-10 && 2.8<=ym(j,1) && ym(j,1)<=11
m(j,i)=1;
elseif -14<=xm(1,i) && xm(1,i)<14 && 14<=ym(j,1) && ym(j,1)<=15
m(j,i)=1;
elseif -11<=xm(1,i) && xm(1,i)<11 && 11<=ym(j,1) && ym(j,1)<=12
m(j,i)=1;
elseif 14<=xm(1,i) && xm(1,i)<15 && -15<=ym(j,1) && ym(j,1)<=15
m(j,i)=1;
elseif -5<=xm(1,i) && xm(1,i)<14 && -15<=ym(j,1) && ym(j,1)<=-14
m(j,i)=1;
elseif 10<=r && r<=16 && 0.95*pi/3.5<=ang && ang<=1.05*pi/3.5
m(j,i)=1;
elseif xm(1,i)<0.98*xw || 0.98*xe<xm(1,i) || ym(j,1)<0.98*ys || 0.98*yn<ym(j,1) % Boundaries
m(j,i)=1;
end
end
end
subplot(1,size(u,1),kk);
mesh(xm,ym,m); axis image; view(0, 90); xlabel('x'); ylabel('y');
x=x-v/w*sin(th)+v/w*sin(th+w*dT);%state update
y=y+v/w*cos(th)-v/w*cos(th+w*dT);%state update
th=th+w*dT;%state update
zcount = 0;
for k=0:n-1
th=th+2*pi*k/n;
xo=xo+r*cos(th); %corridnates(x,y) for the robot location
xo=(xo*0.05/32)+1; xo=ceil(xo);
yo=yo+r*sin(th); %r is length(r)
yo=(yo*0.05/32)+1; yo=ceil(yo);
[xo, yo, m(xo,yo)]
while m(xo,yo) ==0 %if the location is not occupied
fprintf('m was 0 at (%g,%g)', xo, yo);
zcount = zcount + 1;
r=rr+0.05;
if m(xo,yo) == 1 %if the location is occupied
subplot(1,2,2); plot(m(xo,yo) ,'b.'); hold on;
end
end
end
fprintf('Number of times m(xo,yo) was 0 was %d\n', zcount);
end
ans = 1×3
1 2 1
ans = 1×3
1 2 1
ans = 1×3
2 1 1
ans = 1×3
1 1 1
Number of times m(xo,yo) was 0 was 0
ans = 1×3
1 1 1
ans = 1×3
2 1 1
ans = 1×3
1 2 1
ans = 1×3
2 2 1
Number of times m(xo,yo) was 0 was 0
[r,c] = find(m ~= 1, 1)
r = 8
c = 8
figure; imagesc(m(1:10,1:10))
So that area is solid 1's.
You only iterate by the number of sensors, even though clearly you are intending to move a robot.
Looking at the code, I get the impression that you are intending to keep moving the robot repeatedly until something (I am not sure what) happens. Your rules for where to move are not at all clear.
But it is really doubtful that you intend to move only once for each sensor.
And you are not doing any collision avoidance. Collision avoidance requires being at a location and making guesses about where to go and checking whether the path to the new location is free, and if not then choosing a different potential location.
Why is that the required output? What are the model requirements and rules?
I have to test each point in the map if it's occupied or not, then if it's not occupied the robot should keep moving but if it's occupied then I should label the empty map with blue dot or any mark.
From your stated requirements, I do not understand your left and right output figures. I think you may have a requirements and design issue rather than a Matlab question.
For a general programming point of view (whether Matlab or not), you should get the program to run with a number of well defined functions each having a clearly stated purpose. There should be more comments so that all can understand your design. One function might be CreateInitialMaze(). Your main script should be fairly small and devoid of math computations. Design the main script with as many high-level functions as you need to do your job.
When I said, "Please put all your code in a code block.", I did not mean for you to eliminate your functions.
Unless you have a Matlab question, I recommend closing this question, and starting fresh with the general programming principles I suggested.

Accedi per commentare.

You initialize a playing field of free space and obstacles.
You start at some particular location.
At any one point, you have a goal that you use to figure out which direction you would ideally like to go. You test to see if you can go in that direction. If so, you go there and loop back to the next step. If you cannot go in the most desired direction, you figure out the next best direction to go, and see if you can go there; if you cannot, then the next best after that, and so on. Unless this was the very first step and you happened to land in the one open spot surrounded by barriers, there is always somewhere you can go.
You will, however, find that in practice you need to be a bit more clever than the above.
1 R 2
-|_|-
3|G|4
5 6 7
Let R be the robot, and G be the goal. The "ideal" direction would be for the robot to move downward. But of course if it does, then it cannot go any further towards G, and needs to move back to [1], or the location it came from [R], or to [2] . If it moves back to [R] then it gets back to the place where the "ideal" move is straight down towards G... getting stuck again. If it moved to [1] or to [2], then in the next step, the "ideal" move would be to move diagonally down, getting stuck in the same rut again.
How to deal with this, how to avoid cycling through positions due to lack of long-term planning, or due to limited "visibility" not permitting you to know what the "best" step is, is key to robotics. You can research algorithms for this, or you can try to come up with one yourself.

Richiesto:

il 19 Apr 2021

Riaperto:

il 28 Apr 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by