Post on 21-Dec-2015
transcript
Intro to Robots
Control Paradigms
Intro to Robots
How We Control the Robot
computer
IDLE
program
robot
programmyrolibrary
Intro to Robots
Programming Paradigm up to now:
• The paradigm until now has been a continuous loop containing sense, reason, act.
• 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?
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 controlparadigm
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 behaviour-driven control.
• Corral Example: Three behaviours:– Cruise– Avoid obstacles– Seek light
Intro to Robots
Behaviour-Driven Control
input output
T- translate componentR- rotate component
Boolean variables indicatingwhether the behaviour module has a recommendation
3 behaviourdecision makingmodules
actual behaviour
Intro to Robots
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:
• We say this strategy has a subsumption architecture.
Weight the behaviours (call it prioritizing) and implement the recommended translate and rotate parameters from the most heavily weighted behaviour.
Exercise: Explain the meaning of the term ‘subsumption architecture in this context.
Intro to Robots
Three Program Behaviours:
• Cruise Behaviour:
• This is the default behaviour. • It always has a recommendation (True) so the robot will
never stop.
cruiseSpeed = 0.8turnSpeed = 0.8lightThresh = 80
def cruise(): return [True,cruiseSpeed,0]
alwaysTrue
don’tturn
returning a list
Intro to Robots
Three Program Behaviours:
• Avoidance Behaviour:
• This behaviour will override the default behaviour. • It sometimes recommends True and other times False.
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]
Intro to Robots
Three Program Behaviours:
• LightSeeking Behaviour:
• This behaviour preempts the avoidance behaviour. • It sometimes recommends True and other times False.
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]
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.
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]
Exercise: What should wedo if L == R < lightThresh?How would this change the code?
Intro to Robots
The main() algorithm:
• Like many programs that are based on an abstract model, the main loop is trivial.
• Somehow the program should stop once the robot is out of the corral. How would it know this?
def main(): while True: T, R = arbitrate() move(T, R)
main()
the arbitrate() function decideswhat behaviour to actuallyexhibit and move() exhibitsthe behaviour
loop forever
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.
• This function shows one of the most powerful Python features – the fact that a variable value can be seen to be a function name.
behaviors = [seekLight, avoid, cruise]
# Decide which behavior, in order of priority# has a recommendation for the robotdef arbitrate(): for behavior in behaviors: output, T, R = behavior() if output: # return on the first True recommendation return [T, R]
variable valueis 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– Loan duration (in months)
Contract Price * (1 + Sales Tax Rate/100.0) – Down Payment
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 “, totalCostmain()
Intro to Robots
Programming Language Structure.
• Not like English or any natural language.
• Three building blocks: sequence, selection, iteration.
Do A
Do B
sequence selection iteration
Do A Do B
T FC?
C? Do BT
F
Intro to Robots
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.
Stillwant
to play
Playone
round
T
F
Intro to Robots
Example (cont):
• The process of “playing one round” consists of a number of events in sequence:
TF
Print out“Computer
Wins”
Print out“You Win”
Computermakes achoice
(R, S, P)
You make a choice(R, S, P)
ComputerWins?
Print out“It’s a Tie”
Youwin?T F
Intro to Robots
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)?
Stillwant
to play
T
F
TF
Print out“Computer
Wins”
Print out“You Win”
Computermakes achoice
(R, S, P)
You make a choice(R, S, P)
ComputerWins?
Print out“It’s a Tie”
Youwin?T F
Intro to Robots
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'
options = ['Rock', 'Scissors', 'Paper']
def computerPlay(): return options[choice([0,1,2])]
choice(<set>) picks one from the set
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]
Intro to Robots
Exercise:
• Run the RockScissorsPaper program and implement a scoring system.