You'll be writing two kinds of Python programs this semester:
Now start pyrobot on your machine and duplicate what you saw in the
first video, running the avoid.py brain. Rather than
exiting Pyro when you finish duplicating the demo in the video, type
dir(robot)into the line marked
Command: at the bottom of the screen
and press enter. Robot here is a Pyro object,
and Pyro's dir operation returns a list of the
methods understood by an object. We'll learn about objects
and methods in a couple of weeks.
Try some of the robot methods to see what they do. For example
robot.move(1, 0.5)turns on the motors so that the robot moves forward at speed 1 and rotates at a rate of 0.5. (We'll worry about the units later.) With the motors turning like this, the simulated robot will eventually jam up against a wall or an obstacle, and the motors will keep turning, which you can verify by dragging the robot with the mouse and letting it go. To stop the robot, do
robot.move(0,0)Also try
robot.range.distances()which shows a list of the distances registered by the eight sonars. You can experiment with more of the robot methods now, or you can view the second Pyro video that explains some of these. Or you can just wait for the first lab.
esc key, then pressing ?, then
pressing the letter t. Work through the tutorial,
through the section on files. You may want to just skim this now and
come back to it later, but at least get a sense for what's there.
Something not mentioned in the tutorial's "basic cursor control"
section is that you can move the cursor by using the left, right, up
down arrow keys, just as you might expect, as an alternative to the
C-b, C-f, C-p, C-n
keys described in the tutorial. You can also move the cursor to a new
position by clicking with the mouse. You can also mark a section of
text by dragging over it with the mouse, rather than using the marking
commands described in the tutorial section on inserting and deleting.
On Windows or the Mac, pressing delete will delete the marked region.
On GNU/Linux, use C-w to delete the region as described
in the tutorial.
C-X C-F command. Give the file a name with extension
.py, for example mytest.py. This should
give you a blank window. The bar at the bottom should say
(Python) to indicate that you are in Python mode.
What you should do when entering Python mode is
to type C-c ! to start the python interpreter. This will split the
window into two buffers, with your program on top and the Python
interpreter on the bottom. Note that your blinking cursor will be in
the Python interpreter window. Use C-x o to return to the buffer
with your program, and then type C-c C-c after typing
some Python code.. This last Emacs command
will execute the buffer with your program, effectively defining the
procedure myProc.
Define a procedure that takes numeric input x and returns the product of x and x+1 and x+2:
def myProc(x):
return x*(x+1)*(x+2)
Notice that when you go to the second line (by typing
enter) the line automatically indents to the correct
position for Python code. You should also notice that when you type
the two close parentheses, the cursor briefly flashes to the matching
open parenthesis. These are some of the ways that Emacs assists you
in writing Python code.
Now type C-c C-c (i.e., type control-C twice). This will
evaluate your program and split the window into two buffers, with
your program on top and the Python interpreter on the bottom. In the interpreter
buffer, you ought to be able to evaluate
>>> myProc(100)and see the result. You can also go back to the
mytest.py
buffer (by clicking with the mouse or typing C-X o, edit
your procedure so that is does something else, or define a new
procedure, and run your code again.
That's the basic operation of working with Emacs and Python.
key binding --- ------- C-c C-c execute buffer C-c ! go to the Python shell C-c | execute region ESC C-x execute def or class C-c C-n move to next statement C-c C-p move to previous statement ESC C-e move to end of def or class ESC C-a move to beginning of def or class ESC C-h mark def or class as region C-c # comment region C-U C-c # uncomment region C-c ? show help for Python modeThere are also a bunch more Emacs Python commands, which you can read about by typing
C-c ?.
C-c C-c and do not have the python interpreter
running, in a window, confusing things happen.
Notice that when you start the second line of your procedure
definition (by typing enter) the line automatically
indents to the correct position for Python code. (If it does not,
perhaps you forgot the colon at the end of "def myProc(x):"). You
should also notice that when you type the two close parentheses, the
cursor briefly flashes to the matching open parenthesis. These are
some of the ways that Emacs assists you in writing Python code.