Cozmo Rainbow Cube Lights RGB

Kinvert did another Cozmo project. This time we coded Cozmo so his cube lights shine rainbow colors. We did this in Python and you can do it too.

We do this using Python and the Anki Cozmo SDK.

Cozmo’s Cube Lights

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

Cozmo loves to play with his cubes and one of the ways to make this more fun is by changing the colors of his cube lights.

This is pretty easy to do and you can find tutorials out there showing you how.

Anki also has some examples.

However generally you can only show a few different colors with the cubes.

Kinvert found a fun way to not only show any color, but also to cycle smoothly through the colors of the rainbow.

We made Cozmo’s cube lights rainbow color with RGB.

Cozmo Rainbow Cube Lights RGB Commands

https://youtu.be/Z5Nq-YT2xZM

Here are the Python commands we used to make Cozmo’s cube lights show RGB rainbow colors.

  • cozmo.run_program()
  • robot.world.get_light_cube()
  • LightCube1Id
  • for loops
  • if, elif, and, else
  • cozmo.lights.Light()
  • cozmo.lights.Color()
  • cube.set_lights()
  • time.sleep()

Python Code for Cozmo Cube Lights Rainbow

Here is the code. We’ll be making a more in depth explanation of how the code works and how you can customize it in our online course.


"""
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
from cozmo.objects import LightCube1Id, LightCube2Id, LightCube3Id
import time

def rainbow_cubes(robot: cozmo.robot.Robot):
    cube1 = robot.world.get_light_cube(LightCube1Id)  # looks like a paperclip
    cube2 = robot.world.get_light_cube(LightCube2Id)  # looks like a lamp / heart
    cube3 = robot.world.get_light_cube(LightCube3Id)  # looks like the letters 'ab' over 'T'
    
    for j in range(5):
        for i in range(360):
            if i <= 60:
                thing = cozmo.lights.Light(cozmo.lights.Color(rgb=(255, int(4.2*i), 0)))
            elif i > 60 and i <= 120:
                thing = cozmo.lights.Light(cozmo.lights.Color(rgb=(int(255-4.2*(i-60)), 255, 0)))
            elif i > 120 and i <= 180:
                thing = cozmo.lights.Light(cozmo.lights.Color(rgb=(0, 255, int(4.2*(i-120)))))
            elif i > 180 and i <= 240:
                thing = cozmo.lights.Light(cozmo.lights.Color(rgb=(0, int(255-4.2*(i-180)), 255)))
            elif i > 240 and i <= 300:
                thing = cozmo.lights.Light(cozmo.lights.Color(rgb=(int(4.2*(i-240)), 0, 255)))
            else:
                thing = cozmo.lights.Light(cozmo.lights.Color(rgb=(255, 0, int(255-4.2*(i-300)))))
                
            robot.set_center_backpack_lights(thing)
        
            if cube1 is not None:
                cube1.set_lights(thing)
            else:
                cozmo.logger.warning("Cozmo is not connected to a LightCube1Id cube - check the battery.")
                print("https://www.kinvert.com/replace-cube-battery-cozmo-vector/")
        
            if cube2 is not None:
                cube2.set_lights(thing)
            else:
                cozmo.logger.warning("Cozmo is not connected to a LightCube2Id cube - check the battery.")
                print("https://www.kinvert.com/replace-cube-battery-cozmo-vector/")
        
            if cube3 is not None:
                cube3.set_lights(thing)
            else:
                cozmo.logger.warning("Cozmo is not connected to a LightCube3Id cube - check the battery.")
                print("https://www.kinvert.com/replace-cube-battery-cozmo-vector/")
        
            time.sleep(0.01)
    cube1.set_lights(cozmo.lights.off_light)
    cube2.set_lights(cozmo.lights.off_light)
    cube3.set_lights(cozmo.lights.off_light)

cozmo.run_program(rainbow_cubes)

Improvements for Python Code

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

If you wanted the colors to change more smooth and slow, what would you change in the code?

How would you get the colors to start at red, then go to purple then blue – instead of starting red and going to orange then yellow?

If you wanted to have each cube 120 degrees out of phase, how would you do that?

https://youtu.be/6JMJVuUGDQ0

Reading Rainbow – More Stuff to Read

But don’t take my word for it.

See for yourself, we have a lot of other articles that teach coding and give fun activities to do with Cozmo.

We have more articles that use the Anki Cozmo SDK, as well as an article about Cozmo Vs Vector and the Anki Vector SDK.

Kinvert has made many Cozmo Games, Vector Games, Cozmo Projects, and Anki Vector Examples.

If you don’t feel ready to learn Python, check out What is Robotics, Block Coding, and Age to Teach Kids Python.

Notice – Anki Has Closed How to Save Anki Cozmo.

We also have an amazing mailing list. If you want to stay up to date with Cozmo and / or Vector, this is the resource for you. Remember to find that confirmation email and click that button. You can unsubscribe at any time.

Leave a Reply

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