Git Product home page Git Product logo

python-build-hat's Introduction

Build HAT

The Python Build HAT library supports the Raspberry Pi Build HAT, an add-on board for the Raspberry Pi computer, which allows control of up to four LEGO® TECHNIC™ motors and sensors included in the SPIKE™ Portfolio.

Hardware

The Build HAT provides four connectors for LEGO® Technic™ motors and sensors from the SPIKE™ Portfolio. The available sensors include a distance sensor, a colour sensor, and a versatile force sensor. The angular motors come in a range of sizes and include integrated encoders that can be queried to find their position.

The Build HAT fits all Raspberry Pi computers with a 40-pin GPIO header, including — with the addition of a ribbon cable or other extension device — Raspberry Pi 400. Connected LEGO® Technic™ devices can easily be controlled in Python, alongside standard Raspberry Pi accessories such as a camera module.

Documentation

Library documentation: https://buildhat.readthedocs.io

Hardware documentation: https://www.raspberrypi.com/documentation/accessories/build-hat.html

Projects and inspiration: https://projects.raspberrypi.org/en/pathways/lego-intro

Installation

To install the Build HAT library, enter the following commands in a terminal:

pip3 install buildhat

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()

Building locally

Using asdf:

asdf install

Then:

pip3 install . --user

Building the 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

python-build-hat's People

Contributors

chrisruk avatar grega avatar highflying avatar iwanimsand avatar lurch avatar mutesplash avatar per-oestergaard avatar rmj10 avatar seakrueger avatar topshed avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

python-build-hat's Issues

Look at exception produced when position set very frequently

build_hat.HubProtocolError: Unexpected reply to Output Goto Abs Position

Example that triggers issue:

from time import sleep
from build_hat import BuildHAT

bh = BuildHAT()
mouth_r = bh.port.C.motor
mouth_l = bh.port.D.motor

def move_mouth (l_pos,r_pos, speed=100):
    mouth_l.run_to_position(l_pos*-1,speed,blocking=False)
    mouth_r.run_to_position(r_pos,speed,blocking=False)

# Will crash after a while with
# build_hat.HubProtocolError: Unexpected reply to Output Goto Abs Position
while True:
    move_mouth(-20, -20)
    sleep(1)
    move_mouth(-45, -45)
    sleep(1)
    move_mouth(-45, -45)
    sleep(1)

Possible bug with setting motor position

Possible bug with setting motor position for both single motors and pair motors. If you run for rotations then set position it seems to go through more rotations than necessary to get to position

Test motor stalling

Test using:

bh.port.D.motor.run_for_degrees(3, 100)
bh.port.D.motor.run_for_degrees(201, 100)

Look at exception produced when motor is physically prevented from turning - provide a clearer / more useful error message

Just tried this and got:

Traceback (most recent call last):
  File "motor.py", line 6, in <module>
    motor.run_for_rotations(10)
  File "/home/pi/.local/lib/python3.7/site-packages/build_hat-0.3.0-py3.7-linux-armv7l.egg/buildhat/motors.py", line 30, in run_for_rotations
    self._motor.run_for_rotations(rotations, self.default_speed)
build_hat.HubProtocolError: Motor stalled

Is that an ok message?

Bug: When you do start / stop motor for N seconds, then run_to_position, doesn't work correctly - Firmware

#!/usr/bin/python3

import time
from signal import pause
from buildhat import Motor

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

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

motor.when_rotated = handle_motor

"""
When you do start / stop motor for N seconds, then run_to_position, doesn't work correctly
"""

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

print("Run to position 0")
motor.run_to_position(0)
time.sleep(3)

pause()

Need way to determine when hat is ready along with all sensors

In https://github.com/RaspberryPiFoundation/hub_module/blob/master/buildhat/devices.py currently
have a 'sleep' statement, which was necessary as sometimes the distance sensor wouldn't be detected without it.

Need to find a way to detect when a sensor is ready.

I notice in the following function there is also a delay present to: 'Give the HAT a chance to recognise what is attached to it'

Hub_init(HubObject *self, PyObject *args, PyObject *kwds)

The delay in this function could maybe be increased instead.

Deadlock while closing hat

Thread 1:

#0  0xb6ed1a3c in __GI___pthread_timedjoin_ex (threadid=3050890336, thread_return=0x0, abstime=<optimized out>, 
    block=<optimized out>) at pthread_join_common.c:89
