How to navigate through a matrix
7 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentario
Voss
el 28 de Mzo. de 2022
What do you have so far?
(Feel free to attach your code to the question using the paperclip icon.)
Respuestas (1)
Ayush
el 16 de Nov. de 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:
- 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.
- You can then use conditions to check if the floor is clean or not and do operations accordingly.
- 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.
- 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:');
disp(floorMatrix);
disp('Initial Robot Position:');
disp(robotPosition);
% 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
% Display the final floor matrix
disp('Final Floor Matrix:');
disp(floorMatrix);
For more information on “randi” function you can refer the link below:
Regards,
Ayush
0 comentarios
Ver también
Categorías
Más información sobre Robotics en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!