SGV CUE 2017 Auto Robots

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 112

Program an Autonomous

Maze Solving Robot with


Block Coding

https://www.kokoteach.com/sgv-cue-2017/
About Your Presenters

 Esteban : esjmorales@gmail.com  Yoko : kokoteachshop@gmail.com


 BS & MS in Computer Science  MA in Educational Technology
 Data Scientist / Programmer  Garvey School District Teacher
Glaucoma Department UCLA
 Applied technology (Scratch,
 Taught Software Engineering at HummingBird, WeDo) to various
CSULB lessons
 Interested in teaching computer  Interested in applying technology to
science the classroom
Objective

 To program two robots to solve a simply connected maze

 The projects address all levels of the Engineering Design NGSS


 K-2 Engineering Design
 3-5 Engineering Design
 MS Engineering Design
 HS Engineering Design
K-2 Engineering Design
 K-2-ETS1-1. Ask questions, make observations, and gather information about a
situation people want to change to define a simple problem that can be solved through
the development of a new or improved object or tool.

 Situation / Problems involving autonomous robots


 Tool to Develop / Improve: autonomous robots

 Reaching dangerous or unreachable places to humans


 Exploration:
 Mars Rover
 https://spaceplace.nasa.gov/mars-rovers/en/
 https://mars.nasa.gov/participate/funzone/
 https://www.dexterindustries.com/shop/mars-rover-unit-gopigo-single-classroom-license/
 Sentry Deep-Sea Robot
 Search and Rescue:
 Center for Robot-Assisted Search and Rescue: http://crasar.org/
 Safety and Improving Our Lives:
 Self driving cars
 Roomba
K-2 Engineering Design
 K-2-ETS1-3. Analyze data from tests of two objects designed to solve the
same problem to compare the strengths and weaknesses of how each performs.

 Two objects: GoPiGo and Finch robot (or same robot programmed differently)
 Problem: autonomously solve a maze
 Data: Time it took to solve each maze. Did it solve every maze? Are specific
obstacles difficult for the robot?

 Make modifications to the code (ie move faster, turn shaper)


 Did it increase maze solving time? Did changes improve the robot?
3-5 Engineering Design
 3-5-ETS1-1. Define a simple design problem reflecting a need or a want that
includes specified criteria for success and constraints on materials, time, or
cost.

 Need / Want: See previous slide of various needs of autonomous robots

 Design Problem: Our robots solving the maze but, we must define our problem in
more details before we can design the solution

 Success: Did our robot solve the maze? Set a time constraint.

 Constraints: What does our robot need? How can we build our maze? How much
does it all cost?
3-5 Engineering Design
 3-5-ETS1-2. Generate and compare multiple possible solutions to a problem
based on how well each is likely to meet the criteria and constraints of the
problem.

 Problem: Program robot to autonomously solve a maze. Why? See previous slides 

 Multiple Solutions: Two robots OR one robot and modify the code OR one robot
running very different programs

 Compare: Time it took? Did it work? Certain mazes solved faster?

 Constraints: Our maze constraints


3-5 Engineering Design
 3-5-ETS1-3. Plan and carry out fair tests in which variables are controlled
and failure points are considered to identify aspects of a model or prototype
that can be improved.

 Model: Our two robots Prototype: Our simulations

 Controlled variables: the maze itself and robots

 Improvement: what can we do to solve the maze faster?


MS Engineering Design
 MS-ETS1-1. Define the criteria and constraints of a design problem with
sufficient precision to ensure a successful solution, taking into account relevant
scientific principles and potential impacts on people and the natural
environment that may limit possible solutions.
 See previous slides!
 We define our problem (taking into account real world examples), we define our maze
/ robot constraints that may limit possible solutions
 Relevant scientific principles: the physics that will limit our robot

 MS-ETS1-2. Evaluate competing design solutions using a systematic process to


determine how well they meet the criteria and constraints of the problem.
 See previous slides 
 Real world competition solutions and your class can come up with competing solutions
MS Engineering Design
 MS-ETS1-3. Analyze data from tests to determine similarities and differences
