weston schreiber & joshua gabrielse robotics summer training programming #3: intro to...

57
Weston Schreiber & Joshua Gabrielse Robotics Summer Training Programming #3: Intro to Computational Thinking

Upload: cecily-holmes

Post on 21-Jan-2016

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Weston Schreiber & Joshua Gabrielse Robotics Summer Training Programming #3: Intro to Computational Thinking

Weston Schreiber & Joshua Gabrielse

Robotics Summer Training

Programming #3: Intro to Computational Thinking

Page 2: Weston Schreiber & Joshua Gabrielse Robotics Summer Training Programming #3: Intro to Computational Thinking

Weston Schreiber & Joshua Gabrielse

Objectives:• Understand the 3 stages to programming• Create an algorithm that accomplishes a

programming task• Understand the purpose of variables, if

statements, loops, and functions in programs

Page 3: Weston Schreiber & Joshua Gabrielse Robotics Summer Training Programming #3: Intro to Computational Thinking

Weston Schreiber & Joshua Gabrielse

1st Rule of Programming

Computers are stupid!!!!

They do EXACTLY what they are told,

nothing less, nothing more, and they never get the

idea.

Page 4: Weston Schreiber & Joshua Gabrielse Robotics Summer Training Programming #3: Intro to Computational Thinking

Weston Schreiber & Joshua Gabrielse

Stage 1: Planning(Algorithm, Flowchart, Pseudocode)

Stage 2: Coding(Variables, Functions, if statements, Loops)

Stage 3: Error Analysis(Syntax Errors, Logic Errors, Readdress Plan)

Stage 1: Planning(Algorithm, Flowchart, Pseudocode)

Page 5: Weston Schreiber & Joshua Gabrielse Robotics Summer Training Programming #3: Intro to Computational Thinking

Weston Schreiber & Joshua Gabrielse

Flowcharts & Algorithm Planning• Two Types of Parts to a Programming Flowchart:

Actions Decisions

“Yes”

“No”

Robotics Examples“Turn on wheel motors” “Is the front bumper

pushed?”

“Lift Gripper Arm” “Has the robot turned 3 times?”

“Stop all motors” “Has the encoder registered over 1000 counts?”

Page 6: Weston Schreiber & Joshua Gabrielse Robotics Summer Training Programming #3: Intro to Computational Thinking

Weston Schreiber & Joshua Gabrielse

Flowcharts Recap:

If the answer to Question 1 is “yes,” and the answer to Question 2 is “no,” what order do the steps happen?

Click the number corresponding to the correct answer on your clicker.

A) Step 1, Step 2

B) Step 2, Step 3B

C) Step 1, Step 2, Step 3A

D) Step 1, Step 2, Step 3B

Page 7: Weston Schreiber & Joshua Gabrielse Robotics Summer Training Programming #3: Intro to Computational Thinking

Weston Schreiber & Joshua Gabrielse

Stage 1: Planning(Algorithm, Flowchart, Pseudocode)

Stage 2: Coding(Variables, Functions, if statements, Loops)

Stage 3: Error Analysis(Syntax Errors, Logic Errors, Readdress Plan)

Page 8: Weston Schreiber & Joshua Gabrielse Robotics Summer Training Programming #3: Intro to Computational Thinking

Weston Schreiber & Joshua Gabrielse

Basics of Computer Programming

Variables- place to store changeable information

Functions- block of reusable code

Conditional ( if ) Statements - code that only runs if a condition is TRUE

Loops (while, for )- section of code that repeats

Variables- place to store changeable information

Page 9: Weston Schreiber & Joshua Gabrielse Robotics Summer Training Programming #3: Intro to Computational Thinking

Weston Schreiber & Joshua Gabrielse

Basics of Computer Programming

Variables- place to store changeable information

•What are some variables in your everyday life?

•What are some variables PowerPoint is storing to run this presentation?

Page 10: Weston Schreiber & Joshua Gabrielse Robotics Summer Training Programming #3: Intro to Computational Thinking

Weston Schreiber & Joshua Gabrielse

VariablesA place to store changeable information.

BEGIN

END

int x

Example:

