In: Computer Science
Develop a python program to autonomously operate an adeept tank robot to pick up an object placed randomly in an arena.
Following is the code for an autonomously navigating robot. The code is pretty easy to understand. Numpy is the only dependency needed to generate a 2D matrix that behaves like the world in which the robot lives in.
CODE:
import numpy as np
from random import choice
# generating a random 2D world with 1s and 0s
# In this 2D world if a tile at :
# location(x,y) = 1 object is present
# location(x,y) = 0 object is not present
# Length of the world
L = 5
# Breadth of the world
B = 5
world = np.random.randint(2, size=(L, B))
print(world.shape)
print(world)
# lets initialize the robot in this world at some location
robot_x = 0
robot_y = 0
# for the robot to autonomously navigate in this world lets program it as follows
# the robot can randomly move left right up or down
# let the robot move 10 steps
for steps in range(10):
# check if robot found any item
if world[robot_x,robot_y] == 1:
print(f'Robot picked up item at location ({robot_x},{robot_y})')
world[robot_x,robot_y] = 0
moves = ['L','R','U','D']
# select a random move
random_move = choice(moves)
if random_move == 'L':
robot_x = (robot_x - 1)%B # we mod it with the breath to avoid the robot going out of the world
elif random_move == 'R':
robot_x = (robot_x + 1)%B # we mod it with the breath to avoid the robot going out of the world
if random_move == 'U':
robot_y = (robot_y - 1)%L # we mod it with the length to avoid the robot going out of the world
if random_move == 'L':
robot_y = (robot_y + 1)%L # we mod it with the length to avoid the robot going out of the world
# lets see how the world looks after 10 steps
print(world)
# final location of the robot
print(robot_x,robot_y)