モーション#
導入#
Motors control how parts of a robot move. Motors can be used to raise an arm, turn a claw, spin a wheel, or move another part of a build. Two motors can work together as a drivetrain to move and turn the whole robot.
各モーターは「デバイス」ウィンドウで設定します。ビルドによっては、モーター名が変わる場合があります。例えば、Competition Advanced 2.0ビルドでは、モーター名は arm_motorとなっています。カスタムロボットでは、異なるモーター名を使用する場合があります。
デフォルトでは、 FORWARD モーターを反時計回りに回転させ、 REVERSE はモーターを時計回りに回転させます。デバイスウィンドウでモーターの回転方向を反転するように設定すると、回転方向が切り替わります。
モーターをプログラミングする方法は数多くあります。以下に、すべてのモーションメソッドの一覧を示します。
動作 — モーターを停止および回転させる。
spin— モーターを永久に正転または逆転させます。spin_for— モーターを特定の距離だけ回転させます。spin_to_position— モーターを特定の位置まで回転させます。stop— モーターの回転を停止します。
ミューテーター — モーター設定を調整します。
set_velocity— モーターに回転速度を指示します。set_max_torque— モーターが回転中にどれだけの力で押せるかを設定します。set_position— モーターの現在の位置を新しい値に変更します。set_stopping— モーターがどのように停止するかを設定します: ブレーキ、惰性走行、または保持。set_timeout— モーターが動作を完了しようとする時間を設定します。
ゲッター — モーターの状態を確認してください。
get_position— モーターの現在位置を返します。get_velocity— モーターの回転速度を-100%から100%までのパーセンテージで返します。get_current— モーターがバッテリーからどれだけの電力を使用しているかを、0% から 100% のパーセンテージで返します。is_stopped— モーターが動作を終了したかどうかをブール値として返します。is_moving— モーターが回転しているかどうかをブール値として返します。
行動#
spin#
spin モーターを正転または逆転させ、永久に回転させます。モーターは、別の方向に回転させたり停止させたりするなど、別の動作が与えられるまで回転し続けます。
Usage:
arm_motor.spin(direction)
パラメータ |
説明 |
|---|---|
|
モーターの回転方向: |
# Build Used: Competition Advanced 2.0
def main():
# Raise the ArmMotor, then stop
arm_motor.spin(REVERSE)
wait(1, SECONDS)
arm_motor.stop()
# Start threads — Do not delete
start_thread(main)
spin_for#
spin_for は、モーターを特定の距離だけ回転させます。回転はモーターの現在位置を基準としています。プロジェクトは、モーターの回転が終了するまで待機してから、次のコードを実行します。
Usage:
arm_motor.spin_for(direction, angle, units, wait)
パラメータ |
説明 |
|---|---|
|
モーターの回転方向: |
|
モーターが回転する距離。0 |
|
オプション。距離の単位: |
|
オプション |
# Build Used: Competition Advanced 2.0
def main():
# Raise and lower the ArmMotor
arm_motor.spin_for(REVERSE, 180, DEGREES)
wait(1, SECONDS)
arm_motor.spin_for(FORWARD, 180, DEGREES)
# Start threads — Do not delete
start_thread(main)
spin_to_position#
spin_to_position はモーターを特定の位置まで回転させます。
モーターの位置は、回転した角度を表し、 DEGREES または TURNSで測定されます。1回転は360度に相当します。プロジェクトの開始時には、モーターの位置は0度に設定されます。モーターの位置は、set_positionメソッドを使用して設定することもできます。
位置値は絶対値です。つまり、回転方向はモーターの現在位置によって決まります。
例えば、モーターが0度の位置から始まり、720度の位置まで回転した場合、2回転前進します。その後、360度の位置まで回転した場合、360度は720度より小さいため、1回転後退します。
Usage:
arm_motor.spin_to_position(angle, units, wait)
パラメータ |
説明 |
|---|---|
|
モーターが回転する位置の値 |
|
オプション。位置単位: |
|
オプション |
# Build Used: Competition Advanced 2.0
def main():
# Return ArmMotor to 0 degrees after raising
arm_motor.spin_for(REVERSE, 180, DEGREES)
wait(1, SECONDS)
arm_motor.spin_to_position(0, DEGREES)
# Start threads — Do not delete
start_thread(main)
stop#
stop モーターの回転を停止します。
Usage:
arm_motor.stop()
パラメータ |
説明 |
|---|---|
このメソッドにはパラメータがありません。 |
# Build Used: Competition Advanced 2.0
def main():
# Raise the ArmMotor
arm_motor.spin(REVERSE)
wait(1, SECONDS)
arm_motor.stop()
# Start threads — Do not delete
start_thread(main)
変異誘発者#
set_velocity#
set_velocity は、モーターの回転速度を指定します。パーセンテージが高いほどモーターは速く回転し、低いほど遅く回転します。
すべてのプロジェクトは、各モーターがデフォルトで50%の速度で回転する状態から始まります。
注: 速度を速くするとモーターの回転速度は速くなりますが、精度が低下する可能性があります。速度を遅くするとモーターの回転速度は遅くなりますが、精度は向上する可能性があります。
Usage:
arm_motor.set_velocity(velocity)
パラメータ |
説明 |
|---|---|
|
回転速度は0%から100%までです。これは |
# Build Used: Competition Advanced 2.0
def main():
# Move the ArmMotor at different velocities
arm_motor.spin_for(REVERSE, 180, DEGREES)
wait(1, SECONDS)
# Move slow
arm_motor.set_velocity(20)
arm_motor.spin_for(FORWARD, 180, DEGREES)
wait(1, SECONDS)
# Move fast
arm_motor.set_velocity(100)
arm_motor.spin_for(REVERSE, 180, DEGREES)
# Start threads — Do not delete
start_thread(main)
set_max_torque#
トルクとは回転力のことです。モーターが回転する際に、どれだけの力で押したり引いたりできるかを示します。
set_max_torque は、モーターが使用できる最大トルクを設定します。
パーセンテージを高くすると、重い物を持ち上げる時など、モーターがより強く押すことができます。パーセンテージを低くすると、モーターが押せる力が制限されます。これは、モーターが詰まったり、可動範囲の限界に達したりした場合に、ロボットを保護するのに役立ちます。
すべてのプロジェクトは、各モーターのトルクがデフォルトで50%に設定された状態から始まります。
Usage:
arm_motor.set_max_torque(value)
パラメータ |
説明 |
|---|---|
|
モーターが使用できる最大トルクは0%から100%までです。これは、整数 |
# Build Used: Competition Advanced 2.0
def main():
# Move the ArmMotor at different torques
arm_motor.spin_for(REVERSE, 180, DEGREES)
wait(1, SECONDS)
# Move with less torque
arm_motor.set_max_torque(20)
arm_motor.spin_for(FORWARD, 180, DEGREES)
wait(1, SECONDS)
# Move with maximum torque
arm_motor.set_max_torque(100)
arm_motor.spin_for(REVERSE, 180, DEGREES)
# Start threads — Do not delete
start_thread(main)
set_position#
A motor’s position is how far it has spun, measured in DEGREES or TURNS. One turn is equal to 360 degrees. set_position changes the motor’s current position to a new value.
例えば、モーターが180度まで回転している場合、位置を0度に設定すると、その位置は180度から0度にリセットされます。その後、モーターはその新しい値に基づいて回転できるようになります。
Usage:
arm_motor.set_position(position, units)
パラメータ |
説明 |
|---|---|
|
モーターに設定する位置値 |
|
オプション。位置単位: |
# Build Used: Competition Advanced 2.0
def main():
# Return ArmMotor to 0 degrees after raising
arm_motor.set_position(0, DEGREES)
arm_motor.spin_for(REVERSE, 180, DEGREES)
wait(1, SECONDS)
arm_motor.spin_to_position(0, DEGREES)
# Start threads — Do not delete
start_thread(main)
set_stopping#
set_stopping は、モーターがどのように停止するかを設定します。ブレーキ、惰性走行、または保持のいずれかです。
Usage:
arm_motor.set_stopping(mode)
パラメータ |
説明 |
|---|---|
|
モーターの停止方法:
|
例えば、Competition Advanced 2.0ビルドを使用し、 arm_motor を HOLDに設定した場合、モーターは次のアームの動きが発生するまでアームを現在の位置に保持します。つまり、アームが上がった状態でモーターが停止した場合、モーターは重力に逆らってアームを空中に保持します。
この方法を使用しない場合、モーターは停止時に BRAKE を使用します。
# Build Used: Competition Advanced 2.0
def main():
# Move the ArmMotor, then coast to a stop
arm_motor.set_velocity(20)
arm_motor.set_stopping(COAST)
arm_motor.spin(REVERSE)
wait(1, SECONDS)
arm_motor.stop()
# Start threads — Do not delete
start_thread(main)
set_timeout#
set_timeout set_timeout は、モーターが動作を完了するまでの試行時間を設定します。モーターがその時間内に動作を完了できない場合、試行を停止して次のコード行に進みます。これにより、モーターが動作中に停止してしまうのを防ぎます。
Usage:
arm_motor.set_timeout(value, units)
パラメータ |
説明 |
|---|---|
|
The amount of time the motor can try to finish a movement. This can be a positive |
|
オプション。時間の単位: |
# Build Used: Competition Advanced 2.0
def main():
# Move ArmMotor as far as possible for 1 second before resetting
arm_motor.set_position(0, DEGREES)
arm_motor.set_timeout(1, SECONDS)
arm_motor.spin_for(REVERSE, 180, DEGREES)
arm_motor.spin_to_position(0, DEGREES)
# Start threads — Do not delete
start_thread(main)
ゲッター#
get_position#
A motor’s position is how far it has spun, measured in DEGREES or TURNS. One turn is equal to 360 degrees. get_position returns the motor’s current position.
プロジェクト開始時、モーターの位置は0度に設定されます。モーターが1回転すると、位置は360度、つまり1回転となります。モーターが逆方向に回転すると、位置はマイナスになります。
Usage:
arm_motor.get_position(units)
パラメータ |
説明 |
|---|---|
|
オプション。モーター位置を戻す単位: |
# Build Used: Competition Advanced 2.0
def main():
# Display how far the ArmMotor moves after one second
arm_motor.set_position(0, DEGREES)
console.print("Start: ")
console.print(arm_motor.get_position(DEGREES))
console.new_line()
arm_motor.spin(REVERSE)
wait(1, SECONDS)
arm_motor.stop()
console.print("After: ")
console.print(arm_motor.get_position(DEGREES))
# Start threads — Do not delete
start_thread(main)
get_velocity#
get_velocity は、モーターの回転速度を -100% から 100% までのパーセンテージで返します。
正の値はモーターが正方向に回転していることを意味し、負の値はモーターが逆方向に回転していることを意味します。
Usage:
arm_motor.get_velocity()
パラメータ |
説明 |
|---|---|
このメソッドにはパラメータがありません。 |
# Build Used: Competition Advanced 2.0
def main():
# Display the ArmMotor's velocity when it moves
console.print("Resting: ")
console.print(arm_motor.get_velocity())
console.new_line()
arm_motor.spin(REVERSE)
wait(1, SECONDS)
console.print("Moving: ")
console.print(arm_motor.get_velocity())
arm_motor.stop()
# Start threads — Do not delete
start_thread(main)
get_current#
get_current 、モーターがバッテリーからどれだけの電力を使用しているかを、0% から 100% までのパーセンテージで返します。
電流値が高いということは、モーターがより多くの電力を消費していることを意味します。これは、モーターが重い物を持ち上げているとき、物体に押し付けられているとき、または動かなくなってしまったときに発生する可能性があります。
Usage:
arm_motor.get_current()
パラメータ |
説明 |
|---|---|
このメソッドにはパラメータがありません。 |
# Build Used: Competition Advanced 2.0
def main():
# Display the motor's current when it moves
console.print("Resting: ")
console.print(arm_motor.get_current())
console.new_line()
arm_motor.spin(REVERSE)
wait(1, SECONDS)
console.print("Moving: ")
console.print(arm_motor.get_current())
arm_motor.stop()
# Start threads — Do not delete
start_thread(main)
is_stopped#
is_stopped は、モーターの動作が完了したかどうかをブール値で返します。これは、モーターの動作に基づいて他の動作のタイミングを制御するために使用できます。
True— モーターの動作が完了しました。False— モーターはまだ動いています。
このメソッドは、 wait パラメータを持つ次のモーションメソッドと連携して動作します: spin_for および spin_to_position。
Usage:
arm_motor.is_stopped()
パラメータ |
説明 |
|---|---|
このメソッドにはパラメータがありません。 |
# Build Used: Competition Advanced 2.0
def main():
# Turn the Eye Sensor's light on while ArmMotor is moving
arm_motor.spin_for(REVERSE, 180, DEGREES, wait=False)
wait(0.1, SECONDS)
while True:
if arm_motor.is_stopped():
eye.set_light(OFF)
else:
eye.set_light(ON)
# Start threads — Do not delete
start_thread(main)
is_moving#
is_moving は、モーターが回転しているかどうかをブール値で返します。これは、モーターの動きに基づいて他の動作のタイミングを制御するために使用できます。
True— モーターが回転しています。False— モーターが回転していません。
このメソッドは、 wait パラメータを持つ次のモーションメソッドと連携して動作します: spin_for および spin_to_position。
Usage:
arm_motor.is_moving()
パラメータ |
説明 |
|---|---|
このメソッドにはパラメータがありません。 |
# Build Used: Competition Advanced 2.0
def main():
# Turn the Eye Sensor's light on while ArmMotor is moving
arm_motor.spin_for(REVERSE, 180, DEGREES, wait=False)
wait(0.1, SECONDS)
while True:
if arm_motor.is_moving():
eye.set_light(ON)
else:
eye.set_light(OFF)
# Start threads — Do not delete
start_thread(main)