# Calypso Module 3: Pursuit

Learning Goal: In this module you will learn how to make Cozmo pursue an object. This is the first half of a key idiom (programming trick) called Pursue and Consume which we'll study in the next module.

### The Pursue Rule

A pursue rule looks for an object and causes motion towards that object. In the example below, Cozmo is pursuing cubes using the action "move toward". The cube tile with the question mark means he's not looking for a specific cube; any cube will do.

Let's try it out and see how it works.

1. If you are already running a Calypso program, bring up the Home menu (Start button on the game controller, or Home key on the keyboard) and select "New".

2. Enter the pursue rule above. Make sure you use the cube tile with the question mark.

3. Take Cozmo off his charger. He won't move if he's on the charger.

4. Put two cubes in front of Cozmo. Don't put them right next to each other; spread them apart, but make sure that he can see both of them. (Check this in the Camera Viewer.)

5. Run the program: press the Back button on the game controller or the backspace key on the keyboard.

6. In the world map you will see the LOS (Line of Sight) indicator from Cozmo to one of the cubes. And you should see Cozmo turn toward that cube and drive up to it. He'll stop when he gets close.

7. Slowly move the cube away from Cozmo, and he'll follow it around.

8. Cozmo goes to where the cube is on his world map. If you grab the cube and quickly move it out of his field of view, he won't be able to update his world map, so he'll go to the wrong place.

### Choosing Among Cubes

When you started the program, Cozmo saw two cubes. How did he decide which one to pursue?

Try setting up the experiment again, placing two cubes in front of Cozmo and then running the program. Vary the starting positions of the cubes and see if you can figure out how Cozmo chooses a cube to pursue.

### The First Law of Calypso

The behavior of Cozmo programs is governed by the laws of Calypso, just as the behavior of objects in the real world is governed by the laws of physics. The First Law of Calypso is that each rule that looks for an object will pick the closest one that matches what it's looking for. So if we have a pursue rule that is looking for cubes, it will pick the closest cube.

Here is a graphic illustrating the First Law:

Now that you know the First Law, you can predict what Cozmo will do when he's told to pursue something.

### Guided Pursuit

Let's combine the Let Me Drive idiom from the previous module with the pursue rule:

This is your first two-line program. To try it out, follow these steps:

1. Lay out two cubes so Cozmo can see them, and make sure one cube is closer to him than the other.

2. When you run the program, Cozmo will turn toward the closest cube and start moving toward it.

3. Use the left stick on the game controller to steer Cozmo toward the other cube. This is possible because with Let Me Drive, the gamepad controls are able to override what the other rules say.

4. If you turn Cozmo toward the second cube but then let go of the left stick, the pursue rule will take him back toward the first cube, which is still closer.

5. If you drive Cozmo far enough that the second cube becomes the closest cube, then you can let go of the left stick and Cozmo will continue toward the second cube on his own, thanks to the pursue rule. The pursue rule always chooses the closest cube, and it will change its choice if another cube becomes the closest.

### Simulator Mode

It can be more convenient to experiment with guided pursuit in simulator mode instead of using a real robot. Simply unplug the Fire tablet from your computer, quit Calypso, and start Calypso again. Calypso will warn you that it could not find the robot, and offer the option of continuing in simulator mode.

In simulator mode the world map always starts out with all three cubes present, in fixed positions. You can use the pursue rule to take your simulated Cozmo to the nearest cube, and the Let Me Drive idiom to guide him to a different cube.

### Rules That Run and Rules That Don't

When you run the two line program above, you'll notice that most of the time the Let Me Drive rule is dimmed while the pursue rule is at full brightness. This indicates that the pursue rule is "running", while the Let Me Drive rule is not.

A rule "runs" whenever its WHEN part is true. Since Cozmo is continuously seeing a cube, the pursue rule's WHEN part is true all the time, so it is constantly running. But the Let Me Drive rule's WHEN part only becomes true when you press one of the gamepad controls. When you're not touching the gamepad, "WHEN gamepad" is false, so the Let Me Drive rule cannot run. When you press one of the sticks you'll see the rule brighten because it's running. When you let go, it dims again.

### Pursuing Red Cubes

You can do this exercise either in simulator mode or with the real robot.
1. Program Cozmo to pursue red cubes, using the pursue rule below instead of the previous rule:

2. When you start the program, none of the cubes are red, so Cozmo doesn't go anywhere. The pursue rule cannot run because its WHEN part is not true.

3. Right click on a cube in the world map and select "change color". Set the color to red.

4. Now Cozmo should pursue that cube. When he reaches it, he stays there.

5. Pick another cube on the world map and color that one red as well. Why doesn't Cozmo go to this cube? (Answer: because of the First Law.)

6. Now change the color of the cube Cozmo is currently visiting to purple.

7. What does Cozmo do when you do this? Why does he act that way?

### Review and Discovery

Do the Module 3 Review and Discovery activity to review what you've learned.

### Next Module

In the next module you'll learn the rest of the Pursue and Consume idiom, and how to reason about Pursue and Consume programs.

Back to Calypso Curriculum overview.