Interactive Programming: Python & Robots

About
Syllabus
Announcements
Reflections
From Tortoise to Finch
Meet Spot
Spy Pup
Robots at Work
The DARPA Robotics Challenge
Silicon-based Comedy
Music
Finch Software
Finch Hardware
Birdbrain Lessons
Turtle Lessons

Related Links

Announcements

November 17, 2018

Expo today!

Parents will be invited into the classroom for the last 15 minutes of class so you can show them what you've been working on.

What Next?

If you have access to a finch after the course is over, you could continue with the Birdbrain lessons. Want to use python classes with the finch? Here's an idea for a canary module with a Canary class that inherits from the Finch class.

In the winter session, there will be a course called Microcontrollers in Action: Programming & Electronics for students in grades 6 to 8. This course will involve work with Raspberry Pi or Arduino, and looks like a good follow up for this course. Contact CTD for further details.

Try out TunePad, a tool for creating music using the Python programming language. You don't need to be a musician to create music with TunePad—you just need an interest in coding and music!

For lessons focused on improving your Python skills, see Codecademy's Learn Python 3 course.

Another great Python skill-building site is CodingBat.com.

November 10, 2018

Python and Robots Progress Survey

Take this survey to let us know how you feel about your progress and what you want to work on next. We'll try to accommodate people the best we can given the limited number of robots and student expectations. Remember, many teams of advanced programmers worked for years on robots to make it to the finals of the DARPA challenge just to have them fall over and quit before finishing. Your primary goal should be to learn as much as you can about programming and robotics, not to achieve a particular outcome with the finch.

If you finish the survey before everybody else is done, you can work on any of the turtle lessons that you haven't completed yet.

Kahoot

After the survey, we will play Kahoot! for a bit and then back to finch work. I'll use the survey as a guide to try to everybody taking the next steps that will work best for them.

Turtle vs. Finch

Now that you have experience with both the turtle and the finch, we can have a discussion comparing the two. We'll hold our discussion at break time.

Turtle changes position inside a loop and stops when some particular change in location or user event occurs.

Finch wheels given a certain velocity and continues at that velocity until it gets certain input from sensors or a user event.

Why are these different? What is the difference between the finch and the screen turtle? The finch and the early mechanical turtle?

Random turn

The range of valid inputs for wheels is -1 to 1, for both left and right wheels. You can get a number in the range of -1 to 1 like this:


from finch import *
import random

f = Finch()
left = random.random() * 2 - 1
right = random.random() * 2 - 1

f.wheels(left, right)

November 3, 2018

Panicky Turtle

The code below makes the turtle panic when it hits the right wall of the screen.


from turtle import *
import time                 # for sleep()
import random

t = Turtle()
t.penup()
t.turtlesize(5,5)
rightmost = window_width() / 2

# Switch between red and black as long as the turtle
# can move forward without going off the screen
canMove = True
while(canMove):
    t.color("red")
    t.fd(5)
    time.sleep(0.1)
    t.color("black")
    t.fd(5)
    time.sleep(0.1)

    # update canMove variable
    canMove = t.xcor() < rightmost
    

# When the turtle hits the right wall, sound the alarm using color changes and movement!
t.back(40)
for i in range(10):
    t.color("green")
    randomAngle = random.randint(0, 90) - 45
    t.left(randomAngle)
    time.sleep(0.1)
    t.color("blue")
    randomAngle = random.randint(0, 90) - 45
    t.left(randomAngle)
    time.sleep(0.1)

How might you apply ideas from this code to complete Exercise 2 of Obstacles: Finch Security System? How could you make the finch's led light blink? What condition must be met for the finch to start sounding the alarm? How do you make the finch move, turn, and make noise?

After Lesson 8

Depending on your prior programming experience, some of you will need a couple more classes to finish up Lesson 8. Others will be ready to move ahead. Below are some code snippets and ideas to help roboticists who are done with Lesson 8 and want to tackle further lessons.

Lesson 9 - Functions with the Finch

If you haven't written functions yet, or would like to read more about them, see Python Functions.

Lesson 10 - Strings with Finch

Try out the snippet below. What does it do?


