Python is the most widely used language with the BrickPi. Below are instructions for getting started with the BrickPi and Python. If using the Dexter Industries image, you can skip most of the material below: Python examples and modules come fully installed.
The driver file is located under “BrickPi_Python” and is named “BrickPi.py”. BrickPi.py contains all of the necessary routines for commanding the motors and sensors, and collecting information from the BrickPi.
Compiling and Running
To run an example program such as the example for the DI-dCompass, open the command line, and change the directory to where the examples are located. Type the following command:
sudo python DI-dCompass.py
Note that the command “sudo” is necessary: the libraries used for serial communication between the Raspberry Pi and BrickPi requires administrator privileges. Wait, what’s sudo?
This library was originally written for Python 2.7.
Examples for Sensors
Example programs are written in python for a number of sensors, and the BrickPi LEDs.
We have two examples for making the LED’s blink, and for dimming the LEDS: “LED.py” and “LED PWM.py”
Examples are included for the following LEGO sensors:
- Color Sensor
- Motor Encoder
- Motor Run
- Touch Sensor
- Ultrasonic Sensor
- Analog Sensor (Sound, for example)
- EV3 Sensors
Examples are included for the following Dexter Industries Sensors:
We have developed the following example projects in our Python repository. Each one has more detailed information commented into the code about how setup and run the example:
- Robotic Arm – This example controls the robotic arm seen throughout the website. In this example, the robotic arm is controlled remotely through SSH.
- Shooter Robot Mouse – This example controls the shooter robot with a mouse. After running the program, the shooter can be controlled with a mouse.
- WiFi Car – This example controls the movement of a robotic car over WiFi, with keyboard commands.
- BrickPi Twitter Example – Setup your BrickPi to Tweet the temperature.
- Browser Controller Robot – Control your robot through a web-page. This project serves up a webpage for control in your browser.
- Thingspeak Temperature Logging – Allow LEGO to become part of the Internet of Things: push your temperature data up onto the Thingspeak website!
Advanced users might want to install or reinstall the Python drivers. We’ve left this documentation in place to help you do this. If using the Dexter Industries image, you can skip the steps below: Python examples and modules come fully installed.
Examples for Python are in github here: https://github.com/DexterInd/BrickPi_Python
Clone Github Repository
You can manually download a zip file from the Python drivers and examples page, or you can clone the github repository. Cloning the github repository is easy, and we have a detailed description of how to setup git and git going here.
To clone the BrickPi Python repository, connect your Raspberry Pi to the internet and enter the following command in the command line:
The most recent drivers and examples should be on your Raspberry Pi and ready to use.
The `BrickPi` module can be installed using the `setup.py` script included in the BrickPi_Python repository.
First, open a terminal program on the Raspberry Pi, and change directories to the location (directory) you cloned the BrickPi_Python.
Once there, we’ll install python setuptools, and then install the BrickPi Python module:
sudo apt-get install python-setuptools sudo python setup.py install
This will install the `BrickPi` module globally. To use the module in your own Python scripts, just import it:
Developed a driver for a sensor you don’t see up there? Tell us in the forums, or pull request through Github!