クラス Servo

# Import Servo class
from robot_hat import Servo

# Create Servo object with PWM object
servo0 = Servo("P0")

# Set servo to position 0, here 0 is the center position,
# angle ranges from -90 to 90
servo0.angle(0)

# Sweep servo from 0 to 90 degrees, then 90 to -90 degrees, finally back to 0
import time
for i in range(0, 91):
    servo0.angle(i)
    time.sleep(0.05)
for i in range(90, -91, -1):
    servo0.angle(i)
    time.sleep(0.05)
for i in range(-90, 1):
    servo0.angle(i)
    time.sleep(0.05)


# Servos are all controls with pulse width, some
# from 500 ~ 2500 like most from SunFounder.
# You can directly set the pulse width

# Set servo to 1500 pulse width (-90 degree)
servo0.pulse_width_time(500)
# Set servo to 1500 pulse width (0 degree)
servo0.pulse_width_time(1500)
# Set servo to 1500 pulse width (90 degree)
servo0.pulse_width_time(2500)

API

class robot_hat.Servo(channel, address=None, *args, **kwargs)

ベースクラス: PWM

サーボモータークラス

__init__(channel, address=None, *args, **kwargs)

サーボモータークラスを初期化する

パラメータ

channel (int/str) -- PWMチャンネル番号(0-14/P0-P14)

angle(angle)

サーボモーターの角度を設定する

パラメータ

angle (float) -- 角度(-90~90)

pulse_width_time(pulse_width_time)

サーボモーターのパルス幅を設定する

パラメータ

pulse_width_time (float) -- パルス幅時間(500~2500)