慣性#
介紹#
The IQ (2nd gen) Brain includes a built-in Inertial Sensor. This sensor measures how the Brain is moving and turning.
慣性感測器由兩部分組成。陀螺儀測量轉向,例如航向、旋轉、陀螺儀速率和方向。加速度計測量運動變化,例如沿 x 軸、y 軸和 z 軸的加速或減速。
本頁面使用 brain_inertial 作為慣性感測器的範例名稱。請根據需要將其替換為您自己配置的名稱。
以下是所有可用方法的清單:
方向控制 — 讀取和控制航向、旋轉和感測器校準。
heading— 返回感測器朝向的方向,從 0 到 359.99 度。rotation— 返回感測器旋轉了多遠。calibrate— 校正慣性感測器。set_heading— 將慣性感測器的目前航向設定為新的航向值。set_rotation— 將慣性感測器的目前旋轉設定為新的旋轉值。is_calibrating— 返回慣性感測器目前是否正在校準。reset_heading— 將慣性感測器的目前航向重設為 0 度。reset_rotation— 將慣性感測器的目前旋轉重設為 0 度。changed— 註冊一個函數,當慣性感測器的航向改變時運行。
運動學-測量加速度、旋轉速度和傾斜度。
acceleration— 返回感知器在選定軸上加速或減速的速度。gyro_rate— 返回感測器在選定軸上的旋轉速度。orientation— 返回慣性感測器的橫滾角、俯仰角或偏航角。
建構函數 — 手動初始化和配置慣性感測器。
Inertial— 建立一個慣性感測器。
方向#
heading#
航向是指感測器朝向的方向,以度為單位,範圍從 0 到 359.99。0 航向 heading 目前航向。
初始航向為0度。如果感測器旋轉超過359.99度,航向將回落至0度。
Usage:
brain_inertial.heading(units)
參數 |
描述 |
|---|---|
|
可選。航向單位: |
# Display the heading after turning
drivetrain.turn_for(RIGHT, 450, DEGREES)
brain.screen.print(brain_inertial.heading())
rotation#
旋轉角度是指感測器轉動的程度,以度為單位。與航向不同,旋轉角度可以超過 359.99 度 rotation 返回慣性感測器的目前旋轉角度。
向右轉動會增加旋轉角度,向左轉動會減少旋轉角度。例如,向右轉動兩圈將旋轉 720 度。從 0 度向左轉一圈將旋轉 -360 度。
Usage:
brain_inertial.rotation(units)
參數 |
描述 |
|---|---|
|
可選。旋轉單位: |
# Display the rotation value after turning
drivetrain.turn_for(RIGHT, 450, DEGREES)
brain.screen.print(brain_inertial.rotation())
calibrate#
calibrate 用於校準慣性感測器。所有後續程式碼行將等待校準完成後再運行。校準透過測量和補償指定時間段內的感測器雜訊和漂移,幫助感測器正確測量轉彎。在此期間,大腦 必須完全靜止在穩定的表面上。校準期間的移動會導致結果不準確。
VEX Brain 會在每個專案開始時自動嘗試校準內建慣性感測器。但是,如果在專案啟動過程中機器人被搬運或移動,感測器可能無法正確校準或校準結果不準確。
Usage:
brain_inertial.calibrate()
範圍 |
描述 |
|---|---|
此方法沒有參數。 |
# Start calibration
brain_inertial.calibrate()
# Print after calibration
while brain_inertial.is_calibrating():
brain.screen.clear_screen()
brain.screen.set_cursor(1, 1)
brain.screen.print("Inertial Sensor")
brain.screen.next_row()
brain.screen.print("Calibrating")
wait(50, MSEC)
brain.screen.clear_screen()
brain.screen.set_cursor(1, 1)
brain.screen.print("Done!")
set_heading#
航向是指感測器朝向的方向,以度為單位,範圍從 0 到 359.99。 set_heading() 將慣性感測器的目前航向變更為新的航向值。
例如,如果感測器已轉向右側,將航向設定為 0 度,則該右側位置將成為新的 0 度。然後,感測器可以根據該新方向追蹤其他航向。
Usage:
brain_inertial.set_heading(value, units)
參數 |
描述 |
|---|---|
|
要設定的感測器航向值,單位為度。該值可以介於 0 到 359.99 之間。 |
|
可選。航向單位: |
# Turn the robot around
brain_inertial.set_heading(180)
drivetrain.turn_to_heading(0)
set_rotation#
旋轉角度是指感測器轉動的程度,以度為單位。與航向不同,旋轉角度可以超過 359.99 度,也可以低於 0 度。專案開始時,旋轉角度值設定為 0 度。 `set_rotation() set_rotation() 用於將慣性感測器的當前旋轉角度變更為新值。
例如,如果感測器向右旋轉了兩圈,其旋轉角度將為 720 度。將旋轉角度設為 0 度會將旋轉角度從 720 度重設為 0 度。然後,感測器可以根據這個新值追蹤旋轉。
Usage:
brain_inertial.set_rotation(value, units)
參數 |
描述 |
|---|---|
|
要設定的感測器旋轉值,單位為度。可以是整數或浮點數(十進制數)。 |
|
可選。旋轉單位: |
# Turn the robot around
brain_inertial.set_rotation(-180)
drivetrain.turn_to_rotation(0)
is_calibrating#
is_calibrating 返回慣性感測器目前是否正在校準。
True— 慣性感測器正在校準。False— 慣性感測器未進行校準。
Usage:
brain_inertial.is_calibrating()
範圍 |
描述 |
|---|---|
此方法沒有參數。 |
# Start calibration
brain_inertial.calibrate()
# Print while waiting for calibration
while brain_inertial.is_calibrating():
brain.screen.clear_screen()
brain.screen.set_cursor(1, 1)
brain.screen.print("Inertial Sensor")
brain.screen.next_row()
brain.screen.print("Calibrating")
wait(50, MSEC)
brain.screen.clear_screen()
brain.screen.set_cursor(1, 1)
brain.screen.print("Done!")
reset_heading#
reset_heading 將慣性感測器的目前航向重設為 0 度。
使用此方法後,感測器的當前方向將變為新的 0 度航向。
Usage:
brain_inertial.reset_heading()
參數 |
描述 |
|---|---|
此方法沒有參數。 |
# Turn the robot before and after resetting the heading
drivetrain.turn_to_heading(90, DEGREES)
wait(0.5, SECONDS)
brain_inertial.reset_heading()
drivetrain.turn_to_heading(90, DEGREES)
reset_rotation#
reset_rotation 將慣性感測器的目前旋轉角度重設為 0 度。
使用這種方法後,感測器會從新的 0 度旋轉值開始追蹤未來的旋轉。
Usage:
brain_inertial.reset_rotation()
參數 |
描述 |
|---|---|
此方法沒有參數。 |
# Turn the robot before and after resetting the rotation
drivetrain.turn_to_rotation(-90, DEGREES)
wait(0.5, SECONDS)
brain_inertial.reset_rotation()
drivetrain.turn_to_rotation(-90, DEGREES)
changed#
changed 註冊一個當慣性感測器的航向改變時要呼叫的函數。
Usage:
brain_inertial.changed(callback, arg)
參數 |
描述 |
|---|---|
|
當慣性感測器航向改變時運行的函數。 |
|
可選。要傳遞給回呼函數的參數元組。 |
def heading_changed():
brain.screen.set_cursor(1, 1)
brain.screen.clear_screen()
brain.screen.print("my heading ")
brain.screen.next_row()
brain.screen.print("has changed!")
wait(0.1, SECONDS)
# Call the function when the inertial heading is changed
wait(1, SECONDS)
drivetrain.turn_for(RIGHT, 90, DEGREES, wait=False)
brain_inertial.changed(heading_changed)
wait(15, MSEC)
運動#
acceleration#
加速度是指感測器加速或減速的快慢。加速度為 acceleration 返回所選軸上慣性感測器的加速度值,範圍從 -4.0 G 到 4.0 G,以浮點數形式表示。
G 相當於你靜止坐著時感受到的重力加速度。
該值可以是正數或負數,這取決於所選軸上的加速度方向。軸選項為 XAXIS YAXISZAXIS ZAXIS
Usage:
brain_inertial.acceleration(axis)
參數 |
描述 |
|---|---|
軸 |
測量加速度的軸:
|
# Display acceleration after moving
vexcode_brain_precision = 2
drivetrain.set_drive_velocity(100, PERCENT)
brain.screen.print("Resting: ")
brain.screen.next_row()
brain.screen.print(brain_inertial.acceleration(XAXIS))
brain.screen.next_row()
wait(1, SECONDS)
drivetrain.drive_for(FORWARD, 500, MM, wait=False)
wait(0.01, SECONDS)
brain.screen.print("Startup: ")
brain.screen.next_row()
brain.screen.print(brain_inertial.acceleration(XAXIS))
gyro_rate#
陀螺儀速率是指慣性感測器旋轉的速度。 gyro_rate 為 gyro_rate ,返回慣性感測器在所選軸上的目前旋轉速度。
該值可以是正數也可以是負數,這取決於感測器繞該軸旋轉的方向。
Usage:
brain_inertial.gyro_rate(axis, units)
參數 |
描述 |
|---|---|
|
返回陀螺儀速率的軸:
|
|
可選。陀螺儀速率單位:
|
# Display the z-axis gyro rate
drivetrain.turn(RIGHT)
wait(1, SECONDS)
brain.screen.print(brain_inertial.gyro_rate(ZAXIS, RPM))
drivetrain.stop()
orientation#
方向是指慣性感測器在選定轉動軸上的當前角度。方向 orientation 表示慣性感測器的俯仰角或偏航角。
橫滾、俯仰和偏航描述了感測器傾斜或旋轉的不同方式。
Usage:
brain_inertial.orientation(axis, units)
參數 |
描述 |
|---|---|
|
返回方向角:
|
|
可選。方向單位: |
# Display the roll, pitch, and yaw of the Brain as it
# is rotated by hand
while True:
brain.screen.clear_screen()
brain.screen.print(brain_inertial.orientation(OrientationType.ROLL))
brain.screen.next_row()
brain.screen.print(brain_inertial.orientation(OrientationType.PITCH))
brain.screen.next_row()
brain.screen.print(brain_inertial.orientation(OrientationType.YAW))
brain.screen.next_row()
brain.screen.set_cursor(1, 1)
wait(0.1, SECONDS)
建構函數#
建構函數用於手動建立 Inertial 對象,例如在 VEXcode 之外配置慣性感測器時。
Inertial#
Inertial 創建一個慣性感測器。
Usage:
Inertial(smartport)
範圍 |
描述 |
|---|---|
|
可選。如果使用 IQ(第二代)大腦 內建的慣性感測器,則無需智慧型連接埠。如果連接外部慣性感測器,請指定慣性感測器所連接的智慧型端口,格式為 |
# Create a new object "brain_inertial" with the
# Inertial class
brain_inertial = Inertial()
left_drive_smart = Motor(Ports.PORT1, 1.0, False)
right_drive_smart = Motor(Ports.PORT6, 1.0, True)
drivetrain = SmartDrive(left_drive_smart, right_drive_smart, brain_inertial, 200)
brain_inertial.set_heading(180)
drivetrain.turn_to_heading(0)


