Want to get the most out of your Cozmo? He can help you learn to program. Kinvert will show you how to program Cozmo to change his backpack light colors.
Finally, before diving right in, if you don’t have the SDK you can still program Cozmo in Code Lab. If you want to learn a bit about this you should check out Kinvert’s Ultimate Guide to Block Coding.
Feeling like even more of a beginner? That’s fine! Check out What is Robotics?
If you want to see what Anki has to say about Kinvert, check out https://developer.anki.com/blog/features/interview/from-stem-to-learn/.
Show Cozmo’s Festive Mood With Backpack Lights
At the time of writing this it is nearly Christmas. So let’s have Cozmo help set the mood by changing the color of his backpack lights.
There are many things you can control on Cozmo. This is only one of many available commands you can use.
When you combine many things like this is where programming Cozmo really becomes interesting.
For the purpose of this article we will only be covering the backpack lights of Cozmo. In addition we’ll only be covering one of multiple available commands for this.
We have another article where you can change cozmo’s backpack lights to any color. We even describe RGB colorspace. If you want to see that check out Cozmo Backpack Lights in RGB with Python.
Let’s take a look at the commands we’ll be using to make this happen. Let the festivities begin!
Commands to Light Up Cozmo’s Back
What commands will we be using? Here they are:
- Argument – cozmo.lights.red_light
- Argument – cozmo.lights.green_light
Not too many commands here since this is a fairly straight forward program.
Python Code to Change Cozmo’s Backpack Light Colors
Here we go. Let’s dive in to the code.
Since it is nearly Christmas, we’ll be changing the colors of Cozmo’s backpack lights to red and green. Ultimately you can choose any color you want. But keep in mind, with this command, there are only so many color choices.
If you want to be able to shine Cozmo’s backpack lights any color, you’ll want to check out our article which uses RGB. We link to that further up the article.
First, we need to do our imports. First, we import the Cozmo module as this is what gives us access to Cozmo’s backpack lights.
Next, we import the time module. This is what will enable us to use time.sleep() to make the lights shine for a certain amount of time.
We define our function, which is called deck_the_backpack_lights(). In it, we use robot.set_all_backpack_lights() and pass it the arguments of cozmo.lights.red_light and cozmo.lights.green_light.
We also do our time.sleep() which tells Cozmo how long to show each color.
Finally, we call our function which controls the backpack lights with the cozmo.run_program() function.
""" 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 import time def deck_the_backpack_lights(robot: cozmo.robot.Robot): '''Deck the halls with festive backpack lights''' robot.set_all_backpack_lights(cozmo.lights.red_light) time.sleep(2) robot.set_all_backpack_lights(cozmo.lights.green_light) time.sleep(2) cozmo.run_program(deck_the_backpack_lights)
How could we have made the lights blink multiple times?
What if we wanted to double the amount of time the lights shined?
How could we get Cozmo to do other stuff to help set the Christmas mood while coloring his backpack lights?
Be sure to comment below with your code.
This was a pretty straight forward Cozmo Coding Example. There are some interesting improvements you can make. One of these involves a for loop. If you check out the Cozmo Examples article you’ll see at least one program that uses for loops.
Was this helpful? Did you make any improvements to the code? Let us know in the comments below.
Cozmo Examples, Tutorials, and Projects for STEM Education Curriculum is the place to go for Cozmo examples, in case you don’t have Vector yet.
If you want to stay up to date with all things Cozmo and Vector related, you should consider our mailing list. You can unsubscribe at any time.