Intro to Robots Robots are Everywhere. Intro to Robots Various robots in use today lawnmower pet...
-
date post
20-Dec-2015 -
Category
Documents
-
view
225 -
download
1
Transcript of Intro to Robots Robots are Everywhere. Intro to Robots Various robots in use today lawnmower pet...
Intro to Robots
Robots are Everywhere
Intro to Robots
Various robots in use today
lawnmower
pet baby seal for convalescents
gutter cleaner
home security botcalled WowWee
See me
Intro to Robots
Robots of interest to us move around (rovers):
• Scribbler is such a robot– Two independently powered wheels, one not powered
(for balance)– Function that controls the motors:
motors(LEFT,RIGHT) -1 ≤ LEFT,RIGHT ≤ 1
motors(1.0,1.0) -- full speed aheadmotors(-1.0,-1.0) -- full speed in reversemotors(0,x) -- turn left if x > 0; back left if x < 0
forward(SPEED), 0 ≤ SPEED ≤ 1
backward(SPEED), 0 ≤ SPEED ≤ 1
def forward (SPEED): motors(SPEED, SPEED)
def backward (SPEED): L = R = motors(L, R)
-1.0*SPEED-1.0*SPEED??
Intro to Robots
Exercise:
• What do you think that the following command will do?
• How does this compare to
motors(-1,1)
motors(0,1)
Intro to Robots
Draw a box
• Put a magic marker in the center hole of Scribbler• Place Scribbler on a writeable surface.• forward(), turnLeft() and turnRight() all have a second
version that includes a time parameter
def box(): forward(0.75, 1) turnLeft(0.5, 1.3) forward(0.75, 1) turnLeft(0.5, 1.3) forward(0.75, 1) turnLeft(0.5, 1.3) forward(0.75, 1) turnLeft(0.5, 1.3)
Remember this is a time parameter so we need to experiment to see what “turning left” for a full second really does to figure out what time is needed for a ¼ turn.
forward(SPEED,TIME)turnLeft(SPEED,TIME)turnRight(SPEED,TIME) # TIME given in seconds
Intro to Robots
Exercise:
• How could we modify the box() function so that it would draw boxes of different sizes?
def box(SIZE): forward(0.75, SIZE) turnLeft(0.5, 1.3) forward(0.75, SIZE) turnLeft(0.5, 1.3) forward(0.75, SIZE) turnLeft(0.5, 1.3) forward(0.75, SIZE) turnLeft(0.5, 1.3)
Intro to Robots
Two Versions of box():
• What makes one of these more useful than the other?
def box(): forward(0.75, 1) turnLeft(0.5, 1.3) forward(0.75, 1) turnLeft(0.5, 1.3) forward(0.75, 1) turnLeft(0.5, 1.3) forward(0.75, 1) turnLeft(0.5, 1.3)
def box(SIZE): forward(0.75, SIZE) turnLeft(0.5, 1.3) forward(0.75, SIZE) turnLeft(0.5, 1.3) forward(0.75, SIZE) turnLeft(0.5, 1.3) forward(0.75, SIZE) turnLeft(0.5, 1.3)
Intro to Robots
Exercise:
• If I ran the program:
• What would the picture look like?
box(1)box(2)box(3)box(4)
Scribbler starts here
Intro to Robots
More Python (Variables, Expressions and Statements):
• Variable: A variable is a name for a value. The value also has a type.
• Above we have a variable– its name is x,– its value is 2 and – its type is integer
• In programming languages x = 2 doesn’t ask a question, it makes a statement.
• It’s not, “Is x equal to 2?” but rather “the value of x is 2”.
x = 2
Intro to Robots
Values and Types:
Python has a function called type()that will tell you the data type of a value or a variable
Intro to Robots
Variables:
• Variables are fundamental to the expressiveness of a programming language.
• Just like y = f(x) describes an entire graph while y = f(2) only describes the point (2,f(2)), a program written using variables can potentially solve many problems; one for each different value assigned to the variable.
• The above is called an assignment statement. Until the variable, x, has its value reassigned, its value remains as 2.
x = 2
Intro to Robots
Variables(2):
• Depending on the value assigned to a variable, it takes on a different type.
First a is an integer but whenwe assign it a string value itbecomes a string
Warning: Most programming languages won’t let you do this; once an integer, alwaysan integer, for example.
Intro to Robots
Legal Variable Names:
• X123• my_variable• __AStrangeVariable__• a2Wx34
• 123x• $S• My_#
•Variable names can contain letters, digits and underscores.•Variable names can’t begin with a digit.
Legal Names Illegal Names
• Variable names are case sensitive: MyVar and myVar are different variables with different names.
Intro to Robots
Legal Variable Names(2):
• Python has 28 key (reserved) words that are part of the language. These can’t be used as variable names.
• However, variable names like
are legal, if inadvisable.
and continue else for import not raiseassert def except from in or returnbreak del exec global is pass tryclass elif finally if lambda print while
AndWhileELSE
Intro to Robots
Program Statements:
• A statement is an instruction that Python can interpret and act upon
Some statements have “output” - a displayed result.
Assignment statements have nosuch output although they still “do”something
Intro to Robots
Expressions:
• An expression is something like
• If you type in an expression, python will evaluate it and print out its value.
• Variable names are also expressions
2 + ( 3 - 4 ) * 5
Intro to Robots
Expressions (2):
• However, expressions found in script files are evaluated but result in no output.
• The above file can be loaded and executed but will produce no output whatsoever.
# exp_only.py
1+ 2“Hello, world!”X = 3Y = 4X + Y
Intro to Robots
Operators:
• Python uses the following keystroke characters for arithmetic operations:
• Examples:
+ addition- subtraction* multiplication/ division** power
2 + 3 = 54 – 8 = -45 * 6 = 304/3 = 1 # integer division4.0/3 = 1.333332**3 = 8
Intro to Robots
Exercises:
• Evaluate:
3 * ( 4 – 2**2) + 7 / 2 = ?
Intro to Robots
Operands:
• The expressions that operators act upon are called operands.
• Operands can be expressions using literals like 4 and 5.2 or variable names. However, you can’t use a variable name unless you’ve already assigned it a value.
Using the variablex is ok but using y is not because y has not been initialized as x has
Intro to Robots
Operator Precedence:
• Rules of Precedence:– Parentheses are evaluated first
– Exponents come after parentheses and before anything else.
3 * ( 2 + 4 * 6) - 2
3 * ( 2 + 24) - 2
3 * ( 26) - 2
78 - 2
76
2 * 3**22 * 918
Intro to Robots
Operator Precedence:
• Rules of Precedence (cont):– Unary operators like negation are
lower than exponentiation and higher than multiplication
– Multiplication and division come next and have the same precedence and are evaluated left-to-right
– Addition and subtraction come next and have the same precedence and are evaluated left-to-right
( )
**
-()
*/
+-
Intro to Robots
Operator Precedence:
• Rules of Precedence:– Unary operators like negation are lower than
exponentiation and higher than multiplication
– Multiplication and division come next and have the same precedence and are evaluated left-to-right
-1 ** 2-1
2 * -1 + 3-2 + 31
3 – 2 * 4 + 1 * 63 – 8 + 1 * 63 – 8 + 6-5 + 61
Intro to Robots
Operator Precedence:
• Rules of Precedence:– Addition and subtraction come next and have the
same precedence and are evaluated left-to-right
3 – 2 + 4 + 8 – 1 + 41 + 4 + 8 – 1 + 45 + 8 – 1 + 413 – 1 + 412 + 416
Intro to Robots
String Operations
• Although you can’t do arithmetic on strings you can use + and *.
• * means “repeat” so ‘No ‘+3 is illegal but ‘No ‘*3 means repeat the string three times.
msg1 = ‘The time has come’msg2 = ‘ the walrus said.’print msg1 + msg2The time has come the walrus said.
+ means “concatenate” two strings into one. Both argumentsmust be strings
When you use + you needto put the space in the string itself.
Print ‘Fun ‘*3 + ‘in the warm California sun.’
Fun Fun Fun in the warm California sun.
Intro to Robots
The special power of ,
• As I said, you can’t express ‘No ‘+3 but you are allowed to write ‘No ‘,3?
• Try printing out ‘No ‘,3.
• It looks like , acts like + but not really. To really see the true meaning of the comma operator try combining two strings using , and assigning their value to a third variable.
Print ‘No ‘,3No 3
msg1 = “Good-bye, “msg2 = “cruel world!”msg3 = msg1,msg2msg3
(‘Good-bye,’,’cruel world!’)
Intro to Robots
The special power of , continued:
• So , is an operator that makes a list.• And when you print out a list the print function prints all
the elements of the list separated by a space.• We’ll have lots more on lists later in the course.
Intro to Robots
Comments:
• The single-line comment symbol in Python is #.• Everything on a line following # is a comment and not to
be executed or interpreted as Python.• The above is true unless the # is inside a string.