among several design solutions to identify the best characteristics of each
that can be combined into a new solution to better meet the criteria for success.
 MS-ETS1-4. Develop a model to generate data for iterative testing and
modification of a proposed object, tool, or process such that an optimal design
can be achieved.

 Tool: our robots


 Data to test: time it takes to solve the maze
 Iterative Testing / Modifications:
 When setting the speeds start at 40%, then try 50%, 60%, 70%, … 80%
 Iteratively change the turn radius and test results
 Several Design Solutions:
 Modifying the code and see which is the best solution. (we’ll see where we can make
changes)
HS Engineering Design
 HS-ETS1-1. Analyze a major global challenge to specify qualitative and
quantitative criteria and constraints for solutions that account for societal needs
and wants.
 You may consider what we know autonomous robots are used for as a global challenge
 Rescuing people from natural disasters, space exploration
 Center for Robot-Assisted Search and Rescue: http://crasar.org/ NASA

 HS-ETS1-2. Design a solution to a complex real-world problem by breaking it


down into smaller, more manageable problems that can be solved through
engineering.
 YES DEFINITELY

 HS-ETS1-4. Use a computer simulation to model the impact of proposed


solutions to a complex real-world problem with numerous criteria and
constraints on interactions within and between systems relevant to the problem.
 We will first create a computer simulation BEFORE we code our robots
Computer Science Concepts
 Computational Thinking
 Students will break down the problem into smaller pieces, find the patterns, and create their
algorithm
 Algorithms
 Students will develop the sequence of steps to solve the maze
 Events and Event-Based Programming
 Event is just an external trigger, sensor inputs in our case
 State Diagrams
 Students will use State Diagrams to help develop their algorithm
 Conditional Statements & Loops
 Debugging
 Students will very likely not get it right the first time 
The Problem
Defining our Problem

 K-2-ETS1-1. Ask questions, make observations, and gather information


about a situation people want to change to define a simple problem…
 3-5-ETS1-1. Define a simple design problem reflecting a need or a want
that includes specified criteria…
 MS-ETS1-1. Define the criteria and constraints of a design problem with
sufficient precision to ensure a successful solution…
Problem
 Start with research on autonomous robots
 See previous slides for examples and links
 Problem: A robot must autonomously solve a maze
 Solution definition: Robot goes from start to the end

 Problem Constraints:
 Robot solves maze by executing a program (NO CONTROLLING THE ROBOT MANUALLY)
 Maze is simply connected (like house)
 Start and End of the maze are on outer boundaries
 Maze is made from NON-REFLECTIVE material (for sensors sake), light colors best
 Maze is sturdy enough
 Maze is wide enough
 Optional: time constraint
Start Start Finish

Finish

NO Yes
Our Tools
Our Tools

 What do we have to work with?


Finch

GoPiGo 3
Our Tools

 Both robots have:


 Can move forward, backward, left, right
 Obstacle sensors

 Both robots programmable with block coding


 Both can use: Scratch
 Finch Robot: Snap!
 GoPiGo: Bloxter

 Looks like we have everything we need 


Finch Robot
BirdBrain Technologies
Finch Robot

 https://www.finchrobot.com/
 $99 (10% educational discount available)
 Finch Robot Loan Program:
 https://www.finchrobot.com/finch-robot-loan-program
 Features:
 Motors (forward, backward, left, right)
 obstacle sensor
 Multicolor LED on the front beak
 Accelerometers
 x, y, z
 Buzzer
 Light, temperature
 Plugs in via USB, no batteries needed
Finch Robot – Hardware

 https://www.finchrobot.com/finch-hardware
 For our purposes, we’ll focus on the obstacle sensors and motors

 Obstacle Sensor
 Return TRUE or FALSE if something is detected, does NOT return a distance
 Has two, where the “eyes” are, each sensors returns TRUE or FALSE
 Range is 3 to 12 inches
 “Due to the limitations of this type of sensor very narrow objects or objects made
of certain black plastics may not register as obstacles.”
Finch Robot – Hardware

 https://www.finchrobot.com/finch-hardware

 Motors
 Two gear motors (turn 360 degrees clock wise or counter clock wise)
 “The Finch's top speed is roughly 15 inches per second.”