Create a variable (a place to store integers) called x.

Assign x a value of 1.

Assign x the value of the expression 1 + 2.

Assign x equal to the previous value of x plus 1.

x = 1

x = 1 + 2

x = x + 1

Page 11: Weston Schreiber & Joshua Gabrielse Robotics Summer Training Programming #3: Intro to Computational Thinking

Weston Schreiber & Joshua Gabrielse

What is the value of x after line 1?

BEGIN

END

int x

Example:

Create a variable (a place to store integers) called x.

Assign x a value of 1.

Assign x the value of the expression 1 + 2.

Assign x equal to the previous value of x plus 1.

x = 1

x = 1 + 2

x = x + 1

line 0

line 1

line 2

line 3

Click the number corresponding to the correct answer on your clicker.

A) 1 B) 2 C) 3 D) 4 E) 5 F) 0 G) a random number

Page 12: Weston Schreiber & Joshua Gabrielse Robotics Summer Training Programming #3: Intro to Computational Thinking

Weston Schreiber & Joshua Gabrielse

What is the value of x after line 2?

BEGIN

END

int x

Example:

Create a variable (a place to store integers) called x.

Assign x a value of 1.

Assign x the value of the expression 1 + 2.

Assign x equal to the previous value of x plus 1.

x = 1

x = 1 + 2

x = x + 1

line 0

line 1

line 2

line 3

Click the number corresponding to the correct answer on your clicker.

A) 1 B) 2 C) 3 D) 4 E) 5 F) 0 G) a random number

Page 13: Weston Schreiber & Joshua Gabrielse Robotics Summer Training Programming #3: Intro to Computational Thinking

Weston Schreiber & Joshua Gabrielse

What is the value of x after line 3?

BEGIN

END

int x

Example:

Create a variable (a place to store integers) called x.

Assign x a value of 1.

Assign x the value of the expression 1 + 2.

Assign x equal to the previous value of x plus 1.

x = 1

x = 1 + 2

x = x + 1

line 0

line 1

line 2

line 3

Click the number corresponding to the correct answer on your clicker.

A) 1 B) 2 C) 3 D) 4 E) 5 F) 0 G) a random number

Page 14: Weston Schreiber & Joshua Gabrielse Robotics Summer Training Programming #3: Intro to Computational Thinking

Weston Schreiber & Joshua Gabrielse

What is the value of x after line 0?

BEGIN

END

int x

Example:

Create a variable (a place to store integers) called x.

Assign x a value of 1.

Assign x the value of the expression 1 + 2.

Assign x equal to the previous value of x plus 1.

x = 1

x = 1 + 2

x = x + 1

line 0

line 1

line 2

line 3

Click the number corresponding to the correct answer on your clicker.

A) 1 B) 2 C) 3 D) 4 E) 5 F) 0 G) a random number

Page 15: Weston Schreiber & Joshua Gabrielse Robotics Summer Training Programming #3: Intro to Computational Thinking

Weston Schreiber & Joshua Gabrielse

Declaring Variables

Computers & robots need to know three things in order to create variables:1.Type: how much memory to reserve, and how it is used

• Integers take less memory than floating point decimals.• Example:

• 1 (int) it takes less memory just to store the number• 1.0 (float) it takes extra memory to store the location of

the decimal point2.Name: how to refer to/locate the reserved memory

• Pick names that are logical and easy to remember3.Value: what to store in that memory

• If you don’t give an initial value, whatever random value is left in that memory location will remain there

Double click on the variables block in the flow chart of any function to declare (create) variables.

Page 16: Weston Schreiber & Joshua Gabrielse Robotics Summer Training Programming #3: Intro to Computational Thinking

Weston Schreiber & Joshua Gabrielse

Declaring Variables(simplified)

Pick:1. Type: what kind of information is being stored

2. Name: a descriptive identifier (you pick the name)

3. Value: the information initially stored in the variable

Double click on the variables block

Page 17: Weston Schreiber & Joshua Gabrielse Robotics Summer Training Programming #3: Intro to Computational Thinking

Weston Schreiber & Joshua Gabrielse

Variable Types

