220 likes | 241 Views
IDLE and Myro. Institute for Personal Robots in Education (IPRE) . Starting IDLE and Myro. IDLE is the development environment ( I ntegrated D eve L opment E nvironment) provided with python. With IDLE, you can:
E N D
IDLE and Myro Institute for Personal Robots in Education (IPRE)
Starting IDLE and Myro • IDLE is the development environment (Integrated DeveLopment Environment) provided with python. • With IDLE, you can: • Load, edit and save text files that contain programs. When a text file contains a python program, you should save it with a .py extension (instead of .txt) to indicate that it contains a python program. • Type python statements interactively • When you installed Myro, it installed a “StartPython” icon on your desktop. Double-click this icon to start the IDLE window.
The IDLE Window Notice the three arrow brackets? (>>>) This is the python “prompt”. You can type commands at the prompt and the python interpretor will execute them.
Loading the Myro module Type from myro import * at the prompt. This command loads everything from the Myro module.
Initializing the Robot – Finding your COM port • Once you have the Myro module loaded, you have to tell it which communication port (COM port) your robot is attached to. If you have not yet set up a Bluetooth connection with your robot, do so now. • When you set up the Bluetooth connection, your operating system will tell you what COM port to use. • With windows, you will look for an “Outgoing COM port” number, and will look something like: COM5 • On MacOSX or Linux the COM port may look like this: /dev/tty.COM5 or /dev/rfcomm0
Initializing the Robot – init() or initialize() If you type init() Myro will pop up a window asking for the name of your communications port (COM5 in this example). You may also use the longer version:initialize()
Your first function call! • When you typed init() at the prompt, you were calling a function that has the name init. • A function call is simply the name of a function followed by an open and close parenthesis. • Another example: getName() • Functions can also accept data (called parameters) between the open and close parenthesis. For example, you can give the init function a string containing the name of your communications port like this:init(“COM5”)
Adding a parameter to init() If you include the name of the communications port as a parameter: init(“COM5”) or init(“/dev/rfcomm1”) (depending upon the name of your communications port) Myro will not pop up a window to ask you for the name.
What is your robot's name? • By default, your robots name is “Scribby”. • When you connect to your robot using the init() function call, Myro lists the version number of the software your robot is running, and it's name • You can retrieve your robot's name at any time by calling the getName() function, which returns a string containing your robots current name. • >>> getName()'Scribby'
Changing your robot's name • You can change your robots name by using the setName()function. • Unlike the initialize() and getName() functions, the setName() function requires a parameter. You must include a string that is the new name for your robot:>>>setName(“newName”)>>> • After you change the robot's name, you can use the getName() function again to see the new name:>>>getName()'newName'
Driving your robot around! • Myro includes a function called joyStick() that lets you drive your robot around. When you call the joyStick() function, it opens up a window with a circle. By clicking inside the circle with your mouse, you can move your robot forward, backwards, or direct it to turn left or right.
Driving your robot around! • By clicking near the Forward label, you will cause your robot to move forward at full speed. • An arrow indicates the direction your robot is traveling. • If you move your mouse pointer to the left or right the robot will also turn in that direction while moving forward.
Optional: Using a USB gamepad! • If you have a USB Gamepad connected to your computer, you can use it to direct your robot. • Instead of calling the joyStick() function, try calling the gamepad() function. • In addition to driving the robot around with the four-way controller, your gamepad buttons will make the robot beep, speak, and take pictures!>>>gamepad()>>>
Taking a Picture! • The takePicture() function will take a picture (using the camera on your robot) and return it. • However, IDLE doesn't know how to show a picture, so you have to use the show() function to show the picture. • Try this:>>> p = takePicture()>>> show( p )>>>
Your first Variable! • You did something interesting when you typed:>>> p = takePicture()>>> show( p ) • You created a Variable named p, and you stored a picture in it. • On the second line, you gave the variable (p) to the show() function as a parameter. • A variable is a name (in this case, the letter p) that can point to a value. • The single equal sign (=) is used to point a variable to a value.
More examples with Variables • The print command will print the value that a variable points to. • Try this:>>> myVariable = 5>>> print myVariable5 • You can change what a variable points to by using the single equal sign again.>>> myVariable = 10>>> print myVariable10
Advanced tricks with functions • In this code, we use the variable p to point to the picture that is returned by the takePicture() function:>>> p = takePicture()>>> show( p ) • We do not need to use a variable to point to the picture. Instead, we could do the following trick:>>> show( takePicture() ) • We are calling one function (takePicture) and using the picture that it returns as the parameter to another function (show) all on one line. • Although this is more compact, using a variable to temporarily point at the picture makes the previous example easier to read and understand.
More functions to control your robot • You can make your robot move by using functions! • Many of the following functions take parameters that specify a speed (for the motors) and a length of time (the duration). • The possible range of speeds go from zero to one (0 is stopped, and 1 is full speed). • The duration is measured in seconds. • The forward( speed, duration) function can be used like this: • Move the robot forward at full speed for one second:>>> forward(1,1) • Move the robot forward at half speed for two seconds:>>> forward(0.5, 2)
Making a Program! • You can combine several function calls together into one file to create a Python program. • Use the File->New menu to open a new window in IDLE • Type in a few commands:forward(1,1)turnRight(1,1)forward(1,1)turnRight(1,1) • Now, save the file as dance.py using the File->Save menu command. • You can run these commands by using the Run->Run Module command, or by pressing F5.
Other Functions: • Look at the Myro Reference Manual to learn about other functions you can use. • Here are a few of our favorites: • forward(speed,duration) • backward(speed,duration) • turnLeft(speed,duration) • turnRight(speed,duration) • beep(duration, frequency) – Try a frequency of 440 to start! If the frequency is too low (under ~300) or too high (over ~10,000) you won't be able to hear it.
Some details... • If you have already imported the myro module and initialized your robot in the idle window, you should not need to include the startup commands in your programs:from myro import *init() • However, if you double click on the program file instead of the StartPython icon, you may need to include those commands at the beginning of your program to make it work. • Although you can double-click on any file with a .py extension to start IDLE, you should not! Instead, always open the IDLE window with the StartPython link, and then use the FILE->Open menu to open your code.