logo资料库

Buildhat库简介(Build-hat-python-library).pdf

第1页 / 共14页
第2页 / 共14页
第3页 / 共14页
第4页 / 共14页
第5页 / 共14页
第6页 / 共14页
第7页 / 共14页
第8页 / 共14页
资料共14页,剩余部分请下载后查看
Build HAT Python Library
Colophon
Legal Disclaimer Notice
License
Introduction
Installation
Usage
Programming Bootloader
Library
ColorSensor
ColorDistanceSensor
DistanceSensor
ForceSensor
Matrix
Motor
MotorPair
Build HAT Python Library Colophon © 2020 Raspberry Pi (Trading) Ltd. This documentation is licensed under a Creative Commons Attribution-NoDerivatives 4.0 International (CC BY-ND). build-date: 2021-10-22 build-version: githash: 91fd59e-dirty Legal Disclaimer Notice TECHNICAL AND RELIABILITY DATA FOR RASPBERRY PI PRODUCTS (INCLUDING DATASHEETS) AS MODIFIED FROM TIME TO TIME (“RESOURCES”) ARE PROVIDED BY RASPBERRY PI (TRADING) LTD (“RPTL) "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW IN NO EVENT SHALL RPTL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THE RESOURCES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. RPTL reserves the right to make any enhancements, improvements, corrections or any other modifications to the RESOURCES or any products described in them at any time and without further notice. The RESOURCES are intended for skilled users with suitable levels of design knowledge. Users are solely responsible for their selection and use of the RESOURCES and any application of the products described in them. User agrees to indemnify and hold RPTL harmless against all liabilities, costs, damages or other losses arising out of their use of the RESOURCES. RPTL grants users permission to use the RESOURCES solely in conjunction with the Raspberry Pi products. All other use of the RESOURCES is prohibited. No licence is granted to any other RPTL or other third party intellectual property right. HIGH RISK ACTIVITIES. Raspberry Pi products are not designed, manufactured or intended for use in hazardous environments requiring fail safe performance, such as in the operation of nuclear facilities, aircraft navigation or communication systems, air traffic control, weapons systems or safety-critical applications (including life support systems and other medical devices), in which the failure of the products could lead directly to death, personal injury or severe physical or environmental damage (“High Risk Activities”). RPTL specifically disclaims any express or implied warranty of fitness for High Risk Activities and accepts no liability for use or inclusions of Raspberry Pi products in High Risk Activities. Raspberry Pi products are provided subject to RPTL’s Standard Terms. RPTL’s provision of the RESOURCES does not expand or otherwise modify RPTL’s Standard Terms including but not limited to the disclaimers and warranties expressed in them. License The Build HAT Python library is released under the MIT License. Copyright (c) 2020-2021 Raspberry Pi Foundation Copyright (c) 2017-2021 LEGO System A/S - Aastvej 1, 7190 Billund, DK Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: Legal Disclaimer Notice 1
Build HAT Python Library The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. License 2
Build HAT Python Library Introduction The Build HAT library has been created to support the Raspberry Pi Build HAT, an add-on board for the Raspberry Pi computer which allows control of up to 4 LEGO® Technic™ motors and sensors included in the SPIKE™ Portfolio. Figure 1. The Raspberry Pi Build HAT Figure 2. The LEGO® LPF2 connector Other LEGO® devices may be supported if they use the LPF2 connector: In order to drive motors, your Raspberry Pi and Build HAT will need an external 8V power supply. For best results, use the official Raspberry Pi Build HAT power supply.  WARNING The API for the Build HAT is undergoing active development and is subject to change. An online version of this documentation can be found at https://buildhat.readthedocs.io/. Introduction 3
Build HAT Python Library Installation The Python library can be installed using pip, $ pip3 install buildhat-*.whl Alternatively it can be cloned from its Github repositry, $ git clone https://github.com/RaspberryPiFoundation/python-build-hat.git $ cd python-build-hat and installed. If using asdf first by, $ asdf install and then, $ pip3 install . --user Building the library, $ ./build.sh Installation 4
Build HAT Python Library Usage See the Library section for detailed documentation for the available Python objects. 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() Usage 5
Build HAT Python Library Programming Bootloader You can use openocd to program the bootloader. This can be installed by, $ 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 Then use the folloing 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" Programming Bootloader 6
Build HAT Python Library Library ColorSensor The LEGO® Education SPIKE™ Colour Sensor (LEGO® Colour Sensor 45605) can sort between 8 different colours and can measure reflected and ambient or natural light.  1 from buildhat import ColorSensor  2  3 color = ColorSensor('C')  4  5 print("HSV", color.get_color_hsv())  6 print("RGBI", color.get_color_rgbi())  7 print("Ambient", color.get_ambient_light())  8 print("Reflected", color.get_reflected_light())  9 print("Color", color.get_color()) 10 11 print("Waiting for color black") 12 color.wait_until_color("black") 13 print("Found color black") 14 15 print("Waiting for color white") 16 color.wait_until_color("white") 17 print("Found color white") 18 19 while True: 20 c = color.wait_for_new_color() 21 print("Found new color", c) ColorDistanceSensor The LEGO® Color and Distance Sensor 88007 can sort between six different colors and objects within 5 to 10 cm range  WARNING Support for this device is experimental and not all features are available yet.  1 from buildhat import ColorDistanceSensor  2  3 color = ColorDistanceSensor('C')  4  5 print("RGBI", color.get_color_rgb())  6 print("Ambient", color.get_ambient_light())  7 print("Reflected", color.get_reflected_light())  8 print("Color", color.get_color())  9 10 print("Waiting for color black") ColorSensor 7
分享到:
收藏