Tuesday, February 2, 2010

GvR:Teaching your robot new tricks

We will look at the same topic here that is addressed in Tutorial 5 of the GvR lessons. If you've worked through the lessons this will be review. If not you may want to look at that after you've read this.

As we saw in the previous post on GvR the robot is limited to moving forward and turning left. This may seem like an artificial limitation but I'll give you an example of a simple robot, built with Lego Mindstorms that had the same limitations. The mind storms kit came with two motors. If you wanted a mobile robot that did more than move you had to accomplish the movement with one motor. Working from plans published online and in some Mindstorms books I had, I built a robot that was similar in function to GvR. One motor was used to move the robot and the right (or left) wheel was setup with a "ratchet gear". Basically a piece was used to keep the gear from moving backward. So when the motor ran backward the ratcheted wheel couldn't turn. The result was the robot moved forward and turned when the motor ran backward. Turning a particular direction was a the result of running the motor backward for a certain amount of time.

While GvR has a limited number of commands it does have a define command. The define command allows you to create new commands for GvR by stringing together combinations of the basic commands and those that you have defined. We will define a few new commands, including the one in Tutorial 5, and then put some of them to work.

First things first we will create a simple world file that places GvR in center of the bottom row of the map. (Note: this assumes the default window when you start GvR.) Enter the following into the world editor and save it as bottomcenter.wld:

robot 5 1 N 0

The next thing  we'll do is define the turnright command as done in tutorial 5.

define turnright:
    turnleft
    turnleft
    turnleft

Enter this into the code window. If you were to click the Execute button at this point you would get an out of instructions error with no apparent action. The system behaves the same as when there is no code in the Code Window. So let's have the robot move in a zig zag pattern. We'll incorporate the looping that we did in the last entry and have the robot move ahead a few spaces on the map. Add the following below the define statement in the code window:

do 2:
    turnleft
    move
    turnright
    move

do 2:
    turnright
    move
    turnleft
    move
turnoff

Click the reload button and then the Execute button and watch what happens. The end result is the robot 4 spaces up from where it started.facing north. Let's add another command. Add the following below the last turnleft in the definition of turnright and above the first do 2: in the command group.


define turnaround:
    turnleft
    turnleft


This command will turn the robot around so it can reverse direction. You may want to save the program at this time. Give a name you'll remember. If we Reload and Execute now nothing will have changed. We have defined the new command but we haven't used it. So add the following lines before the turnoff command:

turnaround
do 4:
    move
turnaround


Save your file again (use a different name if you want to preserve the previous program). Click Reload and the click Execute. Hopefully GvR ends up where he started. Remember that if the robot is moving to fast you can select "Set Speed" from the Setup menu and slow it down. If you want to examine what it does step by step use the Step button instead of the Execute button.

We'll define one more new command that will include one of our other new commands, to show that once you define a new command you can use it anywhere you use a built in command. Then we'll add it into our program just to make sure it works.

The new define looks like this:

define backup:
    turnaround
    move
    turnaround


It should be added to the code window after the turnaround define before the movement commands. It is important that it come after the definition of turnaround or it will generate an error.

Once you've done that add the following before the first turnaround command in the existing code.

move
backup

Save the program, Reload and Execute. 

Your final program will look like this:
define turnright:
    turnleft
    turnleft
    turnleft

define turnaround:
    turnleft
    turnleft

define backup:
    turnaround
    move
    turnaround

do 2:
    turnleft
    move
    turnright
    move

do 2:
    turnright
    move
    turnleft
    move

move
backup

turnaround
do 4:
    move
turnaround

turnoff

You might want to save a copy of the program that includes only the defines. This can be useful for adding those defines to other programs. Remember that once you close GvR or reload with a different program, those defined commands will be forgotten.

Experiment with your new commands and maybe add some more. More next time.

No comments:

Post a Comment