Control Paradigms

Control Paradigms
Intro to Robots
How We Control the Robot
robot
computer
myro
library
program
IDLE
program
Intro to Robots
Programming Paradigm up to now:
• The paradigm until now has been a continuous loop
containing sense, reason, act.
def main():
# do forever or for some time
# or until a certain condition is satisfied
# sense and transform sensor values
# reason or decide what to do next
# do it
direct control
paradigm
• Problem is that the reasoning part can become very
complicated and the program becomes hard to
understand (for the user) and potentially faulty.
• Corral exiting program has complex strategy
• What happens, for example, if multiple sensor events
require multiple, simultaneous responses?
Intro to Robots
A New Design Paradigm
• Direct Control Programs are sensor-driven: react to a
sensor event.
• Alternatively you can control a program by focusing on
goals chosen for the robot. This is called behaviourdriven control.
• Corral Example: Three behaviours:
– Cruise
– Avoid obstacles
– Seek light
Intro to Robots
Behaviour-Driven Control
T- translate component
R- rotate component
input
output
3 behaviour
decision making
modules
Boolean variables indicating
whether the behaviour
module has a recommendation
Intro to Robots
actual behaviour
Explanation:
• Example: Suppose there is no visible light. Then the
SeekLight Module will have no recommendation.
• The Arbiter Module needs to accept recommendations
from the three Behavioural Modules and then decide on
a behaviour for the robot.
• Various strategies for the Arbiter Module are possible.
• Our Strategy:
Weight the behaviours (call it prioritizing) and
implement the recommended translate and rotate
parameters from the most heavily weighted behaviour.
• We say this strategy has a subsumption architecture.
Exercise: Explain the meaning of the term
‘subsumption architecture in this context.
Intro to Robots
Three Program Behaviours:
• Cruise Behaviour:
cruiseSpeed = 0.8
turnSpeed = 0.8
lightThresh = 80
returning a list
def cruise():
return [True,cruiseSpeed,0]
always
True
don’t
turn
• This is the default behaviour.
• It always has a recommendation (True) so the robot will
never stop.
Intro to Robots
Three Program Behaviours:
• Avoidance Behaviour:
def avoid():
# see if there are any obstacles
L, R = getIR()
L=1-L
R=1-R
if L:
return [True, 0, -turnSpeed]
elif R:
return [True, 0, turnSpeed]
else:
return [False, 0, 0]
• This behaviour will override the default behaviour.
• It sometimes recommends True and other times False.
Intro to Robots
Three Program Behaviours:
• LightSeeking Behaviour:
def seekLight():
L, C, R = getLight()
if L < lightThresh:
return True, cruiseSpeed/2.0, turnSpeed
elif R < lightThresh:
return True, cruiseSpeed/2.0, -turnSpeed
else:
return [False, 0, 0]
• This behaviour preempts the avoidance behaviour.
• It sometimes recommends True and other times False.
Intro to Robots
Issues with seekLight():
• seekLight() has no behavioural response if light is
detected straight ahead. Why?
• seekLight() gives preference to light detected on its left
hand side. This works ok if there is only one source of
light and it is not focused (like a flashlight).
• A bright light, like a flashlight, shining on the center might
make both the left and right light sensors register below
the threshold.
• An alternative might be to check both left and right
sensors simultaneously for being below the threshold
and choosing the one that is lower in that case.
Intro to Robots
Alternative seekLight():
• In this version, an intense light on the right that spills
over and pulls the left light sensor below the threshold
will be brighter on the right and so the behaviour will be
to turn to the right.
Exercise: What should we
do if L == R < lightThresh?
How would this change the
code?
def seekLight2():
L, C, R = getLight()
if L < lightThresh and R < lightThresh:
if L < R:
# force R over the threshold
R = lightThresh + 1
else:
# force L over the threshold
L = lightThresh + 1
if L < lightThresh:
return True, cruiseSpeed/2.0, turnSpeed
elif R < lightThresh:
return True, cruiseSpeed/2.0, -turnSpeed
else:
return [False, 0, 0]
Intro to Robots
The main() algorithm:
• Like many programs that are based on an abstract
model, the main loop is trivial.
def main():
while True:
T, R = arbitrate()
move(T, R)
loop forever
the arbitrate() function decides
what behaviour to actually
exhibit and move() exhibits
the behaviour
main()
• Somehow the program should stop once the robot is out
of the corral. How would it know this?
Intro to Robots
The arbitrate() function:
• arbitrate() has to give highest priority to light seeking
behaviour, followed by obstacle avoidance behaviour
and as a default it suggests cruise behaviour.
behaviors = [seekLight, avoid, cruise]
variable value
# Decide which behavior, in order of priority
is function name
# has a recommendation for the robot
def arbitrate():
for behavior in behaviors:
output, T, R = behavior()
if output: # return on the first True recommendation
return [T, R]
• This function shows one of the most powerful Python
features – the fact that a variable value can be seen to
be a function name.
Intro to Robots
Python Math Library:
• Sometimes we will manage Scribbler using statistical
and probabilistic functions. We build such functions
from things found in the math library.
exp(x)
ceil(x)
floor(x)
log(x)
log10(x)
pow(x,y)
sqrt(x)
Intro to Robots
Example:
• A Python program that will tell you what your monthly
payment will be on a new car given:
– Contract price of car
– Sales tax rate
– Down payment
– Yearly interest rate Contract Price * (1 + Sales Tax Rate/100.0) –
Down Payment
– Loan duration (in months)
Yearly Interest Rate / 12.0
Loan Duration (in months)
Intro to Robots
Program Design:
def main():
# First, note the cost of the car (Cost),
# the amount of money you have saved (Cash),
# and the sales tax rate (TaxRate)
# Also, note the financials: the interest rate (APR),
# and the term of the loan (Term)
# The interest rate quoted is generally the annual
# percentage rate (APR)
# Convert it to monthly rate (by dividing it by 12) (MR)
# Next, compute the sales tax you will pay (SalesTax)
# Use the money left to make a down payment (DownPayment)
# Then determine the amount you will borrow (LoanAmount)
# Plug in all of the values in the formula and compute
# the monthly payment (MP)
# Also, compute the total cost of the car. (TotalCost)
# Output all the results
main()
Intro to Robots
Program Code:
def main():
loanAmount = 0
# First, note the cost of the car (Cost)
cost = input("Enter the cost of the car: $")
# the amount of money you have saved (Cash),
cash = input("Enter the amount of money you saved: $")
# and the sales tax rate (TaxRate) (6% e.g.)
salesTaxRate = input("Enter Sales Tax Rate: (0.06 == 6%): ")
salesTax = cost * salesTaxRate
if salesTax > cash:
print "Down payment does not cover sales tax. “
return
else:
loanAmount = cost + salesTax - cash
Intro to Robots
Program Code 2:
# Also, note the financials: the interest rate (APR),
yearlyInterestRate = input("Enter Interest Rate: (0.06 == 6%): ")
monthlyInterestRate = yearlyInterestRate / 12.0
# and the term of the loan (Term)
loanTerm = input("Enter loan term (in months) ")
# Plug in all of the values in the formula and compute
# the monthly payment (MP)
monthlyPayment = (loanAmount * monthlyInterestRate)/
(1 - exp(-1*loanTerm*(1 + monthlyInterestRate)))
# Also, compute the total cost of the car. (TotalCost)
totalCost = cash + monthlyPayment * loanTerm
# Output all the results
print “Monthly payment “, monthlyPayment, “ Total Cost “, totalCost
main()
Intro to Robots
Programming Language Structure.
• Not like English or any natural
language.
• Three building blocks:
sequence, selection, iteration.
sequence
Do A
selection
T
Do A
C?
iteration
F
Do B
C?
Do B
F
Intro to Robots
T
Do B
Traditional Program Design in a Nutshell:
• Describe the problem in terms of sequence, selection
and iteration and you have a good start on a program
design.
• Previous page diagrams are called flow charts. They
show the “flow” of program logic.
• Boxes are usually filled with English or some other
natural language.
Intro to Robots
Example:
• Design and implement a program that plays the game
called “Rock, Scissors, Paper”.
• We need to think in terms of sequence, selection and
iteration.
• This game is played in “rounds”. It consists of several
rounds, one after another. This is an iteration.
Still
want
to play
T
Play
one
round
F
Intro to Robots
Example (cont):
• The process of “playing
one round” consists of a
number of events in
sequence:
Print out
“You Win”
T
Print out
“It’s a Tie”
You
win?
F
F
Computer
Wins?
Computer
makes a
choice
(R, S, P)
You make
a choice
(R, S, P)
Intro to Robots
Print out
“Computer
Wins”
T
Put It All Together:
• Observe that this program
consists of the structures
sequence, selection and
iteration.
• There are details to be
filled out:
– How do you indicate
you still want to play?
– How does the computer
pick its play (without
looking at yours)?
Print out
“You Win”
T
You
win?
F
Computer
Wins?
You make
a choice
(R, S, P)
F
T
Print out
“Computer
Wins”
F
Computer
makes a
choice
(R, S, P)
Still
want
to play
Intro to Robots
Print out
“It’s a Tie”
T
The Program:
from random import *
def main():
while (wantToPlay()):
myMove = youPlay()
if myMove == 'No Play':
print '\n'
continue
computerMove = computerPlay()
winner = whoWins(myMove,computerMove)
print "You played: ", myMove
print "Computer played: ", computerMove
if winner == 'C':
print "Computer Wins\n"
elif winner == 'Y':
print "You Win\n"
else:
print "It's a Tie!\n"
main()
Intro to Robots
The Program:
def wantToPlay():
answer = raw_input("Do you want to play? ")
print '\n'
return answer in ['y', 'Y', 'yes', 'YES', 'Yes' ]
def youPlay():
answer = raw_input("What is your move? (R,S,P) ")
if answer in ['R','r']:
return 'Rock'
elif answer in ['S','s']:
return 'Scissors'
elif answer in ['P','p']:
return 'Paper'
else:
return 'No Play'
dict = {}
dict['RockScissors'] = 'Y'
dict['RockPaper'] = 'C'
dict['RockRock'] = 'T'
dict['ScissorsScissors'] = 'T'
dict['ScissorsRock'] = 'C'
dict['ScissorsPaper'] = 'Y'
dict['PaperScissors'] = 'C'
dict['PaperRock'] = 'Y'
dict['PaperPaper'] = 'T‘
def whoWins(y,c):
play = y + c
return dict[play]
options = ['Rock', 'Scissors', 'Paper']
def computerPlay():
return options[choice([0,1,2])]
choice(<set>) picks one from the set
Intro to Robots
Exercise:
• Run the RockScissorsPaper program and implement a
scoring system.
Intro to Robots