Calypso Module 7: The "Default Value" Idiom

Learning Goal: In this module you will learn how to make Cozmo distinguish usual from unusual situations.

Reacting to the Presence of a Cube

Let's imagine that Cozmo is afraid of cube 3. Suppose we want Cozmo to glow green most of the time, but if cube 3 is in sight, we want him to glow orange. First, let's see how to make him glow green:

Notice that this rule has an empty WHEN part, so it will run all the time.

Now let's add a rule that makes him glow orange when cube 3 is in view. We can't just say "WHEN see cube3", because that will be true for as long as cube 3 appears on the world map, whether or not Cozmo can currently see it. So we need to add an "in frame" tile, which means that cube 3 is currently in his field of view. The new program looks like this:

Let's try out the program:

1. Hide cube 3 from Cozmo and start the program. Cozmo should be green.

2. Place cube 3 where Cozmo can see it. Now Cozmo should be orange. The "glow me green" action in the second rule should be dimmed, because that action is being blocked by the "glow me orange" action in the first rule.

3. Take cube 3 away, and Cozmo should turn green again. Why does he turn green again? (Answer: the first rule cannot run when cube 3 is not in sight, so its action no longer blocks the second rule's action.)

The Default Value Idiom

The word "default" means a pre-selected choice used when no alternative choice has been requested. For example, at McDonald's, the default number of pickle slices on a hamburger is 1. You can ask for no pickles. Or you can ask for extra pickles. But if you don't say anything, you will get 1 pickle slice on your hamburger or cheeseburger. That is the default value for pickle slices. (For Big Macs or Quarter Pounders the default value is 2 pickle slices. For Filet-o-Fish it is none.)

In the program we wrote above, Cozmo's default color was green. He will be green most of the time. The only time he won't be green is when he has cube 3 in sight; then he'll be orange.

Now let's add one more bit of behavior: when we press the "A" button we will make Cozmo glow red in an attempt to scare off the cube.

Let's consider the different situations that might occur and how these rules make Cozmo respond to them:

• If the "A" button is pressed, Cozmo will glow red. It doesn't matter if cube 3 is visible or not; the first rule's action blocks the other two.

• If the "A" button is not pressed, but cube 3 is visible, Cozmo will glow orange. The first rule cannot run, and the second rule blocks the third rule's action.

• If the "A" button is not pressed, and cube 3 is not visible, then neither rule 1 nor rule 2 can run. Rule 3 can always run; it's the default. Since rule 3's action is not blocked by the other rules, Cozmo will take the default action and glow green.

Changing the Rule Order

The default rule must always be the last one in the set; its action should only be taken if none of the other rules can run. What happens if we put the default rule first?

What will happen when you run this program? What law explains this?

Laws Explain Why Idioms Work

So far we've learned several idioms and several laws. What is the relationship between the two?

Idioms are specific rules or sets of rules that occur frequently in programming because they solve problems that come up often. "Let Me Drive", "Pursue and Consume", and "Default Value" are just three examples; there are many more. Each idiom solves a specific problem, and a single program will typically only make use of a few of them.

Laws, on the other hand, are universal. They apply to every program, and they are what make idioms work. For example, Pursue and Consume depends on the First Law, which determines which object is pursued first. Default Value depends on the Third Law, which explains how rules for specific situations can override the default when those situations occur.

There are five major Laws of Calypso. You will learn the other two in later modules. With each law you will also learn new idioms that depend on that law.

Review and Discovery

Do the Module 7 Review and Discovery activity (coming soon) to review what you've learned.

Next Module

In the next module you'll learn how Cozmo recognizes faces.

Back to Calypso Curriculum overview.