Introduction: When COZMO, the Robot Meets the Raspberry Pi
This Instructable is competing on contests: "Remote Control", "Wheels", and "Raspberry Pi", If you like it, please give your vote by clicking at the above banner. Thanks a lot! ;-)
When you start to discover COZMO, you will learn that this little guy is really smart that evolves the more you play with him. Better than words, the below movie can give a taste of Cozmo:
But, as explained on Cozmo's website,
"Cozmo’s unprecedented combination of advanced robotics hardware and software are part of what makes him an innovative consumer experience. But it’s also what makes him, in conjunction with the Cozmo SDK, a groundbreaking education platform that’s expressive, motivating, and engaging"
And that's is what we will develop here in this tutorial. Using the Cozmo's SDK integrated with Raspberry Pi, we will open a fantastic window, integrating Robotics and Physical Computing!
The below video will give you a taste of what we will explore here:
Step 1: BoM - Bill of Material
- COZMO - $139.99
- Raspberry Pi - $34.00
- Android Device - $49.99
- LEDs (Red, Yellow, Green)
Note: You will need an Android device (phone or tablet) to work with Cozmo. The cheapest device would be an Amazon Fire, that can be found by around $50.00. I use here a Samsung old tablet, that worked fine. Spite that iPhone and iPad work as Cozmo App, I did not try then with Raspberry Pi.
Step 2: A Little Bit of Cozmo's Technology
Step 3: Cozmo and Raspberry Pi - Initial Setup
In order to program Cozmo using Python, we will use its SDK. To do that, we must have Cozmo Mobile App installed on our mobile device and that device must be tethered to Rpi via USB cable.
- Raspberry Pi (preferible RPi 3)
- Raspbian (I used the latest version 4.9)
- Python 3.5.1 or later (the last one installed on RPI is 3.5.5)
- WiFi connection
- An Android mobile device with the Cozmo app installed, connected to the Rpi via USB cable
The above picture shows the general idea.
Step 4: Mobile Device Setup
Android devices require the installation of Android Debug Bridge (ADB) in order to run the Cozmo SDK. This is required for the RPi to communicate with the Android mobile device over a USB cable and runs automatically when required.
First of all, you must enable USB Debugging on your phone. To do that,
- Go to Settings/General/About device:
- Tap seven (7) times on the Build Number.
- This will open another link: Developer Option, that is hidden by default.
- Then, under Settings -> Developer Options, enableUSB debugging.
Install the ADB (Android Debug Bridge) on RPi with the command:
sudo apt-get install android-tools-adb
Now connect your phone to one of RPi USB connectors.
- When the Allow USB Debugging? popup displays on your phone,
- Tap OK.
At the Rpi command line, type below command to confirm that your device shows:
At least one device should show in the result, for example:
List of devices attached 31455667754d65432 device
If you have not authorized your phone yet, the word “unauthorized” will appear in front of device (see RPi terminal Print Screen above)
Step 5: SDK Installation
If you have the latest version of Raspbian installed, you have already Python3 and Pip3 installed and configured in your Rpi.
Now, its time to SDK Installation:
To install the SDK, type the following into the Terminal window:
pip3 install --user 'cozmo[camera]'
Note that the [camera] option adds support for processing images from Cozmo’s camera.
The RPi terminal Print Screen shows the result after a successful installation.
Step 6: Downloads SDK Examples
Now you must download some examples to start playing with Cozmo SDK.
Open your Rpi Browser and go to this link:
A compressed file will be downloaded on your Rpi Downloads directory. Choose a proper directory to extract the files. In my case, I extracted them at:
See the Print Screen of RPi File Manager. There you will find among the subdirectories:
tutorialsThere you willl find greta examples to start playing with Cozmo and RPi using Python language.
Step 7: Running the First Program: Hello World!
Let's run our first Cozmo program, using its SDK on an RPi!
You can run the program on Terminal, for that type:
And execute the program 01_hello_world.py using Python 3:
Or you can open the Python3 IDE or “Thonny IDE” (that I prefer). In this case, once you had open Thonny, go to "SDK examples directory":
and open the program:
The above Print Screen shows Thommy IDE.
Run the program.
Cozmo should say “Hello World!”, as shown in the movie:
Step 8: The Complete IPI
There are a lot of things to explore using the Cozmo SDK. For starting, go to Tutorial file and play with the other examples there. We will not go deeper here because our goal is to interact Cozmo with Raspberry Pi.
You can find a full description of Cozmo IPI at COZMO IPI
Only as an example, the below code:
''' Drive a Square ''' import cozmo from cozmo.util import degrees, distance_mm, speed_mmps def cozmo_program(robot: cozmo.robot.Robot): robot.say_text("Hello marcelo, let's drive a square").wait_for_completed() for _ in range (4): robot.drive_straight(distance_mm(150), speed_mmps(50)).wait_for_completed() robot.say_text("Left").wait_for_completed() robot.turn_in_place(degrees(90)).wait_for_completed() robot.say_text("and finished!").wait_for_completed() cozmo.run_program(cozmo_program)
The movie shows the result:
Step 9: Physical Computing With Raspberry Pi
Some time ago, I published here a tutorial:
Using the GPIO Zero Library and a Raspberry Pi is a very simple way to learn electronics. With a very few code in Python you will control actuators, read sensors, etc. If you are not familiar with Physical computing, I suggest that you give a look at my tutorial.
OK, saying that! Let's put together a simple circuit together our Raspberry Pi, where we will have as output 3 LEDs and a Push-Button as an input. The above diagram shows how to do it. Pay attention to GPIO numbers where the components are connected.
As usual, let's "blink a LED" (The Physical Computing version of "Hello World"), so we will confirm that everything is OK:
Open your Terminal, Python IDE or Thonny IDE and type the following code:
from gpiozero import LED from time import sleep led = LED(13) while True: led.on() sleep(1) led.off() sleep(1)
Run the program and if everything is OK, your RED LED should blink.
Step 10: New "Hello World" With Cozmo and RPi
Now that we have our RPi "talking" with a LED and the Cozmo, but separately. It is time to do it at the same time.
Enter with below program in your IDE:
import cozmo from cozmo.util import degrees from time import sleep from gpiozero import LED led = LED(13) def cozmo_program(robot: cozmo.robot.Robot): robot.set_head_angle(degrees(30)).wait_for_completed() robot.say_text("Hello Raspbery pi").wait_for_completed() robot.set_lift_height(30.0).wait_for_completed() hello_rpi() robot.set_head_angle(degrees(0)).wait_for_completed() robot.set_lift_height(0.0).wait_for_completed() def hello_rpi(): for _ in range(3): led.on() sleep(1) led.off() sleep(1) cozmo.run_program(cozmo_program)
Now we have a specific function created to blink the red LED 3 times. This function will be called by Cozmo, after he salute the RPI. The below movie shows the result:
Step 11: Triggering Cozmo With External Events
Let's now, create an external and physical event, that will trigger a reaction on Cozmo. We will press a button and Cozmo will confirm this action to us as shown below:
Very cool, isn't? ;-)
The code is very simple:
import cozmo from cozmo.util import degrees from gpiozero import LED, Button from signal import pause led = LED(13) button = Button(20) def cozmo_program(robot: cozmo.robot.Robot): button.wait_for_press() led.on() robot.set_head_angle(degrees(30)).wait_for_completed() robot.say_text("Hello Raspbery pi. Button pressed").wait_for_completed() robot.set_head_angle(degrees(0)).wait_for_completed() led.off() cozmo.run_program(cozmo_program)
Step 12: More Interaction
Let's mixed actions form an external user and Cozmo, where he will move and use its backpack lights. This example is only to show that from now on, the sky is the limit!
OPS! The men went to the Moon already! So, no limit, folks! ;-)
The code for the above example can be seen here:
from gpiozero import LED, Button from time import sleep red = LED(13) yellow = LED(19) green = LED(26) button = Button(20) def cozmo_program(robot: cozmo.robot.Robot): robot.say_text("Hello Raspbery pi. Press the button").wait_for_completed() button.wait_for_press() robot.drive_straight(distance_mm(150), speed_mmps(50)).wait_for_completed() robot.turn_in_place(degrees(90)).wait_for_completed() red.on() robot.set_all_backpack_lights(cozmo.lights.red_light) robot.say_text("red").wait_for_completed() button.wait_for_press() red.off() yellow.on() robot.set_center_backpack_lights(cozmo.lights.white_light) robot.say_text("yellow").wait_for_completed() sleep(1) button.wait_for_press() yellow.off() green.on() robot.set_all_backpack_lights(cozmo.lights.green_light) robot.say_text("green").wait_for_completed() sleep(1) button.wait_for_press() green.off() robot.set_all_backpack_lights(cozmo.lights.off_light) robot.say_text("bye, bye Raspbery pi").wait_for_completed() robot.turn_in_place(degrees(90)).wait_for_completed() robot.drive_straight(distance_mm(150), speed_mmps(50)).wait_for_completed() robot.turn_in_place(degrees(180)).wait_for_completed() cozmo.run_program(cozmo_program)
Step 13: Conclusion
As always, I hope this project can help others find their way into the exciting world of electronics!
For more projects, please visit my blog: MJRoBot.org
Saludos from the south of the world!
See you at my next instructable!