Presentation is loading. Please wait.

Presentation is loading. Please wait.

Intro to Robots Robots are Everywhere. Intro to Robots Various robots in use today lawnmower pet baby seal for convalescents gutter cleaner home security.

Similar presentations


Presentation on theme: "Intro to Robots Robots are Everywhere. Intro to Robots Various robots in use today lawnmower pet baby seal for convalescents gutter cleaner home security."— Presentation transcript:

1 Intro to Robots Robots are Everywhere

2 Intro to Robots Various robots in use today lawnmower pet baby seal for convalescents gutter cleaner home security bot called WowWee See me

3 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 ahead motors(-1.0,-1.0) -- full speed in reverse motors(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 ? ?

4 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)

5 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

6 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)

7 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)

8 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

9 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

10 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

11 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

12 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.

13 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 NamesIllegal Names Variable names are case sensitive: MyVar and myVar are different variables with different names.

14 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 raise assert def except from in or return break del exec global is pass try class elif finally if lambda print while And While ELSE

15 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 no such output although they still “do” something

16 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

17 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 = 3 Y = 4 X + Y

18 Intro to Robots Operators: Python uses the following keystroke characters for arithmetic operations: Examples: + addition - subtraction * multiplication / division ** power 2 + 3 = 5 4 – 8 = -4 5 * 6 = 30 4/3 = 1 # integer division 4.0/3 = 1.33333 2**3 = 8

19 Intro to Robots Exercises: Evaluate: 3 * ( 4 – 2**2) + 7 / 2 = ?

20 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

21 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**2 2 * 9 18

22 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 ( ) ** -() */ +-

23 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 2 * -1 + 3 -2 + 3 1 3 – 2 * 4 + 1 * 6 3 – 8 + 1 * 6 3 – 8 + 6 -5 + 6 1

24 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

25 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 + 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. Print ‘Fun ‘*3 + ‘in the warm California sun.’ Fun Fun Fun in the warm California sun.

26 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 ‘,3 No 3 msg1 = “Good-bye, “ msg2 = “cruel world!” msg3 = msg1,msg2 msg3 (‘Good-bye,’,’cruel world!’)

27 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.

28 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.


Download ppt "Intro to Robots Robots are Everywhere. Intro to Robots Various robots in use today lawnmower pet baby seal for convalescents gutter cleaner home security."

Similar presentations


Ads by Google