word = 'oxygen'
for char in word:
    print(char)

In the Thonny shell, try running the following statements:


>>> word = 'oxygen'
>>> 'y' in word
>>> 'z' in word

Try some other characters besides 'y' and 'z'. When does the operator "in" return True? When does it return False?

For more on strings, see Python Strings.

Lesson 11 — Lists with the Finch

Lists are collections of items. The items can be strings, numbers, boolean values, or other objects. Here are examples of lists:


>>> words = ['oxygen', 'hydrogen', 'nitrogen']
>>> primes = [2, 3, 5, 7, 11]

You can use in and for with lists the way you use them with strings:


>>> 'oxygen' in words
>>>  for prime in primes:
         print(prime)

You can create an empty list by using the left and right square brackets with nothing in between them:


>>> empty_list = []

You can also add items to a list with the append method. For more details, see Python List append() Method.

For more on lists, see Python Lists.

Files and Dictionaries

Lesson 12 requires some understanding of file handling. Help is available on the following web pages:

For Lesson 13, you should know something about Python Dictionaries.

October 27, 2018

I saw some really cool programs that people wrote last week, including some great work with finch movement, finch sounds and turtle graphics.

A couple of people have started working with sensors.

To start off class today, we'll discuss questions I got from last week's reflections and programming ideas that will help with our upcoming pair programming.

while loops

You may have come across the code in the Python turtle documentation. We'll discuss how this code works and take a closer look at while loops and for loops.


# https://docs.python.org/3.2/library/turtle.html
from turtle import *
color('red', 'yellow')
begin_fill()
while True:
    forward(200)
    left(170)
    if abs(pos()) < 1:
        break
end_fill()
done()

We'll start by looking a simpler while True example, then consider a while loop with a condition

Using a random number generator

The code below uses a random number generator to change the color of the turtle.


from turtle import *
import random
import time

s = Screen()
t = Turtle()
s.colormode(255)

for i in range(10):
    r = random.randint(0, 255)
    g = random.randint(0, 255)
    b = random.randint(0, 255)
    t.color(r,g,b)
    time.sleep(1)
    
t.color(0,0,0)

How could you write this code using a while loop? How would you modify this code to randomly change the color of the LED on the finch?

Below is a program that Ari wrote that a random number generator to control the finch.


from finch import Finch
import time
import random
finch = Finch()
for i in range(9 ** 169):
    ## constantly randomized variables:
    red = random.randint(0,255)
    green = random.randint(0,255)
    blue = random.randint(0,255)
    right = random.randint(-75,100)
    left = random.randint(-75,100)
    ## looped code
    finch.led(red,green,blue) ## Disco light
    right /= 100 ## Dividing right # by 100
    left /= 100 ## Dividing left # by 100
    finch.wheels(right,left) ## Wheel mover
    time.sleep(0.25) ## Waiting 1 fourth of a second

Obstacle Sensors

For our pair programming time today, we'll work on Lesson 8 — Obstacle Sensors. You must complete exercises 1 through 3 before you start on the maze. Depending on time, we might not get to the maze (exercise 4 and extensions) until next week.

October 20, 2018

Sound

Jasmine got her Finch to sing Yankee Doodle Dandy!

Below is code to play the scales. You might want to modify it to create your own compositions!


from finch import *

LOW_C = 523
D = 587
E = 659
F = 698
G = 784
A = 880
B = 988
HI_C = 1047

frequencies = [LOW_C, D, E, F, G, A, B, HI_C]

finch = Finch()
duration = 1

for frequency in frequencies:
    finch.buzzer_with_delay(duration, frequency)

The program above uses a list to keep track of frequencies. See Lesson 13 if you're interested in using a dictionary to match keys with notes.

Compress

Kupu and Neel did a nice job of compressing their maze folders.

if, elif, and else

Connor has come up with a couple of cool strategies for interactive steering. Here's one that works for both the turtle and the finch:


from turtle import *
import sys

ezra = Turtle()

