慣性#
介紹#
慣性感測器測量機器人的運動和轉向情況。
慣性感測器由兩部分組成。陀螺儀測量轉向,例如航向、旋轉、陀螺儀速率和方向。加速度計測量運動變化,例如沿 x 軸、y 軸和 z 軸的加速或減速。
以下是可用方法清單:
方向控制 — 讀取和控制航向、旋轉和感測器校準。
heading— 返回感測器朝向的方向,從 0 到 359.99 度。rotation— 返回感測器旋轉了多遠。gyro_rate— 返回感測器在選定軸上的旋轉速度。orientation— 返回慣性感測器的橫滾角、俯仰角或偏航角。calibrate— 校正慣性感測器。set_heading— 將慣性感測器的目前航向設定為新的航向值。set_rotation— 將慣性感測器的目前旋轉設定為新的旋轉值。reset_heading— 將慣性感測器的目前航向重設為 0 度。reset_rotation— 將慣性感測器的目前旋轉重設為 0 度。is_calibrating— 返回慣性感測器目前是否正在校準。changed— 註冊一個函數,當慣性感測器的值改變時運行。installed— 檢查慣性感知器是否已安裝。
運動——測量加速度、碰撞和轉向行為。
acceleration— 返回感知器在選定軸上加速或減速的速度。collision— 註冊一個當慣性感測器偵測到碰撞時要運行的函數。set_turn_type— 設定哪個轉彎方向回傳正航向值。get_turn_type— 傳回哪個轉彎方向回傳正航向值。
構造器
Inertial– 建立一個慣性感測器。
方向#
heading#
航向是指感測器朝向的方向,以度為單位,範圍從 0 到 359.99。0 航向 heading。
初始航向為0度。如果感測器旋轉超過359.99度,航向將回落至0度。
Usage:
inertial_1.heading(units)
參數 |
描述 |
|---|---|
|
可選。航向單位: |
# Display the heading after turning
drivetrain.turn_for(RIGHT, 450, DEGREES)
brain.screen.print(inertial_1.heading())
rotation#
旋轉角度是指感測器轉動的程度,以度為單位。與航向不同,旋轉角度可以超過 359.99 度 rotation 返回慣性感測器的目前旋轉角度(浮點數)。
向右轉動會增加旋轉角度,向左轉動會減少旋轉角度。例如,向右轉動兩圈將旋轉 720 度。從 0 度向左轉一圈將旋轉 -360 度。
Usage:
inertial_1.rotation(units)
參數 |
描述 |
|---|---|
|
可選。旋轉單位: |
# Display the rotation after turning
drivetrain.turn_for(RIGHT, 450, DEGREES)
brain.screen.print(inertial_1.rotation())
gyro_rate#
陀螺儀速率是指慣性感測器的旋轉速度。 gyro_rate gyro_rate ,返回所選軸上慣性感測器的目前旋轉速度,數值為浮點數。
該值可以是正數也可以是負數,這取決於感測器繞該軸旋轉的方向。
Usage:
inertial_1.gyro_rate(axis, units)
參數 |
描述 |
|---|---|
|
返回陀螺儀速率的軸:
|
|
可選。陀螺儀速率單位:
|
# Display the rate of change of
# rotation while turning
drivetrain.turn(RIGHT)
wait(1, SECONDS)
brain.screen.print(inertial_1.gyro_rate(AxisType.ZAXIS, VelocityUnits.DPS))
drivetrain.stop()
orientation#
方向是指慣性感測器在選定的轉動軸上的當前角度。方向 orientation 返回慣性感測器的橫滾角、俯仰角或偏航角,範圍從 -180.00 度到 180.00 度,以浮點數形式表示。
橫滾、俯仰和偏航描述了感測器傾斜或旋轉的不同方式。
Usage:
inertial_1.orientation(type, 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(inertial_1.orientation(AxisType.ROLL, DEGREES))
brain.screen.next_row()
brain.screen.print(inertial_1.orientation(AxisType.PITCH, DEGREES))
brain.screen.next_row()
brain.screen.print(inertial_1.orientation(AxisType.YAW, DEGREES))
brain.screen.next_row()
brain.screen.set_cursor(1, 1)
wait(0.1, SECONDS)
calibrate#
calibrate 校準慣性感測器。
校準有助於感測器正確測量轉彎角度。校準過程中請保持感測器靜止。如果在校準過程中感測器移動,則航向、旋轉、陀螺儀速率和方向值可能無法正確測量。
程式將等待校準完成後再執行下一行程式碼。
Usage:
inertial_1.calibrate()
參數 |
描述 |
|---|---|
此方法沒有參數。 |
# Start calibration.
inertial_1.calibrate()
# Print that the Inertial Sensor is calibrating while
# waiting for it to finish calibrating.
while inertial_1.is_calibrating():
brain.screen.clear()
brain.screen.print("Inertial Sensor Calibrating")
wait(50, MSEC)
set_heading#
航向是指感測器朝向的方向,以度為單位,範圍從 0 到 359.99。 set_heading 將慣性感測器的目前航向變更為新的航向值。
例如,如果感測器已轉向右側,將航向設定為 0 度,則該右側位置將成為新的 0 度。然後,感測器可以根據該新方向追蹤其他航向。
Usage:
inertial_1.set_heading(value, units)
參數 |
描述 |
|---|---|
|
要設定的感測器航向值,單位為度。該值可以介於 0 到 359.99 之間。 |
|
可選。航向單位: |
# Turn the robot around
inertial_1.set_heading(180, DEGREES)
drivetrain.turn_to_heading(0)
set_rotation#
旋轉角度是指感測器轉動的程度,以度為單位。與航向不同,旋轉角度可以超過 359.99 度,也可以低於 0 度。專案開始時,旋轉角度值設定為 0 度。 `set_rotation 會將慣性感測器的目前旋轉角度變更為新值。
例如,如果感測器向右旋轉了兩圈,其旋轉角度將為 720 度。將旋轉角度設為 0 度會將旋轉角度從 720 度重設為 0 度。然後,感測器可以根據這個新值追蹤旋轉。
Usage:
inertial_1.set_rotation(value, units)
參數 |
描述 |
|---|---|
|
要設定的感測器旋轉值,單位為度。可以是整數或浮點數(十進制數)。 |
|
可選。旋轉單位: |
# Turn the robot around
inertial_1.set_rotation(-180, DEGREES)
drivetrain.turn_to_rotation(0)
reset_heading#
reset_heading 將慣性感測器的目前航向重設為 0 度。
使用此方法後,感測器的當前方向將變為新的 0 度航向。
Usage:
inertial_1.reset_heading()
參數 |
描述 |
|---|---|
此方法沒有參數。 |
reset_rotation#
reset_rotation 將慣性感測器的目前旋轉角度重設為 0 度。
使用這種方法後,感測器會從新的 0 度旋轉值開始追蹤未來的旋轉。
Usage:
inertial_1.reset_rotation()
參數 |
描述 |
|---|---|
此方法沒有參數。 |
is_calibrating#
is_calibrating 返回慣性感測器目前是否正在校準。
True— 慣性感測器正在校準。False— 慣性感測器未進行校準。
Usage:
inertial_1.is_calibrating()
參數 |
描述 |
|---|---|
此方法沒有參數。 |
changed#
changed 註冊一個函數,當慣性感測器的值改變時,函數將會被呼叫。
Usage:
inertial_1.changed(callback, arg)
參數 |
描述 |
|---|---|
|
先前定義的函數,當慣性感測器的值改變時執行。 |
|
可選。包含要傳遞給回呼函數的參數的元組。有關更多信息,請參閱使用帶參數的函數。 |
def my_function():
brain.screen.print("Value changed!!")
# Call my_function whenever inertial_1's value changes
inertial_1.changed(my_function)
installed#
installed 檢查慣性感測器是否已安裝。
True— 慣性感測器已安裝。False— 慣性感測器未安裝。
Usage:
inertial_1.installed()
參數 |
描述 |
|---|---|
此方法沒有參數。 |
運動#
acceleration#
加速度是指感測器加速或減速的快慢。加速度為 acceleration 返回慣性感測器在所選軸上的加速度,範圍從 -4.0 G 到 4.0 G,以浮點數形式表示。
G 相當於你靜止坐著時感受到的重力加速度。
The value can be positive or negative depending on the direction of acceleration on the selected axis. The axis options are AxisType.XAXIS, AxisType.YAXIS, and AxisType.ZAXIS.
Usage:
inertial_1.acceleration(type)
參數 |
描述 |
|---|---|
|
測量加速度的軸:
|
# Return the acceleration for the Z axis of the Inertial Sensor
zaccel = inertial_1.acceleration(AxisType.ZAXIS)
collision#
collision 註冊一個函數,當慣性感測器偵測到突然衝擊或碰撞時,該函數將被呼叫。
Usage:
inertial_1.collision(callback, arg)
參數 |
描述 |
|---|---|
|
先前定義的函數,當偵測到碰撞時執行。 |
|
可選。包含要傳遞給回呼函數的參數的元組。有關更多信息,請參閱使用帶參數的函數。 |
def my_function():
brain.screen.print("Collision!")
# Call my_function whenever a collision is detected
inertial_1.collision(my_function)
set_turn_type#
set_turn_type 設定哪個轉彎方向回傳正航向值。
Usage:
inertial_1.set_turn_type(turntype)
參數 |
描述 |
|---|---|
|
設定的轉向類型:
|
get_turn_type#
get_turn_type 傳回哪個轉彎方向回傳正航向值。
LEFT— 左轉返回正航向值。RIGHT— 右轉返回正航向值。
Usage:
inertial_1.get_turn_type()
參數 |
描述 |
|---|---|
此方法沒有參數。 |
建構函數#
Inertial#
Inertial 創建一個慣性感測器。
Usage:
Inertial(smartport)
參數 |
描述 |
|---|---|
|
慣性感測器連接的智慧型端口,寫成 |
# Create a new object "inertial_1" with the Inertial class.
inertial_1 = Inertial(Ports.PORT1)