Finch Robot – Setting Up

 You’ll need to download the BirdBrain Robot Server:


 https://www.finchrobot.com/software/snap
 Windows, Mac, Linux, Chromebook
 This application allows you to connect your Finch (or HummingBird) and
program it via Snap! or Scratch

 We’ll be working with Scratch today, which uses additional blocks


 Snap! is very similar, in fact, a descendent of Scratch
Finch Tutorials

 Snap! offers very simple blocks to regular Snap!

 Several tutorials a sample projects:


 https://www.finchrobot.com/software/snap

 We’ll look at Finch with Scratch as we go


GoPiGo
Dexter Industries
GoPiGo

 GoPiGo is a HAT and a kit built for the RaspberryPi


 Similar to Hummingbird (Arduino board), but in this case separate hardware

+ =
GoPiGo

 https://www.dexterindustries.com/gopigo3/
 A moving robot with sensors
 Cost: $200 for starter kit
 Starter kit comes with everything you need including a RaspberryPi
 Classroom kit $990 (5 starter kits)
 Educational discount and trail kit (45)
 https://www.dexterindustries.com/dextered/
 Assembly Instructions
 https://www.dexterindustries.com/gopigo3-tutorials-documentation/

 Requires 8 AA batteries
https://www.dexterindustries.com/GoPiGo/learning/hardware-port-description/
Connecting Sensors and LEDs
 Connecting wires is straight
forward using connectors
 They can only go in one way
 There are two types of sensors
ports:

 I2C Port:
 Analog / Digital Ports:
 Distance Sensor (laser, more
 Ultrasonic sensor (distance, cm in)
precise, cm in)
Connecting Sensors and LEDs

 Analog / Digital Ports:  I2C Port:


 Ultrasonic sensor (distance, cm in)  Distance Sensor (laser, more
precise, cm in)
 Additional LEDs
 Line follower
 Light sensor (0 to 74)
 Sound sensor
 Buzzer (plays at different
frequencies)
Starting It Up

 LEDs will blink, wait for a solid green LED in the back to light up
 Then search for the WiFi Signal:

 Connect to it.
 When the solid green LED turns solid blue, that means you are connected
 Your browser should automatically open up to bloxter.com
Our Tools

 Both robots have:


 Can move forward, backward, left, right
 Two obstacle sensors

 Both robots programmable with block coding

 Looks like we have everything we need 


So Far…

 Researched Related Real World Problems and Solutions


 Defined Our Problem and Solution with Criteria and Limitations
 Defined Our Tools
Breaking Down the
Problem
Our Approach
Breaking Down the Problem

3-5-ETS1-2. Generate and compare multiple possible solutions to a problem based


on how well each is likely to meet the criteria and constraints of the problem.
3-5-ETS1-3. Plan and carry out fair tests in which variables are controlled and
failure points are considered to identify aspects of a model or prototype that can be
improved.
MS-ETS1-2. Evaluate competing design solutions using a systematic process to
determine how well they meet the criteria and constraints of the problem.
MS-ETS1-3. Analyze data from tests to determine similarities and differences among
several design solutions to identify the best characteristics of each that can be
combined into a new solution to better meet the criteria for success.
HS-ETS1-2. Design a solution to a complex real-world problem by breaking it down
into smaller, more manageable problems that can be solved through engineering.
Let’s Look at Our Robots
Think Like a Robot

 You’re the robot:


 You can move in any direction
 The obstacle sensors

Finch GoPiGo
Think Like a Robot

 You’re the robot:


 You CANNOT HEAR
 You CANNOT SEE

 Obstacle sensors are more similar to you holding out your arms
 And in the case of the Finch, your hands are only out in front of you
 And in the case of the GoPiGo, one hand is in front and the other is to your side

Finch GoPiGo
Let’s Break It Down…

 Remember, we can move in any direction and we can use our hands/arms to sense
obstacles
 Let’s discuss how we would solve the following cases

Finch GoPiGo
Simple Maze 1
Start

Finch GoPiGo

Finish
Simple Maze 2
Start
Start

