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

  • Thread starter Thread starter dtrent258
  • Start date Start date
  • Tags Tags
    Matlab Matrix
Click For Summary

Discussion Overview

The discussion revolves around creating a MATLAB function for simulating an automated robot vacuum that navigates a matrix representing a floor plan. The robot must avoid obstacles, clean dirty tiles, and return to its charger, with participants seeking help to refine their code and logic for obstacle avoidance and movement.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes the problem of programming a robot vacuum to navigate a matrix, detailing the values representing the robot, charger, dirty floor, clean floor, and obstacles.
  • Another participant suggests using code formatting to improve readability of the posted MATLAB script.
  • A participant expresses their inexperience with MATLAB, indicating they are new to the subject and may make mistakes.
  • Another participant points out issues with the for loops in the script, suggesting that the inner loop should have a defined range.
  • One participant questions the logic of nested if statements, implying that the inner if statement may never be executed if the outer one is true.
  • A participant proposes using an elseif statement instead of an inner if statement, depending on the intended logic of the code.

Areas of Agreement / Disagreement

Participants express various concerns about the script's logic and structure, but there is no consensus on the best approach to resolve the issues raised. Multiple viewpoints on how to correct the code remain present.

Contextual Notes

Participants have noted limitations in the current script, including potential logical errors in nested if statements and the need for proper loop ranges. The discussion does not resolve these issues, leaving them open for further exploration.

Who May Find This Useful

Individuals interested in MATLAB programming, particularly those working on robotics simulations or similar coding challenges, may find this discussion relevant.

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

  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 18 ·
Replies
18
Views
4K
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K