📄 drinkrobots.tz
字号:
#Name: lzhou ID:048774
#This programm carries out the basic functions of this mutil-agent system
#Load three basic classes
@include "Control.tz"
@include "Mobile.tz"
@include "Stationary.tz"
#Define the name of controller
Controller drinkRobotsControl.
#Define the class of control
Control: drinkRobotsControl{
#Define all needed variables in this class
+ variables:
arenafloorShape(object).
arenafloor(object).
bottle-exsist (int).
empty-bottle-exsist (int).
#To init the background color, shadows and variables
+ to init:
#set color, shadows and variables.
self set-background-color to (.2,.4,.8).
self enable-lighting.
self enable-smooth-drawing.
self move-light to (0,60,40).
self enable-shadows.
bottle-exsist = 0.
empty-bottle-exsist = 0.
#To create and set the floor of enviorment
arenafloorShape = (new Cube init-with size(160,2,160)).
arenafloor = new Stationary.
arenafloor register with-shape arenafloorShape at-location(0,0,0).
arenafloor catch-shadows.
arenafloor set-color to (.4,.6,.9).
arenafloor set-texture-scale to 10 .
#To set the camera
print "Setting up the simulation".
self point-camera at (0,0,0) from (128,49.7,116.1).
self set-z-clip to 400.0 .
#To define all requested objects
5 new dispenserBots.
5 new collectorBots.
10 new thirstyProgrammers.
1 new drinkMachine.
1 new recycleBin.
1 new fourFences.
#Set to return a state to judge if a new bottle is exsist
+ to get-bottle-exsist:
return bottle-exsist.
#To create a method to change bottle-exsist state
+ to set-bottle-exsist the-number exsist (int):
bottle-exsist = exsist.
#To create a method to return a state to judge if a empty bottle is exsist
+ to get-empty-bottle-exsist:
return empty-bottle-exsist.
#To create a method to change empty-bottle-exsist state
+ to set-empty-bottle-exsist the-number bottle-exsist (int):
empty-bottle-exsist = bottle-exsist.
self update-neighbors.
#To keep executing the controller
+ to iterate:
self update-neighbors.
super iterate.
}
#Define a main drinkRobot class belong to Mobile class
Mobile:drinkRobot(aka drinkRobots){
#Define all variables in this class
+ variables:
drinkRobotShape(object).
current-velocity(vector).
current-rotation(matrix).
current-location(vector).
new-velocity(vector).
new-location(vector).
ownTouchSensor(object).
ownWheelEffector(object).
owndrinkBottle(object).
keep-going(int).
time-since-turn(int).
angle-of-turn(float).
angle-to-turn(float).
#closest(object).
pi(float).
first-direction (vector).
second-direction (vector).
#To initialize all variables
+ to init:
#Define the shape, size, velocity of all robot(dispenserBot and collectorBots)
drinkRobotShape = (new Cube init-with size(4,3,4)).
self set-shape to (drinkRobotShape).
self set-velocity to (10,0,10).
keep-going = 1 .
time-since-turn = 0 .
angle-of-turn = 0.0 .
pi = 3.14159 .
# Enable collision handling for the feederBot. The method to do this is defined below.
self setup-collision-handler.
# Create a touch sensor and a wheel effector for the feederBot. The classes for
# these objects are defined below.
ownTouchSensor = (new touchSensor).
ownWheelEffector = (new wheelEffector).
# Attach all sensors and effector to the feederBot .
self keep-sensors-and-effectors-attached.
#Define a method to setup collision handler
+ to setup-collision-handler:
self handle-collisions with-type "fourFences" with-method "avoid".
self handle-collisions with-type "drinkMachine" with-method "avoid2".
self handle-collisions with-type "recycleBin" with-method "avoid3".
#Define a method to turn direction
+ to turn-by angle angle-of-turn(float):
current-velocity = (self get-velocity).
self rotate around-axis(0,1,0) by angle-of-turn.
current-rotation = (self get-rotation).
new-velocity = current-rotation*current-velocity.
self set-velocity to new-velocity.
self point vertex (0,0,1) at new-velocity.
#Define a method to
+ to find-angle from first-direction(vector) to second-direction(vector):
angle-to-turn = angle(first-direction, second-direction - (0,0.00001,0)).
if (cross(first-direction, second-direction))::y < 0.0:
return - angle-to-turn.
else: return angle-to-turn.
#Define a method to avoid the collisions between four fences and robots
+ to avoid fourFences fourFencesObject(object):
current-location = (self get-location).
new-location = current-location-(current-velocity*0.1).
self move to new-location.
angle-of-turn=90*pi/180.
self turn-by angle angle-of-turn.
#Define a method to avoid the collisions between drinkMachine and robots
+ to avoid2 drinkMachine drinkMachine(object):
current-location = (self get-location).
new-location = current-location-(current-velocity*0.1).
self move to new-location.
angle-of-turn=90*pi/180.
self turn-by angle angle-of-turn.
#Define a method to avoid the collisions between recycleBin and robots
+ to avoid3 recycleBin recycleBin(object):
current-location = (self get-location).
new-location = current-location-(current-velocity*0.1).
self move to new-location.
angle-of-turn=90*pi/180.
self turn-by angle angle-of-turn.
#Define a method: Move and orientate light sensor and wheel effector to match new hoverBot
# position and orientation, ensuring that they remain attached to the hoverBot.
+ to keep-sensors-and-effectors-attached:
current-location = (self get-location).
current-velocity = (self get-velocity).
ownTouchSensor move-sensor to-location current-location.
ownTouchSensor turn-sensor to-direction current-velocity.
ownWheelEffector move-effector to-location current-location + (0,-0.5,0).
#Define a method: move drinkbottle(empty or full) and wheel effector to new position
+ to keep-drinkBottle-and-effectors-attached:
current-location = (self get-location).
current-velocity = (self get-velocity).
owndrinkBottle move-drinkBottle to-location current-location.
#To keep excuteing codes below
+ to iterate:
self keep-sensors-and-effectors-attached.
}
#Define a class for dispenserbot as a sub class of drinkRobot class
#It will inherit all methods and variables defined in main class but this is more sepscific
drinkRobot: dispenserBot(aka dispenserBots){
#Define all variables
+ variables:
drinkMachineList(list).
drinkMachineShape(object).
thirstyProgrammersList(list).
thirstyProgrammersShape(object).
mini-distance(float).
distance(float).
objectdirection(vector).
bottlestate(int).
closestdrinkMachine(object).
activeateObtain(int).
activeateDelive(int).
activeateRun(int).
obtainbottlecommand(int).
deliverbottlecommand(int).
wandercommand(int).
drinkbottleList(list).
drinkbottleShape(object).
closestbottle(object).
current-location2(vector).
distance2(float).
mini-distance2(float).
robotCommand(int).
noCommand(int).
#To init variables as well as the color of disrobot and the location at the beginning
+ to init:
self set-color to (1.0, 1.0, 0.0).
self set-neighborhood-size to 5.0.
self move to (-2.5, 2.5, -2.5) + random[(50,0,50)] - (25,0,25).
bottlestate = 0 .
mini-distance = 20 .
robotCommand = 0 .
noCommand = 0.
obtainbottlecommand = 3 .
deliverbottlecommand = 2 .
wandercommand = 1.
#Define a method to judge if a dispenser robot has a bottle.
+ to set-bottle-state value val(int):
bottlestate = val.
#To return a dispenser robot state about bottle
+ to get-bottle-state:
return bottlestate.
#Define a method: a behavior for dispenser robots to obtain a full bottle
#from drinkMachine
+ to obtain bottle drinkMachineShape(object):
# read all drinkMachine to a list
drinkMachineList = all drinkMachine.
#make a for loop for drinkMachine to judge which drinkMachine is close to dispenser robot
for each drinkMachineShape in (drinkMachineList):{
#get a distance between a drinkMachine and robt
distance = |(self get-location) - (drinkMachineShape get-location)|.
#return a bottle state
bottlestate = self get-bottle-state.
current-location2 = self get-location.
#judge if robot is less closer than 20m. This distance=from the centre of drinkMachine to the centre of square
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -