wall following robot

19
A) Use your program to generate the robot motion for the wall-following system on page 28? I define follows for this problem. grid length = 10; grid = 10 by 10 matrix This is done by executing following program: ;;; Define grid length parameter. Grid is square (defparameter *grid-length* 10) ;;; Define grid block and fill it with null inside cell. (defparameter *grid* (make-array (list *grid-length* *grid-length*) :initial-element nil)) Now if we see the grid following is the output: > *grid* #2A((NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL) (NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL) (NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL) (NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL) (NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL) (NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL) (NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL) (NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL) (NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL) (NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL)) Since it is matrix and in lisp matrix notation starts from 0. So, here is the notation of matrix in lisp. Where each (i,j) in cell of matrix are the row & column location of the cell. (0,0) (0,1) (0,2) (0,3) (1,0) (1,1) (1,2) (1,3) (2,0) (2,1) (2,2) (2,3) Now lets make a wall. Wall is created by executing following function: ;;; Utility function of making wall inside grid. (defun grid-wall (row1 row2 col1 col2) (do ((x row1)) ((>= x (+ 1 row2)) 'endd) (do ((y (- col1 1))) ((>= y col2) 'end) (setf y (+ y 1)) (setf (aref *grid* x y) 'wall) ) (setf x (+ x 1)) ))

Upload: rajendra-rana-bhat

Post on 30-Oct-2014

25 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Wall Following Robot

A) Use your program to generate the robot motion for the wall-following system on page 28? I define follows for this problem. grid length = 10; grid = 10 by 10 matrix This is done by executing following program: ;;; Define grid length parameter. Grid is square (defparameter *grid-length* 10) ;;; Define grid block and fill it with null inside cell. (defparameter *grid* (make-array (list *grid-length* *grid-length*) :initial-element nil)) Now if we see the grid following is the output: > *grid* #2A((NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL) (NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL) (NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL) (NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL) (NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL) (NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL) (NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL) (NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL) (NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL) (NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL)) Since it is matrix and in lisp matrix notation starts from 0. So, here is the notation of matrix in lisp. Where each (i,j) in cell of matrix are the row & column location of the cell.

(0,0) (0,1) (0,2) (0,3) (1,0) (1,1) (1,2) (1,3) (2,0) (2,1) (2,2) (2,3)

