1 of 51

1

FREE VIRTUAL CODEBOT MISSION

Turkey Trot: Gobbler Hill

Code your virtual CodeBot to race up a hill

2 of 51

2

THANKSGIVING

What's a Turkey Trot?

  • On Thanksgiving morning, thousands of people run a fun race called a Turkey Trot.
  • Some runners dress up as turkeys or pilgrims!
  • Today your virtual CodeBot is the turkey.
  • Your job: write the code that gets it to the top of Gobbler Hill as fast as you can.
  • Line-following robots are real!
  • They race at robotics competitions and zip around warehouses packing online orders.

3 of 51

3

DID YOU KNOW?

The first Turkey Trot was in 1896

On Thanksgiving Day, the YMCA in Buffalo, New York, held a race of about 5 miles on dirt roads. It still runs every year, and it is older than the Boston Marathon.

1896

First race in Buffalo

6

Runners started

4

Runners finished

FUN FACT HUNT (OPTIONAL)

Who won the very first Turkey Trot? See if you can find out!

4 of 51

4

THE MISSION

For this project you will:

  • Code a turkey that follows a line up a hill.
  • Use real sensor data to fix a steering bug.
  • Use the accelerometer to count the hills.
  • Time the race with CodeBot's clock.
  • Tune your code for the fastest time.

5 of 51

5

CHOOSE YOUR PATH

Two ways to race

Race Day path

  • About 30 minutes
  • No coding experience needed
  • You will:
  • Load the finished program
  • Read it and predict what happens
  • Tune it for the fastest time

Build It path

  • About two class periods
  • The challenge: for coders who already know if / else, loops, and functions
  • You will:
  • Write the whole program yourself
  • Build it one step at a time. Are you brave enough?

Your teacher will tell you which path to take. Everyone starts with Getting Started.

6 of 51

6

GETTING STARTED

Log in and open the Sandbox

  • Go to make.firialabs.com
  • Log in with your account.
  • This saves your code. New File stays grayed out until you log in!
  • Open the Sandbox.
  • It is the icon on the right side bar, near the bottom.
  • Click it once. Clicking it again leaves the Sandbox.

7 of 51

7

GETTING STARTED

Choose the virtual CodeBot

  • Click No Device Connected at the bottom left.
  • Choose Sim CodeBot, then click OK.
  • Does it say "Cannot Change Target Device"? Open the Sandbox first (slide 6).
  • A 3D world opens with your CodeBot in it.

8 of 51

8

GETTING STARTED

Choose the race course

  • The world starts as Sumo Garden.
  • Click Sumo Garden at the bottom of the 3D view.
  • Choose Line-follow 101.
  • That's the Turkey Trot course!
  • Set the camera to Chase.
  • The camera menu is at the bottom of the 3D view. Chase follows your turkey, so it never drives out of sight.

9 of 51

9

GETTING STARTED

Start your program

  • In the text editor, start a new file: File, then New File.
  • Grayed out? Log in first (slide 6).
  • Name it TurkeyTrot.
  • What goes in this file depends on your path:
  • Build It path: you will add to this one program in every step.
  • Race Day path: you will paste one finished program into it.

10 of 51

10

GETTING STARTED

Three buttons you will use a lot

Run

The green button above the code.

Console

The button at the bottom right. Your program's messages show up here.

RESET

Puts your turkey back at the start line. Press it before every run!

11 of 51

11

RACE DAY PATH

Race Day path: load the finished program

  • Build It path? Skip this slide and go on to Part 1.
  • Your teacher will share turkey_trot_part2.txt with you.
  • Open it, copy all of it, and paste it into your TurkeyTrot file.
  • Before you run it, read the code.
  • Find SPEED and TURN near the top (lines 4 and 5). Predict: what will happen if you make SPEED bigger?
  • Find the line that prints FINISH! What do you think it will print?
  • Press RESET, then click Run. Watch your turkey race to the top!
  • Turkey crashed? Click the square Stop button before you change anything.
  • Now jump to Part 3: Race Day on slide 44.

12 of 51

12

TURKEY TROT

Part 1: Follow the line

Get your turkey up the hill.

13 of 51

13

PART 1

