モジュール modules

クラス Ultrasonic

# Import Ultrasonic and Pin class
from robot_hat import Ultrasonic, Pin

# Create Motor object
us = Ultrasonic(Pin("D2"), Pin("D3"))

# Read distance
distance = us.read()
print(f"Distance: {distance}cm")

API

class robot_hat.modules.Ultrasonic(trig, echo, timeout=0.02)
__init__(trig, echo, timeout=0.02)

クラス ADXL345

# Import ADXL345 class
from robot_hat import ADXL345

# Create ADXL345 object
adxl = ADXL345()
# or with a custom I2C address
adxl = ADXL345(address=0x53)

# Read acceleration of each axis
x = adxl.read(adxl.X)
y = adxl.read(adxl.Y)
z = adxl.read(adxl.Z)
print(f"Acceleration: {x}, {y}, {z}")

# Or read all axis at once
x, y, z = adxl.read()
print(f"Acceleration: {x}, {y}, {z}")
# Or print all axis at once
print(f"Acceleration: {adxl.read()}")

API

class robot_hat.ADXL345(*args, address: int = 83, bus: int = 1, **kwargs)

ベースクラス: I2C

ADXL345モジュール

X = 0

X

Y = 1

Y

Z = 2

Z

__init__(*args, address: int = 83, bus: int = 1, **kwargs)

ADXL345を初期化する

パラメータ

address (int) -- ADXL345のアドレス

read(axis: int = None) Union[float, List[float]]

ADXL345から軸を読み取る

パラメータ

axis (int) -- 軸の値(g)を読み取る、ADXL345.X、ADXL345.YまたはADXL345.Z、すべての軸の場合はNone

戻り値

軸の値、またはすべての軸のリスト

戻り値の型

float/list

クラス RGB_LED

# Import RGB_LED and PWM class
from robot_hat import RGB_LED, PWM

# Create RGB_LED object for common anode RGB LED
rgb = RGB_LED(PWM(0), PWM(1), PWM(2), common=RGB_LED.ANODE)
# or for common cathode RGB LED
rgb = RGB_LED(PWM(0), PWM(1), PWM(2), common=RGB_LED.CATHODE)

# Set color with 24 bit int
rgb.color(0xFF0000) # Red
# Set color with RGB tuple
rgb.color((0, 255, 0)) # Green
# Set color with RGB List
rgb.color([0, 0, 255]) # Blue
# Set color with RGB hex string starts with “#”
rgb.color("#FFFF00") # Yellow

API

class robot_hat.RGB_LED(r_pin: PWM, g_pin: PWM, b_pin: PWM, common: int = 1)

シンプルな3ピンRGB LED

ANODE = 1

共通アノード

CATHODE = 0

共通カソード

__init__(r_pin: PWM, g_pin: PWM, b_pin: PWM, common: int = 1)

RGB LEDを初期化する

パラメータ
  • r_pin (robot_hat.PWM) -- 赤用のPWMオブジェクト

  • g_pin (robot_hat.PWM) -- 緑用のPWMオブジェクト

  • b_pin (robot_hat.PWM) -- 青用のPWMオブジェクト

  • common (int) -- RGB_LED.ANODEまたはRGB_LED.CATHODE、デフォルトはANODE

例外
  • ValueError -- 共通がANODEまたはCATHODEでない場合

  • TypeError -- r_pin、g_pin、またはb_pinがPWMオブジェクトでない場合

color(color: Union[str, Tuple[int, int, int], List[int], int])

RGB LEDに色を書き込む

パラメータ

color (str/int/tuple/list) -- 書き込む色、"#"で始まる16進数文字列、24ビット整数、または(red, green, blue)のタプル

クラス Buzzer

インポートと初期化

# Import Buzzer class
from robot_hat import Buzzer
# Import Pin for active buzzer
from robot_hat import Pin
# Import PWM for passive buzzer
from robot_hat import PWM
# import Music class for tones
from robot_hat import Music
# Import time for sleep
import time

music = Music()
# Create Buzzer object for passive buzzer
p_buzzer = Buzzer(PWM(0))
# Create Buzzer object for active buzzer
a_buzzer = Buzzer(Pin("D0"))

アクティブブザーのビープ音

while True:
    a_buzzer.on()
    time.sleep(0.5)
    a_buzzer.off()
    time.sleep(0.5)

パッシブブザーの簡単な使用方法

# Play a Tone for 1 second
p_buzzer.play(music.note("C3"), duration=1)
# take adventage of the music beat as duration
# set song tempo of the beat value
music.tempo(120, 1/4)
# Play note with a quarter beat
p_buzzer.play(music.note("C3"), music.beat(1/4))

パッシブブザーの手動制御

# Play a tone
p_buzzer.play(music.note("C4"))
# Pause for 1 second
time.sleep(1)
# Play another tone
p_buzzer.play(music.note("C5"))
# Pause for 1 second
time.sleep(1)
# Stop playing
p_buzzer.off()

曲を演奏する!ベイビーシャーク!

