There are many ways to learn Python. A particularly funny way is to do it together with the little robot COZMO and its SDK for Python.
When you see COZMO in interaction for the first time, you have to think of WALL-E, the little robot from the Pixar movie of the same name. ANKI's developers have developed the robot in such a way that it reacts very naturally. He also looks cute. You just have to love him!
But COZMO is not just an expensive technical toy. There's much more. With an Android or iOS app you can program COZMO. There are three difficulty levels: the "Sandbox" mode, the "Constructor" mode and an SDK for Python.
The "Sandbox" mode is a simple sequential block system similar to Lego Boost. The "Constructor "mode can be used to develop more complex programs. Programming is also done with blocks, similar to Scratch. Both options are intuitive and easy to use on an iPad. My 10 years old son had no problems and started programming directly.
Install the COZMO SDK
In order to use the COZMO SDK for Python you need an iOS or Android smartphone with installed COZMO App, which is connected to the wifi network of the COZMO and at the same time tethered via USB cable to your computer. This setting sounds a bit strange but works quite well.
The SDK needs Python 3.5.1 or higher to run. I used a Python virtual env to try it out.
$ source P3ENV/bin/activate
Use pip to install the SDK within your venv.
$ pip install 'cozmo[camera]'
Your first Program: Let COZMO Drive in a Square
The first test program will COZMO let drive in a sqare with 150 mm and a speed of 50 mm/s.
Open a new Python file and name it whatever you want, in my case drive.py. Import the the basic SDK and the cozmo.utils for degrees, distance and speed.
import cozmo
from cozmo.util import degrees, distance_mm, speed_mmps
'''
COZMO drives in a square
'''
def squaredrive(robot: cozmo.robot.Robot):
for _ in range(4):
robot.drive_straight(distance_mm(150), speed_mmps(50)).wait_for_completed()
robot.turn_in_place(degrees(90)).wait_for_completed()
cozmo.run_program(squaredrive)
To run the program we have to start the COZMO app on our smartphone connected with COZMO via wifi. Open the settings menu (the gear icon) and enable the SDK. A terminal-like view appears. Now run the Python program on your computer.
$ python drive.py
The program does exactly what we wanted, COZMO drives in a square, yeah!
Due to the large number of libraries Python provides, the programming possibilities are almost infinite.
A good start is the COZMO SDK online documentation:
http://cozmosdk.anki.com/docs/index.html. Here, you can find installation guides and programming tutorials and downloads.
Another good resource is the COZMO SDK forum:
https://forums.anki.com/ (You have to register to use it.)