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
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.
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)
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?”
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
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)
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
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?
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
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
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
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
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
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.
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
Weston Schreiber & Joshua Gabrielse
Variable Types
When in doubt, pick int!
Need to use decimals?
Use double!
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:
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
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?
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 )
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
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.
Weston Schreiber & Joshua Gabrielse
Autonomous Goal #1:
• Drive forward and stop.
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)
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()
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)
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!
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)
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)
Weston Schreiber & Joshua Gabrielse
• Basics of Program Complete
• Rest: Advanced Concepts
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
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
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.
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
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
Weston Schreiber & Joshua Gabrielse
Basics of Computer Programming
Conditional ( if ) Statements - code that only runs if a condition is TRUE
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.
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.”
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
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;
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;
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
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;
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;
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
Weston Schreiber & Joshua Gabrielse
Basics of Computer Programming
Loops (while, for )- section of code that repeats
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
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
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
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.
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.
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()
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()
Weston Schreiber & Joshua Gabrielse
One P
ossible Algorithm
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.
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