Please help with script in matlab (avoiding objects in a matrix)

  • Thread starter Thread starter dtrent258
  • Start date Start date
  • Tags Tags
    Matlab Matrix
AI Thread Summary
The discussion revolves around creating a MATLAB function for a robot vacuum that navigates a matrix to clean while avoiding obstacles. The robot, represented by a value of 1, must clean all dirty tiles (value 3) and navigate around obstacles (value 0) without skipping them. The user is struggling with the logic to avoid obstacles and return to the previous path after encountering one. Suggestions include improving the for loop structure and using conditional statements effectively to manage the robot's movements. The user is encouraged to refine their code for better readability and functionality.
dtrent258
Messages
4
Reaction score
0
1. The problem statement.
Create a function in MATLAB for a simulation of a automated robot vacuum to avoid obstacles while cleaning an undisclosed floor plan. the floor plan which will be in the form of a matrix not known beforehand. The robot (represented by a value of 1) can move horizontally (left to right, down one, right to left...) or vertically (top to bottom, over one, bottom to top...), but the robot should "clean" 100% of the floor plan even if the surface has a value of a clean tile (represented by a 4). While traversing the matrix the robot will encounter dirty floor (represented by the value 3) as well as obstacles (represented by the value 0). when a dirty tile is encountered value will be changed to a "4". When an obstacle is encountered the robot should go around it instead of just skipping over, and then continue on cleaning. once the matrix is cleaned the robot will return to the charger (represented by a 2). I've some code written but can not seem to find a good way of avoiding obstacles and return to the same column or row I was on. If anyone has any suggestions it would be greatly appreciated. I've included a sample matrix just for me to practice on, the real matrix will be 30 rows by 25 columns. Values in the matrix are as follows:

1= robot
2= charger (always located at (2,2) in the matrix, also robot will always begin here as well)
3= dirty floor
4= clean floor
0= wall or obstacle (the perimeter of the matrix will always be a wall, but the interior will vary from matrix to matrix)




Homework Equations


My code has the robot moving up and down. I have tried writing script to move the robot left or right depending on which way isn't blocked. but I have to do that for each time it hits an obstacle, plus I can't find a good way to return to the row or column that I was on.


The Attempt at a Solution



a=xlsread('robmat');
p=size(a);
n=p(1);
m=p(2);
time=0; % time taken to clean(1 for clean, 5 for dirty)
bat=0; % battery life used ( +1 for every move)
for i=3:1:n-1;
for j=2;​
if a(i,j)==3;​
a(i,j)=4;​
time=time+5;​
bat=bat+1;​
if a(i,j)==4;​
time=time+1;​
bat=bat+1;​
end​
end​
end​
end
for i=n-1:-1:2;
for j=3;​
if a(i,j)==3;​
a(i,j)=4;​
time=time+5;​
bat=bat+1;​
if a(i,j)==4;​
time=time+1;​
bat=bat+1;​
end​
end​
end​
end
a


[fprintf('Time taken equals: %d seconds\n',time);]
[fprintf('Amount of battery charge used equals: %d\n',bat);]
 

Attachments

Last edited:
Physics news on Phys.org
Welcome to PF!

One way to help people respond to your question is to edit your post and add [ code ] tags (no spaces between the square brackets and the code tag kind of like html formatting) around the MATLAB script so that it will be properly boxed in and will preserve any code indentation you may have.

As an example using a portion of your code:

Code:
...

for i=n-1:-1:2;
    for j=3;
        if a(i,j)==3;
            a(i,j)=4;
            time=time+5;
            bat=bat+1;
        if a(i,j)==4;
            time=time+1;
            bat=bat+1;
    end
end

...

which I assume is what you meant
 
Alright thanks, I'll try and edit it so it's easier to read. I should probably mention that this is my first MATLAB class I've taken, so I'll most likely make a lot of mistakes, but I'm trying my best to keep up with it. thanks again.
 
I think that I've made it easier to read, I hope that this helps some.
 
Okay there are numerous problems with your script:

the i,j for loops should be something like this, basically the inner j for loop must have some sort of range, right?

Code:
for i=3:1:n-1;

    for j=2:1:n-1:

...

    end
end

next the nested if statements are a problem how can the inner if ever be true if the outer one is true.

I haven't studied your logic yet but wanted to point out these two things
 
I think I see what you're saying. So I should replace the inner if statement with an elseif statement?.
 
dtrent258 said:
I think I see what you're saying. So I should replace the inner if statement with an elseif statement?.

That depends on what you're trying to do, right?

You need to understand how you would solve in order to tell the computer how to solve it by thinking like a computer.
 

Similar threads

Back
Top