Getting Started with the Autonomous Kit for the Sphero RVR

Pages
Favorited Favorite 2

Software Overview: Part 2 - Python

Python is a popular and fairly intuitive programming language used by various industries, including Fortune 100 companies. What makes Python so popular isn't the simplicity of the high-level language architecture, but rather the adaptability to deploy code across various platforms. Python is an interpreted language, which essentially means that as long as a platform (eg. computer, server, etc.) has an interpreter, it can execute Python code. (*This is an over generalization of how Python operates; for more details check out this GeeksforGeeks article and the Inside The Python Virtual Machine eBook.) A perfect example of Python's cross-platform functionality is that a program written on a Windows PC does not need to be rewritten to work on a Mac or a Linux machine. This adaptability has allowed Python to spread across various markets and become widely adopted. Another benefit is that Python is developed under an OSI-approved open source license, making it freely usable and distributable, even for commercial use. Now, there are a few drawbacks to working with Python, but they are greatly overshadowed by its benefits for the purposes of this kit.

(*More information on Python can be found on the Python Software Foundation's website.)

How is Python used with the Raspberry Pi?

Python allows users to interact with the physical world through the Raspberry Pi's general purpose input/outputs (GPIO). The GPIO is an interface that users can electrically connect various sensors and devices to, which can be operated through the Raspberry Pi using Python. Essentially, this means that:

  1. Users can connect a GPS module (Like the one included in these kits) to the GPIO of the Raspberry Pi.
  2. Users can interface and control the GPIO with Python through the Raspbian OS.
  3. This grants users direct access to the GPS module with Python, allowing users to retrieve GPS coordinate data.

Now that we have established that Python is the software used to interface with the hardware connected to the Raspberry Pi, this section will briefly overview the Python application and how it is used with this kit. (This section, barely skims the surface of Python and how it can be used with the Raspberry Pi.)

(*More information on using Python with the Raspberry Pi can be found on the Raspberry Pi Foundation's website. For more information on the commands below, click on the bolded header/code box.)

Suggested Tutorials:

For users who have never used Python, this is a great tutorial to start with.

Python Programming Tutorial: Getting Started with the Raspberry Pi

June 27, 2018

This guide will show you how to write programs on your Raspberry Pi using Python to control hardware.

Installing Packages

pip3

A package management system for installing Python 3 packages hosted on PyPI.

  • pip3 install <Package Name>- Installs a Python package (and any linked dependencies) hosted on PyPI.
  • pip3 uninstall <Package Name>- Removes a Python package hosted on PyPI.
  • pip3 install --upgrade <Package Name>- Updates a Python package hosted on PyPI.

Python Applications and Tools

ipython3

A simple shell program for Python 3 to write and execute line by line Python instructions.

  • Once inside the shell, users can execute instructions line by line as if it were an actual Python script. This is useful for:
    • Code development.
    • Catching errors.
    • Debugging, where an output error might be suppressed.
  • On loops (eg. for or while) and if statements, users will need to hit Enter twice to close the loop or statement.
  • Ctrl + C- Terminate proccess (or keyboard interrupt).
  • exit() or Ctrl + D- Exit shell.
  • %save <Filename> <Line Numbers>- Save Line Numbers to Filename.py
    • %save -r test 1-10- Saves lines 1 to 10 to test.py

python3

Executes programs using Python 3.

  • python3 filename.py- Executes filename.py with Python 3.
  • Ctrl + C- Terminate proccess/script (or keyboard interrupt).

pipenv

A packaging tool for Python that is designed to consolidate and simplify a development workflow. This is primarily for the Sphero SDK.

  • pipenv shell- Launches shell for virtual environment.
  • exit or Ctrl + D- Exit shell.

Keyboard Commands

  • Ctrl + C- Kill process (sends interrupt command).
  • Ctrl + D- Exit (sends end of file command).