# MATLAB code for simulated Roomba to detect randomly generated obstacles

13 views (last 30 days)
Karl Schmitkons on 26 Apr 2021
Commented: Karl Schmitkons on 29 Apr 2021
My son has a project involving making a simulated Roomba robot vacuum cleaner go around a generated room map, detect the obstacles, and then output a new room map with the obstacles in their (correct) detected places. The user inputs the desired rectangular room size, then the DrawMap function generates a room on screen and takes random generated values from a 4x4 matrix to places "obstacles" in the displayed map. He has that part done and working correctly, but is having trouble coming up with code to 1) move the Roomba (basically the index) vertically and horizontally through the "room," 2) detect the obstacles (depicted as lines or figures outlined by the * character on screen), and 3) mark the obstacles for display in the new room map. I would guess the detected/marked obstacles would be stored in a second 4x4 matrix that would then be used to display the obstacles in the new map. Any suggestions? The DrawMap function was new to him so he does not know how that works...

Clayton Gotberg on 26 Apr 2021
This is a fascinating project but offering much help with the code probably means you're going to need to go into more detail about your requirements and your current codebase.
My questions, offhand:
1. How large and what shape is the detection area for the Roomba?
2. How exactly are the objects generated and what forms do they take?
3. Is the simulated Roomba allowed to pass through the obstacles?
4. How "stupid" do you want the Roomba to be about things like the size and shape of the room and obstacles?
5. How do you want the Roomba to move through the room? Should it prioritize vertical paths over horizontal? When it finds an object, should it try to discover the entirety of the object or should it try to get back to the original path? What if there are regions that are theoretically unobstructed but which the Roomba is too "large" to enter?
My thoughts on solutions:
1. Decide on a plan for how the Roomba should move in an unknown environment and when it encounters obstacles. For example, choose to move vertically until an object is found, then (assuming a collision should redirect the Roomba) have the Roomba follow the outline of the object clockwise until it reaches the original position again, at which time it should move to the other side of the object and continue scanning up and down.
2. Have the detection logic work as follows: the Roomba is in position (x,y) (assume there aren't already obstacles in detection range). First, it attempts to move to position (x+1,y). The program then verifies that no obstacles are inside range. If so, it attempts to move to (x+2,y). If there is an obstacle in detection range, the Roomba's position again becomes (x,y) and the detected position of the obstacle is recorded.
3. Rather than trying to reduce the size of the stored obstacles by storing them in a small matrix like that (which I assume contains the coordinates for two corners of a rectangle or a corner, length and height) let the detected obstacles appear in a matrix like they would on a map.
I've drawn out what I mean by 2 and 3 below:
Move 1:
% Room map
***********************************************
* *
* *
* ***** *
* * A * *
* ***** *
* *
* [] * % The Roomba is two row from impacting A
* * % and is at position (x,y)
***********************************************
% Search map
***********************************************
* *
* *
* *
* *
* *
* *
* [] * % the Roomba knows nothing
* *
***********************************************
Move 2:
***********************************************
* *
* *
* ***** *
* * A * *
* ***** *
* [] *
* * % The Roomba is one row from impacting A
* * % at position (x+1,y)
***********************************************
Move 3:
% Room map
***********************************************
* *
* *
* ***** *
* * A * *
* []*** *
* *
* * % The Roomba impacts A
* * % at position (x+2,y)
***********************************************
***********************************************
* *
* *
* ***** *
* * A * *
* ***** *
* [] *
* * % The Roombra is moved back to position (x+1,y)
* *
***********************************************
% Search map
***********************************************
* *
* *
* *
* *
* ** *
* [] *
* * % to the output map
***********************************************
Move 4:
***********************************************
* *
* *
* ***** *
* * A * *
* ***** *
* [] *
* * % The Roombra is moved to position (x+1,y-2)
* * % to get around what it has seen of the obstacle
***********************************************
Karl Schmitkons on 29 Apr 2021
Thank you, Clayton. I sent this to my son and he used a fair amount of it to rework his coding (actually simplify what he had). He never did figure out how the DrawMap function (user defined & given to him by his professor?) so he ditched that and came up with his own method using a larger matrix. I'm not sure what all he did but he told my your suggestions gave him some good ideas, so thanks again!

### Categories

Find more on Multiobjective Optimization in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!