Intro to Robots Robots: Chapter 1. Intro to Robots Imagination is more important than knowledge...

35
Intro to Robots Robots: Chapter 1

Transcript of Intro to Robots Robots: Chapter 1. Intro to Robots Imagination is more important than knowledge...

Page 1: Intro to Robots Robots: Chapter 1. Intro to Robots Imagination is more important than knowledge Einstein Exercise: Compare goals of an imagination-based.

Intro to Robots

Robots: Chapter 1

Page 2: Intro to Robots Robots: Chapter 1. Intro to Robots Imagination is more important than knowledge Einstein Exercise: Compare goals of an imagination-based.

Intro to Robots

Imagination is more important than knowledge Einstein

Exercise: Compare goals of an imagination-based

versus knowledge-based course.

Page 3: Intro to Robots Robots: Chapter 1. Intro to Robots Imagination is more important than knowledge Einstein Exercise: Compare goals of an imagination-based.

Intro to Robots

About this Course

• I need your presence• I need you to be interested

me hopefully, not you

Page 4: Intro to Robots Robots: Chapter 1. Intro to Robots Imagination is more important than knowledge Einstein Exercise: Compare goals of an imagination-based.

Intro to Robots

Encouragement

Page 5: Intro to Robots Robots: Chapter 1. Intro to Robots Imagination is more important than knowledge Einstein Exercise: Compare goals of an imagination-based.

Intro to Robots

Motives?

• CS Department:– Attract new majors– Get non-majors to take more CS courses

• Students:– Interested in Computing but not sure.– Used to like stuff like this.– Job skills

Page 6: Intro to Robots Robots: Chapter 1. Intro to Robots Imagination is more important than knowledge Einstein Exercise: Compare goals of an imagination-based.

Intro to Robots

Robots as Surrogates

• We have many surrogates that represent us in our communications with devices; not all are robots:– Identifiers

• ATM card• Car key

– Avatars• DVD player• Computer program• Robot

Exercise: Find out the meaning of the word “avatar”.

Page 7: Intro to Robots Robots: Chapter 1. Intro to Robots Imagination is more important than knowledge Einstein Exercise: Compare goals of an imagination-based.

Intro to Robots

Robots and Computers

• Computers are “useless tools” but given a program to run they become a highly specialized and extremely efficient tool for a particular task.

• Robots share this property of computers and add features such as mobility, physicality and sensory perception.

like a robot withouta program to run

Page 8: Intro to Robots Robots: Chapter 1. Intro to Robots Imagination is more important than knowledge Einstein Exercise: Compare goals of an imagination-based.

Intro to Robots

What is a Robot?

• “a mechanism guided by automatic controls “ Merriam-Webster

• Our interest is in “guiding” a robot through programs we write. “Automatic controls” usually means there is a computer at work.

Page 9: Intro to Robots Robots: Chapter 1. Intro to Robots Imagination is more important than knowledge Einstein Exercise: Compare goals of an imagination-based.

Intro to Robots

What is not a robot?

• Why is a car NOT a robot?– mechanism? – guided? – automatic controls?

• Is a fighter jet a robot?– All switches activate computer programs that activate

devices. – Example: LiftWheelsSwitch– No, but it contains many smaller robots.

yes

yes

no

# $45 million mistake; the following was missingIf ( no weight on wheels) lift wheel carriage;

Page 10: Intro to Robots Robots: Chapter 1. Intro to Robots Imagination is more important than knowledge Einstein Exercise: Compare goals of an imagination-based.

Intro to Robots

History

• First robots were in manufacturing; fixed base, moveable arm.

• More recently, robots have become personal (toys, wheelchairs), educational (Scribbler) or microscopically small (nanobots).– Example: There exists a small sensor that can count

molecules in a chemical sample. It has a switch that is 1.5 nanometers across (1.5 x 10-9meters across)

• Similar trajectory for the history of computing

Page 11: Intro to Robots Robots: Chapter 1. Intro to Robots Imagination is more important than knowledge Einstein Exercise: Compare goals of an imagination-based.

