CS81 Lab2: Building a simulator

Due by noon next Friday

As you saw in the last lab, working with physical robots in the real world is challenging. There are many issues that you must deal with such as maintaining battery power and handling the variability of sensors. In order to explore adaptive robots, we will run experiments that last much longer than the battery life of a robot and we want to avoid damaging the robot as it explores how it is capable of moving in the world. Therefore it is common practice in the field to explore adaptive control mechanisms for robots in simulation first, and then, once they prove successful, port them to physical robots.

In this lab we will create our own basic simulator from scratch. A key feature of our simulator is that it will allow us to run it with the graphics turned off, significantly speeding up the computation time. Constructing it in this way will make it a bit more difficult to implement, but will be extremely useful when we run evolution experiments later.


Git started

Go through the following steps to setup your directory for this lab.

  1. First you need to run setup81 to create a git repository for the lab.

    If you want to work alone do:

    setup81 labs/02 none
    If you want to work with a partner, then one of you needs to run the following while the other one waits until it finishes.
    setup81 labs/02 partnerUsername
    Once the script finishes, the other partner should run it on their account.

  2. For the next step only one partner should copy over the starting point code.
    cd ~/cs81/labs/02
    cp -r ~meeden/public/cs81/labs/02/* ./
    
    This will copy over two files called testGraphics.py and simulator.py.

  3. Whether you are working alone or with a partner, you should now add all of the files to your git repo, commit, and push them as shown below.
    git add *
    git commit -m "lab2 start"
    git push
    

  4. If you are working with a partner, your partner can now pull the changes in.
    cd ~/cs81/labs/02
    git pull
    

Overview of the simulator

We will write our simulator in Python using the Zelle graphics library, which was introduced in cs21. If you didn't take cs21 or need to refresh your memory, take a few minutes to explore this graphics library. Open the file testGraphics.py, look over the code, and then execute it. Try adding another circle of a different size and color to the program. Next try adding a line. Once you understand the basics of the graphics library you can move on to building the simulator.

Open the file simulator.py; you'll notice that I have provided you with starting point code for several classes. I have stubbed in some methods for you, which you should implement. Feel free to include additional helper methods as needed. A short description of each class is provided below along with a summary of the class variables you will need.


Incremental implementation and testing

You should develop your code incrementally. Start by ignoring the graphical aspects of the simulator. Instead focus on getting the internal mechanisms functioning correctly.

Begin by implementing the constructor of the World class, but leave the other methods for later. Even though the World class is not complete, create a main program to test that your constructor works correctly and that you can print a world's width and height.

Next, work on the Agent class, starting with its constructor and then focusing on the methods that move the robot in the world (update, translate, rotate). Check out the section about geometry to help you implement translate. Also implement the getStatus method to get a summary of an agent's x, y, and heading. Update your main program to test that your movement commands are updating the robot's location and heading correctly. Don't worry about keeping your robot in the world or calculating sensor values yet.

Then, work on the VisibleAgent class and go back and finish the World class. Seeing a graphical representation of your robot will make it easier to debug.

Go back and complete the rest of the Agent class, including the checkStall and computeDistanceToWall methods.

Once your simulator is fully implemented build some additional brains. Create a brain that causes the agent to move in a spiral motion. Create a brain that uses the distance to a wall, rather than stall, to avoid walls.

Enhancements: If you'd like (this is not required), implement aditional functionality into your simulator. Currently, the simulator does not detect when two agents are in contact, allowing agents to pass through each other. Add the ability to detect collisions between agents, having the stall sensor be triggered.


Geometry

In order to simulate robots we'll need to review some geometry. In the picture shown below, the agent is positioned at location (100, 120) with a heading of -45. A ray has been drawn from the center of the agent's body at the angle of the its current heading.

The unit vector v for this ray can be computed as:

v = ( cos(radians(heading)), sin(radians(heading)) )
We can use this unit vector to solve key aspects of our simulation:
Submit

When you are completely done, be sure that you have pushed all of your changes to the repo. Use the git status command to verify this. If there are uncommitted changes, be sure to do another add, commit, and push cycle.