Cozmo Stack Cubes With Python

Kinvert will show you how to get Cozmo to stack his cubes with a Python program using the Anki Cozmo SDK. You’re free to try the code out yourself.

This is one of many Anki Cozmo Python Examples that we’ve written.

Cozmo and His Cubes

Cozmo is curious by nature and can get bored. He often keeps himself occupied playing with his cubes.

One of the things Cozmo loves doing is stacking and organizing his cubes. This is something we can do with the Python SDK.

Not only is it fun, and something Cozmo also enjoys, it is also a good milestone for students. Getting Cozmo to stack cubes is a good point at which students start developing self confidence in programming Cozmo.

Time to take a look at the commands we’ll be using to help Cozmo stack his cubes.

Cozmo Stack Cubes Python Commands

https://www.youtube.com/watch?v=uaaYmv3HzKo

Here are the Python commands we’ll use to get Cozmo to create the cube stack.

  • cozmo.run_program()
  • robot.start_behavior()
  • cozmo.behavior.BehaviorTypes.LookAroundInPlace
  • robot.world.wait_until_observe_num_objects()
  • cozmo.objects.LightCube
  • robot.pickup_object()
  • robot.place_on_object()

This isn’t the simplest program we’ve written for Cozmo. Don’t worry, we’re about to explain how the code works.

Stacking Up the Cozmo Code

I’ll go in the order this stuff will happen.

First of all, we import cozmo, which is what gives us access to Cozmo’s commands.

We define our kinvert_stack_cubes() function, which is where the majority of the code exists.

cozmo.run_program() is what connects with Cozmo, and begins executing our kinvert_stack_cubes() function.

The first thing we need is for Cozmo to find two of his Cubes. To do this we begin a behavior we’re naming lookaround. We do this with robot.start_behavior().

We need to tell that method what behavior to do, so we pass in cozmo.behavior.BehaviorTypes.LookAroundInPlace.

A list of cubes is made with robot.world.wait_until_observe_num_objects(), after which we stop looking around with lookaround.stop().

The last two things we do are lift one of the cubes using robot.pickup_objects(), and then we stack with robot.place_on_objects().

Here is the code itself.


"""
Copyright Kinvert All Rights Reserved
If you would like to use this code for
business or education please contact
us for permission at:
www.kinvert.com/
Free for personal use
"""
import cozmo

def kinvert_stack_cubes(robot: cozmo.robot.Robot):
    lookaround = robot.start_behavior(cozmo.behavior.BehaviorTypes.LookAroundInPlace)
    cubes = robot.world.wait_until_observe_num_objects(num=2, object_type=cozmo.objects.LightCube, timeout=60)
    lookaround.stop()
    
    robot.pickup_object(cubes[0], num_retries=2).wait_for_completed()
    robot.place_on_object(cubes[1], num_retries=2).wait_for_completed()

cozmo.run_program(kinvert_stack_cubes)

Go ahead and give the code a try 🙂

Improving the Cube Stacking Code

We always like to find ways to get you guys modifying the code.

What would you do if you wanted to reverse the order of the stack? Right now he’s stacking the first cube in the list on top of the second cube in the list. How would you get Cozmo to reverse that order? Get him to stack the second cube in the list on top of the first cube in the list.

What Should I Do Next?

Kinvert has a ton of STEM related information. If you like Cozmo, here are some of the other things you might be interested in.

If this code was a bit much you could read about What is Robotics, Age to Teach Kids Python, Block Coding, and Robotic Dinosaurs.

3D Printing is another topic we cover. What is ABS, TPU Filament, and 3D Printer Repair are a few of our popular articles on that.

We also write about Cozmo Vs Vector, Anki Vector Examples, the Anki Vector SDK and the Cozmo Cube Battery.

If you’re looking to learn or teach with Cozmo then Cozmo Curriculum and Homeschool Robotics might be good choices for you.

Please note – Anki Shut Down How to Save Anki Cozmo.

Finally, we have an amazing mailing list that keeps people all over the world up to date with Cozmo. You can unsubscribe at any time.

Leave a Reply

Your email address will not be published. Required fields are marked *