Intro to Robots

Exercise:

• Go to the web and find examples of robots in the following areas:– Baking– Tourism– Mining– Transportation– Sports

Page 12: Intro to Robots Robots: Chapter 1. Intro to Robots Imagination is more important than knowledge Einstein Exercise: Compare goals of an imagination-based.

Intro to Robots

Robots vs Computers

• Robots implement “automatic controls” by having an embedded computer running a program that gives the robot explicit instructions about what to do next.

• The robot used in this course has an on-board computer that we can download a program to and have it run by the robot’s computer.

• This robot’s computer works in tandem with your desktop computer that provides a user interface.

Two computers: one forwriting code and the other for executing it.

Page 13: Intro to Robots Robots: Chapter 1. Intro to Robots Imagination is more important than knowledge Einstein Exercise: Compare goals of an imagination-based.

Intro to Robots

Computer Robot

Program containing robotinstructions, which are transmittedby the program, via Bluetoothto the robot for execution

Program that receives robotinstructions from Bluetooth,executes them and returnsto the computer any desired output

return value

function call

Page 14: Intro to Robots Robots: Chapter 1. Intro to Robots Imagination is more important than knowledge Einstein Exercise: Compare goals of an imagination-based.

Intro to Robots

Robots vs Computers

• We write the program on a desktop computer but it will run on the robot’s computer.

• Programming is specifying in advance a sequence of things a program (and hence our robot) should do when the program is run (automatic control).

• Programs are written in a programming language. The language we will use is called python.

• Myro is a software system that includes and extends the python programming language to specifically work with the Scribbler robot

Page 15: Intro to Robots Robots: Chapter 1. Intro to Robots Imagination is more important than knowledge Einstein Exercise: Compare goals of an imagination-based.

Intro to Robots

Python Sample Code:

# program running on the computerfrom myro import * # import the myro library

def main(): # define the main() function initialize('com4') # connect to the robot while(True): forward(0.5) # command sent to robot – # move forward at half speed until further notice wait(1) # command processed on computer only - # sleep for 1 second turnRight(0.5,1) # command sent to robot - # turn right at half speed for 1 second

main() # function call to main(); executes main()

Page 16: Intro to Robots Robots: Chapter 1. Intro to Robots Imagination is more important than knowledge Einstein Exercise: Compare goals of an imagination-based.

Intro to Robots