Step 1: Warm up with a gobble

  • Every program starts by importing the code it needs.
  • from botcore import * gives you CodeBot's motors, sensors, lights, and speaker.
  • from time import sleep lets your program pause.
  • Define a function named gobble() that plays five notes on the speaker.
  • Use a for loop to play each note in a list.
  • Then call gobble() in the main program.
  • Indenting: press Tab to indent a line and Shift+Tab to move back.

14 of 51

14

PART 1

Step 1: Warm up with a gobble

Your code could look like this:

New and changed lines are highlighted.

A list holds several values in square brackets. The for loop takes the notes one at a time.

15 of 51

15

PART 1

Step 1: Test your code

TEST YOUR CODE

  1. Click Run.
  2. You should hear a quick gobble: five short notes.
  3. No sound? Check that your computer's sound is on and spkr.off() is NOT indented under the for loop.

16 of 51

16

PART 1

Step 2: Trot forward

  • Make a constant named SPEED near the top. Set it to 40.
  • A constant is a variable you set once and don't change while the program runs.
  • Its name is in capital letters.
  • In the main program, turn on the motors with motors.enable(True).
  • Run both wheels at SPEED, sleep for 3 seconds, then turn the motors off.

17 of 51

17

PART 1

Step 2: Trot forward

Add the highlighted lines:

New and changed lines are highlighted. The numbers on the left are line numbers.

18 of 51

18

PART 1

Step 2: Test your code

TEST YOUR CODE

  1. Press RESET, then click Run.
  2. Your turkey gobbles, trots forward along the start of the course, and stops.
  3. Predict: if it kept going, would it stay on the line all the way up the hill? Why or why not?

19 of 51

19

PART 1

Step 3: Meet the line sensors

  • CodeBot has 5 line sensors underneath, numbered 0 to 4 from left to right.
  • ls.check() looks at all 5 and gives back a tuple: 5 True or False values in a row, one per sensor.
  • True means that sensor sees the dark line.
  • Store them in a variable: vals = ls.check()
  • vals[0] is the far left sensor.
  • vals[4] is the far right sensor.

Front of CodeBot

the line

0

False

1

False

2

True

3

False

4

False

left

right

vals = (False, False, True, False, False)

20 of 51

20

PART 1

Step 3: Steer to stay on the line

  • Add a constant TURN = 15. It is how much to speed up one wheel and slow the other.
  • Delete the last 4 lines of Step 2. A while True: loop will steer instead, using a bang-bang controller: CodeBot bangs back and forth between left and right turns. The algorithm:
  • If the line is on the left (vals[0] or vals[1]): turn left by slowing the LEFT wheel.
  • Else if the line is on the right (vals[3] or vals[4]): turn right.
  • Else: go straight.
  • End the loop with a tiny sleep(0.01) so the simulator can keep up.

Try it first: write this yourself before you look at the next slide!

21 of 51

21

PART 1

Step 3: Steer to stay on the line

Your code could look like this:

New and changed lines are highlighted. The numbers on the left are line numbers.

Also add:

Line 5: TURN = 15

Delete or change these lines:

motors.run(LEFT, SPEED)

motors.run(RIGHT, SPEED)

sleep(3)

motors.enable(False)

22 of 51

22

PART 1

Step 3: Test your code

TEST YOUR CODE (WATCH CLOSELY!)

  1. Press RESET, then click Run.
  2. leds.ls(vals) lights the sensor LEDs under CodeBot, so you can see what each sensor sees.
  3. Your turkey starts to follow the line, then zigzags wildly and runs off the course.
  4. Why? Watch the sensor lights closely. Do they only light up on the black line?

23 of 51

23

PART 1

Step 4: Be a sensor detective

  • The white course has gray stripes. The sensors might be confusing them with the line!
  • ls.read(0) gives the actual number sensor 0 sees, from 0 to 4095.
  • Darker means a bigger number.
  • Write a short test program that creeps forward and prints all 5 numbers 12 times.
  • Save your TurkeyTrot file. Start a new file named sensor_test.

24 of 51

24

PART 1

Step 4: Be a sensor detective

Your sensor test could look like this:

New and changed lines are highlighted.

25 of 51

25

PART 1

Step 4: Read the data

TEST YOUR CODE

  1. Press RESET, then Run sensor_test. Open the console.
  2. Look at the numbers. The black line reads 4095. White reads about 286. The gray stripes read about 1200 to 2700.
  3. Great detective work! Think: what number could you pick that is above the stripes but below the line?