When in doubt, pick int!

Need to use decimals?

Use double!

Page 18: Weston Schreiber & Joshua Gabrielse Robotics Summer Training Programming #3: Intro to Computational Thinking

Weston Schreiber & Joshua Gabrielse

What are the key parts to a function?Click the number corresponding to the correct answer on your clicker.

A) Name, Type, Value

B) Name, Parameters, Description

C) Name, Parameters, Action

D) Name, Description, Value

Functions Recap:

Page 19: Weston Schreiber & Joshua Gabrielse Robotics Summer Training Programming #3: Intro to Computational Thinking

Weston Schreiber & Joshua Gabrielse

Basics of Computer Programming

Variables- place to store changeable information

Functions- block of reusable code

Conditional ( if ) Statements - code that only runs if a condition is TRUE

Loops (while, for )- section of code that repeats

Page 20: Weston Schreiber & Joshua Gabrielse Robotics Summer Training Programming #3: Intro to Computational Thinking

Weston Schreiber & Joshua Gabrielse

Basics of Computer Programming

Functions- block of reusable code

•What are some “functions” in your daily life?

•What are some functions PowerPoint might use to run this presentation?

Page 21: Weston Schreiber & Joshua Gabrielse Robotics Summer Training Programming #3: Intro to Computational Thinking

Weston Schreiber & Joshua Gabrielse

EasyC Built-In Functions:All the tools we’ll need to control the flow of the program (or, which code to run and when to run it)

Important Function: Wait( ms )

Page 22: Weston Schreiber & Joshua Gabrielse Robotics Summer Training Programming #3: Intro to Computational Thinking

Weston Schreiber & Joshua Gabrielse

Writing Your 1st Program

1. Write a Plan for your Controller Setup

2. Start a New Program

3. Operator Control

4. Build & Download to Robot

5. Autonomous Program

Page 23: Weston Schreiber & Joshua Gabrielse Robotics Summer Training Programming #3: Intro to Computational Thinking

Weston Schreiber & Joshua Gabrielse

Objectives:• Create a simple autonomous

program

• Have your robot drive on its own!

• Create an autonomous program for your robot to score a goal.

Page 24: Weston Schreiber & Joshua Gabrielse Robotics Summer Training Programming #3: Intro to Computational Thinking

Weston Schreiber & Joshua Gabrielse

Autonomous Goal #1:

• Drive forward and stop.

Page 25: Weston Schreiber & Joshua Gabrielse Robotics Summer Training Programming #3: Intro to Computational Thinking

Weston Schreiber & Joshua Gabrielse

Stage 1: Planning(Algorithm, Flowchart, Pseudocode)

Stage 2: Coding(Variables, Functions, if statements, Loops)

Stage 3: Error Analysis(Syntax Errors, Logic Errors, Readdress Plan)

Page 26: Weston Schreiber & Joshua Gabrielse Robotics Summer Training Programming #3: Intro to Computational Thinking

Weston Schreiber & Joshua Gabrielse

Which set of function calls would correctly accomplish our goal?Click the number corresponding to the correct answer on your clicker.

A. B. C. DriveStraight()

Stop()

Wait()

DriveStraight()

Wait()

Stop()

DriveStraight()

Wait()

Stop()

Page 27: Weston Schreiber & Joshua Gabrielse Robotics Summer Training Programming #3: Intro to Computational Thinking

Weston Schreiber & Joshua Gabrielse

Stage 1: Planning(Algorithm, Flowchart, Pseudocode)

Stage 2: Coding(Variables, Functions, if statements, Loops)

Stage 3: Error Analysis(Syntax Errors, Logic Errors, Readdress Plan)

Page 28: Weston Schreiber & Joshua Gabrielse Robotics Summer Training Programming #3: Intro to Computational Thinking

Weston Schreiber & Joshua Gabrielse

Stage 2: Coding(Variables, Functions, if statements, Loops)

• Recall the functions we discussed the other day

•DriveStraight(), Stop()

• Write the code for those functions.

• Create your autonomous program!

Page 29: Weston Schreiber & Joshua Gabrielse Robotics Summer Training Programming #3: Intro to Computational Thinking