Homework (due next class :

• Read Chapter 1 of our text.• Answer one of the first 6 questions at the end of the

chapter. Write a one-paragraph answer.

Page 17: Intro to Robots Robots: Chapter 1. Intro to Robots Imagination is more important than knowledge Einstein Exercise: Compare goals of an imagination-based.

Intro to Robots

Programming Languages:

• Some times we are asked to solve a completely new kind of problem with a computer. The first thing we must do is decide what programming language we will use to solve the problem.

• Sometimes, the best thing to do is to create a new language altogether.

• Lesson to learn: Some times programming problems are “hard” because we don’t have the right language in which to express a solution.

Exercise: Examples of languages used in specialized situations.

Page 18: Intro to Robots Robots: Chapter 1. Intro to Robots Imagination is more important than knowledge Einstein Exercise: Compare goals of an imagination-based.

Intro to Robots

Exercises:

• List some programming languages you have heard about. • What makes a programming language “interactive”?• Give an example of an interpreted language; an example

of a compiled language.• Is your browser a program? How does it work? (google)• What is a GUI? Give examples.• What is a CLI (Command Line Interface)?• What is IDLE (Integrated DeveLopment Environment for

Python)?

Page 19: Intro to Robots Robots: Chapter 1. Intro to Robots Imagination is more important than knowledge Einstein Exercise: Compare goals of an imagination-based.

Intro to Robots

Interpreted vs Compiled

python

Java, C, C++

Page 20: Intro to Robots Robots: Chapter 1. Intro to Robots Imagination is more important than knowledge Einstein Exercise: Compare goals of an imagination-based.

Intro to Robots

Exercises:

• What does a PC do most of the time?

• What does the following mean?I hate computers, they never do what I want them to do;only what I tell them to do.

99% of the time it isdoing nothing

Page 21: Intro to Robots Robots: Chapter 1. Intro to Robots Imagination is more important than knowledge Einstein Exercise: Compare goals of an imagination-based.

Intro to Robots

So why Python?

• Java • Python

# SampleClass.javaimport java.lang.*;

Class SampleClass { public static void main (String [] args) { System.out.println(2+4); }}

> javac SampleClass.java

> java SampleClass 6

compiler step

execution step

interpreter step

coding step

Page 22: Intro to Robots Robots: Chapter 1. Intro to Robots Imagination is more important than knowledge Einstein Exercise: Compare goals of an imagination-based.

Intro to Robots

Info about IDLE:

http://www.python.org/idle/doc/idlemain.html

Page 23: Intro to Robots Robots: Chapter 1. Intro to Robots Imagination is more important than knowledge Einstein Exercise: Compare goals of an imagination-based.

Intro to Robots

More Info about IDLE:

Console OutputBrown

StdErr OutputRed

Page 24: Intro to Robots Robots: Chapter 1. Intro to Robots Imagination is more important than knowledge Einstein Exercise: Compare goals of an imagination-based.

Intro to Robots

Python/IDLE IndentationEntering a keyword like def or while automatically causes thefollow-on lines to be indented.

def is a keyword that announces the definition of a function

while is a key word that announcesa block of code that is executed aslong as the condition betweenwhile and : is true.

It is up to the programmer to endthe indentation. This automaticallyends the corresponding program block.

This function, in a while-loop, multiplies

((((…(1 * num) * (num-1)) * (num-2)) * (num-3)) * … * 2) * 1

each parenthesis being assigned to result until result holdsthe value of the final multiplication. The function then returnsas its value, the value of result.

parameter

function value

Page 25: Intro to Robots Robots: Chapter 1. Intro to Robots Imagination is more important than knowledge Einstein Exercise: Compare goals of an imagination-based.

Intro to Robots

Recursive Function Example

An alternative definitionof factorial(). In this definition, called a recursivedefinition, the value is definedas

factorial(n) = n * factorial(n-1)

or written mathematically as

n! = n * (n-1)!

With the complete multiplicationpostponed until the functioncalls factorial(1) who return value is 1.

Page 26: Intro to Robots Robots: Chapter 1. Intro to Robots Imagination is more important than knowledge Einstein Exercise: Compare goals of an imagination-based.

Intro to Robots

Another Recursive Function Example

Output in a python shell

comment

function definition

The function hanoi()has four arguments n – number of disks From – initial peg name To – final peg name Using – third peg name

If number of disks iszero then do nothing

Otherwise:move n-1 disks from From to Using move 1 disk from From to To and finallymove n-1 disks from Using to To.

Page 27: Intro to Robots Robots: Chapter 1. Intro to Robots Imagination is more important than knowledge Einstein Exercise: Compare goals of an imagination-based.

Intro to Robots

More on python programming

• Files can contain programs. Python files have the file extension *.py

• What makes a program?– Input: Usually from keyboard, file, mouse– Output: Usually to monitor, file, printer– Calculations: math, algorithms– Conditional execution: if (condition): Do something– Repetition: while (condition): Do something

> python myfile.py

Page 28: Intro to Robots Robots: Chapter 1. Intro to Robots Imagination is more important than knowledge Einstein Exercise: Compare goals of an imagination-based.

Intro to Robots

What is Debugging?

• Program errors are called bugs.• Getting rid of “logical bugs” is called debugging.• Remember:

• A “logical bug” occurs when you think you are telling the program to one thing but you actually have told it to something completely different.

I hate computers, they never do what I want them to do;only what I tell them to do.

Exercise: Search the web for the first computer bug

Page 29: Intro to Robots Robots: Chapter 1. Intro to Robots Imagination is more important than knowledge Einstein Exercise: Compare goals of an imagination-based.

Intro to Robots

Different kinds of errors:• Logical errors: These errors can be found by trying to

work back from the output to see why it is as it is. You spend your time being a detective.

• Syntax errors: Programs must be letter perfect, any missing punctuation or error in sentence structure will be caught by the interpreter and spit out at you.

• Runtime errors: Some errors are neither syntax nor logical. For example, you ask correctly to read a file that doesn’t exist. We call these errors exceptions. Hard to avoid or prevent; just “handle” them after they happen.

• Stages of becoming a good programmer1. No more syntax errors2. No more logical errors3. Handle exceptions (runtime errors are hard to avoid)

Page 30: Intro to Robots Robots: Chapter 1. Intro to Robots Imagination is more important than knowledge Einstein Exercise: Compare goals of an imagination-based.

Intro to Robots

Natural vs Formal Languages

• Natural: ambiguous, redundant, non-literal• Formal: non-ambiguous, concise, literal.

• Poetry vs Prose

Page 31: Intro to Robots Robots: Chapter 1. Intro to Robots Imagination is more important than knowledge Einstein Exercise: Compare goals of an imagination-based.

Intro to Robots

Exercise:

• Type 1: Write a sentence in English that is well-structured but with unrecognizable tokens (words)

• Type 2: Write a sentence in English that has recognizable tokens but is not well-structured.

• How does the reader determine that the sentence is either of type 1 or type 2?

• This is called parsing. • The first step is having the computer execute your

program is to have it parsed. Errors found when parsing are syntax errors.

Jes kkrz weqayi mmeeswq.

Easily night the category hidden.

Page 32: Intro to Robots Robots: Chapter 1. Intro to Robots Imagination is more important than knowledge Einstein Exercise: Compare goals of an imagination-based.

Intro to Robots

Reading Programs:

• Don’t try to do it like reading text – top-to-bottom, left-to-right.

• Look for structure:– Sequences– Conditionals– Repetitions– Function calls with mnemonic names

• Then pay attention to the details• Imagine a book that has a one line summary for each

page:– Read entire book at one sitting (summary lines)– Go back and read the entire book.

Page 33: Intro to Robots Robots: Chapter 1. Intro to Robots Imagination is more important than knowledge Einstein Exercise: Compare goals of an imagination-based.

Intro to Robots

from myro import *

englishToSpanish = {}englishToSpanish['cat'] = 'gato'englishToSpanish['dog'] = 'perro'englishToSpanish['cow'] = 'vaca'englishToSpanish['horse'] = 'caballo'englishToSpanish['chicken'] = 'pollo'

eng = ask('English word')

while eng != 'done': print "English: ", eng, " Spanish: ", englishToSpanish[eng] eng = ask('English word')

print "All done!"

load myro

create and loadour dictionary

prompt for a word

If not doneprint out translation

ask for another word

key value

Run Me

Page 34: Intro to Robots Robots: Chapter 1. Intro to Robots Imagination is more important than knowledge Einstein Exercise: Compare goals of an imagination-based.

Intro to Robots

Program Analysis:

• First, creates and loads a dictionary• Next, repeatedly asks for word to translate until done.• Finally, when done it says so

• Program structure: a sequence of things, one of them a repetition.

• Errors: No syntax or logical errors but can cause a runtime error. Can you find it?

Page 35: Intro to Robots Robots: Chapter 1. Intro to Robots Imagination is more important than knowledge Einstein Exercise: Compare goals of an imagination-based.

Intro to Robots

To avoid runtimeerror we only print out what is in the dictionary

from myro import *

# initialize dictionaryenglishToSpanish = {}englishToSpanish['cat'] = 'gato'englishToSpanish['dog'] = 'perro'englishToSpanish['cow'] = 'vaca'englishToSpanish['horse'] = 'caballo'englishToSpanish['chicken'] = 'pollo'

# ask for a word in Englisheng = ask('English word')

while eng != 'done': # if word is in dictionary print out translation if englishToSpanish.has_key(eng): print "English: ", eng, " Spanish: ", englishToSpanish[eng] else: print eng, " not in dictionary" # get another word eng = ask('English word')

# program all done, say so.print "All done!"

Run Me