#1  0xb6597b48 in i2c_close_hat () at src/i2c.c:1127
#2  0xb659479c in Hub_finalize (self=<BuildHAT at remote 0xb65dcb38>) at src/hubmodule.c:231
#3  0x001589a0 in finalize_garbage (collectable=0xbeffa370, collectable@entry=0xbeffa368)
    at ../Modules/gcmodule.c:703
#4  0x0015976c in collect (generation=generation@entry=2, n_collected=n_collected@entry=0x0, 
    n_uncollectable=n_uncollectable@entry=0x0, nofail=nofail@entry=1) at ../Modules/gcmodule.c:905
#5  0x0015a300 in _PyGC_CollectNoFail () at ../Modules/gcmodule.c:1605
#6  0x00123518 in PyImport_Cleanup () at ../Python/import.c:526
#7  0x00132058 in Py_FinalizeEx () at ../Python/pylifecycle.c:1195
#8  0x00032fd4 in pymain_main (pymain=pymain@entry=0xbeffa418) at ../Modules/main.c:3040
#9  0x00033068 in _Py_UnixMain (argc=2, argv=0xbeffa5b4) at ../Modules/main.c:3073
#10 0x0002c268 in main (argc=<optimized out>, argv=<optimized out>) at ../Programs/python.c:15

Thread 2:

#0  __GI___poll (timeout=-1, nfds=1, fds=0xb658ee30) at ../sysdeps/unix/sysv/linux/poll.c:29
#1  __GI___poll (fds=0xb658ee30, nfds=1, timeout=-1) at ../sysdeps/unix/sysv/linux/poll.c:26
#2  0xb65aa8d0 in get_callback (pitem=0xb658ee48) at src/callback.c:68
#3  0xb65aab04 in run_callbacks (arg=0x0) at src/callback.c:136
#4  0xb6ed0494 in start_thread (arg=0xb658f460) at pthread_create.c:486
#5  0xb6d3f578 in ?? () at ../sysdeps/unix/sysv/linux/arm/clone.S:73 from /lib/arm-linux-gnueabihf/libc.so.6
Backtrace stopped: previous frame identical to this frame (corrupt stack?)

Thread 3:

#0  futex_abstimed_wait_cancelable (private=<optimized out>, abstime=0xb5d8dd80, expected=0, 
    futex_word=0x47b138 <_PyRuntime+720>) at ../sysdeps/unix/sysv/linux/futex-internal.h:205
#1  __pthread_cond_wait_common (abstime=0xb5d8dd80, mutex=0x0, cond=0x47b110 <_PyRuntime+680>)
    at pthread_cond_wait.c:539
#2  __pthread_cond_timedwait (cond=0x47b110 <_PyRuntime+680>, mutex=0x0, abstime=abstime@entry=0xb5d8dd80)
    at pthread_cond_wait.c:667
#3  0x000f9ac4 in PyCOND_TIMEDWAIT (us=<optimized out>, mut=<optimized out>, cond=<optimized out>)
    at ../Python/condvar.h:90
#4  take_gil (tstate=tstate@entry=0xb4c00650) at ../Python/ceval_gil.h:208
#5  0x000f9d8c in PyEval_RestoreThread (tstate=tstate@entry=0xb4c00650) at ../Python/ceval.c:271
#6  0x001349b4 in PyGILState_Ensure () at ../Python/pystate.c:1067
#7  0xb659e7ac in port_attach_port (port_id=1 '\001', type_id=62, hw_revision=0xb4c007f7 "", 
    fw_revision=0xb4c007fb "") at src/port.c:855
#8  0xb6596794 in handle_attached_io_message (buffer=0xb4c007f0 "\017", nbytes=15) at src/i2c.c:567
#9  0xb65971e4 in handle_immediate (buffer=0xb4c007f0 "\017", nbytes=15) at src/i2c.c:860
#10 0xb6597464 in run_comms_rx (args=0x0) at src/i2c.c:943
#11 0xb6ed0494 in start_thread (arg=0xb5d8e460) at pthread_create.c:486
#12 0xb6d3f578 in ?? () at ../sysdeps/unix/sysv/linux/arm/clone.S:73 from /lib/arm-linux-gnueabihf/libc.so.6
Backtrace stopped: previous frame identical to this frame (corrupt stack?)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.