Finch GoPiGo

Finish
Finish
Simple Maze 3

Start

Finish

Finch GoPiGo
Simple Maze 4

Start

Finish
Complex Maze
Start Finish
Approaches
 We can walk forward until we hit something, then always
turn left (or always turn right)
 Will solve some mazes but can get stuck in a loop

 We can walk forward until we hit something, then turn left


or right
 How do we choose left or right? Randomly
 This will eventually solve the maze, but is inefficient

 We need to know where we’ve been, but how?


Wall Follower Algorithm

 Follow the maze always touching the wall to your left or right

 Well, since all the walls are simply connected, you can imagine
they’re made all with one single line, which means if we follow
that line we will eventually reach the end
 No randomness, no back tracking, will always find the end

 So… we always touch the wall and follow the wall

 This is the algorithm we will develop


Wall Follower Algorithm
Start Start
Start

Finish

Finish

Finish
Complex Maze
Start Finish
Breaking Down the
Algorithm
States, Events, and State Diagram
Breaking Down the Problem

3-5-ETS1-2. Generate and compare multiple possible solutions to a problem based


on how well each is likely to meet the criteria and constraints of the problem.
3-5-ETS1-3. Plan and carry out fair tests in which variables are controlled and
failure points are considered to identify aspects of a model or prototype that can be
improved.
MS-ETS1-2. Evaluate competing design solutions using a systematic process to
determine how well they meet the criteria and constraints of the problem.
MS-ETS1-3. Analyze data from tests to determine similarities and differences among
several design solutions to identify the best characteristics of each that can be
combined into a new solution to better meet the criteria for success.
HS-ETS1-2. Design a solution to a complex real-world problem by breaking it down
into smaller, more manageable problems that can be solved through engineering.
Wall Follower Algorithm
 Let’s break down the algorithm by looking at all possible scenarios

Start Start or Start or


Finish Finish

GoPiGo

Finch

Finish
State & Events
 Let’s break it down using States & Events

 State
 A snapshot
 A stage in the behavior / pattern of an object (our robot)
 In our case: what is our robot doing?
 Going forward, backward, left, right?

 Event
 External trigger
 In our case: obstacle sensors returning TRUE / FALSE
State Diagrams
 Diagram describe the behavior of systems
 Composed of finite number of states

 Great exercise for your students to help break down the problem, to find and
define behaviors / patterns, and to organize the steps of their algorithm to solve
the problem

 And it’s a diagram used in computer science

 You can draw these by hand, we’ll use https://www.draw.io it’s free and online

 You can also do these using Google Drawings


State Diagrams

 State diagrams have:


 Initial State
 End State
 State
 Transition (event that triggers the next state)
 Conditionals
State Diagrams

Final State

Simple State Conditional


Initial State
State Diagrams – Example

 Let’s consider a door with two states:


 Open
 Closed
State Diagrams – Example
 Let’s consider ourselves, what states do we have?
 Awake
 Asleep
 Healthy
 Unhealthy
 Hungry
 Scared
 …etc

 And numerous external triggers (aka Events) that can move us between States
State Diagrams – Example
 And numerous external triggers (aka Events) that can move us between States
State Diagram – Example

 How about a vending machine?


 For simplicity our vending machine does not give change, only accepts
nickels, dimes, quarters, and only sells one item which is dispensed when $25
is given

 What are our possible states?


 0 cents (start), 5 cents, 10 cents, 15 cents, 20 cents, 25 cents (end state)
 What are our transitions / events?
 Money being inserted
 What are our possible states?
 0 cents (start), 5 cents, 10 cents, 15 cents, 20 cents, 25 cents (end state)
 What are our transitions / events?
 Money being inserted
Breaking Down the
Algorithm
States, Events, and State Diagram
Left-Wall Follower Algorithm
 Remember, our robot must always “sense” the wall to the left

 It solves the maze by always touching the left wall

 Our robot has two sensors, Left Sensor and Right Sensor

GoPiGo Finch

LEFT
GPG F

RIGHT LEFT
RIGHT
Possible Maze Scenarios - GPG
GoPiGo Algorithm Break
GoPiGo Down
LEFT
GPG

