menu 1 [start]
; other menu items are debugging and testing programs
menu 3 [rove]
menu 4 [not-pet]
menu 5 [eat-and-poop]
menu 6 [sense]
; global variable is-moving to turn the movement method on and off
; movement is turned off while any other action is executed
; and turned back on when nothing else is.
global [is-moving]
to start
setis-moving 1
forever[movement] ;roving method
forever[setis-moving 0 escape setis-moving 1] ;takes over if it runs into something
when [poop-bumper][setis-moving 0 poop setis-moving 1] ; waits to poop
when [tongue-bumper][setis-moving 0 eat setis-moving 1] ;waits to eat
forever[wag] ;looks for petting
end
;; if the dog is not doing anything else,
;; it will choose one of three random movements
;; to execute: go-forward, stop-moving, or random-turn.
to movement
if (is-moving = 1) [
random-move random 10
wait 5 + random 20]
end
;; helper method for movement:
;; picks a random movement and executes
;; there is a 60% chance that it will go-forward
;; 30% chance it will stop-moving, and 10% chance
;; that it will turn
to random-move :number
if :number = 0 [go-forward]
if :number = 1 [go-forward]
if :number = 2 [go-forward]
if :number = 3 [go-forward]
if :number = 4 [go-forward]
if :number = 5 [go-forward]
if :number = 6 [stop-moving]
if :number = 7 [stop-moving]
if :number = 8 [stop-moving]
if :number = 9 [random-turn (random 1)]
end
;; moves away and turns if it runs into an obstacle while roving
to escape
if front-bumper [turn-backward]
if back-bumper [turn-forward]
end
;; when either of the light sensors become dark (i.e. someone pets the dog)
;; it will stop and wag its tail
to wag
if or back-sensor > 230 head-sensor > 230
[setis-moving 0
stop-moving
wag-tail
wait 20
setis-moving 1]
end
;; helper method for wag: the actual wagging movement
to wag-tail
repeat 7 [tail onfor 5 rd]
end
;; when the poop-bumper is pressed, stop moving
;; and open and close the trapdoor
to poop
stop-moving
trap-door thisway onfor 5
wait 5
trap-door thatway onfor 5
end
;; when the tongue-bumper is pressed, it stops moving,
;; wags its tail, and eats
to eat
stop-moving
wag-tail
wait 20
end
; sub-methods for leg movement
;;;;;;;;
;movement
;;;;;;;;
to stop-moving
left-wheel off
right-wheel off
end
to go-forward
left-wheel on thisway
right-wheel on thisway
end
to go-backward
left-wheel on thatway
right-wheel on thatway
end
to right-turn
right-wheel rd on
left-wheel on
end
to left-turn
left-wheel rd on
right-wheel on
end
; method for escape: front-bumper
to turn-backward
go-backward
wait 15
left-wheel thatway
right-wheel thatway
random-turn (random 1)
wait 35
left-wheel thisway on
right-wheel thisway on
end
; method for escape: back-bumper
to turn-forward
left-wheel thisway
right-wheel thisway
go-forward
wait 15
random-turn (random 1)
wait 35
left-wheel thisway
right-wheel thisway
left-wheel on
right-wheel on
end
; sub-method that picks a random direction to turn
to random-turn :number
if :number = 0 [left-turn]
if :number = 1 [right-turn]
end
;;;;;;;end movement
; here the various sensors/motors/switches are designated
;;;;;;
;variables
;;;;;;
to front-bumper
output switch 7
end
to back-bumper
output switch 8
end
to poop-bumper
output switch 9
end
to tongue-bumper
output switch 10
end
to left-wheel
a,
end
to right-wheel
b,
end
to tail
c,
end
to trap-door
d,
end
to head-sensor
output (sensor 0)
end
to back-sensor
output (sensor 1)
end
;;;;;;;; end variables
;; some debugging methods
; rove: moves and avoids obstacles
to rove
setis-moving 1
forever[movement] ;roving method
forever[setis-moving 0 escape setis-moving 1] ;takes over if it runs into something
end
; not-pet: program without the wag method - removes use of light sensors
to not-pet
setis-moving 1
forever[movement] ;roving method
forever[setis-moving 0 escape setis-moving 1] ;takes over if it runs into something
when [poop-bumper][setis-moving 0 poop setis-moving 1] ; waits to poop
when [tongue-bumper][setis-moving 0 eat setis-moving 1] ;waits to eat
end
; eat-and-poop: no movement
to eat-and-poop
setis-moving 1
when [poop-bumper][setis-moving 0 poop setis-moving 1] ; waits to poop
when [tongue-bumper][setis-moving 0 eat setis-moving 1] ;waits to eat
end
; sense: no movement, tests wag, poop, and eat
to sense
setis-moving 1
when [poop-bumper][setis-moving 0 poop setis-moving 1] ; waits to poop
when [tongue-bumper][setis-moving 0 eat setis-moving 1] ;waits to eat
forever[wag] ;looks for petting
end
|