Presentation is loading. Please wait.

Presentation is loading. Please wait.

Intro to Robots Lab 2. Intro to Robots Defining Functions: Define a function and watch its behaviour A useful function is wait(SECONDS) that causes the.

Similar presentations


Presentation on theme: "Intro to Robots Lab 2. Intro to Robots Defining Functions: Define a function and watch its behaviour A useful function is wait(SECONDS) that causes the."— Presentation transcript:

1 Intro to Robots Lab 2

2 Intro to Robots Defining Functions: Define a function and watch its behaviour A useful function is wait(SECONDS) that causes the robot to pause and do nothing. >>> def yoyo(): forward(1) backward(1) stop() >>> yoyo() >>> def yoyo(): forward(1) wait(1) backward(1) wait(1) stop() >>> yoyo()

3 Intro to Robots Defining Functions: A function with parameters is more useful than one without. A function with two parameters is more useful than one with only one parameter. >>> def yoyo1(speed): forward(speed) wait(1) backward(speed) wait(1) stop() >>> yoyo1() >>> def yoyo2(speed,waitTime): forward(speed) wait(waitTime) backward(speed) wait(waitTime) stop() >>> yoyo2()

4 Intro to Robots Exercise: Write a program that will make the robot yoyo() in all four directions. Mark the spot where the robot starts and finally stops. See how close these are to one another. Take two robots and try to make one the mirror image of the other doing the same exercise.

5 Intro to Robots Saving Programs: From IDLE open a New Window and enter the text: # File: moves.py # Purpose: A couple of useful robot commands to try out as a module # First import myro and connect to the robot # [these two lines may not be necessary] from myro import * initialize("comX") # Define the new functions... def yoyo(speed, waitTime): forward(speed) wait(waitTime) backward(speed) wait(waitTime) stop() def wiggle(speed, waitTime): rotate(-speed) wait(waitTime) rotate(speed) wait(waitTime) stop() The correct com port number is on the label of the Bluetooth/Serial adapter

6 Intro to Robots Saving Programs: Next save this in a file called moves.py. Put the file in the same directory as the Start Python icon. (This might be C:\Python24)

7 Intro to Robots Loading and Running a Program: Next load the moves.py module you just saved.

8 Intro to Robots Practicing: Now use the yoyo() and wiggle() functions with different arguments to make the robot do something. Make your activity into a function called dance(). Did you parameterize dance() or not? What would be a good parameter for dance()?

9 Intro to Robots A Word on Function Syntax: The following two are valid syntaxes. The following is invalid. def yoyo(speed, waitTime): forward(speed) wait(waitTime) backward(speed) wait(waitTime) stop() def yoyo(speed, waitTime): forward(speed); wait(waitTime) backward(speed); wait(waitTime) stop() def yoyo(speed, waitTime): forward(speed) wait(waitTime) backward(speed) wait(waitTime) stop() So either a \newline character or a ; can end a statement can’t have different indentations without a control structure like if:


Download ppt "Intro to Robots Lab 2. Intro to Robots Defining Functions: Define a function and watch its behaviour A useful function is wait(SECONDS) that causes the."

Similar presentations


Ads by Google