Robots are Everywhere

Download Report

Transcript Robots are Everywhere

Robots are Everywhere
Intro to Robots
Various robots in use today
home security bot
called WowWee
lawnmower
gutter cleaner
See me
pet baby seal for
convalescents
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(1.0,1.0)
motors(-1.0,-1.0)
motors(0,x)
motors(LEFT,RIGHT)
-1 ≤ LEFT,RIGHT ≤ 1
-- full speed ahead
-- full speed in reverse
-- turn left if x > 0; back left if x < 0
forward(SPEED), 0 ≤ SPEED ≤ 1
def forward (SPEED):
motors(SPEED, SPEED)
backward(SPEED), 0 ≤ SPEED ≤ 1
def backward (SPEED):
?
L = -1.0*SPEED
R = -1.0*SPEED
?
motors(L, R)
Intro to Robots
Exercise:
• What do you think that the following command will do?
motors(-1,1)
• How does this compare to
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
forward(SPEED,TIME)
turnLeft(SPEED,TIME)
turnRight(SPEED,TIME)
# TIME given in seconds
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.
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:
box(1)
box(2)
box(3)
box(4)
• What would the picture look like?
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.
x=2
• 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”.
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.
x=2
• The above is called an assignment statement. Until the
variable, x, has its value reassigned, its value remains as
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 when
we assign it a string value it
becomes a string
Warning: Most programming languages
won’t let you do this; once an integer, always
an integer, for example.
Intro to Robots
Legal Variable Names:
•Variable names can contain letters, digits and underscores.
•Variable names can’t begin with a digit.
Legal Names
•
•
•
•
X123
my_variable
__AStrangeVariable__
a2Wx34
Illegal Names
• 123x
• $S
• My_#
• 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.
and
assert
break
class
continue
def
del
elif
else
except
exec
finally
for
from
global
if
import
in
is
lambda
• However, variable names like
And
While
ELSE
are legal, if inadvisable.
Intro to Robots
not
or
pass
print
raise
return
try
while
Program Statements:
• A statement is an instruction that Python can interpret
and act upon
Some statements have “output” a displayed result.
Assignment statements have no
such output although they still “do”
something
Intro to Robots
Expressions:
• An expression is something like
2+(3-4)*5
• If you type in an expression, python will evaluate it and
print out its value.
• Variable names are also expressions
Intro to Robots
Expressions (2):
• However, expressions found in script files are evaluated
but result in no output.
# exp_only.py
1+ 2
“Hello, world!”
X=3
Y=4
X+Y
• The above file can be loaded and executed but will
produce no output whatsoever.
Intro to Robots
Operators:
• Python uses the following keystroke characters for
arithmetic operations:
+
*
/
**
addition
subtraction
multiplication
division
power
• Examples:
2+3=5
4 – 8 = -4
5 * 6 = 30
4/3 = 1
4.0/3 = 1.33333
2**3 = 8
# integer division
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 variable
x 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
3 * ( 2 + 4 * 6) - 2
3 * ( 2 + 24) - 2
3 * ( 26) - 2
78 - 2
76
– Exponents come after parentheses and before
anything else.
2 * 3**2
2*9
18
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
-1 ** 2
-1
2 * -1 + 3
-2 + 3
1
– Multiplication and division come next and have the
same precedence and are evaluated left-to-right
3–2*4+1*6
3–8+1*6
3–8+6
-5 + 6
1
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+4
1+4+8–1+4
5+8–1+4
13 – 1 + 4
12 + 4
16
Intro to Robots
String Operations
• Although you can’t do arithmetic on strings you can use
+ and *.
msg1 = ‘The time has come’
msg2 = ‘ the walrus said.’
print msg1 + msg2
The time has come the walrus said.
+ means “concatenate” two
strings into one. Both arguments
must be strings
When you use + you need
to put the space in the
string itself.
• * means “repeat” so ‘No ‘+3 is illegal but ‘No ‘*3 means
repeat the string three times.
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.
Print ‘No ‘,3
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.
msg1 = “Good-bye, “
msg2 = “cruel world!”
msg3 = msg1,msg2
msg3
(‘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.
Intro to Robots