Activity: Apple Harvest

Code Materials


The goal of this activity is to make Cozmo pick the apples (cubes) when they're ripe (turned red) and take them to the collection truck. Then a human has to deliver the apples to market before they spoil (turn purple).

Learning Goals

This activity teaches students about programming multiple characters (robot plus cubes), use of timers, random numbers, and use of custom markers. It also provides additional practice with state machine programming.


The apple truck is represented by a cardboard sheet or manila folder with a custom marker on it. The wall should be decorated with pictures of apples, apple trucks, or something similar. (Examples: 1, 2, 3.) Have students do a Google image search on "apple truck" or "apple harvest" and choose their own images to download and print.

A good way to motivate this activity is to show some videos of agricultural robots engaged in apple harvesting. Google "robot apple harvesting" for videos. Here are some examples:


1. Finding the Apples

Cozmo's job is to pick ripe apples (we'll assume that red apples are ripe)) and bring them to the apple truck. He starts out not knowing where any of the apples are, so the first thing he needs to do is look for them. We can get him to look around using the "turn wander" action. But sometimes he may need a little extra help, such as when his head isn't tilted at the right angle to see an apple, so we'll include a "Let Me Drive" rule as well:

If we surround Cozmo with cubes and run this program, eventually all the cubes should end up on his world map, unless one cube is hidden by another. In that case we can use the left stick to move Cozmo a bit, so he can see better.

2. Picking the Ripe Red Apples

We can use the familiar "Pursue and Consume" idiom to pick a red apple. Once we have it, we will need to bring it to the apple truck, which we'll do on page 2. So when Cozmo successfully grabs an apple he should switch pages.

Note that the pursue and consume rules must come before the turn wander rule, so that Cozmo will wander only if he does not see a red cube.

3. Bringing Apples to the Apple Truck

On page 2 we want Cozmo to bring his apple to the apple truck and drop it off there. The apple truck has a "two circles" marker that Cozmo can see. If he's found this marker and it's on his world map, we can use a pursue rule to take him there. Otherwise he will have to wander until he finds it, or we can use the gamepad to guide him to find it.

The marker should be placed low enough that Cozmo can see it while he's holding a cube. Once he's "bumped" the marker he should drop the cube. But if he drops it while still bumping the marker, the cube will be up against the wall and may cause problems, so instead Cozmo should back up first.

To test this program, let Cozmo find the cubes. Then right click on a cube in the world map and set its color to red.

4. Manual Control of Cube Color

We'd like our apples to start out green and ripen to yellow and then red. In this section we will see how to manually control a cube's color using the gamepad. For cube1, every time we press the A button we would like it to change color as follows: from none to green, from green to yellow, from yellow to red, and from red back to none.

"WHEN see me <color>" can be used by a character to examine its own color. The following rules almost do what we want:

How do these rules work? When the program starts, the cube has no color, so whe we press the A button, the second rule glows the cube green. But now that the cube is green, why doesn't the third rule glow it yellow? After all, Fifth Law of Calypso says that "earlier actions affect later rules". This is true, but the second and third rule's actions are in conflict, because the cube cannot be two colors at once. And the Third Law of Calypso says "when actions conflict, the earliest wins". So the third rule's "glow me yellow" action is blocked by the second rules "glow me green" action -- but only on the current rule cycle. On the next rule cycle the cube starts out green, so if the A-button is still depressed, the second rule cannot run but the third rule can, and it changes the cube to yellow.

Test this by running the program and holding the A-button down. You will see the cube change color continuously. The problem is that when you simply want to progress to the next color in the sequence, it can be very difficult to press and release the A-button within one rule cycle. Your fingers aren't fast enough.

To solve this problem, we add an action to the first rule with a "once" modifier. Since indented rules can run only if the parent action succeeds (Fourth Law of Calypso), the "once" modifier will prevent any indented rules from running on additional rule cycles while the A-button remains pressed. Try this version and you'll see:

To get all three cubes to behave this way, we can copy the code from cube1 to the others by switching to the state machine view (press "up" on the D-Pad) and then pressing the X button or typing control-C. Then, edit the rules for cube2 so it checks the B button instead of the A button, and make cube3 check the X button. Now you can control all three cubes independently.

Using this new functionality you can run the AppleHarvest program and control when each apple becomes ready for picking.

5. Timed Ripening

Suppose you want the apples to ripen by themselves instead of manually controlling them. We can do this using a timer. Because we don't want all the apples to ripen in synchrony, we will include some random variation in the time intervals.

Apples should always start out green. We only want to make them green once, so this rule doesn't interfere with other rules on the page:

Once an apple is green, we'd like it to eventually ripen to yellow. How long should this take? Let's say at least 4 seconds, but no more than 14 seconds. So we're going to start with the number 4 and add to it some number between 0 and 10, and that's how long we're going to wait:

Copy these rules to all three cubes and run the program. Notice that the cubes all immediately turn green, but then they turn yellow at different times.

Let's make the final ripening step, from yellow to red, take between 10 and 40 seconds. What rules will accomplish this?

6. Completing the Harvesting Cycle

Let's add some additional functionality to allow Cozmo to score points for successfully harvested apples, and to allow the cubes to be reused once an apple is harvested.

A cube can tell that it has been picked up by using "WHEN held-by". Once picked up, the cube should wait until it is put down ("held-by" becomes false), and then stop glowing and score a point becanse an apple has been delivered to the apple truck.

Now we're going to let the user simulate delivering apples to the market by relocating and tapping on the cube. But we don't want Cozmo's accidentally bumping into the cube to be mistaken for a tap, so we will give him a couple of seconds to move on before we switch to a page where we wait for a tap.

On page 3 we will wait for a tap. When it comes, we'll move to page 4 and score 3 additional points for having delivered an apple to market. But if it doesn't come within 30 seconds, the apple will "spoiled" and there will be a penalty to pay (page 5).

First, here are the rules to wait for a tap:

On page 4 we score the additional points for a successful delivery. Then, after a pause of 30 seconds, we go back to page 1, allowing the cube to represent a new green apple.

If the user doesn't deliver the apple within 30 seconds, page 5 marks it as "spoiled" by glowing it purple. Moreover, there is a two minute penalty period where the cube remains spoiled before the cube is put back into action again as a new green apple.


The Apple Harvest scenario can be extended by implementing additional functionality, such as:
  1. Have Cozmo say something every time he picks or drops off an apple.
  2. Allow unpicked apples to go rotten if Cozmo doesn't pick them quickly enough.
  3. Have two apple trucks, one headed to the fruit market and the other headed to the applesauce factory. Have the user tell Cozmo which truck to take each apple to.

Back to top of page

Back to New Brighton Curriculum