陀螺儀#

介紹#

陀螺儀感測器是一種運動感測器,用於偵測繞單軸的旋轉。它可以測量機器人的旋轉角度,從而實現對自身姿態和航向的追蹤。

本頁面使用 drivetrain_gyro 作為陀螺儀感測器名稱範例。請根據需要將其替換為您自己配置的名稱。

以下是所有可用方法的清單:

操作-校準或調整陀螺儀感測器的航向和旋轉角度。

  • calibrate — 校準陀螺儀感測器以實現穩定的航向追蹤。

  • set_heading — 將陀螺儀感測器的航向設定為指定值。

  • set_rotation — 設定陀螺儀感應器的旋轉值。

  • reset_heading — 將陀螺儀感測器的航向設定為 0。

  • reset_rotation — 將陀螺儀感測器的旋轉設定為 0。

獲取器 — 讀取陀螺儀感測器的航向、旋轉和狀態。

  • heading — 傳回目前標題。

  • rotation — 返回累積旋轉。

  • rate — 傳回繞 x、y 或 z 軸的角速度。

  • is_calibrating — 返回陀螺儀感測器是否正在校準。

  • installed — 返回陀螺儀感應器是否連接到大腦。

回調函數-當陀螺儀感測器偵測到運動或變化時運行程式碼。

  • changed — 註冊一個當陀螺儀感測器偵測到變化時要呼叫的函數。

構造函數-手動初始化和配置陀螺儀感測器。

  • Gyro — 建立一個陀螺儀感應器。

行動#

calibrate#

calibrate 校準陀螺儀感測器。所有後續程式碼行將等待校準完成後再執行。校準是一個內部程序,用於測量和補償感測器在指定時間段內的雜訊和漂移。在此期間,陀螺儀感測器必須保持完全靜止(即,放置在穩定的表面上,沒有任何外部移動)。校準期間的移動會導致結果不準確。

Usage:
calibrate()

範圍

描述

此方法沒有參數。

# Start Gyro Sensor calibration
drivetrain_gyro.calibrate()
# Print after calibration
while drivetrain_gyro.is_calibrating():
    wait(0.1, SECONDS)
brain.screen.print("Done!")

set_heading#

set_heading 將陀螺儀感應器的航向設定為特定值。

Usage:
set_heading(value, units)

參數

描述

價值

要設定的標題值。

單位

可選。用於表示新航向的單位:

  • DEGREES (預設)
  • TURNS
# Turn the robot around
drivetrain_gyro.set_heading(180)
drivetrain.turn_to_heading(0)

set_rotation#

set_rotation 將陀螺儀感應器的旋轉角度設定為特定值。

Usage:
set_rotation(value, units)

參數

描述

value

要設定的標題值。

units

可選。用於表示新旋轉值的單位:

  • DEGREES (預設)
  • TURNS
# Turn the robot around
drivetrain_gyro.set_rotation(-180)
drivetrain.turn_to_rotation(0)

reset_heading#

reset_heading 將陀螺儀感應器的航向重設為 0。

Usage:
reset_heading()

參數

描述

此方法沒有參數。

# Turn the robot before and after resetting the heading
drivetrain.turn_to_heading(90, DEGREES)
wait(0.5,SECONDS)
drivetrain_gyro.reset_heading()
drivetrain.turn_to_heading(90, DEGREES)

reset_rotation#

reset_rotation 將陀螺儀感應器的旋轉重設為 0。

Usage:
reset_rotation()

參數

描述

此方法沒有參數。

# Turn the robot before and after resetting the rotation
drivetrain.turn_to_rotation(-90, DEGREES)
wait(0.5,SECONDS)
drivetrain_gyro.reset_rotation()
drivetrain.turn_to_rotation(-90, DEGREES)

獲取器#

heading#

heading 返回陀螺儀感應器的目前航向。

Usage:
heading(units)

參數

描述

units

可選。用於表示航向的單位:

  • DEGREES (預設)
  • TURNS
# Display the heading after turning
drivetrain.turn_for(RIGHT, 450, DEGREES)
brain.screen.print(drivetrain_gyro.heading())

rotation#

rotation 返回陀螺儀感應器的目前旋轉角度。

Usage:
rotation(units)

參數

描述

units

可選。旋轉單位:

  • DEGREES (預設)
  • TURNS
# Display the rotation after turning
drivetrain.turn_for(RIGHT, 450, DEGREES)
brain.screen.print(drivetrain_gyro.rotation())

rate#

rate 返回陀螺儀感測器旋轉的目前變化率,單位為指定單位。

Usage:
rate(units)

參數

描述

units

可選。陀螺儀速率的單位:

  • DPS (預設)
  • RPM
  • PERCENT
# Display the rate of change of 
# rotation while turning
drivetrain.turn(RIGHT)
wait(1, SECONDS)
brain.screen.print(drivetrain_gyro.rate())
drivetrain.stop()

is_calibrating#

is_calibrating 檢查陀螺儀感測器目前是否正在校準。

  • True — 陀螺儀感測器正在校準。

  • False — 陀螺儀感測器未校準。

Usage:
is_calibrating()

範圍

描述

此方法沒有參數。

# Start Gyro Sensor calibration.
drivetrain_gyro.calibrate()
# Print after calibration
while drivetrain_gyro.is_calibrating():
    wait(0.1, SECONDS)
brain.screen.print("Done!")

installed#

installed 返回一個布林值,指示陀螺儀感應器是否連接到大腦。

  • True — 陀螺儀感應器與大腦相連。

  • False — 陀螺儀感測器未連接到大腦。

Usage:
installed()

參數

描述

此方法沒有參數。

# Display a message if the Gyro Sensor is
# installed.
if drivetrain_gyro.installed():
    brain.screen.print("Installed!")

打回來#

changed#

changed 註冊回調函數,用於在陀螺儀感測器的航向變更時執行此操作。

Usage:
changed(callback, arg)

參數

描述

callback

當陀螺儀感測器航向改變時要呼叫的回調函數。

arg

可選。要傳遞給回呼函數的參數元組。

def gyro_changed():
    brain.screen.clear_screen()
    brain.screen.set_cursor(1, 1)
    brain.screen.print("Heading: ")
    brain.screen.print(drivetrain_gyro.heading()) 
# Display the heading when the gyro detects a change
drivetrain.turn_for(RIGHT, 90, DEGREES, wait=False)
drivetrain_gyro.changed(gyro_changed)

建構函數#

建構函數用於手動建立 Gyro 對象,這是在 VEXcode 之外配置陀螺儀感測器所必需的。

Gyro#

Gyro 創建一個陀螺儀感應器。

Usage:
Gyro(smartport)

範圍

描述

smartport

陀螺儀感測器連接的智慧型端口,記為 PORTx,其中 x 是端口號。

# Create the Brain
brain = Brain()
# When constructing Gyro Sensor "drivetrain_gyro" with a 
# SmartDrive "drivetrain"
drivetrain_gyro = Gyro(Ports.PORT1)
drivetrain = SmartDrive(left_drive_smart, right_drive_smart, drivetrain_gyro, 200)

drivetrain_gyro.set_heading(180)
drivetrain.turn_to_heading(0)