モジュール motor

クラス Motors

初期化

# Import Motor class
from robot_hat import Motors

# Create Motor object
motors = Motors()

モーターを直接制御します。モーター1/2はPCBのマークに従います

# Motor 1 clockwise at 100% speed
motors[1].speed(100)
# Motor 2 counter-clockwise at 100% speed
motors[2].speed(-100)
# Stop all motors
motors.stop()

ハイレベル制御の設定を行います。ハイレベル制御は、単純な前進、後退、左、右、停止から、ジョイスティック制御、モーター方向のキャリブレーションなどの複雑な機能を提供します。

注釈

これらの設定は一度だけ実行する必要があり、設定ファイルに保存されます。次にMotorsクラスをロードするときは、設定ファイルからロードされます。

# Setup left and right motors
motors.set_left_id(1)
motors.set_right_id(2)
# Go forward and see if both motor directions are correct
motors.forward(100)
# if you found a motor is running in the wrong direction
# Use these function to correct it
motors.set_left_reverse()
motors.set_right_reverse()
# Run forward again and see if both motor directions are correct
motors.forward(100)

これでロボットを制御できます

import time

motors.forward(100)
time.sleep(1)
motors.backward(100)
time.sleep(1)
motors.turn_left(100)
time.sleep(1)
motors.turn_right(100)
time.sleep(1)
motors.stop()

API

class robot_hat.Motors(db='/root/.config/robot-hat/robot-hat.conf', *args, **kwargs)

ベースクラス: _Basic_class

__init__(db='/root/.config/robot-hat/robot-hat.conf', *args, **kwargs)

robot_hat.motor.Motorでモーターを初期化

パラメータ

db (str) -- 設定ファイルのパス

__getitem__(key)

特定のモーターを取得する

stop()

すべてのモーターを停止する

property left

左のモーター

property right

右のモーター

set_left_id(id)

左のモーターIDを設定します。この機能は一度だけ実行する必要があり、モーターIDを設定ファイルに保存し、クラスが初期化されるときにモーターIDをロードします。

パラメータ

id (int) -- モーターID(1または2)

set_right_id(id)

右のモーターIDを設定します。この機能は一度だけ実行する必要があり、モーターIDを設定ファイルに保存し、クラスが初期化されるときにモーターIDをロードします。

パラメータ

id (int) -- モーターID(1または2)

set_left_reverse()

左のモーターを逆回転に設定します。この機能は一度だけ実行する必要があり、逆回転の状態を設定ファイルに保存し、クラスが初期化されるときに逆回転の状態をロードします。

戻り値

現在逆回転しているかどうか

戻り値の型

bool

set_right_reverse()

右のモーターを逆回転に設定します。この機能は一度だけ実行する必要があり、逆回転の状態を設定ファイルに保存し、クラスが初期化されるときに逆回転の状態をロードします。

戻り値

現在逆回転しているかどうか

戻り値の型

bool

speed(left_speed, right_speed)

モーター速度を設定する

パラメータ
  • left_speed (float) -- 左モーターの速度(-100.0~100.0)

  • right_speed (float) -- 右モーターの速度(-100.0~100.0)

forward(speed)

前進

パラメータ

speed (float) -- モーター速度(-100.0~100.0)

backward(speed)

後退

パラメータ

speed (float) -- モーター速度(-100.0~100.0)

turn_left(speed)

左折

パラメータ

speed (float) -- モーター速度(-100.0~100.0)

turn_right(speed)

右折

パラメータ

speed (float) -- モーター速度(-100.0~100.0)

クラス Motor

# Import Motor class
from robot_hat import Motor, PWM, Pin

# Create Motor object
motor = Motor(PWM("P13"), Pin("D4"))

# Motor clockwise at 100% speed
motor.speed(100)
# Motor counter-clockwise at 100% speed
motor.speed(-100)

# If you like to reverse the motor direction
motor.set_is_reverse(True)

API

class robot_hat.Motor(pwm, dir, is_reversed=False)

ベースクラス: object

__init__(pwm, dir, is_reversed=False)

モーターを初期化する

パラメータ
speed(speed=None)

モーター速度の取得または設定

パラメータ

speed (float) -- モーター速度(-100.0~100.0)

set_is_reverse(is_reverse)

モーターの逆転を設定するかどうか

パラメータ

is_reverse (bool) -- True または False