Weston Schreiber & Joshua Gabrielse

Stage 1: Planning(Algorithm, Flowchart, Pseudocode)

Stage 2: Coding(Variables, Functions, if statements, Loops)

Stage 3: Error Analysis(Syntax Errors, Logic Errors, Readdress Plan)

Page 30: Weston Schreiber & Joshua Gabrielse Robotics Summer Training Programming #3: Intro to Computational Thinking

Weston Schreiber & Joshua Gabrielse

Competition SwitchesEnable/Disable & Autonomous/Operator Control

Hardware: plug the ethernet cable into the competition port on the joystick

Software: the computer has to be connected directly to the robot controller or through VEXnet from the joystick (via the programming port)

Page 31: Weston Schreiber & Joshua Gabrielse Robotics Summer Training Programming #3: Intro to Computational Thinking

Weston Schreiber & Joshua Gabrielse

• Basics of Program Complete

• Rest: Advanced Concepts

Page 32: Weston Schreiber & Joshua Gabrielse Robotics Summer Training Programming #3: Intro to Computational Thinking

Weston Schreiber & Joshua Gabrielse

Naming Conventions

1. Pick descriptive names.

2. Follow capitalization standards• Variable names start with lower case letters.• Function names start with capital letters.• Constants are all capitals.

3. Use camel case for multi-word names.• Example 1: camelCase• Example 2: CamelCase

Page 33: Weston Schreiber & Joshua Gabrielse Robotics Summer Training Programming #3: Intro to Computational Thinking

Weston Schreiber & Joshua Gabrielse

What is DriveForward according to our naming

convention?

A) a variable

B) a function

C) a constant

D) it doesn’t fit our naming convention

Page 34: Weston Schreiber & Joshua Gabrielse Robotics Summer Training Programming #3: Intro to Computational Thinking

Weston Schreiber & Joshua Gabrielse

What is PUSHED according to our naming

convention?

A) a variable

B) a function

C) a constant

D) it doesn’t fit our naming convention

I’m supposed to be pushing the button

with my nose.

Page 35: Weston Schreiber & Joshua Gabrielse Robotics Summer Training Programming #3: Intro to Computational Thinking

Weston Schreiber & Joshua Gabrielse

What is frontBumper according to our naming

convention?

A) a variable

B) a function

C) a constant

D) it doesn’t fit our naming convention

Page 36: Weston Schreiber & Joshua Gabrielse Robotics Summer Training Programming #3: Intro to Computational Thinking

Weston Schreiber & Joshua Gabrielse

Basics of Computer Programming

Variables- place to store changeable information

Functions- block of reusable code

Conditional ( if ) Statements - code that only runs if a condition is TRUE

Loops (while, for )- section of code that repeats

Page 37: Weston Schreiber & Joshua Gabrielse Robotics Summer Training Programming #3: Intro to Computational Thinking

Weston Schreiber & Joshua Gabrielse

Basics of Computer Programming

Conditional ( if ) Statements - code that only runs if a condition is TRUE

Page 38: Weston Schreiber & Joshua Gabrielse Robotics Summer Training Programming #3: Intro to Computational Thinking

Weston Schreiber & Joshua Gabrielse

If Conditions – Structure & Function

Condition: any statement that can be evaluated as true or false.

CONDITION

ACTION

• IF the CONDITION is TRUE the ACTION happens and the program continues after the IF.

• IF the CONDITION is not true the ACTION never happens and the program continues after the IF.

Page 39: Weston Schreiber & Joshua Gabrielse Robotics Summer Training Programming #3: Intro to Computational Thinking

Weston Schreiber & Joshua Gabrielse

If Conditions – Structure & Function

Condition: Compares two variables/values using a comparison operator

Symbol Definition== equal!= not equal> greater than

>= greater than or equal to

< less than<= less than or equal to

( A [symbol] B )

Double equal signs (==) check to see if a statement is true.

Ex: x == 4

Single equal signs (=) assign a value to a

variable. Ex: x = 4

Assign is just programmer-talk for “set equal to.”

