Cozmo Cube Lights in Python

Kinvert will show you an easy way to control Cozmo’s cube lights using the Anki Cozmo SDK and Python. Make your own modifications to the cube light colors.

Please note, this will require good Cube Batteries so be sure to check out How to Replace Cozmo’s Cube Batteries.

This is one of many Anki Cozmo SDK Examples in Python.

In a previous example, we made Cozmo Rainbow Cube Lights With RGB.

Cozmo’s Cube Lights

Anki Vector vs Cozmo differences between Cozmo has 3 cubes while Vector seems to have only 1

One of the cool things about Cozmo and his Cubes, is that the Cubes have lights in them. These can shine at different colors and it helps make things more interactive.

It’s also a great feature that can be used in making games to play with Cozmo using the Anki Cozmo SDK.

You can set each Cube to a different color. Even cooler, you can set each light on each cube to a different color.

This is one of the things between Cozmo and Vector that is a clear winner for Cozmo. We wrote about the differences between the two at Cozmo Vs Vector.

Cozmo Cube Light Commands

Though there are more options for Cozmo’s Cube lights, we’ll only be covering one way to do it. At the end of the article we’ll link to any other examples we have about Cozmo’s Cube lights.

Here are the commands we’ll be using this time:

  • cozmo.run_program()
  • robot.world.get_light_cube()
  • cube.set_lights()
  • cozmo.lights.red_light (also white and blue)
  • time.sleep()

I’ll describe a bit of how the code works and then you can try it for yourself.

Shedding Some Light on the Code

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

We do our usual import of Cozmo and also import Time as we will be using the time.sleep() command. This is what will end up telling the lights how long to shine.

The next thing that happens is cozmo.run_program() which will execute the kinvert_cube_lights() function we wrote.

Within kinvert_cube_lights() we create cube variables for each cube and set them with robot.world.get_light_cube().

If the cube is None it means Cozmo didn’t connect to the cube which means you probably need new cube batteries. Again, if this happens please head to How to Replace Cozmo’s Cube Batteries for help with that.

If the cube is connected, we run cube.set_lights() and we pass in the colors we want. I’m in the USA so I chose to make the cubes Red, White, and Blue. We do that with cozmo.lights.red_light and so on.

I did put in a print that lets you know if the cube failed to connect.

Python Code for Cozmo Cube Lights

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
import time

def kinvert_cube_lights(robot: cozmo.robot.Robot):
    cube1 = robot.world.get_light_cube(1)
    cube2 = robot.world.get_light_cube(2)
    cube3 = robot.world.get_light_cube(3)
    if cube1:
        cube1.set_lights(cozmo.lights.red_light)
    else:
        print('You might need a new cube battery https://www.kinvert.com/replace-cube-battery-cozmo-vector/')
    if cube2:
        cube2.set_lights(cozmo.lights.white_light)
    else:
        print('You might need a new cube battery https://www.kinvert.com/replace-cube-battery-cozmo-vector/')
    if cube3:
        cube3.set_lights(cozmo.lights.blue_light)
    else:
        print('You might need a new cube battery https://www.kinvert.com/replace-cube-battery-cozmo-vector/')
    time.sleep(4)

cozmo.run_program(kinvert_cube_lights)

This is longer than most of our introductory programs. That’s mainly due to the fact that we don’t know ahead of time if you have good Cube batteries.

Python Code Improvements

What would you do if you wanted to change the color of the cubes?

How would you get the Cube lights to shine for 8 seconds instead of just 4 seconds?

If you wanted to shine all the lights green, then change them all the red, how would you change the code? Keep in mind you might need to add more lines of code.

More Stuff You Might Like

Cozmo drawing for online coding class learn to program anki cozmo robot in python

Kinvert covers a lot of STEM related stuff.

If the code above seems like a bit too much technical jargon you might look at What is Robotics, Block Coding, and Age to Teach Kids Python.

We also have articles on things like What is ABS, Flexible TPU Filament, and 3D Printer Repair.

Please note – Anki Closed How to Save Anki Cozmo.

There is also a great mailing list for you to join if you want to stay up to date with all things Cozmo. You can unsubscribe at any time.

Leave a Reply

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