music.tempo(120, 1/4)

# Make a Shark-doo-doo function as is all about it
def shark_doo_doo():
    p_buzzer.play(music.note("C5"), music.beat(1/8))
    p_buzzer.play(music.note("C5"), music.beat(1/8))
    p_buzzer.play(music.note("C5"), music.beat(1/8))
    p_buzzer.play(music.note("C5"), music.beat(1/16))
    p_buzzer.play(music.note("C5"), music.beat(1/16 + 1/16))
    p_buzzer.play(music.note("C5"), music.beat(1/16))
    p_buzzer.play(music.note("C5"), music.beat(1/8))

# loop any times you want from baby to maybe great great great grandpa!
for _ in range(3):
    print("Measure 1")
    p_buzzer.play(music.note("G4"), music.beat(1/4))
    p_buzzer.play(music.note("A4"), music.beat(1/4))
    print("Measure 2")
    shark_doo_doo()
    p_buzzer.play(music.note("G4"), music.beat(1/8))
    p_buzzer.play(music.note("A4"), music.beat(1/8))
    print("Measure 3")
    shark_doo_doo()
    p_buzzer.play(music.note("G4"), music.beat(1/8))
    p_buzzer.play(music.note("A4"), music.beat(1/8))
    print("Measure 4")
    shark_doo_doo()
    p_buzzer.play(music.note("C5"), music.beat(1/8))
    p_buzzer.play(music.note("C5"), music.beat(1/8))
    print("Measure 5")
    p_buzzer.play(music.note("B4"), music.beat(1/4))
    time.sleep(music.beat(1/4))

API

class robot_hat.Buzzer(buzzer: Union[PWM, Pin])
__init__(buzzer: Union[PWM, Pin])

ブザーを初期化する

パラメータ

pwm (robot_hat.PWM/robot_hat.Pin) -- パッシブブザー用のPWMオブジェクトまたはアクティブブザー用のピンオブジェクト

on()

ブザーをオンにする

off()

ブザーをオフにする

freq(freq: float)

パッシブブザーの周波数を設定する

パラメータ

freq (int/float) -- ブザーの周波数、Music.NOTESを使用して音符の周波数を取得する

例外

TypeError -- アクティブブザーに設定した場合

play(freq: float, duration: float = None)

周波数を演奏する

パラメータ
  • freq (float) -- 演奏する周波数、Music.note()を使用して音符の周波数を取得できる

  • duration (float) -- 各音符の持続時間(秒)、Noneは連続して演奏することを意味する

例外

TypeError -- アクティブブザーに設定した場合

クラス Grayscale_Module

# Import Grayscale_Module and ADC class
from robot_hat import Grayscale_Module, ADC

# Create Grayscale_Module object, reference should be calculate from the value reads on white
# and black ground, then take the middle as reference
gs = Grayscale_Module(ADC(0), ADC(1), ADC(2), reference=2000)

# Read Grayscale_Module datas
datas = gs.read()
print(f"Grayscale Module datas: {datas}")
# or read a specific channel
l = gs.read(gs.LEFT)
m = gs.read(gs.MIDDLE)
r = gs.read(gs.RIGHT)
print(f"Grayscale Module left channel: {l}")
print(f"Grayscale Module middle channel: {m}")
print(f"Grayscale Module right channel: {r}")

# Read Grayscale_Module simple states
state = gs.read_status()
print(f"Grayscale_Module state: {state}")

API

class robot_hat.Grayscale_Module(pin0: ADC, pin1: ADC, pin2: ADC, reference: int = None)

3チャンネルグレースケールモジュール

LEFT = 0

左チャンネル

MIDDLE = 1

中央チャンネル

RIGHT = 2

右チャンネル

__init__(pin0: ADC, pin1: ADC, pin2: ADC, reference: int = None)

グレースケールモジュールを初期化する

パラメータ
  • pin0 (robot_hat.ADC/int) -- チャンネル0用のADCオブジェクトまたは整数

  • pin1 (robot_hat.ADC/int) -- チャンネル1用のADCオブジェクトまたは整数

  • pin2 (robot_hat.ADC/int) -- チャンネル2用のADCオブジェクトまたは整数

  • reference (1*3 list, [int, int, int]) -- 基準電圧

reference(ref: list = None) list

基準値の取得と設定

パラメータ

ref (list) -- 基準値、基準値を取得する場合はNone

戻り値

基準値

戻り値の型

list

read_status(datas: list = None) list

ライン状態の読み取り

パラメータ

datas (list) -- グレースケールデータのリスト、Noneの場合はセンサーから読み取り

戻り値

ライン状態のリスト、0は白、1は黒

戻り値の型

list

read(channel: int = None) list

チャンネルまたはすべてのデータを読み取る

パラメータ

channel (int/None) -- 読み取るチャンネル、すべてを読み取るには空のままにする。0、1、2またはGrayscale_Module.LEFT、Grayscale_Module.CENTER、Grayscale_Module.RIGHT

戻り値

グレースケールデータのリスト

戻り値の型

list