26 of 51

26

PART 1

Step 5: Use a threshold

  • A threshold is a cutoff number. Readings above it count as the line.
  • Your data says the stripes read below about 2700 and the line reads 4095, so 3000 works well.
  • Go back to TurkeyTrot. Add a constant THRESHOLD = 3000.
  • Change ls.check() to ls.check(THRESHOLD).

27 of 51

27

PART 1

Step 5: Use a threshold

Add the highlighted lines:

New and changed lines are highlighted. The numbers on the left are line numbers.

Delete or change these lines:

vals = ls.check()

28 of 51

28

PART 1

Step 5: Test your code

TEST YOUR CODE (WATCH CLOSELY!)

  1. Press RESET, then click Run.
  2. Your turkey follows the line up both ramps. Nice!
  3. But wait... there is a bug! At the top, your turkey keeps going and tumbles off the hill. It doesn't know the race is over.

29 of 51

29

PART 1

Part 1: Think about it

  1. Why did the gray stripes fool your turkey in Step 3?
  2. How did the sensor numbers help you choose THRESHOLD?
  3. Why does your turkey fall off the top of the hill?

You can stop here, or continue to Part 2 to give your turkey a finish line.

30 of 51

30

TURKEY TROT

Part 2: Find the finish line

Teach your turkey to know when it reaches the top.

31 of 51

31

PART 2

Step 6: Feel the hill

  • CodeBot has an accelerometer. It measures the pull of gravity in 3 directions: X, Y, and Z.
  • That's how it can tell when CodeBot tilts.
  • accel.read() gives back a tuple of 3 numbers (x, y, z).
  • accel.read()[1] picks out y, the direction CodeBot's nose points.
  • When CodeBot's nose tips up a hill, y drops.
  • Before the race starts, save the flat-ground y in a variable named flat.
  • In the loop, read the tilt. If it is more than 2000 below flat, CodeBot is climbing: light the user LEDs. Else turn them off.

32 of 51

32

PART 2

Step 6: Feel the hill

Add the highlighted lines:

New and changed lines are highlighted. The numbers on the left are line numbers.

Why isn't flat zero? CodeBot's nose points slightly down, even on flat ground. So gravity pulls a little along y, and flat reads about 5660.

33 of 51

33

PART 2

Step 6: Test your code

TEST YOUR CODE

  1. Press RESET, then click Run. Open the console.
  2. You should see Flat ground: and a number around 5660. The user LEDs light up on each ramp and go dark on the flat part between them.
  3. Your turkey still falls off the top. You'll fix that soon!

34 of 51

34

PART 2

Step 7: Count the hills

  • To know it reached the top, your turkey needs to count the hills it climbs.
  • Make a variable hills = 0 before the race.
  • While climbing, add 1 to hills and print the count.
  • Predict: when your turkey reaches the top, how many hills will it have counted?

35 of 51

35

PART 2

Step 7: Count the hills

Add the highlighted lines:

New and changed lines are highlighted. The numbers on the left are line numbers.

Also add:

Line 17: hills = 0

Delete or change these lines:

print("Flat ground:", flat)

# Feel the hill

36 of 51

36

PART 2

Step 7: Test your code

TEST YOUR CODE (WATCH CLOSELY!)

  1. Press RESET, then click Run. Watch the console.
  2. But wait... there is a bug! It counts Hill 1, Hill 2, Hill 3... hundreds of hills!
  3. Why? The loop runs about 100 times every second. Every time through the loop while climbing, it adds 1.

37 of 51

37

PART 2

Step 8: Count each hill only once

  • Step 7 counted every loop. Turn that bug into a tool: count loops in a variable named climb.
  • While CodeBot is tilted, add 1 to climb. Don't count the hill yet!
  • When CodeBot levels out, check: if climb is more than 50, that was a real hill.
  • Move your hills and print lines here, inside the else. Then set climb back to 0.
  • Why 50? 50 loops is about half a second.
  • A real hill takes seconds to climb, but a quick bump or the jolt of starting only tilts CodeBot for a moment.

Try it first: write this yourself before you look at the next slide!

38 of 51

38

PART 2

Step 8: Count each hill only once

Your code could look like this:

New and changed lines are highlighted.