Page 40: Weston Schreiber & Joshua Gabrielse Robotics Summer Training Programming #3: Intro to Computational Thinking

Weston Schreiber & Joshua Gabrielse

If Conditions – Check Your Understanding

Example If Condition:

Based on variable values, what functions will run?

Click the number corresponding to the correct answer on your clicker.

A) DontVote()B) GoShopping()C) DontVote(), then GoShopping()D) No functions will runE) age < 18

Page 41: Weston Schreiber & Joshua Gabrielse Robotics Summer Training Programming #3: Intro to Computational Thinking

Weston Schreiber & Joshua Gabrielse

If Conditions – Check Your Understanding

Example If Condition:

Based on variable values, what functions will run?

Click the number corresponding to the correct answer on your clicker.

A) GetMoreSupplies()B) TeachArtProject()C) GetMoreSupplies(), TeachArtProject()D) TeachArtProject(), GetMoreSupplies()E) No functions will run

numStudents = 22;

Page 42: Weston Schreiber & Joshua Gabrielse Robotics Summer Training Programming #3: Intro to Computational Thinking

Weston Schreiber & Joshua Gabrielse

If Conditions – Check Your Understanding

Example If Condition:

Based on variable values, what functions will run?

Click the number corresponding to the correct answer on your clicker.

A) TurnFor(), DriveStraightFor()B) DriveStraightFor(), TurnFor()C) frontBumper == PUSHED, DriveStraightFor()D) DriveStraightFor()E) TurnFor()F) No Functions will run

frontBumper = 0;

Page 43: Weston Schreiber & Joshua Gabrielse Robotics Summer Training Programming #3: Intro to Computational Thinking

Weston Schreiber & Joshua Gabrielse

If Else Conditions – Structure & Function

• IF the CONDITION is TRUE, ACTION #1 happens and the program continues after the IF.

• IF the CONDITION is FALSE, ACTION #2 happens and the program continues after the IF.

CONDITION

ACTION #1

ACTION #2

Page 44: Weston Schreiber & Joshua Gabrielse Robotics Summer Training Programming #3: Intro to Computational Thinking

Weston Schreiber & Joshua Gabrielse

If Else Conditions – Check Your Understanding

Based on variable values, what functions will run?

Click the number corresponding to the correct answer on your clicker.

A) BuyVideoGame()B) BuyBook()C) BuyLunch()D) BuyVideoGame(), BuyBook()E) BuyVideoGame(), BuyLunch()F) BuyBook(), BuyLunch()G) All 3 functions will runH) No functions will run

money = 100;

Page 45: Weston Schreiber & Joshua Gabrielse Robotics Summer Training Programming #3: Intro to Computational Thinking

Weston Schreiber & Joshua Gabrielse

If Else Conditions – Check Your Understanding

Based on variable values, what functions will run?

Click the number corresponding to the correct answer on your clicker.

A) DriveStraight()B) Stop()C) FlashLEDS()D) DriveStraight(), Stop()E) DriveStraight(), FlashLEDS()F) Stop(), FlashLEDS()G) All 3 functions will runH) No functions will run

backBumper = 1;

Page 46: Weston Schreiber & Joshua Gabrielse Robotics Summer Training Programming #3: Intro to Computational Thinking

Weston Schreiber & Joshua Gabrielse

Basics of Computer Programming

Variables- place to store changeable information

Functions- block of reusable code

Conditional ( if ) Statements - code that only runs if a condition is TRUE

Loops (while, for )- section of code that repeats

Page 47: Weston Schreiber & Joshua Gabrielse Robotics Summer Training Programming #3: Intro to Computational Thinking

Weston Schreiber & Joshua Gabrielse

Basics of Computer Programming

Loops (while, for )- section of code that repeats

Page 48: Weston Schreiber & Joshua Gabrielse Robotics Summer Training Programming #3: Intro to Computational Thinking

Weston Schreiber & Joshua Gabrielse

While Loops – Structure & Function

Condition: any statement that can be evaluated as true or false.

WHILE the CONDITION is TRUE, keep doing the ACTION(S) repeatedly.

Once the CONDITION is FALSE, the ACTION stops happening and the program continues after the WHILE.

