📄 drinkrobots.tz
字号:
#so it equals 20+2+4
if(distance <= 26):{
#judge if a dispenser has a bottle
if((self get-bottle-state) ==0):{
#make sure the dispenser robot can move to the location of drinkMachine
closestdrinkMachine = drinkMachineShape.
objectdirection = (closestdrinkMachine get-location)- (self get-location).
current-velocity = (self get-velocity).
angle-of-turn = self find-angle from current-velocity to objectdirection.
self turn-by angle angle-of-turn / 10.
#create a new bottle
owndrinkBottle = (new drinkBottles).
#set bottle exsist state to 1
controller set-bottle-exsist the-number 1.
########################################################################################
#set the bottle has been picked up
owndrinkBottle set-be-pickedup value 1.
#set a color to the created bottle
owndrinkBottle set-color to (.1,.1,.1).
##########################################################################################
#Put the created bottle on the top of robot
owndrinkBottle move-drinkBottle to-location current-location2+(0,2,0).
#set robot has a bottle
self set-bottle-state value 1.
print "A dis rob obtains a full bottle".
return 0.
}
# if a robot has a full bottle, it wanders with bottle
else:{# if robot has one bottle and close to home <20 too
owndrinkBottle move-drinkBottle to-location current-location2+(0,2,0).
return 0.
}
}
#if robot is out of 20m
else:{ # out of 20m
#if robot is out of 20m and has a full bottle, then it wanders with bottle
if((self get-bottle-state) ==1):{
owndrinkBottle move-drinkBottle to-location current-location2+(0,2,0).
return 0.
}
#otherwise wander
else:{# otherwise wander
return 0.
}
}
}#end forea
return 1.
###########################################################################################
#Define a method: a robot behavior to delieve a full bottle
+ to delive bottle thirstyProgrammersShape(object):
#get bottle state
bottlestate = self get-bottle-state.
#get a list of all full bottles
drinkbottleList = all drinkBottles.
#set two variables
mini-distance = 10 .
mini-distance2 = 200 .
#get a list of all thirstyProgrammers
thirstyProgrammersList = all thirstyProgrammers.
#make a loop
for each drinkbottleShape in (drinkbottleList ):{
#get distance between robot and drink bottle
distance2 = |(self get-location)-(drinkbottleShape get-location)|.
#get the closest drink bottle
if(distance2 < mini-distance2):{
mini-distance2 = distance2.
closestbottle = drinkbottleShape.
}
}#end forea
#when a robot has a bottle, it drop the bottle to the floor
if((self get-bottle-state) == 1):{#' have bottle
#Make a loop to find the cloest thirstyProgrammers
for each thirstyProgrammersShape in (thirstyProgrammersList):{
distance = |(self get-location) - (thirstyProgrammersShape get-location)|.
#if a robot with a bottle is in 5m, drop the bottle on the current location
if(distance < mini-distance):{#' for each programer nearby a closest bottle(attach by a )
#the dropped bottle becomes white color
closestbottle set-color to (10,10,10).
#set bottle state to 0
self set-bottle-state value 0.
#set picked bottle state to 0
closestbottle set-be-pickedup value 0.
#set a empty bottle state to 1
controller set-empty-bottle-exsist the-number 1.
print "A dis rob delieves a full bottle and place it on the floor".
return 0.
}
}#for
}
return 1.
###########################################################################################
#define a method: robot has wander behavior
+ to wander-randomly:
if(time-since-turn >= random[1000]): {
angle-of-turn = random[(2 * pi)] - pi.
self turn-by angle angle-of-turn.
time-since-turn = 0.
}
self point vertex (0,0,1) at (self get-velocity).
time-since-turn = time-since-turn + 1 .
#define a method: this show a subsumption architecture
+ to activateaction:
#this is Perceptual Schemas, return 0 or 1
activeateObtain = (self obtain bottle drinkMachineShape).
activeateDelive = (self delive bottle thirstyProgrammersShape).
activeateRun = (self wander-randomly).
#the select behavior from hight priority to low priority
robotCommand = noCommand.
if(robotCommand == noCommand) && (activeateObtain == 1):
robotCommand = obtainbottlecommand.
if(robotCommand == noCommand) && (activeateDelive == 1):
robotCommand = deliverbottlecommand.
if(robotCommand == noCommand):
robotCommand = wandercommand.
#This is Motor Schemas to execute the selected behavior
if robotCommand == obtainbottlecommand: self obtain bottle drinkMachineShape.
if robotCommand == deliverbottlecommand: self delive bottle thirstyProgrammersShape.
if robotCommand == wandercommand: self wander-randomly.
#keep executing activateaction
+ to iterate:
super iterate.
self activateaction.
}
######################################################################
#Create a collectorBot as a sub-class of drinkRobot
#It inherit all varabiaes and methods from drinkRobot class and belong to mobile class
drinkRobot: collectorBot(aka collectorBots){
#define variables
+ variables:
bottleList(list).
bottleShape(object).
owndrinkBottleList(list).
owndrinkBottleShape(object).
mini-distance(float).
distance(float).
mini-distance2(float).
distance2(float).
mini-distance4(float).
distance4(float).
objectdirection(vector).
ownbottlestate(int).
length(int).
width(int).
activeateCollect(int).
activeateDeposit(int).
activeateWander(int).
activeateseekRecycleBin(int).
depositbottlecommand(int).
seekRecycleBincommand(int).
collectbottlecommand(int).
wanderrandomcommand(int).
current-location(vector).
robotCommand(int).
noCommand(int).
mini-distance-to-bottle(float).
collectorBotclosestbottle(object).
distance3(float).
distance5(float).
#init the color and location of collectors and other variables at the beginning
+ to init:
self set-color to (9, 9, 9).
self set-neighborhood-size to 5.0.
self move to (-2.5, 2.5, -2.5) + random[(50,0,50)] - (25,0,25).
ownbottlestate = 1 .
mini-distance = 10.
robotCommand = 0 .
noCommand = 0.
activeateseekRecycleBin= 0.
activeateDeposit = 0.
activeateCollect = 0.
activeateWander = 0.
depositbottlecommand = 4.
seekRecycleBincommand = 3.
collectbottlecommand = 2.
wanderrandomcommand = 1.
mini-distance-to-bottle = 200.
length = 4.
width = 4.
#set a state to collector robot
+ to set-ownbottle-state value val(int):
ownbottlestate = val.
#get return state
+ to get-ownbottle-state:
return ownbottlestate.
#define a method: a behavior for collector
+ to collect bottle bottleShape(object): #" return 2.
#get the list of all empty bottles
bottleList = all drinkBottles.
#get the state of ownbottle
ownbottlestate = self get-ownbottle-state.
current-location = self get-location.
#if the empty bottle is exsist
if((controller get-empty-bottle-exsist)==1):{
#make a loop to check all empty bottles
for each bottleShape in (bottleList):{
distance = |(self get-location) - (bottleShape get-location)|.
#find a cloest bottle between robot and an empty bottle
if(distance < mini-distance-to-bottle):{
mini-distance-to-bottle = distance.
collectorBotclosestbottle = bottleShape.
}
}#end foreach
#judge if collector robot is less closer than 5m
distance3 = |(self get-location) - (collectorBotclosestbottle get-location)|.
if((distance3 < 10) && ((collectorBotclosestbottle get-be-pickedup) == 0)):{ #' && (|current-velocity|) ==0)
#if a collector robot does not has a empty bottle, then to pick up the empty bottle
if(ownbottlestate ==1) :{ #' do not have a bottle yet
collectorBotclosestbottle move-drinkBottle to-location current-location+(0,2,0).
self set-ownbottle-state value 0.
collectorBotclosestbottle set-be-pickedup value 3.
print "A collect rob picks up the an empty which is close to it".
return 1.
}
#otherwise wander
else:{ #'have a bottle
collectorBotclosestbottle move-drinkBottle to-location current-location+(0,2,0).
return 0. #' 0 is randowm move
}
}
#robot is out of 5m
else:{ #' over 20 distance then.
if((self get-ownbottle-state) ==0):{
collectorBotclosestbottle move-drinkBottle to-location current-location+(0,2,0).
}
}#'ending (distance3 < 10)
} #endif means not exsit.
else:{
return 0 .
}
return 0.
#Define a method: a behavior for collectors to seek recycleBin
+ to seek-recycleBin:
#get ownbottle state
ownbottlestate = self get-ownbottle-state.
mini-distance2 = 88.
#print "A dis rob is seeking recyle Bin and going there".
#a collect has a empty bottle
if(ownbottlestate == 0):{
#work out distance between collector and recycleBin
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -