Skip to content

SimUKdev/python-build-hat

Repository files navigation

Welcome

This Python module allows you to run the Raspberry Pi Build HAT. It includes detailed documentation -- see below for how to generate and read it.

The build_hat module provides a BuildHAT class that holds information on all of the devices attached to the HAT. Individual motors and sensors can be found under bh.port.X where X is the name of the port and bh is an instance BuildHAT. In addition there are callbacks to inform the code that devices have been attached to or detached from the HAT, status functions to determine exactly what device is on a port, and specialist functions to drive motors.

Build and install

To build the hub package, first create yourself a virtual environment (if you haven't already) and turn it on:

$ python3 -m venv hat_env
$ source hat_env/bin/activate

Now use the setup.py script to build and install the module:

(hub_env) $ ./setup.py build
...much wibbling...
(hub_env) $ ./setup.py install

You should now be able to "import hub" in a Python3 script and have the module available to you.

Optionally the code may be compiled with "DEBUG_UART=1" to enable logging of the UART traffic on the hub.

Documentation

Instructions for regenerating the documentation can be found in docs/README.md. Briefly, assuming you have the appropriate python modules installed:

$ (cd docs; make html)

will rebuild the documentation. The doc tree starts at docs/build/html/index.html

Usage

See the detailed documentation for the Python objects available.

import time
from signal import pause
from buildhat import Motor

motor = Motor('A')
motor.set_default_speed(30)

print("Position", motor.get_aposition())

def handle_motor(speed, pos, apos):
    print("Motor", speed, pos, apos)

motor.when_rotated = handle_motor

print("Run for degrees")
motor.run_for_degrees(360)

print("Run for seconds")
motor.run_for_seconds(5)

print("Run for rotations")
motor.run_for_rotations(2)

print("Start motor")
motor.start()
time.sleep(3)
print("Stop motor")
motor.stop()

pause()

Programming Bootloader

sudo apt install automake autoconf build-essential texinfo libtool libftdi-dev libusb-1.0-0-dev
git clone https://github.com/raspberrypi/openocd.git --recursive --branch rp2040 --depth=1
cd openocd
./bootstrap
./configure --enable-ftdi --enable-sysfsgpio --enable-bcm2835gpio
make -j4
sudo make install
  • Use the following command to program the bootloader:
openocd -s /usr/local/share/openocd/scripts -f interface/raspberrypi-swd.cfg -f target/rp2040.cfg -c "program bootloader.elf verify reset exit"
  • Currently need to place firmware.bin and signature.bin in /tmp for the Python library to load them

Install

sudo apt install python3-pip
pip3 install build_hat-*.whl

About

Build HAT Python library

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 100.0%