# Calypso Module 6: Conflict Resolutionand the Third Law

Learning Goal: In this module you will learn what happens when rules tell Cozmo to do two different things, but he can only do one of them.

### Conflicting Actions

Consider this sample program. What do you think it will do?

You could run this program either on Cozmo with a real cube, or in simulator mode. But before you run it, try to guess what it will do. Here are some possibilities:

1. It could leave the nearest cube glowing orange.
2. It could leave the nearest cube glowing green.
3. It could make the nearest cube switch back and forth between orange and green.
4. It could color one cube orange and another cube green.
5. It could refuse to do anything.

Take your best guess, then run the program and see what happens.

### Why Did It Do That?

Look at the rules while the program is running. Both WHEN parts are highlighted, which means both rules can run. But only the first rule's DO part is highlighted. The second rule's DO part is dimmed because its action is blocked:

Now switch the order of the rules. You can do this by putting the pencil on the "WHEN" block, pressing the green "A" button to pick up the rule, then using the left stick to move the rule up or own, and pressing the "A" button again to drop the rule in its new location.

 Note: when you have picked up a rule, do not move the stick to the right, which would indent the rule. If you indent it accidentally, pick it up and move the stick to the left to un-indent it.

Put Cozmo and the cubes back in their original positions and run the program again now that you've reordered the rules. What happens this time?

### The Third Law of Calypso

By now you've figured out that whichever rule comes first is the one whose action will be taken. The other rule's action will be blocked. This is the Third Law of Calypso: "When actions conflict, the earliest wins." Let's look a bit more closely at this law. First, what does it mean for actions to "conflict"? In Calypso it means there is no way to do both actions at the same time. A cube cannot be "glowed" orange and "glowed" green simultaneously. Cozmo has to choose one of these actions. Which action should he choose?

The Third Law says that the earliest action wins. "Earliest" simply means whichever rule appears first on the page. That's why changing the order of the rules changes the result. If the "glow it green" rule is earliest, and both rules can run, then "glow it green" will win and "glow it orange" will be blocked.

### Two Pursue Rules

Let's look at another example of the Third Law. In the graphic below, Cozmo cannot move toward a red cube and move toward and a blue cube at the same time, especially when they're in opposite directions!

Notice that both rules have the same action: "move toward it". But in the first rule "it" refers to a red cube, while in the second rule "it" refers to a blue cube. Cozmo sees both cubes, so both rules can run. But the first rule's action will override the second rule's action. That's what the Third Law says: the earliest action wins, and any conflicting actions are blocked.

The Pursue2.calypso sample program can be used to try this out:

1. If you're running Calypso, you can load the program by left-clicking on the link above. Otherwise, right-click on the link, choose "Save Link As", and save the program to your Documents/Calypso folder, then run Calypso and load it from there.

2. Enter the two pursue rules shown on the Third Law graphic above.

3. Arrange the cubes so they are well separated, but Cozmo can still see them, then run the program.

4. Which cube does Cozmo go to? What happens when he gets there?

5. Stop the program and rearrange the cubes so that the blue cube (the "deli slicer") is much closer to Cozmo than the red cube ("the paperclip"). Make sure he can still see all the cubes.

6. Run the program again. Which cube does Cozmo go to?

You might be surprised to see that Cozmo always goes to the red cube, even if the blue one is closer. Why do you think he behaves this way?

### The First Law vs. The Third Law

To understand why Cozmo always goes to the red cube, not the closest cube, let's remember what the First Law says: "Each rule picks the closest matching object." So the first rule will pick the closest red cube, and the second rule will pick the closest blue cube. Both rules can run: you can see an LOS indicator for each one showing which cube it picked, and both WHEN parts are highlighted.

The First Law determines which objects Cozmo pays attention to. It doesn't say anything about which actions he takes. That's governed by the Third Law. It's the Third Law that causes Cozmo to always go to the red cube, because that rule comes first.

### Suspending Pursuit So Cozmo Can Recognize Cubes

If the cubes are laid out so that Cozmo cannot see all of them when the program starts, he will pursue whatever cubes appear on his world map. You can use the game controller to turn him toward a new cube; it can take him a couple of seconds to recognize it. But as soon as you let go of the left stick he will try to pursue one of the cubes he's already recognized, so you need a way to get him to hold still. You can do this by holding the right trigger to freeze Cozmo for a few seconds. You can also do it by pressing down on the left stick without moving it.

### Double Pursue and Consume Program

Can the second pursue rule ever actually pursue a cube? It can, but only if the first pursue rule can't run -- which could happen if there were no red cubes. Let's use the Pursue and Consume idiom twice so we can extinguish both red and blue cubes (but leave the green ones alone):

Run this program and notice that after Cozmo grabs the red cube, "see red cube" is no longer true; the first rule is dimmed. This allows the third rule to pursue the blue cube since its action is no longer blocked.

Let's try one more case: suppose we have two red cubes and one blue cube. The Pursue3.calypso sample program can be used to try this out. If Calypso is running, just click on the link to load the same program. Then:

1. Cubes 1 and 2 will be red; cube 3 will be blue. You can verify this by running the program even though Cozmo doesn't have any rules yet.

2. Enter the two Pursue and Consume rule pairs as shown above. Also add a Let Me Drive rule.

3. Position the cubes to form a large triangle with Cozmo in the center. You will have to use the left stick to turn Cozmo so he sees all the cubes when you run the program.

4. In what order do you think the cubes will be grabbed? Will it be 1-2-3? Will it be 1-3-2? Or something else?

5. Run the program and see if you were right. If Cozmo gets stuck at a cube, use the left stick to nudge him a little.

After Cozmo grabs the first red cube, he shifts his attention to the second red cube and begins pursuing that. He drives right past the blue cube. This is because of the Third Law: since the pursue rule for red cubes comes first, it blocks any action that conflicts with it.

But why did Cozmo go to the closer of the two red cubes first? Why didn't he go to the farther one first? This is due to the First Law: each pursue rule picks the closest matching object. For the red pursue rule this would be the closest red cube.

### Rules That Don't Conflict

The Third Law only applies when two rules have conflicting actions. It's perfectly fine for Cozmo to take several actions at once, as long as they don't disagree with each other. For example, he could play a sound and change a cube's color at the same time. The Third Law does nothing to prevent this.

What are some other examples of things Cozmo could do at the same time without causing a conflict?

What are some other examples of actions that do cause a conflict?

• Can Cozmo say two things at the same time?

• Can he say something and play a sound at the same time?

• Can he say something and express an emotion at the same time?

### Review and Discovery

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

### Next Module

In the next module you'll learn how to make Cozmo act one way in unusual situations and another way when things are normal.

Back to Calypso Curriculum overview.