A Robot Simulator Using VPython

When I was a kid, one of my favorite “big” toys was the Big Trak, a programmable robot that looked like a futuristic tank.  It had a keypad allowing you to enter a program consisting of a series of motion commands such as “forward 10,” “turn left 15,” etc.  The Big Trak only had memory for 16 commands– this was in the early ’80s, remember– and it didn’t have any sensors, so you could only dead reckon, and for a short time at that.  But it was still extremely cool, and I had a blast playing with it.

Now fast forward to today.  Apparently I haven’t changed much.  I have been experimenting with the Scribbler Robot from Parallax.  This is also a programmable robot, with two independent motor-driven wheels (and a third tricycle wheel for stability), plus quite a few sensors, including a color camera.  The robot chassis has a hole in the center for inserting a pen or marker, for tracing the path of the robot as it moves.  Better yet, Georgia Tech and Bryn Mawr College have developed a Python API called Myro for controlling the robot from a computer via a wireless Bluetooth interface.  I think this has the potential to be a great learning tool for students.  Plus it’s fun to play with.  (For the students, I mean.  Really.)

As a segue from experimenting with VPython to working with the robot, I developed a robot simulator module, vturtle.py, using VPython.  The idea is that you can experiment with the robot, without the robot.  Using this module, you can create and manipulate a robot (or robots) with simple commands like the following, and see the effects of those commands in on-screen animation:

>>> turtle = vturtle.Turtle()
>>> turtle.forward(20)
>>> turtle.left(90)

You can download the code at the usual location here.  Following are a few screenshots showing the robot in action, tracing a Koch snowflake:

Example using vturtle to draw a Koch snowflake.

Another view of the robot and its trail.

Note that this simulator is more like the Big Trak than the actual Scribbler in that it does not have any real sensing capability.  Hence the name vturtle.py; this is essentially a better Logo-like turtle graphics module.  But I think it has a lot of advantages over other turtle graphics modules, and most of those advantages are thanks to the VPython developers, not me:

First, and most important, all rendering is in a separate thread, so you can move the turtle around “a line at a time” at the command prompt, without the messiness of closing the event/rendering loop with mainloop() calls or whatever.  Second, it is easy to add other objects to the scene, such as walls of a maze, or obstacles to avoid, etc.  Finally, it looks good.  That is, the VPython graphics incorporate lighting, shading, texturing, etc., with mouse-controlled slewing and zooming of the camera at any time, even while the robot is moving.  This goes a long way toward making students’ efforts feel more polished and less cartoonish.

To wrap up, this is some more example code similar to that used to draw the Koch snowflake above:

from visual import *
import vturtle

# Start with a robot in the center of the table.
robot = vturtle.Robot(obstacles=vturtle.table())

# Draw one side of a Koch snowflake.
def koch(size, level):
    if level == 0:
        koch(size / 3.0, level - 1)
        koch(size / 3.0, level - 1)
        koch(size / 3.0, level - 1)
        koch(size / 3.0, level - 1)

koch(60, 2)
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.