Vacuum Cleaner Robot NetLogo Model

Produced for the book series "Exercises for Artificial Intelligence";

Author: W. J. Teahan; Publisher: Ventus Publishing Aps, Denmark.

powered by NetLogo

view/download model file: Vacuum-Cleaner-Robot.nlogo

WHAT IS IT?

This model simulates a vacuum cleaner robot whose task is to clean the floor of a room. The user can draw obstacles in the environment in order to better represent a real life environment. The model implements two basic reactive behaviours for the robot - one using a simple look ahead mechanism that reacts to any obstacles directly ahead; the other implements a boid (see Craig Reynold's work) that employs a basic obstacle avoidance steering behaviour.

This model is an extension of the Obstacle Avoidance 2 model, and part of the Information from that model has been duplicated here.


HOW IT WORKS

The robot turtle agent simply wanders around randomly in the environment avoiding the obstacles. The look ahead behaviour is implemented using NetLogo's patch-ahead command to check to see if there are any obstacles directly ahead, and if there are, it will turn a random amount. The boid behaviour is implemented using NetLogo's in-cone command that implements a turtle with a cone of vision.


WHAT IS ITS PURPOSE?

The purpose of the model is to show that you do not need to have a complicated method for covering the entire space of an environment. Simple reactive methods based on random walking with basic sensing will suffice.


INTERFACE

The model's Interface buttons are defined as follows:

- Setup: This sets up the environment with an outside border. One turtle agent (the robot) is created and placed at a random location.

- Go: The robot starts wandering around the environment avoiding obstacles.

- Draw Obstacles: The user can draw further obstacles in the environment. These are coloured brown.

- Erase Obstacles: The user can erase obstacles in the environment, including the border.

- Toggle Plot Paths: This instructs the robot turtle agent when wandering to put its pen down if it is up, or put it up if it is already down. If the pen is down, this will show the paths the robot takes while wandering around.

- Erase Paths: This will erase any paths that have been drawn in the environment.

- Follow Robot: This allows the perspective of the visualisation to be altered so that it is centred on the robot.

- Stop Following Robot: This will reset the perspective of the visualisation so that it is directly above the origin of the environment looking straight down.

The model's Interface chooser and sliders are defined as follows:

- behaviour: This sets the behaviour of the robot. There are two values:
"Look Ahead": The robot will look directly ahead to see if there is an obstacle at distance defined by the radius-length slider and turn a random amount if there is.
"Boid": The robot uses a basic cone of vision sense as defined by the radius-angle and radius-length sliders to determine if there are any obstacles ahead, and turns a random amount as defined by the rate-of-random-turn slider, with a tendency to turn to the right.

- robot-size: This sets the size of the robot.

- robot-speed: This controls the speed of the boid i.e. how much it moves forward each tick.

- rate-of-random-turn: This controls how much the wandering robot boid turns each time tick. The robot has a tendency to head in a right turning direction as the rate of random turn to the right (as specified by the slider) is twice that of the rate of random turn to the left.

- boundary-width: This sets the width of the outside boundary at the beginning when the Setup button is pressed. The boundary is drawn with the same colour as the obstacles (brown), therefore the robot will avoid this area as well (usually, but sometimes it can become stuck as mentioned below).

- radius-angle: This defines the radius angle of the boid's vision cone.

- radius-length: This defines the radius length of the boid's vision cone if the behaviour is set to "Boid" or the amount the robot looks ahead if the behaviour is set to "Look Ahead".


HOW TO USE IT

Press the Setup button first, then press Go. To see where the boid wanders, press Toggle Plot Paths. These paths can be erased using the Erase Paths button.

You can draw extra obstacles by pressing the Draw Obstacles button and then holding down the mouse at the point where you want the obstacles to be drawn. These can also be erased using the Erase Obstacles if you have made a mistake. You can change the frame of reference so that the visualisation is centred around where the boid currently is situated by pressing the Follow Robot button. To reset the perspective, press the Stop Following Robot button.


THINGS TO NOTICE

Setting the behaviour to "Boid" and the robot-speed to 0.1, rate-of-random-turn to 40, radius-angle to 300, radius-length to 1, and pressing the Toggle Plot Paths button, followed by moving the speed slider (just below the Information tab in the Interface) from "normal speed" to "faster" will result in the robot boid rapidly covering the entire environment while reliably avoiding the obstacles.

Increasing the radius-length value (while keeping the other variables the same) will change how much of the space the robot covers. Instead of covering most of the environment, if the behaviour is set to "Boid", the robot will cover a space that is away from the obstacles as determined by the slider. (Note that the border is also considered an obstacle). If the behaviour is set to "Look Ahead", and the radius-length value is greater than 1, and the paths are being drawn, then this will result in gaps at the four corners of the environment if there are no obstacles inside the environment. (You will need to make the simulation go faster using the speed slider to see this more quickly). Note that if you set the radius-length slider to a higher value for the "Look Ahead" behaviour, it will often get stuck on the outside boundary depending on the width of the boundary. (Why is this?)


THINGS TO TRY

Try adjusting the robot's speed, radius angle and radius length to see how this affects the robot's behaviour. Also try changing the Interface Settings to see if this has any affect.

Try adding obstacles to see how this affects the robot's ability to cover the entire environment. For example, add obstacles in the form of a maze. Try to create "black spots" where the robot never visits. Alternatively, try to trap the robot into a small area, or try to get it stuck.


EXTENDING THE MODEL

Try adding further behaviours to the model. Observe a real vacuum cleaner robot working, and try to recreate its behaviour in this model.

The model could be extended to add gradual acceleration and deceleration. This would enhance the simulation of the robot.


NETLOGO FEATURES

The code uses the patch-ahead and in-cone commands to simulate the robot vision sense.


RELATED MODELS

See the following models: Obstacle Avoidance 1, Obstacle Avoidance 2.


CREDITS AND REFERENCES

This model was created by William John Teahan. Part of the code was based on the Look Ahead Example model in NetLogo's Models Library.

To refer to this model in publications, please use:

Vacuum Cleaner Robot NetLogo model.
Teahan, W. J. (2010). Artificial Intelligence. Ventus Publishing Aps.


PROCEDURES

; Vacuum Cleaner Robot model
;
; Simulates a vacuum cleaner robot that avoids obstacles but quickly covers all of the environment
; using simple reactive behaviour.
; 
; Modified by Bill Teahan (2009)

breed [robots robot] ; Name of the breed of vacuum cleaner robots

globals
[
  background-colour ; colour of the background except for obstacles
  obstacles-colour  ; colour of the obstacles
  robot-colour      ; colour of the robot
]

to setup
  clear-all
  set-default-shape robots "vacuum cleaner robot" ; sets shapes for each breed
  set background-colour yellow + 2 ; set colour of background light yellow
  set obstacles-colour brown ; set colour of obstacles brown
  set robot-colour gray ; set colour of robot

  ask patches
  [
    set pcolor background-colour ; set colour of background
    if (pxcor >= max-pxcor - boundary-width)
      [ set pcolor brown ]
    if (pxcor <= min-pxcor + boundary-width)
      [ set pcolor brown ]
    if (pycor >= max-pycor - boundary-width)
      [ set pcolor brown ]
    if (pycor <= min-pycor + boundary-width)
      [ set pcolor brown ]
  ]

  ; creates colour, size and random location of single robot 
  create-robots 1
  [
    set size robot-size
    set color robot-colour
    let this-patch one-of patches with [pcolor != obstacles-colour]  ; sets an initial random position within the outside boundary
    set xcor [pxcor] of this-patch
    set ycor [pycor] of this-patch
  ]
end   

to make-move
; This defines how the robot should move.

  if (behaviour = "Look Ahead")
    [ ; This behaviour is from the Look Ahead Example model in the Models Library
      let this-patch patch-ahead radius-length
      ifelse (this-patch != nobody) and ([pcolor] of this-patch = obstacles-colour)
        [ lt random-float 360 ]   ;; We see an obstacle patch in front of us. Turn a random amount.
        [ fd 1 ]                  ;; Otherwise, it is safe to move forward.
    ]
  if (behaviour = "Boid")
    [ ; This implements a boid-like behaviour using the in-cone command (like Craig Reynold's boids)
      rt random-float rate-of-random-turn 
      lt (rate-of-random-turn  / 2)
      ; randomly turns randomly as defined by the random-rate-of-turn variable in the interface
      ; with a tendency to turn to the right
      fd robot-speed  
      avoid-obstacles
    ]
end

to go
; The robot moves around.

  ask robots ; robot instructions
  [ make-move ]

  tick  
end

to make-obstacles
; Creates obstacles in the environment.

 if mouse-down?
 [ ask patches
   [ if ((abs (pxcor - mouse-xcor)) < 1) and ((abs (pycor - mouse-ycor)) < 1)
     [set pcolor obstacles-colour]]
 ]
end

to erase-obstacles
; Removes obtacles in the environment.

 if mouse-down?
 [ ask patches
   [ if ((abs (pxcor - mouse-xcor)) < 1) and ((abs (pycor - mouse-ycor)) < 1)
     [set pcolor background-colour]]
 ]
end
   
to avoid-obstacles
; The robot avoids any obstacles in the environment.

  if (count patches in-cone radius-length radius-angle with [pcolor = obstacles-colour] > 0)
  [ ; there is an obstacle nearby
    ask robot 0 
     [
       bk robot-speed
       lt 90
     ]
  ]
end

to plot-paths
; This instructs the agent to move the pen up if it is down, or vice versa.

  ifelse (pen-mode = "up")
    [ pen-down ]
    [ pen-up ]
end
  
;
; Copyright 2009 by Thomas Christy and William John Teahan.  All rights reserved.
;
; Permission to use, modify or redistribute this model is hereby granted,
; provided that both of the following requirements are followed:
; a) this copyright notice is included.
; b) this model will not be redistributed for profit without permission
;    from William John Teahan.
; Contact William John Teahan for appropriate licenses for redistribution for
; profit.
;
; To refer to this model in publications, please use:
;
; Vacuum Cleaner Robot NetLogo model.
; Artificial Intelligence. Teahan, W. J. (2010). Ventus Publishing Aps.
;