Index in position 1 is invalid. Array indices must be positive integers or logical values.
19 views (last 30 days)
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;
rr=1; %Robot radius
r=rr; %ray vector
u=Control(); % control function
n=4; %Number of sensors
dbstop if error
w=100*eps; % 100*eps because eps alone is too small, so causes division by zero problem
m=Map(); %map function
xo=xo+r*cos(th); %corridnates(x,y) for the robot location
yo=yo+r*sin(th); %r is length(r)
while m(xo,yo)==0 %if the location not occupied %%% my error in this line
if m(xo,yo)==1 %if the location occupied
mesh(xo,yo,m); view(0, 90); axis image; hold on;
Paul Hoffrichter on 19 Apr 2021
Edited: Paul Hoffrichter on 19 Apr 2021
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.
Walter Roberson on 22 Apr 2021
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
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 , or the location it came from [R], or to  . 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  or to , 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.