RIGHT
Case 0 – Read Sensor

 Before we do anything, we need to know what is around us

 Robot reads the sensor

 Then we can react

 Left Sensor and Right Sensor


If Sensor Left == TRUE and Sensor Right == FALSE
Case 1 - GPG Move Forward

Left Wall Follower

 Wall to the Left


 Nothing in front

 Trigger:
 Sensor Left: TRUE
 Sensor Right: FALSE

 State:
 Move Forward
Case 2 – Corner Right Turn
Left Wall Follower

 Wall to the left


 Wall in front

 Trigger:
 Sensor Left: TRUE
 Sensor Right: TRUE

 State:
 Turn Right
Case 1 then Case 2
Case 3 – Corner Left Turn
Left Wall Follower

 No wall on the left


 No wall in front or wall in front

 Trigger:
 Sensor Left: FALSE
 Sensor Right: TRUE or FALSE

 State:
 Turn Left
Case 1 and Case 3
GoPiGo - Case 0, 1, 2, 3
GoPiGo

LEFT
GPG

RIGHT
Finch Algorithm Break
Finch Down
F
LEFT
RIGHT
Case 0 – Read Sensor
F
 Before we do anything, we need to know what is around us

 Robot reads the sensor

 Then we can react

 Left Sensor and Right Sensor

 Sensors both in front


If Sensor Left == FALSE and Sensor Right == FALSE
Case 1 – Finch Move Forward and Turn Left

Left Wall Follower

F
 Nothing both sensors
 Nothing in front

 Trigger:
 Sensor Left: FALSE
 Sensor Right: FALSE

 State:
 Move Forward and Turn Left

 Why do we do this…? Why not go straight?


 We need to make sure the wall is always to our left
If Sensor Left == TRUE OR Sensor Right == TRUE
Case 1 – Touching Wall Back Up and Reposition

Left Wall Follower

 Wall to the Left


 So we’re pointing towards wall

 Trigger:
 Sensor Left: TRUE
OR
 Sensor Right: TRUE

 State:
 Back Up and Reposition
Finch Case 0, 1, 2

Finch

F
LEFT
RIGHT
Case 1 then 2 then 1 then 2 then 1 …

F
Case 1 then 2 then 1 then 2 then 1 …

F
Alternatively – No Reading Sensor State
 The sensors are constantly being read
 They’re independent of our code, they’re part of the hardware
 We can treat sensors as Events / Triggers

F GPG
GPG

F
So Far…

 Researched Related Real World Problems and Solutions


 Defined Our Problem and Solution with Criteria and Limitations
 Defined Our Tools
 Explored Possible Solutions by Breaking Down the Problem
 Defined Our Solution (Wall-Follower)
 Defined Patterns / Steps of Our Solution by Breaking Down the Problem
 Defined our Algorithm by Breaking Down the Problem
Things to Consider….
K-2-ETS1-3. Analyze data from tests of two objects designed to solve the same problem to
compare the strengths and weaknesses of how each performs.

3-5-ETS1-2. Generate and compare multiple possible solutions to a problem based on how
well each is likely to meet the criteria and constraints of the problem.
3-5-ETS1-3. Plan and carry out fair tests in which variables are controlled and failure points
are considered to identify aspects of a model or prototype that can be improved.

MS-ETS1-2. Evaluate competing design solutions using a systematic process to determine how
well they meet the criteria and constraints of the problem.
MS-ETS1-3. Analyze data from tests to determine similarities and differences among several
design solutions to identify the best characteristics of each that can be combined into a new
solution to better meet the criteria for success.
MS-ETS1-4. Develop a model to generate data for iterative testing and modification of a
proposed object, tool, or process such that an optimal design can be achieved.

HS-ETS1-3. Evaluate a solution to a complex real-world problem based on prioritized criteria


and trade-offs that account for a range of constraints, including cost, safety, reliability, and
aesthetics as well as possible social, cultural, and environmental impacts.
Finch GoPiGo

 How much should we move forward?  How much should we move forward?
 What should the turning radius be?  How far should the wall be from our
