Cozmo Roll Cube Example – Roll_Cube – RollBlock

Kinvert will help you make Cozmo roll cubes in this example using roll_cube() and RollBlock. First we will use cozmo.robot.Robot.roll_cube() to make Cozmo roll blocks. Next, we will use cozmo.behavior.BehaviorTypes.RollBlock to roll a cube with Cozmo. Let’s get started with our Cozmo Roll Cube Example.

Eventually it will happen to you, and you’ll want to know How to Replace a Cube Battery for Cozmo and Vector.

Why Cozmo Rolls Cubes

Cozmo is a playful robot and easily gets bored. In normal play mode you can spark a trick to make Cozmo roll cubes. However even in his freeplay mode he will roll cubes on his own. He has fun doing other activities as well such as knocking over stacks, stacking cubes, and playing other games.

Cozmo roll cube using Python SDK roll_cube in this Cozmo example lesson

Cozmo Roll Cube is a lesson I like to do early on, as it is easy to write and kids K-12 love it. I’ll show you how.

Cozmo Roll Cube Commands

What commands will we use? Let’s take a look.

  • cozmo.robot.Robot.roll_cube()
  • robot.run_timed_behavior()
  • cozmo.robot.Robot.run_timed_behavior()

http://cozmosdk.anki.com/docs/generated/cozmo.robot.html?#cozmo.robot.Robot.roll_cube

http://cozmosdk.anki.com/docs/generated/cozmo.behavior.html?#cozmo.behavior.BehaviorTypes.RollBlock

Cozmo Roll Cube With cozmo.robot.Robot.roll_cube()

Let’s get Cozmo to roll a cube using the roll_cube() method. Please keep in mind, with this method we need the cube immediately in Cozmo’s view so he can roll it. If the cube isn’t around 4 inches directly in front of him, the moment the code starts, the program could fail. Use the paperclip looking cube.


"""
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/
"""

import cozmo

def kinvert_roll(robot: cozmo.robot.Robot):
    
    cube1 = robot.world.get_light_cube(1)  # looks like a paperclip
    
    robot.roll_cube(cube1).wait_for_completed()

cozmo.run_program(kinvert_roll)

How do you think you could change the code to get Cozmo to roll one of the other cubes, other than the paperclip looking cube?

Cozmo Roll Cube With cozmo.behavior.BehaviorTypes.RollBlock

There is another way to get Cozmo to roll a cube using the Python SDK. This involves using behaviors. It uses different methods, and Cozmo will behave slightly differently, but it will get the same result. Cozmo will roll a cube. Cozmo roll block is slightly different than Cozmo roll cube.


"""
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/
"""

import cozmo

def kinvert_roll(robot: cozmo.robot.Robot):
    lookaround = robot.start_behavior(cozmo.behavior.BehaviorTypes.LookAroundInPlace)

    cube = robot.world.wait_until_observe_num_objects(num=1, object_type=cozmo.objects.LightCube, timeout=10)

    lookaround.stop()

    robot.run_timed_behavior(cozmo.behavior.BehaviorTypes.RollBlock, active_time=60)

cozmo.run_program(kinvert_roll)

In this case, Cozmo will look around until he sees a cube, then roll it. What’s cool about this, is Cozmo will actually turn and look around. In addition, Cozmo will roll whatever light cube he sees. If he sees Cube 3 first, that’s what he’ll roll. A little more complicated, but a bit of an improvement over the previous method.

Spicing Things Up a Bit

At Kinvert we like to keep our lessons fun. After you make Cozmo roll blocks, try having him do a victory dance.

For reference, check out our article on Cozmo Animations Play_Anim_Trigger().

How could you incorporate a bit of what you learn from there to spice this up and have Cozmo do a victory dance?

In Conclusion – Cozmo Roll Cube Example

With very little code we can share some fun with Cozmo. Getting Cozmo to roll cubes is pretty easy, and students love it. Though using the Roll_Cube() method is more concise, using the RollBlock behavior is a little more robust. We like to spice up our lessons and keep them more enjoyable by making use of the emotional power of Cozmo.

Are You a School?

We help schools create amazing STEM programs and makerspaces. If you’re interested in knowing more please go to our Contact Page.

Benefits, Pitfalls, and Resources for Anki Cozmo Curriculum for K-12 STEM Classes

Cozmo Examples, Tutorials, and Projects for K-12 STEM Education Curriculum

Anki Cozmo SDK Programmable Robot for Kids in Python

Are You a Parent or Student?

We have some useful resources I linked above.

In addition, you should check out our Anki Cozmo Robotics Competition Camp and our Robotics for Kids and Teens Course.

If Python isn’t your thing, take a look at What is Robotics, Age to Teach Kids Python, and Block Coding.

If you have any questions or comments please feel free to leave a comment below. Hope you found this helpful and fun!

Leave a Reply

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