CONDITION

ACTION

Page 49: Weston Schreiber & Joshua Gabrielse Robotics Summer Training Programming #3: Intro to Computational Thinking

Weston Schreiber & Joshua Gabrielse

While Loops – Structure & Function

3 Outcomes for a While LoopCONDITION

ACTION

In the C Programming Language:1 = TRUE0 = FALSE

– So –While (1) is always true

and the program is stuck in an endless loop

Loop runs forever Loop runs a number of times and stops

Loop is skipped and never runs

Loop runs forever Loop runs a number of times and stops

Loop is skipped and never runs

• The condition stays TRUE forever

• The condition starts TRUE, but eventually evaluates as FALSE

• The condition is FALSE initially

Page 50: Weston Schreiber & Joshua Gabrielse Robotics Summer Training Programming #3: Intro to Computational Thinking

Weston Schreiber & Joshua Gabrielse

While Loops – Check Your Understanding

Example:

Based on variable values, how many times will the loop repeat before running GoToATM()?

Click the number corresponding to the correct answer on your clicker.

A) The loop will never run.B) 1 timeC) 4 times D) 5 timesE) The loop will never stop

Page 51: Weston Schreiber & Joshua Gabrielse Robotics Summer Training Programming #3: Intro to Computational Thinking

Weston Schreiber & Joshua Gabrielse

While Loops – Check Your Understanding

Example:

Based on variable values, what actions will the robot take?Click the number corresponding to the correct answer on your clicker.

A) The robot will drive straight ahead.B) The robot won’t move.C) The robot will turn for a bit, then drive straight ahead.D) The robot will turn in a circle continuously, never stopping.

Page 52: Weston Schreiber & Joshua Gabrielse Robotics Summer Training Programming #3: Intro to Computational Thinking

Weston Schreiber & Joshua Gabrielse

While Loops – Check Your Understanding

Example:

Based on variable values, what actions will the robot take?Click the number corresponding to the correct answer on your clicker.

A) Drive straight, then Turn continuously.B) Drive straight forever.C) Drive straight, Turn, then Stop.D) Drive straight, then Stop.

Page 53: Weston Schreiber & Joshua Gabrielse Robotics Summer Training Programming #3: Intro to Computational Thinking

Weston Schreiber & Joshua Gabrielse

Autonomous Flowchart (Easier)

• Make a flow chart for a robot with a bumper switch in the front.

• The robot drives forward, stopping when it hits something.

Function HintsDriveForward()CheckFrontBumper()

Page 54: Weston Schreiber & Joshua Gabrielse Robotics Summer Training Programming #3: Intro to Computational Thinking

Weston Schreiber & Joshua Gabrielse

Autonomous Flowchart (Harder)

• Make a flow chart for a robot with a bumper switch in the front and the back.

• Every time the robot runs into something it changes direction.

Function HintsDriveForward()DriveBackward()CheckFrontBumper()CheckBackBumper()

Page 55: Weston Schreiber & Joshua Gabrielse Robotics Summer Training Programming #3: Intro to Computational Thinking

Weston Schreiber & Joshua Gabrielse

One P

ossible Algorithm

Page 56: Weston Schreiber & Joshua Gabrielse Robotics Summer Training Programming #3: Intro to Computational Thinking

Weston Schreiber & Joshua Gabrielse

One P

ossible Algorithm

Equivalent C-Code Generated by EasyC

Go to Window Block & C Programming in EasyC to see both the flowchart and the C code.

Page 57: Weston Schreiber & Joshua Gabrielse Robotics Summer Training Programming #3: Intro to Computational Thinking

Weston Schreiber & Joshua Gabrielse

Aids for Teaching Programming

• Stratch – Intro Programming Tool designed at MIT

– http://scratch.mit.edu/

• Code Academy – Online Instruction/Challenges

– http://www.codecademy.com

• Lucid Charts – Nice web-based Flowchart Creator

– http://www.lucidchart.com/ • Flow Chart Practice

– http://www.cimt.plymouth.ac.uk/projects/mepres/book8/bk8i1/bk8_1i2.htm