print("Press f for forward, b for back, l for left, or r for right")
def go():
  move = input("f, b, l, r, or q to quit: ")
  if move == 'f':
  	ezra.fd(10)
  elif move == 'b':
  	ezra.bk(10)
  elif move == 'l':
  	ezra.lt(90)
  elif move == 'r':
  	ezra.rt(90)
  elif move == 'q':
      bye()
      return
  else:
    pass
  go()

go()
	  

How could you adapt this code to work with the finch?

onkeypress()

Below is a code fragment based on work that Connor did last week. What does it do?


import turtle

speed = 20 #How many pixels to move everytime you click
wn = turtle.Screen()
pr = turtle.Turtle()

def up():
    y = pr.ycor()
    y += speed
    pr.sety(y)

wn.listen()
wn.onkeypress(up, "w")

What's the advantage of this strategy? Could you use this strategy with the finch?

However, in lesson 14, you'll learn how to use the tkinter module with the finch. Here's a short program that uses tkinter that will give you an idea of what you can do with it:


from tkinter import *

root = Tk()

def key(event):
    print("pressed", repr(event.char))
    if event.char == 'q':
        root.destroy()

def callback(event):
    root.focus_set()
    print("clicked at", event.x, event.y)

root.bind("<Key>", key)
root.bind("<Button-1>", callback)

Could this key binding be used to steer the finch? Could the turtle screen's onkeypress method be used to steer the finch?

October 13, 2018

Individual Work Period

To start the day, half the class will work on the next Finch lesson, and half will work on Escaping the Maze with the turtle. Then we'll switch, so everybody gets Finch time and Turtle time.

If you and one other person would like to team up during this time, that's fine. I can also do a random assignment of partners for people who want a random partner assigned to them.

Why do so many robots look like animals and humans?

Robots have been made to mimic all kinds of animal life, including humans, insects, birds, reptiles, mammals, fish, and even an octopus. Why do so many engineers use animals as the basis for their robot designs? We'll discuss this question and look at a short video about a robotic wild dog.

October 6, 2018

Virtual and Physical Robots

We'll watch a video about robots and discuss the difference between working with virtual and physical robots.

Pair Programming

I'll use a Python program to pick random pairs for pair programming. During the pair programming session, you'll work through the first finch lesson from Birdbrain.

Virtual and Physical Robots

True or False?

We'll take a break by playing a game to get to know each other. You'll take turns telling us one statement about yourself that's true, and one that's false. The rest of us has to guess which statement is true.

September 29, 2018

Welcome to Interactive Programming: Python & Robots! Here's what we'll do today:

  • Complete this background survey.
  • Get to know each other.
  • Get lessons as needed on using Python to:
    • Create and run a program
    • Import a library
    • Print text to the screen
    • Accept user input and store it to a variable
  • Learn some important features of the finch robot.
  • Learn about pair programming.
  • Use pair programming to program a finch's movement.
  • Write a short reflection and sending it to tj@tjleone.com (I'll explain reflections later today).

In Between Times

Are you a complete Python newbie? Work through the text and links on the Finch Software page and then A Visual Introduction to Python to learn some Python basics.

If you're already familiar with Python, skim the the Finch Software page to familiarize yourself with Thonny and then go to Python Turtle Graphics chapter of How to Think Like a Computer Scientist

I sometimes make videos available on class web pages. To watch the videos, you can ask for headphones or use your own headphones or earbuds if you brought them.

Prepare for your first Finch lesson

Download the Finch Python software. Unzip the FinchPython120.zip file and open the FinchPython120 folder.

After pairs have been assigned, decide with your partner which mac you'll attach to your Finch to begin the first lesson. Hook up the Finch to the USB port on your mac. You should see the LED cycle through colors.

From the FinchPython120 folder, open dance.py with Thonny. Run the dance.py program to see your Finch dance.

Go to Birdbrain Lesson 1. Run the code shown to get the Finch to go forward a bit and then stop. Switch navigator and driver roles for Exercise 1 and for each exercise after that.

At Home

Here are some things you might want to do at home:


This web page was created to supplement a course offered by The Center for Talent Development. CTD This work is licensed under a Creative Commons Attribution 3.0 Unported License. Creative Commons License Terms of Use. Privacy Policy.