How to navigate through a matrix

6 views (last 30 days)
Austin Vaughan
Austin Vaughan on 28 Mar 2022
Answered: Ayush on 16 Nov 2023
I have been given a project where I am to create an autonomous robot vacuum that can move in different directions in a given matrix to "clean the floor". The value for obstructions is 0, for clean floor is 1, for dirty floor is 2, and the position of the robot is 3. I am having trouble figuring out how to program the robot to navigate up, down, left, and right. Once the robot reaches a dirty square it should then change it to a clean square...
  1 Comment
Voss
Voss on 28 Mar 2022
What do you have so far?
(Feel free to attach your code to the question using the paperclip icon.)

Sign in to comment.

Answers (1)

Ayush
Ayush on 16 Nov 2023
Hi Austin,
I understand that you want to create an autonomous robot vacuum that can move in different directions in a given matrix to "clean the floor".
You can follow the steps as described below:
  1. Create a matrix which is representing the environment, i.e. element with value 0 representing obstruction, value 1 representing clean floor, value 2 representing dirty floor, and value 3 representing initial position of the robot.
  2. You can then use conditions to check if the floor is clean or not and do operations accordingly.
  3. You can use the “randi” function to generate the random values which will represent the directions where the robot can move. For this simulation, I have given 4 directions, i.e. UP, DOWN, RIGHT, and LEFT.
  4. Once the random direction is generated you can put condition according to which the robot will behave, i.e. to avoid obstruction and move to the floor which is accessible.
Here is an example code which will simulate the robot as per the conditions provided. The use of "randi" function here is to generate directions for the robot and this function will also result in repeated directions also, so if you want to use some other method to simulate autonomy to the robot you can change this.
% Example Code
% Define the matrix representing the floor
floorMatrix = [
0 0 0 0 0;
0 1 2 1 0;
0 0 2 2 0;
3 2 1 2 1;
0 0 0 0 0
];
% Define the initial position of the robot
[row_ini, col_ini] = find(floorMatrix == 3);
robotPosition = [row_ini, col_ini];
% Display the initial floor matrix and robot position
disp('Initial Floor Matrix:');
Initial Floor Matrix:
disp(floorMatrix);
0 0 0 0 0 0 1 2 1 0 0 0 2 2 0 3 2 1 2 1 0 0 0 0 0
disp('Initial Robot Position:');
Initial Robot Position:
disp(robotPosition);
4 1
% Navigate the robot until all dirty squares are cleaned
while any(floorMatrix(:) == 2)
% Check if the current position is dirty and clean it
if floorMatrix(robotPosition(1), robotPosition(2)) == 2
floorMatrix(robotPosition(1), robotPosition(2)) = 1;
disp('Cleaned a dirty square!');
disp(floorMatrix);
end
% Move the robot in a random direction (up, down, left, or right)
direction = randi(4); % 1: up, 2: down, 3: left, 4: right
% Update the robot position based on the chosen direction
switch direction
case 1 % Move up
if robotPosition(1) > 1 && floorMatrix(robotPosition(1)-1, robotPosition(2)) ~= 0
robotPosition(1) = robotPosition(1) - 1;
end
case 2 % Move down
if robotPosition(1) < size(floorMatrix, 1) && floorMatrix(robotPosition(1)+1, robotPosition(2)) ~= 0
robotPosition(1) = robotPosition(1) + 1;
end
case 3 % Move left
if robotPosition(2) > 1 && floorMatrix(robotPosition(1), robotPosition(2)-1) ~= 0
robotPosition(2) = robotPosition(2) - 1;
end
case 4 % Move right
if robotPosition(2) < size(floorMatrix, 2) && floorMatrix(robotPosition(1), robotPosition(2)+1) ~= 0
robotPosition(2) = robotPosition(2) + 1;
end
end
% Display the updated robot position
disp('Updated Robot Position:');
disp(robotPosition);
end
Updated Robot Position:
4 1
Updated Robot Position:
4 1
Updated Robot Position:
4 2
Cleaned a dirty square!
0 0 0 0 0 0 1 2 1 0 0 0 2 2 0 3 1 1 2 1 0 0 0 0 0
Updated Robot Position:
4 3
Updated Robot Position:
4 4
Cleaned a dirty square!
0 0 0 0 0 0 1 2 1 0 0 0 2 2 0 3 1 1 1 1 0 0 0 0 0
Updated Robot Position:
3 4
Cleaned a dirty square!
0 0 0 0 0 0 1 2 1 0 0 0 2 1 0 3 1 1 1 1 0 0 0 0 0
Updated Robot Position:
4 4
Updated Robot Position:
3 4
Updated Robot Position:
2 4
Updated Robot Position:
2 3
Cleaned a dirty square!
0 0 0 0 0 0 1 1 1 0 0 0 2 1 0 3 1 1 1 1 0 0 0 0 0
Updated Robot Position:
2 4
Updated Robot Position:
2 4
Updated Robot Position:
3 4
Updated Robot Position:
3 3
Cleaned a dirty square!
0 0 0 0 0 0 1 1 1 0 0 0 1 1 0 3 1 1 1 1 0 0 0 0 0
Updated Robot Position:
3 3
% Display the final floor matrix
disp('Final Floor Matrix:');
Final Floor Matrix:
disp(floorMatrix);
0 0 0 0 0 0 1 1 1 0 0 0 1 1 0 3 1 1 1 1 0 0 0 0 0
For more information on “randi” function you can refer the link below:
Regards,
Ayush

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!