Also add:

Line 18: climb = 0

Delete or change these lines:

hills = hills + 1

print("Hill", hills, "climbed!")

39 of 51

39

PART 2

Step 8: Test your code

TEST YOUR CODE

  1. Press RESET, then click Run.
  2. The console shows Hill 1 climbed! and then Hill 2 climbed! Just two! Now that's a count you can count on.
  3. Your turkey still tumbles off the top. One more step!

40 of 51

40

PART 2

Step 9: The finish line

  • When hills is 2, your turkey is on the summit. Stop the race!
  • Time it with CodeBot's clock: ticks_ms() counts milliseconds.
  • Import ticks_ms and ticks_diff from time.
  • There are 1000 milliseconds in a second, so divide by 1000.
  • Save the start time right after the motors turn on.
  • At the finish: turn off the motors, find the seconds, print the time, light the LEDs, gobble, and break out of the loop.

41 of 51

41

PART 2

Step 9: The finish line

Add the highlighted lines:

New and changed lines are highlighted.

Also add:

Line 2: from time import sleep, ticks_ms, ticks_diff

Line 22: start = ticks_ms()

Delete or change these lines:

from time import sleep

42 of 51

42

PART 2

Step 9: Test your code

TEST YOUR CODE

  1. Press RESET, then click Run.
  2. Your turkey climbs both hills, stops on the summit, lights up, and gobbles. The console shows FINISH! with your time: about 22 seconds at SPEED 40.
  3. Gobble-tastic! Your turkey is autonomous: it senses the course and decides what to do all by itself.

43 of 51

43

PART 2

Part 2: Think about it

  1. CodeBot has no eyes. How does it know it reached the top of the hill?
  2. Why did your hill counter count hundreds of hills in Step 7? How did climb fix it?
  3. Why does the program save start before the loop, and not inside it?

You can stop here, or continue to Part 3 for Race Day.

44 of 51

44

TURKEY TROT

Part 3: Race Day!

How fast can your turkey trot?

45 of 51

45

PART 3

Tune your turkey

  • Change SPEED or TURN.
  • Change just one at a time, so you know what made the difference.
  • Press RESET before every race.
  • Turkey crashed? Click the square Stop button before you change a number.
  • Run each setting 3 times. CodeBot doesn't move exactly the same every time!
  • Keep SPEED + TURN at 100 or less.
  • Motor power only goes to 100, and bigger numbers stop your program with an error.
  • Record every race on your Race Log.

46 of 51

46

PART 3

Race challenges

Bronze

Finish the race.

Silver

Under 18 seconds.

Gold

Under 15 seconds.

Legend

Can you beat 14 seconds?

Add your best time, SPEED, and TURN to the class leaderboard!

47 of 51

47

PART 3

Think about it

  1. What happened when SPEED got too high? Why?
  2. Did the same settings always give the same time? What does that tell you about testing?
  3. What would you change in your code to make your turkey even faster?

48 of 51

48

WRAP-UP

What's inside your racer

  • Your Turkey Trot program is an autonomous robot racer. It uses:
  • Line sensors, and a threshold chosen from real sensor data
  • A bang-bang controller to steer
  • The accelerometer to feel the hills
  • Counters to count loops and hills
  • CodeBot's clock to time the race
  • Self-driving cars and warehouse robots use these same ideas: read sensors, decide, act, repeat.

49 of 51

49

TURKEY TROT

Extensions

Add even more to your Turkey Trot.

50 of 51

50

EXTENSIONS

Extensions

  • DNF detector: if no sensor sees the line for 1.5 seconds, stop and print DNF (did not finish).
  • Hint: save ticks_ms() when the line disappears.
  • Smarter turns: right now, if your turkey loses the line it drives straight.
  • Change the else to drive straight only when the middle sensor sees the line. Otherwise, keep turning!
  • Split times: print the time when each hill is climbed.
  • Victory song: change the notes in gobble(), or write a new function for the finish.
  • Slowest finisher: what is the lowest SPEED that still makes it up the hill?
  • Smoother steering (proportional control): use all 5 sensors to steer a little or a lot, depending on how far the line is from the middle.

51 of 51

51

Nice job, turkey trotter!

Your turkey trotted all the way to the top of Gobbler Hill.

​

Happy Thanksgiving from Firia Labs!