Saturday, January 16, 2010

Guido van Robot: Getting Started

Our first software "toy" is Guido van Robot (aka GvR). As with many of the things we'll look out on this blog let's start with a little history.

GvR is a Python implementation of Karel the Robot. Karel the Robot was originally created in 1981 by Richard Pattis to help teach students fundamental programming concepts and start them on the way to learning the Pascal programming language. The Karel concept has been adapted to many languages including C/C++, Java and our language of choice, python. To learn more about GvR's history visit the History section of the GvR website.

To learn more about the Python programming visit its home on the web at python.org. One of Python's strengths is that it has a simple syntax that makes it easy to learn. GvR implements the Karel concept using a Python like syntax. So after some time with GvR you can start to look at other tutorials on Python and have a leg up. In a later entry I'll provide some additional python resources for further exploration.

Getting GvR
You can get GvR via the download page on the GvR site. The GvR project is hosted on the open source project site sourceforge.net. Follow the link to Download GvRng. If you're using Microsoft Windows there is an installer exe file that has everything needed, including Python. If you are using Linux, the GvR site tells you that you'll need Python and PyGTK. You'll also need the python-gtksourceview package from your distribution. Once you get it installed start it up.

Building your first world
I encourage you to work with the lessons on the GvR site, which are adaptations of the original Karel exercises. We're going to combine some of the pieces in those lessons for our first set of programs.  So here comes your first bit of type in code. 

pwpTurns.wld (type into World Editor)

robot 7 3 N 0
wall 3 7 W
wall 7 7 N
wall 7 3 E
wall 3 2 N



The above code  should be typed into the World Editor tab of the GvR editor. The text is colorized to match what you'll see in GvR. The world editor sets up the map that your robot will move around on. This map consists of 4 "wall" panels and the GvR robot. The map is made up of rows and columns with the robot being placed at an intersection. (In this case the intersection of column 7 and row 3.) The direction of the robot is indicates which way it is facing. (N=up, S=Down, E=Right, W=Left) Walls start are placed on the side of the intersection indicated by the direction. We're going to get the robot to move around a little. When we're done we'll have a program that get's our robot around the map in 5 lines of code.

Step 1: Moving GvR
The first thing we're going to do is move the robot to the first red wall.

Enter the following in to the Code Editor:
move
move
move
move
turnoff

Press the Reload button
Press the Execute button

Well that was easy. The robot moves to the wall and stops. The turnoff instructions gives you a dialog box informing you that the robot has turned off.

Step 2: Getting Loopy 1
That was easy but what if we could move 4 spaces, or any number of spaces in only 2 lines of code? That would make it a lot easier. So let's change our program to what follows:

do 4:
    move
turnoff

Once you've got this in the code editor click Reload and Execute.

The result should be the same but in only 3 lines instead of the 5 lines of the first program. That's a 40% reduction in lines of code. Loops are really useful. We'll look at a different variety of loop in a couple of steps.

Speaking of steps now would be a good time to try the Step option of GvR. Reload your program and instead of pressing Execute press Step. Continue to press Step until you reach the end of the program. Stepping through a program can be useful when the program doesn't do what you want. You can watch what happens as you step through each command. As you experiment with GvR the step option will be a good way to check your programs.

Step 3: Turning GvR
The GvR robot wouldn't be much use if it could only go straight. So it has an instruction turnleft, that allows you to turn the robot. In keeping with the Karel concept of simplicity, the robot only moves forward and only turns left. While this may seem limiting it is all that is needed to get the robot anywhere you need it to go. Add the line turnleft before the turnoff command and then Reload and Execute. The program should look like this:

do 4:
    move
turnleft 
turnoff

Step 4: Going in Circles: Nested loops
One of the things you'll notice is that the move command in the loop is indented. This is a python syntax rule. So let's add an outer loop that will execute our current program 4 times. Change your program to look like what's below.

do 4:
    do 4:
        move
    turnleft 
turnoff

Click Reload and Execute. Now we have a program that does what we set out to do. Our robot goes around in a square.  One last change to the program will give you a springboard to further exploration.

Step 5: Making decisions
Our last step is to replace the inner do 4: loop with a new instruction while. The while instruction will continue to loop while the condition provided is true. In this case we will use one of the conditions that GvR understands front_is_clear. Change your program to look like this:

do 4:
    while front_is_clear:
        move
    turnleft
turnoff

What our program is going to do is move forward until it finds a wall. It will then turn left and do it again, a total of 4 times. This program could be used with a different world with the walls set at other locations as long as the path is 4 legs long.

Experiment on your own. Next time will explore the GvR concept of "beepers" and conditional statements.  

No comments:

Post a Comment