Lab 1: Picobot

If you find typos or problems with the lab instructions, please let us know.

Learning Goals

After completing this lab, you should be able to do the following:

  • Reason about the behavior of a simulated robot in a simple state-machine language

  • Methodically trace the execution of a simple state-machine program

  • Work with a partner using pair programming

  • Find and fix errors in a simple state-machine program

  • Write a program to control a simulated robot so that it navigates through both an empty world and a maze environment

  • Save and submit your files for feedback

Pair Programming

During SPIS, most of your lab assignments will use Pair Programming. This is a system where two people collaborate on a single solution to a coding problem. When doing pair programming, you will work alongside your partner in real-time.

In this system, you have two different roles, and you will alternate between them. These roles are being the driver and being the navigator. The driver is the one typing, and the navigator provides guidance to the driver as to what to do.

Please take a moment to visit this link and learn more about Pair Programming.

To "learn the do's and don'ts" of pair programming and to see pairs in action, you may also want to check out this entertaining (if a bit corny/cheesy) video about pair programming from North Carolina State University: An Introduction to Pair Programming for Students.

In this lab, you will first do some coding individually. But just as in the previous lab, work in-sync with your partner. Then, after you have each become familiar with the basics of Picobot and have finished the first introductory assignment, you will switch to pair programming for the remainder of the lab.

Individual: Picobot Bouncing Off the Walls

This first part of the lab is individual. However, work in-sync with your partner, and support each other. Make sure you are both finished with this individual part and are both comfortable with Picobot before moving on to the pair programming portion of the lab. Don't rush each other. It is okay to take your time. If you help each other, don't share answers, but explain principles and clear up misconceptions.

Getting Started: The Picobot Simulator

For a resource on Picobot, check out Chapter 1 of the book CS for All (or its PDF version). By the way, this is just one of the chapters of the online textbook we recommend as your go-to resource for your journey throughout SPIS. But if you are eager to get started, and we hope you are, you can focus on this chapter on Picobot first.

Based on the information in this CS for All chapter or what you learned in class, try to answer the following questions for yourself. If you are not sure, feel free to discuss with your partner or a mentor. You don’t need to submit these answers, but make sure you are able to formulate an answer for yourself. Confer with your partner to see if they are thinking the same.

  1. Describe the following rule in English:

1 x**x --> N 2

  1. What is the difference between the "*" and the "x" in the description of Picobot’s surroundings?

  2. Write a single Picobot rule that would cause Picobot to go W and crash into a wall (Picobot will report an error if you try this)

Now, let’s start writing a simple Picobot program. Click here for the Picobot simulator and GET STARTED!

Start out by looking at the starting rules that are given to you when you first get to the simulator site. Can you figure out what Picobot will do when you enter these default rules? Run the code and verify your answer. Is it doing what you expect it to do? If not, make sure you understand why before moving on. It is an important skill not only to solve problems, but to understand exactly why your solution works (or not).

Also, feel free to play around a bit and modify the starter code. This is a great way to get going. By the way, whenever you change the code, make sure to click the Enter rules for Picobot button. Otherwise, the changes are not registered and Picobot will simply execute the previous code, confusing you to no end most likely, as it did for us when we forgot to do this :-)

Let’s Get Coding

As a final task before moving on to the pair-programming parts of this lab, try to create the following program. Again, you can ask your partner or the mentors for advice and help, but make an effort to solve this alone. Of course. don’t stay stuck and don’t be afraid to ask for guidance, but don’t ask for the answer. Ask for help in clearing up any concepts that trouble you, and get to the point where you can solve this problem yourself. We realize Picobot programs are not immediately intuitive, so don’t worry if it takes you some time. This is natural.

The task is to have the Picobot bounce off the walls going east-to-west and back. It should move west first until it hits the wall, then turn around and follow the same track east until it hits the wall, and then turn around and do the same thing again (always staying in the same row). Basically, Picobot should end up pinging back and forth, west to east and back, always using the same row.

Once both you and your partner are done with this and you both feel comfortable with Picobot, move on to the next section where you will work on problems together.

Pair Programming: Solving the Empty Room and Maze

For the remainder of the lab, you will be doing Pair Programming. First, decide on who will be the initial navigator and who will be the initial driver. However, make sure you switch roles frequently throughout this lab.

First Steps: Create a Shared Repl

When working with your pair partner, you will be creating code together. Whoever is the driver should type the code in the Picobot simulator on their web browser and then share their screen in Zoom so their partner can collaborate as the navigator (the navigator does not write their own code). When you switch roles, share the code with each other and now work on the web browser of the new navigator, who should share their screen.

To share the code, we will use GitHub and Repl.it, even though we don't use Repl.it to do the coding itself in this lab. The reason is that it also automatically lets you share the code with your mentor. It is also the same procedure we will be following in subsequent labs anyway. For those subsequent labs, Repl.it will allow you to actually code in the same shared document, so you won't have to do what you will be doing in Picobot: code on one computer and then copy the code to your partner's computer when you switch roles.

