傳動系統#
介紹#
123 型機器人採用雙輪驅動系統,使其能夠行駛和轉彎。驅動系統控制機器人的運動方式。
傳動系統可以透過兩種方式測量距離:步數和毫米。一步相當於123格子上的一個方格。
傳動系統編碼方式有很多種。以下列出了所有傳動系統編碼方法:
動作-移動和轉向機器人。
drive— 使機器人無限向前或向後移動。drive_for— 使機器人向前或向後移動指定的距離。drive_until— 使機器人前進或後退,直到偵測到物體、碰撞或線路。turn— 讓機器人永遠向左或向右轉。turn_for— 使機器人向左或向右旋轉指定的度數。turn_to_heading— 將機器人轉向 -359 到 359 度之間的特定航向。stop— 停止機器人的移動。
變異器 — 調整傳動系統設定。
set_timeout— 設定機器人嘗試完成一次動作的時間。set_heading— 將機器人的目前航向改為新的航向。
獲取者 — 查看移動狀態。
is_stopped— 返回機器人是否停止移動,以布林值表示。get_heading— 返回機器人的目前航向,範圍從 0 到 359 度。
行動#
drive#
drive 使機器人無限前進或後退。機器人將持續移動,直到收到其他指令,例如轉向或停止。
Usage:
robot.drive(direction)
參數 |
描述 |
|---|---|
|
機器人移動方向: |
# Drive for 2 seconds.
robot.drive(FORWARD)
wait(2, SECONDS)
robot.stop()
drive_for#
drive_for 使機器人前進或後退指定距離。程式會等待機器人完成移動後才執行下一行程式碼。
Usage:
robot.drive_for(direction, distance, unit)
參數 |
描述 |
|---|---|
|
機器人移動方向: |
|
機器人行駛的距離。可以是整數或小數。 |
|
距離單位: |
# Drive back and forth.
robot.drive_for(FORWARD, 3, STEPS)
robot.drive_for(REVERSE, 3, STEPS)
drive_until#
drive_until 使機器人前進或後退,直到偵測到物體、碰撞或線條。
Usage:
robot.drive_until(direction)
參數 |
描述 |
|---|---|
|
機器人移動方向: |
# Reverse after a crash.
robot.drive_until(FORWARD)
robot.drive_for(REVERSE, 1, STEPS)
turn#
turn 表示機器人無限向左或向右轉向。機器人將持續轉向,直到收到其他指令,例如行駛或停止。
Usage:
robot.turn(direction)
參數 |
描述 |
|---|---|
|
機器人轉向的方向: |
# Turn for 2 seconds.
robot.turn(RIGHT)
wait(2, SECONDS)
robot.stop()
turn_for#
turn_for 參數使機器人向左或向右旋轉指定角度。程式會等待機器人完成旋轉後才執行下一行程式碼。
Usage:
robot.turn_for(direction, degrees)
參數 |
描述 |
|---|---|
|
機器人轉向的方向: |
|
需要旋轉的角度數。可以是整數或小數。 |
# Turn left, then turn around to the right.
robot.turn_for(LEFT, 90)
robot.turn_for(RIGHT, 180)
turn_to_heading#
turn_to_heading 使機器人轉向 -359 度到 359 度之間的特定航向。機器人將沿著最短路徑轉向以達到目標航向。
起始航向為0度。
程式會等到機器人完成轉向後才執行下一行程式碼。
Usage:
robot.turn_to_heading(heading)
參數 |
描述 |
|---|---|
|
機器人應面向的航向,範圍從 -359 度到 359 度。 |
# Turn to face the cardinal directions.
robot.turn_to_heading(90)
wait(2, SECONDS)
robot.turn_to_heading(180)
wait(2, SECONDS)
robot.turn_to_heading(270)
wait(2, SECONDS)
robot.turn_to_heading(0)
wait(2, SECONDS)
stop#
stop 停止機器人的移動。
Usage:
robot.stop()
參數 |
描述 |
|---|---|
此方法沒有參數。 |
# Stop driving after 4 seconds.
robot.drive(FORWARD)
wait(4, SECONDS)
robot.stop()
突變體#
set_timeout#
set_timeout 設定機器人嘗試完成一個動作的超時時間(以秒為單位)。如果機器人無法在規定時間內完成,它將停止嘗試並執行下一行程式碼。這可以防止機器人卡在某個動作中。
Usage:
robot.set_timeout(time, unit)
參數 |
描述 |
|---|---|
|
允許動作進行的時間量。可以是整數或小數。 |
|
時間單位: |
# Turn right after driving for 1 second.
robot.set_timeout(1, SECONDS)
robot.drive_for(FORWARD, 1000, MM)
robot.turn_for(RIGHT, 90)
set_heading#
set_heading 將機器人的目前航向變更為新的航向值。
例如,如果機器人轉向右側,將航向設定為 0 度,則該右側位置將成為新的 0 度。
Usage:
robot.set_heading(heading)
參數 |
描述 |
|---|---|
|
要設定的機器人航向值(以度為單位)。 |
# Face the new 0 degree heading.
robot.set_heading(90)
robot.turn_to_heading(0)
獲取器#
is_stopped#
is_stopped 回傳一個 Boolean,報告機器人是否已停止移動。
True— 機器人已停止。False— 機器人仍在移動。
Usage:
robot.is_stopped()
參數 |
描述 |
|---|---|
此方法沒有參數。 |
# Glow blue after a crash.
robot.drive_until(FORWARD)
while True:
if robot.is_stopped():
robot.led.glow(BLUE)
else:
robot.led.glow(GREEN)
wait(0.05, SECONDS)
get_heading#
get_heading 返回機器人目前面向的方向,以 0 到 359 度之間的整數表示。
機器人的初始航向為0度。
Usage:
robot.get_heading()
參數 |
描述 |
|---|---|
此方法沒有參數。 |
# Display the heading after turning.
robot.turn_for(RIGHT, 450)
console.print(robot.get_heading())