運動#

介紹#

馬達控制機器人各部件的運動。馬達可以用來抬起機械手臂、轉動機械爪、旋轉車輪或移動機器人的其他部件。兩個馬達可以協同工作,組成一個驅動系統,從而驅動整個機器人移動和旋轉。

每個馬達都在“設備”視窗中進行配置。根據版本不同,馬達名稱可能會有所變化。例如,Competition Advanced 2.0 版本中有一個名為 arm_motor馬達。自訂機器人可能會使用不同的馬達名稱。

預設情況下, FORWARD 表示馬達逆時針旋轉, REVERSE 表示馬達順時針旋轉。如果在「設備」視窗中將馬達設定為反轉,則旋轉方向將會互換。

VEX GO 電機,前進方向以加號表示,箭頭表示向左(或逆時針)旋轉。 VEX GO 馬達顯示反向,用減號和箭頭表示它向右(順時針)旋轉。

馬達程式設計方法有很多種。以下列出了所有運動控制方法:

操作-停止並旋轉馬達。

  • spin — 使馬達永遠正轉或反轉。

  • spin_for — 使馬達旋轉特定距離。

  • spin_to_position — 將馬達旋轉到特定位置。

  • stop — 停止馬達旋轉。

變異器 — 調整馬達設定。

  • set_velocity — 告訴馬達旋轉的速度。

  • set_max_torque — 設定馬達在旋轉時允許施加的最大扭力。

  • set_position — 將馬達的目前位置改為新值。

  • set_stopping — 設定馬達停止運動的方式:透過煞車、滑行或保持。

  • set_timeout — 設定馬達嘗試完成一次運動的時間。

Getters — 檢查馬達狀態。

  • get_position — 傳回馬達的目前位置。

  • get_velocity — 返回馬達旋轉的速度,以 -100% 到 100% 的百分比表示。

  • get_current — 傳回馬達從電池使用的功率百分比,從 0% 到 100%。

  • is_stopped — 返回馬達是否停止運轉,以布林值表示。

  • is_moving — 返回馬達是否旋轉,以布林值表示。

行動#

spin#

spin 轉或反轉永不停歇。馬達將持續旋轉,直到收到其他指令,例如改變旋轉方向或停止。

Usage:
arm_motor.spin(direction)

參數

描述

direction

馬達旋轉方向: FORWARD 或 REVERSE。

# 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)

參數

描述

direction

馬達旋轉方向: FORWARD 或 REVERSE。

angle

馬達旋轉的距離。 0 度 DEGREES 使用整數)。 2 圈 TURNS 可以使用整數或小數)。

units

可選。距離單位: DEGREES (預設)或 TURNS。

wait

可選 wait=True (預設值)表示程式會等待馬達停止旋轉後再執行下一行程式碼。 2 wait=False 表示程式會立即執行下一行程式碼。

# 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測量。一圈等於 360 度。在專案開始時,馬達位置設定為 0 度。也可以使用 set_position 方法設定馬達位置。

位置值是絕對值。這意味著旋轉方向取決於馬達的當前位置。

例如,如果馬達從0度開始旋轉到720度,它將正轉兩圈。如果它接著旋轉到360度,它將反轉一圈,因為360小於720。

Usage:
arm_motor.spin_to_position(angle, units, wait)

參數

描述

angle

馬達旋轉到的位置值。 0 度 DEGREES 使用整數)。 2 圈 TURNS 可以使用整數或小數)。

units

可選。位置單位: DEGREES (預設)或 TURNS。

wait

可選 wait=True (預設值)表示程式會等待馬達停止旋轉後再執行下一行程式碼。 2 wait=False 表示程式會立即執行下一行程式碼。

# 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)

參數

描述

velocity

旋轉速度,範圍從 0% 到 100%。可以是整數 integerfloat)。

# 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)

參數

描述

value

馬達可使用的最大扭矩,範圍從 0% 到 100%。這可以是整數 integerfloat)。

# 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#

馬達的位置是指它旋轉的距離,以 DEGREES 或 TURNS測量。 set_position 用於將馬達的目前位置變更為新值。

例如,如果馬達旋轉了 180 度,將位置設為 0 度會將該位置從 180 度重設為 0 度。然後,馬達可以根據這個新值旋轉到其他位置。

Usage:
arm_motor.set_position(position, units)

參數

描述

position

馬達位置值設定。 0 度 DEGREES 使用整數。 2 圈 TURNS 可以使用整數或小數。

units

可選。位置單位: DEGREES (預設)或 TURNS。

# 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)

參數

描述

mode

How the motor will stop:

  • BRAKE — Stops immediately.
  • COAST — Slows to a stop.
  • HOLD — Stops immediately and holds the motor’s position.

例如,如果使用 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 設定馬達嘗試完成一次動作的逾時時間。如果馬達無法在規定時間內完成,它將停止嘗試並執行下一行程式碼。這可以防止馬達卡在某個運動中。

Usage:
arm_motor.set_timeout(value, units)

參數

描述

value

The amount of time the motor can try to finish a movement. This can be a positive integer or decimal (float).

units

可選。時間單位: SECONDS (預設)或 MSEC (毫秒)。

# 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#

馬達的位置是指它旋轉了多少圈,以 DEGREES 或 TURNS測量。 get_position 傳回馬達的目前位置。

專案開始時,馬達位置設定為0度。如果馬達正轉一圈,則位置為360度或1圈。如果馬達反轉,則位置為負值。

Usage:
arm_motor.get_position(units)

參數

描述

units

可選。馬達位置的返回單位: DEGREES (預設)或 TURNS。

# 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 參數的 Motion 方法搭配使用: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 參數的 Motion 方法搭配使用: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)