To set things up, follow these steps:

  1. First, create a shared repl in Repl.it. The procedure to do this is explained under: Creating a Shared Repl. After you followed this procedure, you should now have a shared repl, named spis22-lab01-Alex-Chris (but with your first names instead of Alex and Chris, in the order listed on the pair-partner list), that is linked to a shared repo on GitHub.

  2. One of the partners should create the text files emptyroom.txt and maze.txt in their repl. You do this by clicking on the icon of a file with a + inside it (it will say "Add file" when you hover over it). Because you are doing this in a shared repl, this change should automatically appear in the repl of the pair partner as well (you may need to refresh the page).

These text files emptyroom.txt and maze.txt will be used to share your code with each other when you switch roles, as a change made by one student will automatically appear for the other student as well. BTW, we will not use the main.py file that is in your repl for this lab.

We will also use these emptyroom.txt and maze.txt files to submit your Picobot code to your mentor. You do this by pushing code to GitHub. Only one of you should do this (because everything is shared).

  • One of you should make a commit of your repl. Do both of you see this commit in the commit history in Repl.it? You may need to refresh the page.

  • Do both of you see this commit on GitHub?

We will be using this workflow throughout SPIS. Make sure it all makes sense and don't hesitate to ask questions.

Program Picobot to Solve the Empty Room

Write a program to make Picobot visit every cell in the empty room, no matter where it starts. This is the Picobot problem you see when you get to the Picobot landing page.

Hints to get you started: You both have a program to sweep the room left to right. Can you come up with a simple change to not always cover the same area, but also move north once you hit a wall? This will get you a good part of the way there, covering (on average) half the room. But you will probably get stuck in a corner at the end. You’ll need to add a rule to get you out of this situation and move you to the yet-uncovered part of the room. What would you do next? We encourage you to think carefully about what your states mean before you start implementing rules. This is true in general: think first, then code. Or, as the meme goes, "weeks of coding can save you hours of planning."

Once you are finished with this problem, save your code for the empty room solution in the file emptyroom.txt in Repl.it, push it to your GitHub repo, and then move on to the next problem.

Program Picobot to Solve the Maze

Once you have successfully covered the empty room, use the MAP –> button to find the maze, and implement the maze-covering algorithm. Think carefully about your states. You will likely want to use 4 states where each state represents a different direction in which you are currently moving.

A good way to solve this kind of maze is by using the "right hand rule." Basically, the idea is best explained if you imagine you are Picobot and you have to go through this maze with your eyes closed (so you can only feel what is directly around you). If you would keep your right hand always touching a wall on your right, and keep following that wall as it twists and turns through the maze. Eventually, you will cover the entire maze. Convince yourself that this is indeed true. Does this algorithm make sense? In what kind of maze would this break down? (Fortunately, we are given a maze where this idea will work)

Now, how can you implement this in Picobot? If you want, here are some hints to get you started: Consider carefully which wall your hand will be on if you are heading in each direction, and this will help you figure out which direction to move, and when to change states. For example, if you are heading north, your hand is on the east wall. So as long as there is a wall to your east, and no wall to the north, you keep going north, dragging your hand along the wall. If the wall to the east suddenly disappears, then you turn in that direction to follow the wall. If you suddenly hit a wall to the north, then your hand sweeps around the corner between the eastern and northern walls and you turn in the process so that you are facing west and your hand is now on the northern wall. Make sure you understand all of the possible turns and transitions before you start writing rules. If anything is unclear, ask a tutor. We repeat: do not write rules until you have a firm understanding of what each state means and how different environments affect your direction and forward progress. It’s all about keeping your right hand on the wall …

When you are done and have tested your solution thoroughly, make sure you push the file maze.txt with your code from Repl.it to your GitHub repo.

Are you up for more? There are some fun challenge problems in the next section. You are highly encouraged to try these, but they are not required.

By the way, the style of coding that Picobot uses is an example of finite state machines. We will explore it further later on in SPIS. The concept of state machines is particularly useful if you are interested in robotics, for example. However, many other applications use this style as well.

Challenge Problems

When you've mastered the maze and you're ready for more, try some optional challenge problems (some of these are super challenging). You don’t need to submit these, but you could if you wanted to; just create additional files in your GitHub repo with .txt file extensions, and put an explanation for each of those files in your README.md file.

For example, you could challenge yourself with some of the following problems:

  • For the empty room, see if you can use only 6 rules.

  • For the maze, see if you can use only 8 rules.

  • Scroll through the different maps and see if you can write Picobot programs to solve these other rooms.

  • For the empty room, can you build a machine that covers each cell in the room exactly once?

  • Imagine Picobot is a Roomba vacuum cleaner. You might know that the edges of a room get dustier than the middle of the room (dust kicked up accumulates near the walls). Can you program Picobot to cover the cells that are adjacent to walls at least twice and cover interior cells exactly once?

  • Can you write a single program that works for ALL provided mazes?

  • Can you formulate other criteria you might want to solve? Are there unsolvable requirements?

We hope you enjoyed these Picobot puzzles...