Calypso Module 7: The "Default Value" Idiom
Reacting to the Presence of a CubeLet'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:
The Default Value IdiomThe 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.
Handling Additional CasesNow 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:
Changing the Rule OrderThe 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 WorkSo 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 DiscoveryDo the
Next ModuleIn the next module you'll learn how Cozmo recognizes faces.
Back to Calypso Curriculum overview.
Copyright © 2017 Visionary Machines LLC.