1. Jul 22, 2014

### dtrent258

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)

2. Relevant 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.

3. The attempt at a solution

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);]

#### Attached Files:

• ###### robmat.xlsx
File size:
8 KB
Views:
82
Last edited: Jul 22, 2014
2. Jul 22, 2014

### Staff: Mentor

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 (Text):

...

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

3. Jul 22, 2014

### dtrent258

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.

4. Jul 22, 2014

### dtrent258

I think that I've made it easier to read, I hope that this helps some.

5. Jul 22, 2014

### Staff: Mentor

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 (Text):

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

6. Jul 22, 2014

### dtrent258

I think I see what you're saying. So I should replace the inner if statement with an elseif statement?.

7. Jul 23, 2014

### Staff: Mentor

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.