Now lets make a wall. Wall is created by executing following function: ;;; Utility function of making wall inside grid. (defun grid-wall (row1 row2 col1 col2) (do ((x row1)) ((>= x (+ 1 row2)) 'endd) (do ((y (- col1 1))) ((>= y col2) 'end) (setf y (+ y 1)) (setf (aref *grid* x y) 'wall) ) (setf x (+ x 1)) ))

Page 2: Wall Following Robot

Whereas row1 & row2 and col1 & col2 are normal row and column matrix of the grid. Also following function calls grid-wall to create the position of wall and robot inside the grid. ;; initialization of the grid i.e. draw wall and robot position ;;; inside the grid. (defun initialize-grid () (make-array (list *grid-length* *grid-length*) :initial-element nil) ;; make the walls ;;top wall (grid-wall 0 0 0 9) ;;left wall (grid-wall 0 9 0 0) ;;bottom wall (grid-wall 9 9 0 9) ;;right wall (grid-wall 0 9 9 9) ;; place the robot (setf (aref *grid* 4 5) 'robot)) So, now if we see the grid following is the output. > *grid* #2A((WALL WALL WALL WALL WALL WALL WALL WALL WALL WALL) (WALL NIL NIL NIL NIL NIL NIL NIL NIL WALL) (WALL NIL NIL NIL NIL NIL NIL NIL NIL WALL) (WALL NIL NIL NIL NIL NIL NIL NIL NIL WALL) (WALL NIL NIL NIL NIL ROBOT NIL NIL NIL WALL) (WALL NIL NIL NIL NIL NIL NIL NIL NIL WALL) (WALL NIL NIL NIL NIL NIL NIL NIL NIL WALL) (WALL NIL NIL NIL NIL NIL NIL NIL NIL WALL) (WALL NIL NIL NIL NIL NIL NIL NIL NIL WALL) (WALL WALL WALL WALL WALL WALL WALL WALL WALL WALL)) As can be seen above robot position is in (4,5). This was created when executing initialize-grid function above. I put above grid in matrix tabular form for readability purpose as below. WALL WALL WALL WALL WALL WALL WALL WALL WALL WALLWALL NIL NIL NIL NIL NIL NIL NIL NIL WALLWALL NIL NIL NIL NIL NIL NIL NIL NIL WALLWALL NIL NIL NIL NIL NIL NIL NIL NIL WALLWALL NIL NIL NIL NIL ROBOT NIL NIL NIL WALLWALL NIL NIL NIL NIL NIL NIL NIL NIL WALLWALL NIL NIL NIL NIL NIL NIL NIL NIL WALLWALL NIL NIL NIL NIL NIL NIL NIL NIL WALLWALL NIL NIL NIL NIL NIL NIL NIL NIL WALLWALL WALL WALL WALL WALL WALL WALL WALL WALL WALL

Page 3: Wall Following Robot

Now we need to define sensory inputs and feature vector set and the production rule for above problem. This is done by calling function feature-vector but for this first robot xy position need to be find out. >(robot-xy) (4 5) i.e. robot location is in (4,5) in the grid. Following is the feature input sense and vector and production rules for problem (A). Where s1, s2, s3 … etc are sensory inputs. S1 S2 S3

S8 Robot S4

S7 S6 S5

Feature vector:: X1=s2+s3; X2=s4+s5; X3=s6+s7; X4=s8+s1; Production rules: X4/~X1 North X3/~X4 West X2/~X3 South X1/~X2 East 1 North Above feature vector and production rules are calculated in the function are as follows: ;;;; It will returns the feature vector(x1,x2,x3,x4) sets for sensory inputs. (defun feature-vector () (let* ((coordinate-robot (robot-xy)) (x (first coordinate-robot)) (y (second coordinate-robot)) (s1 (aref *grid* (wrap (- x 1)) (wrap (- y 1)))) (s2 (aref *grid* (wrap (- x 1)) y)) (s3 (aref *grid* (wrap (- x 1)) (wrap (+ y 1)))) (s4 (aref *grid* x (wrap (+ y 1)))) (s5 (aref *grid* (wrap (+ x 1)) (wrap (+ y 1)))) (s6 (aref *grid* (wrap (+ x 1)) y)) (s7 (aref *grid* (wrap (+ x 1)) (wrap (- y 1)))) (s8 (aref *grid* x (wrap (- y 1))))) (list (or s2 s3) ;; x1 (or s4 s5) ;; x2 (or s6 s7) ;; x3 (or s8 s1) ;; x4 ) ))

Page 4: Wall Following Robot

Now we call main program as follows:: (boundaries-follow-robot) Defination of the above function is as below. ;;;First initialize the grid wall then gets the feature vectors set and use the production rule ;;;accordingly to move the robot to east, west, north & south. (defun boundaries-follow-robot () (initialize-grid) (loop (let* ((ls (feature-vector)) (x1 (first ls)) (x2 (second ls)) (x3 (third ls)) (x4 (fourth ls))) (cond ((and x4 (not x1)) (move-north)) ((and x3 (not x4)) (move-west)) ((and x2 (not x3)) (move-south)) ((and x1 (not x2)) (move-east)) (t (move-north))) (format t "Current Robot Position: ~a ~%" (robot-xy)) ) )) NOTE:: Once you run (boundaries-follow-robot) after 1-2 second hit CTRL+BREAK to stop execution After executing above function it goes to infinite loop for which I suggest to hit (ctrl+Break) to break the loop and I got following results. Also in the above code there are function for moving robot to move in different direction like east, north, south & west. Following is the code for that. ;;;Moves the robot west. LEFT (defun move-west () (let* ((xy (robot-xy)) (robot-x (first xy)) (robot-y (second xy)) (destination-x robot-x) (destination-y (wrap (- robot-y 1)))) (unless (aref *grid* destination-x destination-y) ;;execute below only when it is nil. (setf (aref *grid* destination-x destination-y) 'robot) (setf (aref *grid* robot-x robot-y) nil))))

Page 5: Wall Following Robot

;;;Moves the robot south. DOWN (defun move-south () (let* ((xy (robot-xy)) (robot-x (first xy)) (robot-y (second xy)) (destination-x (wrap (+ robot-x 1))) (destination-y robot-y)) (unless (aref *grid* destination-x destination-y) (setf (aref *grid* destination-x destination-y) 'robot) (setf (aref *grid* robot-x robot-y) nil)))) ;;;Moves the robot east. RIGHT (defun move-east () (let* ((xy (robot-xy)) (robot-x (first xy)) (robot-y (second xy)) (destination-x robot-x) (destination-y (wrap (+ robot-y 1)))) (unless (aref *grid* destination-x destination-y) (setf (aref *grid* destination-x destination-y) 'robot) (setf (aref *grid* robot-x robot-y) nil)))) ;;;Moves the robot north. UP (defun move-north () (let* ((xy (robot-xy)) (robot-x (first xy)) (robot-y (second xy)) (destination-x (wrap (- robot-x 1))) (destination-y robot-y)) (unless (aref *grid* destination-x destination-y) (setf (aref *grid* destination-x destination-y) 'robot) (setf (aref *grid* robot-x robot-y) nil)))) ;;;This makes sure it doesn’t go beyond boundary. (defun wrap (n) (mod n *grid-length*)) After executing the function following is the coordinates of the robot as it moves inside the grid. > (BOUNDARIES-FOLLOW-ROBOT) Current Robot Position: (3 5) Current Robot Position: (2 5) Current Robot Position: (1 5) Current Robot Position: (1 6) Current Robot Position: (1 7) Current Robot Position: (1 8) Current Robot Position: (2 8) Current Robot Position: (3 8) Current Robot Position: (4 8) Current Robot Position: (5 8) Current Robot Position: (6 8) Current Robot Position: (7 8)

Page 6: Wall Following Robot

Current Robot Position: (8 8) Current Robot Position: (8 7) Current Robot Position: (8 6) Current Robot Position: (8 5) Current Robot Position: (8 4) Current Robot Position: (8 3) Current Robot Position: (8 2) Current Robot Position: (8 1) Current Robot Position: (7 1) Current Robot Position: (6 1) Current Robot Position: (5 1) Current Robot Position: (4 1) Current Robot Position: (3 1) Current Robot Position: (2 1) Current Robot Position: (1 1) Current Robot Position: (1 2) Current Robot Position: (1 3) Current Robot Position: (1 4) Current Robot Position: (1 5) . . . Basically Robot moves along following coordinates. (4,5) (3,5) (2,5) (1,5) (1,6) (1,7) (1,8) (2,8) (3,8) (4,8) (5,8) (6,8) (7,8) (8,8) (8,7)

(8,6) (8,5) (8,4) (8,3) (8,2) (8,1) (7,1) (6,1) (5,1) (4,1) (3,1) (2,1) (1,1) (1,2) (1,3) (1,4) (1,5) …….. So, now inside the grid robot moves along the coordinates. For instance robot initial position is in (4,5) and shown by R0, then it moves to (3,5) and shown by R1, then it moves to (2,5) as shown by R2, then it moves to (1,5) as shown by R3 ……etc. WALL WALL WALL WALL WALL WALL WALL WALL WALL WALLWALL R27 R28 R29 R30 R3,R31 R4,R32 R5,R33 R6,R34 WALLWALL R26 R2 R7 WALLWALL R25 R1 R8 WALLWALL R24 R0 R9 WALLWALL R23 R10 WALLWALL R22 R11 WALLWALL R21 R12 WALLWALL R20 R19 R18 R17 R16 R15 R14 R13 WALLWALL WALL WALL WALL WALL WALL WALL WALL WALL WALL As can be seen in above grid robot follows the wall perfectly.

Page 7: Wall Following Robot

B) The system on page 73? Following is the input sensors /vectors and production rules for this problems. S1 S2 S3

S8 Robot S4

S7 S6 S5

Feature vector:: S1,S2,S3,S4,S5,S6,S7,S8 Production rules: S2/~S4 East S4/~S6 South S6/~S8 West S8/~S2 North S1 North S3 East S5 South S7 West 1 North As can be seen above production rules need to be changed for above problem w.r.t. (problem A). This was done by writing different feature-vector ;;;; It will returns the feature vector(x1,x2,x3,x4) sets for sensory inputs. (defun feature-vector () (let* ((coordinate-robot (robot-xy)) (x (first coordinate-robot)) (y (second coordinate-robot)) (s1 (aref *grid* (wrap (- x 1)) (wrap (- y 1)))) (s2 (aref *grid* (wrap (- x 1)) y)) (s3 (aref *grid* (wrap (- x 1)) (wrap (+ y 1)))) (s4 (aref *grid* x (wrap (+ y 1)))) (s5 (aref *grid* (wrap (+ x 1)) (wrap (+ y 1)))) (s6 (aref *grid* (wrap (+ x 1)) y)) (s7 (aref *grid* (wrap (+ x 1)) (wrap (- y 1)))) (s8 (aref *grid* x (wrap (- y 1))))) (list s1 s2 s3 s4 s5 s6 s7 s8 ) )) As seen above now it won’t return x1, x2, x3, x4 list instead it will return s1, s2, ….,s8 as list. Also boundaries-follow-robot will also change for it. Which is as follows:

Page 8: Wall Following Robot

;;;First initialize the grid wall then gets the feature vectors set and use the production rule ;;;accordingly to move the robot to east, west, north & south. (defun boundaries-follow-robot () (initialize-grid) (loop (let* ((ls (feature-vector)) (s1 (first ls)) (s2 (second ls)) (s3 (third ls)) (s4 (fourth ls)) (s5 (fifth ls)) (s6 (sixth ls)) (s7 (seventh ls)) (s8 (eighth ls)) ) (cond ((and s2 (not s4)) (move-east)) ((and s4 (not s6)) (move-south)) ((and s6 (not s8)) (move-west)) ((and s8 (not s2)) (move-north)) ( s1 (move-north)) ( s3 (move-east)) ( s5 (move-south)) ( s7 (move-west)) (t (move-north)) ); (format t "Current Robot Position: ~a ~%" (robot-xy)) ))) As seen in above function , this is coded to follow our new production rules like (s2/~s4) east (s4/~s6) south …..etc. After executing above function we got bellow results. Following is the coordinates the robot follow using this production rules.. > (boundaries-follow-robot) Current Robot Position: (3 5) Current Robot Position: (2 5) Current Robot Position: (2 2) Current Robot Position: (1 5) Current Robot Position: (1 6)

Page 9: Wall Following Robot

Current Robot Position: (1 7) Current Robot Position: (1 8) Current Robot Position: (2 8) Current Robot Position: (3 8) Current Robot Position: (4 8) Current Robot Position: (5 8) Current Robot Position: (6 8) Current Robot Position: (7 8) Current Robot Position: (8 8) Current Robot Position: (8 7) Current Robot Position: (8 6) Current Robot Position: (8 5) . . . WALL,0 WALL,1 WALL,2 WALL,3 WALL,4 WALL,5 WALL,6 WALL,7 WALL,8 WALL,9 WALL,1 R29 R30 R3,R31 R4,R32 R5,R33 R6,R34 WALL WALL,2 R26 R2,R27 R28 R1 R7 WALL WALL,3 R25 R0 R8 WALL WALL,4 R24 R9 WALL WALL,5 R23 R10 WALL WALL,6 R22 R11 WALL WALL,7 R21 R12 WALL WALL,8 R20 R19 R18 R17 R16 R15 R14 R13 WALL WALL,9 WALL WALL WALL WALL WALL WALL WALL WALL WALL Hence, as shown above in the above grid , robot moves R0, R1, R2, R3 ..etc. C) Solve Problem 3 in Exam 1? For this we created following grid as shown in problem and by executing suitable function as defined above. WALL,0 WALL,1 WALL,2 WALL,3 WALL,4 WALL,5 WALL,6 WALL,7 WALL,8 WALL,9 WALL,10

WALL,1 WALL WALL,2 R0 WALL WALL,3 WALL WALL,4 WALL WALL,5 WALL WALL WALL WALL WALL WALL WALL WALL,6 WALL WALL WALL WALL WALL WALL,7 WALL WALL WALL WALL WALL WALL,8 WALL

Page 10: Wall Following Robot

WALL,9 WALL WALL,10 WALL WALL WALL WALL WALL WALL WALL WALL WALL WALL Following is the feature inputs and vector S1 S2 S3

S8 Robot S4

S7 S6 S5

Feature vector:: X1=s1+s2; X2=s3+s4; X3=s5+s6; X4=s7+s8; Production rules: X3/~X2 East X4/~X3 South X1/~X4 West X2/~X1 North 1 South Following function were changed for this problem. ;;; Define grid length parameter. Grid is square (defparameter *grid-length* 11) ;;; Define grid block and fill it with null inside cell. (defparameter *grid* (make-array (list *grid-length* *grid-length*) :initial-element nil)) ;; initialization of the grid i.e. draw wall and robot position ;;; inside the grid. (defun initialize-grid () (make-array (list *grid-length* *grid-length*) :initial-element nil) ;; make the walls ;;top wall (grid-wall 0 0 0 10) ;;left wall (grid-wall 0 10 0 0) ;;bottom wall (grid-wall 10 10 0 10) ;;right wall (grid-wall 0 10 10 10) ;;block (grid-wall 5 7 2 3) (grid-wall 5 7 6 7) (grid-wall 5 5 4 5)

Page 11: Wall Following Robot

;; place the robot (setf (aref *grid* 2 5) 'robot)) ;;;; It will returns the feature vector(x1,x2,x3,x4) sets for sensory inputs. (defun feature-vector () (let* ((coordinate-robot (robot-xy)) (x (first coordinate-robot)) (y (second coordinate-robot)) (s1 (aref *grid* (wrap (- x 1)) (wrap (- y 1)))) (s2 (aref *grid* (wrap (- x 1)) y)) (s3 (aref *grid* (wrap (- x 1)) (wrap (+ y 1)))) (s4 (aref *grid* x (wrap (+ y 1)))) (s5 (aref *grid* (wrap (+ x 1)) (wrap (+ y 1)))) (s6 (aref *grid* (wrap (+ x 1)) y)) (s7 (aref *grid* (wrap (+ x 1)) (wrap (- y 1)))) (s8 (aref *grid* x (wrap (- y 1))))) (list (or s1 s2) ;; x1 (or s3 s4) ;; x2 (or s5 s6) ;; x3 (or s7 s8) ;; x4 ) )) ;;;First initialize the grid wall then gets the feature vectors set and use the production rule ;;;accordingly to move the robot to east, west, north & south. (defun boundaries-follow-robot () (initialize-grid) (loop (let* ((ls (feature-vector)) (x1 (first ls)) (x2 (second ls)) (x3 (third ls)) (x4 (fourth ls))) (cond ((and x3 (not x2)) (move-east)) ((and x4 (not x3)) (move-south)) ((and x1 (not x4)) (move-west)) ((and x2 (not x1)) (move-north)) (t (move-south))) (format t "Current Robot Position: ~a ~%" (robot-xy)) ) )) So, finally we execute the the function and it gives following coordinates of the robot movement. > (BOUNDARIES-FOLLOW-ROBOT)

Page 12: Wall Following Robot

Current Robot Position: (3 5) Current Robot Position: (4 5) Current Robot Position: (4 6) Current Robot Position: (4 7) Current Robot Position: (4 8) Current Robot Position: (5 8) Current Robot Position: (6 8) Current Robot Position: (7 8) Current Robot Position: (8 8) Current Robot Position: (8 7) Current Robot Position: (8 6) Current Robot Position: (8 5) Current Robot Position: (7 5) Current Robot Position: (6 5) Current Robot Position: (6 4) Current Robot Position: (7 4) Current Robot Position: (8 4) Current Robot Position: (8 3) Current Robot Position: (8 2) Current Robot Position: (8 1) . . . WALL,0 WALL,1 WALL,2 WALL,3 WALL,4 WALL,5 WALL,6 WALL,7 WALL,8 WALL,9 WALL,10

WALL,1 WALL WALL,2 R0 WALL WALL,3 R1 WALL WALL,4 R2 R3 R4 R5 WALL WALL,5 WALL WALL WALL WALL WALL WALL R6 WALL WALL,6 WALL WALL R15 R14 WALL WALL R7 WALL WALL,7 WALL WALL R16 R13 WALL WALL R8 WALL WALL,8 R20 R19 R18 R17 R12 R11 R10 R9 WALL WALL,9 WALL WALL,10 WALL WALL WALL WALL WALL WALL WALL WALL WALL WALL Robot moves as shown above boundary following the obstacle. Robot movements are given by R0, R1, R2, R3, R4, R5, R6 , R7 , R8 , R9 , R10 …… etc.

Page 13: Wall Following Robot

E)

1) for a robot that does obstacle contouring? Following is the grid for it. WALL,0 WALL,1 WALL,2 WALL,3 WALL,4 WALL,5 WALL,6 WALL,7 WALL,8 WALL,9

WALL,1 WALL WALL,2 R,0 WALL WALL,3 WALL WALL,4 WALL WALL WALL WALL,5 WALL WALL WALL WALL,6 WALL WALL WALL WALL,7 WALL WALL,8 WALL WALL,9 WALL WALL WALL WALL WALL WALL WALL WALL WALL I use following feature matrix and vectors and production rules for it. S1 S2 S3 S8 Robot S4 S7 S6 S5 Feature:: s1,s2,s3,s4,s5,s6,s7,s8 Feature vector:: X1=s1+s2; X2=s3+s4; X3=s5+s6; X4=s7+s8; Production rules: X3/~X2 East X4/~X3 South X1/~X4 West X2/~X1 North 1 South For above feature vector and production rules. I changed the function to accommodate the above rules. ;;; Define grid length parameter. Grid is square (defparameter *grid-length* 10) ;;; Define grid block and fill it with null inside cell. (defparameter *grid*

Page 14: Wall Following Robot

(make-array (list *grid-length* *grid-length*) :initial-element nil)) ;;;; It will returns the feature vector(x1,x2,x3,x4) sets for sensory inputs. (defun feature-vector () (let* ((coordinate-robot (robot-xy)) (x (first coordinate-robot)) (y (second coordinate-robot)) (s1 (aref *grid* (wrap (- x 1)) (wrap (- y 1)))) (s2 (aref *grid* (wrap (- x 1)) y)) (s3 (aref *grid* (wrap (- x 1)) (wrap (+ y 1)))) (s4 (aref *grid* x (wrap (+ y 1)))) (s5 (aref *grid* (wrap (+ x 1)) (wrap (+ y 1)))) (s6 (aref *grid* (wrap (+ x 1)) y)) (s7 (aref *grid* (wrap (+ x 1)) (wrap (- y 1)))) (s8 (aref *grid* x (wrap (- y 1))))) (list (or s1 s2) ;; x1 (or s3 s4) ;; x2 (or s5 s6) ;; x3 (or s7 s8) ;; x4 ) )) ;; initialization of the grid i.e. draw wall and robot position ;;; inside the grid. (defun initialize-grid () (make-array (list *grid-length* *grid-length*) :initial-element nil) ;; make the walls ;;top wall (grid-wall 0 0 0 9) ;;left wall (grid-wall 0 9 0 0) ;;bottom wall (grid-wall 9 9 0 9) ;;right wall (grid-wall 0 9 9 9) ;;wall in middle (grid-wall 4 6 4 5) ;; place the robot (setf (aref *grid* 2 5) 'robot)) ;;;First initialize the grid wall then gets the feature vectors set and use the production rule ;;;accordingly to move the robot to east, west, north & south. (defun boundaries-follow-robot () (initialize-grid) (loop (let* ((ls (feature-vector)) (x1 (first ls)) (x2 (second ls)) (x3 (third ls)) (x4 (fourth ls)))

Page 15: Wall Following Robot

(cond ((and x2 (not x1)) (move-north)) ((and x1 (not x4)) (move-west)) ((and x4 (not x3)) (move-south)) ((and x3 (not x2)) (move-east)) (t (move-south))) (format t "Current Robot Position: ~a ~%" (robot-xy)) ) )) NOTE:: Once you run (boundaries-follow-robot) after 1-2 second hit CTRL+BREAK to stop execution Following is the coordinates of the robot movement >(BOUNDARIES-FOLLOW-ROBOT) Current Robot Position: (3 5) Current Robot Position: (3 6) Current Robot Position: (4 6) Current Robot Position: (5 6) Current Robot Position: (6 6) Current Robot Position: (7 6) Current Robot Position: (7 5) Current Robot Position: (7 4) Current Robot Position: (7 3) Current Robot Position: (6 3) Current Robot Position: (5 3) Current Robot Position: (4 3) Current Robot Position: (3 3) Current Robot Position: (3 4) Current Robot Position: (3 5) Current Robot Position: (3 6) Current Robot Position: (4 6) Current Robot Position: (5 6) . . . WALL,0 WALL,1 WALL,2 WALL,3 WALL,4 WALL,5 WALL,6 WALL,7 WALL,8 WALL,9

WALL,1 WALL WALL,2 R0 WALL WALL,3 R13 R14 R1,R15 R2,R16 WALL WALL,4 R12 WALL WALL R3,R17 WALL WALL,5 R11 WALL WALL R4,R18 WALL WALL,6 R10 WALL WALL R5 WALL

Page 16: Wall Following Robot

WALL,7 R9 R8 R7 R6 WALL WALL,8 WALL WALL,9 WALL WALL WALL WALL WALL WALL WALL WALL WALL Hence as can be seen from above figure it basically countering the obstacle clockwise. 2) Now if a robot that can sense only {s1, s3, s5, s7} then it cannot do obstacle avoidance since it cannot see the cells directly in front of it, i.e., the cells it needs to move to. 3) For it can only sense {s2,s4,s6,s8} only. After using feature matrix/vector and production rule as part B. We get following robot position. >(BOUNDARIES-FOLLOW-ROBOT) Current Robot Position: (1 5) Current Robot Position: (1 6) Current Robot Position: (1 7) Current Robot Position: (1 8) Current Robot Position: (2 8) Current Robot Position: (3 8) Current Robot Position: (4 8) Current Robot Position: (5 8) Current Robot Position: (6 8) Current Robot Position: (7 8) Current Robot Position: (8 8) Current Robot Position: (8 7) Current Robot Position: (8 6) Current Robot Position: (8 5) Current Robot Position: (8 4) Current Robot Position: (8 3) Current Robot Position: (8 2) Current Robot Position: (8 1) Current Robot Position: (7 1) Current Robot Position: (6 1) D) For obstacle avoidance lets use this following grid and robot WALL WALL WALL WALL WALL WALL WALL WALL WALL WALL R WALL WALL WALL WALL WALL WALL WALL WALL WALL WALL WALL WALL WALL WALL WALL WALL WALL WALL WALL WALL WALL WALL WALL WALL WALL WALL WALL WALL WALL WALL

Page 17: Wall Following Robot

S1 S2 S3

R

Feature vectors are as follows:: X1=S1*S2*S3 X5=S1 X2=S2 X6=S3 X3=S1*S2 X7=S1*S3 X4=S2*S3 Production Rules are as follows:: X1+X2 South X3 East X4 West X5 North-East X6 North-West 1 North We need to write code for North-East & North-West and they are as follows: ;;;Moves the robot north-east. UP-RIGHT (defun move-north-east () (let* ((xy (robot-xy)) (robot-x (first xy)) (robot-y (second xy)) (destination-x (wrap (- robot-x 1))) (destination-y (wrap (+ robot-y 1)) ) ) (unless (aref *grid* destination-x destination-y) (setf (aref *grid* destination-x destination-y) 'robot) (setf (aref *grid* robot-x robot-y) nil))) ) ;;;Moves the robot north-west. UP-LEFT (defun move-north-west () (let* ((xy (robot-xy)) (robot-x (first xy)) (robot-y (second xy)) (destination-x (wrap (- robot-x 1))) (destination-y (wrap (- robot-y 1)) )) (unless (aref *grid* destination-x destination-y) (setf (aref *grid* destination-x destination-y) 'robot) (setf (aref *grid* robot-x robot-y) nil)))) Also following function changes were done for this particular problems to follow above production rules. ;;;; It will returns the feature vector(x1,x2,x3,x4,x5,x6,x7) sets for sensory inputs. (defun feature-vector () (let* ((coordinate-robot (robot-xy)) (x (first coordinate-robot))

Page 18: Wall Following Robot

(y (second coordinate-robot)) (s1 (aref *grid* (wrap (- x 1)) (wrap (- y 1)))) (s2 (aref *grid* (wrap (- x 1)) y)) (s3 (aref *grid* (wrap (- x 1)) (wrap (+ y 1))))) (list (and s1 s2 s3) ;; x1 s2 ;; x2 (and s1 s2) ;; x3 (and s2 s3) ;; x4 s1 ;; x5 s3 ;; x6 (and s1 s3) ;; x7 ) )) ;; initialization of the grid i.e. draw wall and robot position ;;; inside the grid. (defun initialize-grid () (make-array (list *grid-length* *grid-length*) :initial-element nil) ;; make the walls ;;top wall (grid-wall 0 0 0 8) ;;left wall (grid-wall 0 8 0 0) ;;bottom wall (grid-wall 8 8 0 8) ;;right wall (grid-wall 0 8 8 8) ;; place the robot (setf (aref *grid* 1 3) 'robot)) ;;;First initialize the grid wall then gets the feature vectors set and use the production rule ;;;accordingly to move the robot to east, west, north & south. (defun boundaries-follow-robot () (initialize-grid) (loop (let* ((ls (feature-vector)) (x1 (first ls)) (x2 (second ls)) (x3 (third ls)) (x4 (fourth ls)) (x5 (fifth ls)) (x6 (sixth ls)) (x7 (seventh ls)) ) (cond ((or x1 x2) (move-south)) (x3 (move-east)) (x4 (move-west))

Page 19: Wall Following Robot

(x5 (move-north-east)) (x6 (move-north-west)) (t (move-north))) (format t "Current Robot Position: ~a ~%" (robot-xy)) ) ) ) NOTE:: Once you run (boundaries-follow-robot) after 1-2 second hit CTRL+BREAK to stop execution Once the program is executed, robot moves along following way and it oscillates along the same path. WALL WALL WALL WALL WALL WALL WALL WALL WALL WALL R0,R7 WALL WALL R3,R4 R2,R5 R1,R6 WALL WALL WALL WALL WALL WALL WALL WALL WALL WALL WALL WALL WALL WALL WALL WALL WALL WALL WALL WALL WALL WALL WALL WALL WALL WALL WALL WALL 2. Clearly if it can sense only S1,S3,S5,S7 then obviously it can’t follow because it won’t be able to see S2, S4, S6, S8 which are just right, left, above and below and create feature vectors. 3. Similary when it can only sense s2,s4,s6 & s7 then it follows part B feature vector and production rules and move.