I am getting errors on my plot of live data collected from a rover.

I am working on code that tracks the path of a rover and plots that path along with any money it encountered. (the code for movment and image recognition of money is correct but im getting errors such as index exceeds aray bounds for the plotting portion. The code in question is all the code under where origin is defined.
n = 0;
while n < 2
d = 0;
while d == 0
speed = 60; %Speed of Robot
obstacle_distance = 20; %Still working on calibrations for this distance
d = gorover(speed, obstacle_distance, rvr)
end
tStart = tic;
while toc(tStart)<10
detectsmoney = detectmoney(rvr);
if detectsmoney == 1
n = n+1;
money = rvr.getPosition;
end
end
rvr.turnAngle(100)
pause(2)
rvr.setDriveSpeed(speed)
clear tStart
d = 0
end
rvr.stop
fprintf('I detected %d dollars',n);
if n > 0
foundmoney = true;
else
foundmoney = false;
end
foundTotal = ShownTotal(rvr,n);
origin = [0, 0];% starting position
position = origin;
path = position; % path with starting position
if path ~= [0, 0]
movement = rvr.getPosition ;
end
scatter(position(1), position(2),'b', 'filled');
hold on;
for i = 1:size(movement, 1)
% Update position and path
position = position + movement(i,:);
path = [path; position];
% Plot path
plot(path(:,1), path(:,2));
% Plot money locations
found_money = ismember(moneyamt, position, 'rows');
if any(found_money)
scatter(position(1), position(2), 'g', 'filled');
end
end
moneyamt = scatter(money(2,1), money(2,2), 'g', 'filled');
title('Position')
legend([origin,moneyamt],{'Origin', 'Money'});
pause;

Answers (1)

You are trying to index to an element in your variable that does not exist.
Position 1 is your rows index, The error message is telling you that there is only one row, and I see you are trying to index to row 2.
A = 1:3
A = 1×3
1 2 3
% Works
A(1,2)
ans = 2
% your error
A(2,2)
Index in position 1 exceeds array bounds. Index must not exceed 1.

5 Comments

Ok I understand that part but how to do I convert/code it to be the correct size without knowing how the rvr.getPosition saves data
You should inspect the value of rvr.getPosition so you know what data you are working with (see here).
Based on the error you are getting, it appears to only return one postion, so why not try plotting row 1 of money instead of row 2 and see if that is what you expect.
OK so if the data is saved as [x y; x y; ....]
money ex. [3 5; 2 6;]
If that money is only saved as 2 points then how do I tell it to seperatly plot each x y coordinate as indivdual plot points?
furthermore, if movement has way more data points then how do I tell it to plot and track that progressive movement as a path where the money is encountered along
unfortunately I am not able to connect to the rover so I cant test my code or get that live data, the errors and trial runs I posted were done earlier and was done through a laptop I no longer have access too.
I also can't connect to the rover, so much of this troubleshooting will have to be done by you. Note the following:
money = [3 5; 2 6;]
money = 2×2
3 5 2 6
A semicolon creates a new row, and a space separates columns. If your data contained more than one point, then you would not have gotten the error you got.
If you don't know how many rows are in your data, but always want to plot the last row, then use 'end' instead of a row number.
money = [3 5; 2 6;];
plot(money(end,1),money(end,2),'o')

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Asked:

on 12 Apr 2023

Commented:

on 13 Apr 2023

Community Treasure Hunt

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

Start Hunting!