Download presentation
Presentation is loading. Please wait.
1
Intro to Robots Lab 8
2
Intro to Robots Exercise 1: Follow the text’s suggestion and Google “color names list” Collect from the list a set of 25 colors or so and in a new Python program put them in a list data structure. Write code that creates a graphics window and then loops through the elements of the colors list, changing the color of the graphics window background. colors = [‘DarkCyan’, ‘DarkGoldenRod’, …]
3
Intro to Robots Exercise 2: Follow up on Exercise 1 by doing the following –Add a point on the graphics window each time you change the background color. –Use some kind of systematic method of assigning the point coordinates like (i,i) and increment i each time you change the background color. The outcome will be something like:
4
Intro to Robots Exercise 3: In class we saw the code that draws the following picture. Extend this code to draw the following picture. Hint 1: Figure out what the four corner point coordinates will be. Hint 2: Use 4 loops
5
Intro to Robots Exercise 4: Add to the program in Exercise 3 by including the code to the loops that control the drawings of the lines. What behaviour do you get? wait(0.3) L.undraw()
6
Intro to Robots Exercise 5: Consider the Bouncing Circle program # Moving circle; Animate a circle... from myro import * from random import * def main(): # create and draw the graphics window winWidth = winHeight = 500 w = GraphWin("Bouncing Circle", winWidth, winHeight) w.setBackground("white") # Create a red circle radius = 25 c = Circle(Point(53, 250), radius) c.setFill("red") c.draw(w) # Animate it dx = dy = 3 main() while timeRemaining(15): # move the circle c.move(dx, dy) # make sure it is within bounds center = c.getCenter() cx, cy = center.getX(), center.getY() if (cx+radius >= winWidth) or (cx-radius <= 0): dx = -dx if (cy+radius >= winHeight) or (cy-radius <= 0): dy = -dy wait(0.01)
7
Intro to Robots Part 1: Modify the previous program so that each time the Circle is moved it leaves a Point at its previous center.
8
Intro to Robots Exercise 5, Part 2: After you draw the Circle, draw a rectangle in the middle of the Bouncing Circle window and color it yellow: Use the coordinates (200,100) and (300,400) Now rerun the program and notice that the ball moves behind the rectangle. R = (Rectangle(Point1,Point2)) # Point1 is the top left-hand corner # Point2 is the bottom right-hand corner (200,100) (300,400)
9
Intro to Robots Exercise 5 (cont): Modify the program so that the ball will bounce off the rectangle just like it bounces off the edge of the graphics window. Initially there are four situations to consider:
10
Intro to Robots Case 1: If the ball approaches the rectangle side whose coordinates are given below then the test for the program variables is: (200,100) (200,400) cx + radius >= 200 and cx – radius <= 300 and cy between 100 and 400 program code: ( (cx + radius >= 200) and (cx – radius = 100) and (cy <= 400)) (300,400) (300,100)
11
Intro to Robots Cases 2, 3 and 4: Evaluate the same expressions for the following three cases: Put this code into the original program and watch the circle move around the window.
12
Intro to Robots Exercise 5 (cont): If you implement this code you will see that everything works well except at the corners. In this case we can see that the test conditions fails. What part of it fails? (200,100) (200,400) ( (cx + radius >= 200) and (cx – radius = 100) and (cy <= 400)) fails:
13
Intro to Robots Exercise 5 (cont): In order for the circle to bounce off the yellow rectangle it must be inside the outer rectangle in one of the areas labeled A, B, C, D or 1 … 8. This is not enough however. In the areas labeled 4 and 5 the circle can inside the outer rectangle but still not be touching the inner rectangle. Variables: (cx,cy) - center of circle radius – radius of circle Inside outer rectangle condition: cx + radius >= 200 and cx – radius = 100 and cy – radius = 200 and cx <= 300 and cy <= 100 Inside B, C, D condition: ? Inside 1 condition: inside outer rectangle and not in A, B, C or D and cx <= 200 and cy <= 100 and 200 – cx <= 100 – cy Inside 2.. 8 condition? Inside 1 and touching rectangle condition: inside 1 and (200 – cx) 2 + (cy – 100) 2 < radius 2
14
Intro to Robots Exercise 5 (cont): Modify your program to take these new conditions into account. The pseudocode is: if inside outer rectangle: if inside A: flip dy elif inside B: flip dx elif inside C: flip dy elif inside D: flip dx elif inside 1: if touching inner rectangle: flip dy elif inside 2: if touching inner rectangle: flip dx... ( 6 more cases) Added clear rectangle to see where Circle center is when Circle touches rectangle
15
Intro to Robots Exercise 5 (cont): A bug: Circle starts here entered area 2 are exited correctly entered area 8 and got “stuck” passed through area 8 ok back and forth, back and forth until it exits area 2
16
Intro to Robots Exercise 5, Bug Analysis: Each time we move the Circle we move it 3 pixels on the x-axis and 3 pixels on the y-axis. When we cross the line into the outer rectangle we flip either the x- or y- increment and the next move typically takes us back outside the rectangle. However this is not necessarily true in the corners... bouncing back we are still inside and so flip again; this keeps us inside until the end of the line.......
17
Intro to Robots Exercise 5, Bug Solution: When you enter the outer rectangle and flip either the x- increment or the y-increment you must not flip again until you have at least left the outer rectangle. Our problem here is that once too far inside the outer rectangle we stay there by flipping back and forth. Code Solution: Add a boolean flag to your program that gets turned on whenever you flip and only turned off once you leave the rectangle. Then don’t ever allow a flip to happen if the flag is turned on.
18
Intro to Robots Exercise 5, Bug Solution Pseudocode Exercise: Implement this pseudocode # outside while-loop turn flag off if inside outer rectangle: if flag turned on: continue # skips to top of while-loop if inside A: flip dy turn flag on elif inside B, C, D:... elif inside 1: if touching inner rectangle: # inside circular arc flip dy turn flag on elif inside 2, …, 8:... ( 6 more cases) else: # outside outer rectangle turn flag off
19
Intro to Robots Exercise 5 (one last time) Make the robot beep each time you flip the x- or y- increment. Make it beep differently if it is hitting the outside will than if it is hitting the rectangle.
20
Intro to Robots Exercise 6 (Sound): The human ear can not distinguish between sounds which are very close together. Execute the beep() command over a range of frequency values to determine your audible range. Execute the beep() command over a range of close-by frequency pairs to see how far apart the pairs need to be for you to distinguish the sounds beep(1,300), beep(1,400), beep(1,1000), beep(1,2000), … beep(1, 300), beep(1, 305), … beep(1, 1000), beep(1, 1010), beep(1, 440), beep(1, 441) …
21
Intro to Robots Exercise 7 (siren): A siren is two sounds repeated constantly. Find a good pair for sounds for a siren.
22
Intro to Robots Exercise 8: Musical Scale Data Structure In the past we have used the dictionary data structure to hold data that comes in pairs. Remember the englishToSpanish dictionary in Chap 1? Create a dictionary called notes with notes from 9 different octaves (from A0 to C8). These are the notes Scribbler is able to make. Method 1: Key in by hand. Method 2:: freqs = [‘C’, ‘C#’, D, ‘D#’, ‘E’, ‘F’, ‘F#’, ‘G’, ‘G#’, ‘A’, ‘A#’, ‘B’] notes = {} notes[‘A0’] = 27.5; notes[‘A#0’] = 29.14; notes[‘B0’] = 30.87; notes[‘C8’] = 4186.0 for i in range(7): j = 1 for f in freqs: notes[f+str(i+1)] = (notes[‘B0’])*2**(i+j/12.0) j = j+1
23
Intro to Robots Musical Notes: Google ‘musical note frequencies’ and compare what you find there with the numbers you have created with the code on the previous page.
24
Intro to Robots Exercise 9: Pick a simple song you remember and make your robot play your song. Remember that the robot can only play chords with two notes.
25
Intro to Robots Exercise 10: Working with another team in the lab, play a duet – one robot plays the treble clef and the other the bass clef notes. http://en.wikipedia.org/wiki/Clef
26
Intro to Robots Exercise 11: Since our robot may at times need to express emotion you might find some short tunes that express –Determination –Sadness –Victory –Curiosity –Plodding along –Full steam ahead –Any other emotions that seem appropriate …
27
Intro to Robots Exercise 12: Write a function that will draw a maple leaf. Now cover a graphics window with maple leaves of different sizes (small, medium, large) and colors (red, yellow, orange). def mapleLeaf(anchor point, size, color, window):...
28
Intro to Robots Exercise 13: Draw the following robot using the function: Now use this function to draw the following picture. def drawRobot(anchor point, window):... def drawPyramid(window):...
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.