left sensor before we turn?
 How much should we back up?
 What if we get TOO close to the wall?
 How should we reposition?
 How should we turn left?
 Should we pause for sensor readings?
 Should we pause for sensor readings?

These are all things you can compare with tests, data results, and data analysis
Robot Simulation
Block Coding Part 1
Robot Simulation
 Ideal Situation
 One robot and computer per 2 to 3 students (paired programming)
 Enough space for everyone to test their robots
 There is enough time for students to finish programming

 Reality
 Robots and computers are not cheap
 You may have 1 robot and computer for every 5 or more students
 Space is limited
 Time with the robots is limited

 Solution
 Robot Simulation
Robot Simulation

HS-ETS1-4. Use a computer simulation to model the impact of proposed


solutions to a complex real-world problem with numerous criteria and constraints
on interactions within and between systems relevant to the problem.
Robot Simulation
K-2-ETS1-3. Analyze data from tests of two objects designed to solve the same
problem to compare the strengths and weaknesses of how each performs.

3-5-ETS1-2. Generate and compare multiple possible solutions to a problem based


on how well each is likely to meet the criteria and constraints of the problem.
3-5-ETS1-3. Plan and carry out fair tests in which variables are controlled and
failure points are considered to identify aspects of a model or prototype that can be
improved.

MS-ETS1-2. Evaluate competing design solutions using a systematic process to


determine how well they meet the criteria and constraints of the problem.
MS-ETS1-3. Analyze data from tests to determine similarities and differences among
several design solutions to identify the best characteristics of each that can be
combined into a new solution to better meet the criteria for success.
MS-ETS1-4. Develop a model to generate data for iterative testing and modification
of a proposed object, tool, or process such that an optimal design can be achieved.
Conditional Statements and Loops
IF THEN, IF ELSE
REPEAT UNTIL, REPEAT, FOREVER
Conditional Statement – if Statement

 If something is TRUE, then do it


 If hypothesis TRUE, then conclusion

 If hungry, then find food

 If sleep, then take nap

 If homework missing, then call parents

 If homework done, then check complete


Conditional Statement – if Statement
The condition, this MUST be TRUE to go inside

We go inside
AND OR

 We can add to our conditional statements with AND / OR

 AND : both must be TRUE

 OR : either or must be TRUE

if Homework Turned in AND Complete, THEN mark complete


if Homework Missing OR incomplete, THEN call parent

if getting ready for work OR you stink, THEN shower


AND OR

 We can use multiple AND / OR to make more complicated conditionals

if Homework Turned In AND Complete AND 80% Correct, THEN mark complete

if hungry AND have spending money AND favorite place open, THEN eat out

if being loud OR making annoying sound OR doing something dumb, THEN dope slap
AND OR

 When combining AND with OR


 Just like in math… use parenthesis to make things clear
 5*5/2 (5 * 5) / 2 vs 5 * (5 / 2)

If (sleepy AND have time for a nap) OR bedtime, THEN go to sleep

If sleepy AND (have time for a nap OR bedtime), THEN go to sleep

If younger sibling OR (being loud AND annoying), THEN dope slap

if (X AND Y) OR (W AND Z), THEN do something


Conditional Statement – if else Statement

 If something is TRUE, then do it, ELSE do something else


 If hypothesis TRUE, then conclusion A, otherwise conclusion B
IF vs IF ELSE

In either case, we go
inside ONLY if the
condition is TRUE

In this example SPACE


is pressed
IF vs IF ELSE

In either case, we still


say “DANCE” since it is
outside our if-
statement
IF vs IF ELSE

We go in here ONLY
when our conditional
is FALSE
IF vs IF ELSE

This is useful if we
want to do something
when our condition is
FALSE
Repeat Until
If this is TRUE

Keep doing what is


inside

We get here until the “repeat until”


is broken by the FALSE conditional
Robot Simulation
Block Coding Part 1
Let’s Open Up Scratch

 Feel free to follow along or just watch


 All Code and sprites online for you
 Ask questions

 Code:
 https://www.kokoteach.com/sgv-cue-2017/
http://sched.co/C5Lv

You might also like