控制器#

介紹#

V5 控制器包含按鈕、兩個搖桿、一個震動馬達和一個螢幕。控制器方法可用於偵測按鈕按下、讀取搖桿移動、使控制器震動、啟用或停用已配置的控制器操作、在控制器事件發生時運行函數,或在控制器螢幕上顯示文字。

已配置的控制器操作是指在「設備」選單中設定的控制器行為。使用 remote_control_code_enabled 可在專案進行期間暫時啟用或停用這些已設定的操作。

本頁面使用 controller_1 作為範例控制器名稱。請根據需要將其替換為您自己配置的名稱。

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

操作 — 使控制器震動或啟用/停用已設定的控制器操作。

螢幕-在控制器螢幕上顯示文字和數字。

  • print — 在控制器畫面上顯示文字、數字或變數值。

  • set_cursor — 將遊標移到控制器畫面上的指定行和列。

  • column — 返回控制器畫面上的目前遊標列。

  • row — 返回控制器畫面上的目前遊標行。

  • clear_screen — 清除控制器畫面。

  • clear_row — 清除控制器螢幕上的一行。

  • next_row — 將遊標移到控制器畫面上的下一行。

Getter 函數-讀取按鈕狀態和搖桿位置。

  • .pressing — 傳回指定按鈕是否被按下。

  • .position — 傳回指定操縱桿軸的位置。

回調函數-當控制器輸入發生變化時執行的函數。

  • .pressed — 當按下指定的按鈕時執行一個函數。

  • .released — 當指定的按鈕被釋放時運行一個函數。

  • .changed — 當操縱桿的位置沿指定軸發生變化時運行函數。

建構子 — 手動建立 Controller 物件。

  • Controller — 建立一個 Controller 物件。

行動#

rumble#

rumble 在控制器上播放震動模式。在模式字串中,句點代表短震動,連字號代表長震動,空格代表停頓。

Usage:
controller_1.rumble(pattern)

範圍

描述

pattern

由句點、連字符和空格組成的字串,代表隆隆聲模式。

# Rumble the Controller with a short-short-long-long pattern
controller_1.rumble("..--")

remote_control_code_enabled#

remote_control_code_enabled 變數用於啟用或停用在「裝置」選單中配置的控制器操作。預設情況下,控制器配置的操作處於啟用狀態。

Usage:
remote_control_code_enabled = state

價值

描述

True

啟用控制器配置的操作。

False

停用控制器配置的操作。

# Drive forward or backward using the left joystick
remote_control_code_enabled = False

while True:
    if controller_1.axis3.position() > 0:
        drivetrain.drive(FORWARD)
    elif controller_1.axis3.position() < 0:
        drivetrain.drive(REVERSE)
    # Press A to use Controller configured actions again
    elif controller_1.buttonA.pressing():
        break
    else:
        drivetrain.stop()

    wait(20, MSEC)

remote_control_code_enabled = True

螢幕#

V5 控制器螢幕有 3 行 19 列,用於顯示文字和數字。

V5 控制器螢幕的螢幕截圖,顯示了可列印區域中的行和列。第 1 行第 1 列從左上角開始,第 3 行第 19 列位於右下角。

print#

controller_1.screen.print 使用目前遊標位置在控制器螢幕上顯示文字、數字或變數值。

Usage:
controller_1.screen.print(value, sep, precision)

範圍

描述

value

若要在控制器螢幕上顯示的文字、數字或變數值。可以用逗號分隔多個值來列印它們。

sep

可選。用於分隔值的字串。必須寫成關鍵字參數,例如 sep=”- “預設值為 ” “。

precision

可選。列印簡單數字時顯示的小數位數。必須以關鍵字參數的形式指定,例如 precision=2。預設值為 2。

# Print a number on the Controller screen
controller_1.screen.print(1)

# Print multiple numbers separated by hyphens
controller_1.screen.print(1, 2, 3, 4, sep="-")

# Print a number using one decimal place
controller_1.screen.print(1 / 3, precision=1)

set_cursor#

set_cursor 將遊標移到控制器螢幕上的指定行和列。下一個列印值將顯示在該位置。

Usage:
controller_1.screen.set_cursor(row, col)

範圍

描述

row

遊標所在行,從 1 到 3。

col

遊標所在列,從 1 到 19。

# Print text at row 2, column 1
controller_1.screen.set_cursor(2, 1)
controller_1.screen.print("Ready")

column#

column 返回目前將在控制器螢幕上列印文字的列。

Usage:
controller_1.screen.column()

參數

描述

此方法沒有參數。

# Print the current column number
controller_1.screen.set_cursor(2, 3)
controller_1.screen.print(controller_1.screen.column())

row#

row 返回目前行,文字將列印在控制器螢幕上。

Usage:
controller_1.screen.row()

參數

描述

此方法沒有參數。

# Print the current row number
controller_1.screen.set_cursor(2, 3)
controller_1.screen.print(controller_1.screen.row())

clear_screen#

clear_screen 清除控制器螢幕並將遊標移回起始位置。

Usage:
controller_1.screen.clear_screen()

參數

描述

此方法沒有參數。

# Print text, then clear the Controller screen
controller_1.screen.print("VEX V5")
wait(2, SECONDS)
controller_1.screen.clear_screen()

clear_row#

clear_row 清除控制器畫面上的一行。

Usage:
controller_1.screen.clear_row(row)

範圍

描述

row

可選。要清除的行,從 1 到 3如果沒有提供行,則清除目前遊標所在行。

# Clear row 2 on the Controller screen
controller_1.screen.clear_row(2)

next_row#

next_row 將遊標移到控制器畫面下一行的第 1 列。下一個列印值將顯示在該行。

Usage:
controller_1.screen.next_row()

參數

描述

此方法沒有參數。

# Print on two rows
controller_1.screen.print("VEX V5")
controller_1.screen.next_row()
controller_1.screen.print("Controller")

獲取器#

.pressing#

.pressing 返回控制器上的特定按鈕目前是否被按下。此方法必須在特定的按鈕物件上調用,例如 controller_1.buttonA。

  • True — 指定的按鈕正在被按下。

  • False — 指定的按鈕未按下。

用法:
此方法可以使用其中一個可用的按鈕物件:

V5 控制器的正面和背面,按鈕以黃色高亮顯示。控制器正面左側有上、下、左、右方向鍵,右側從 12 點鐘方向順時針方向依序為 X、A、B、Y 鍵。控制器背面左側和右側分別是 L1、L2、R1 和 R2 鍵。

按鈕

命令

buttonA

controller_1.buttonA.pressing() — A 按鈕

buttonB

controller_1.buttonB.pressing() — B 按鈕

buttonX

controller_1.buttonX.pressing() — X 按鈕

buttonY

controller_1.buttonY.pressing() — Y 按鈕

buttonDown

controller_1.buttonDown.pressing() — 向下 按鈕

buttonUp

controller_1.buttonUp.pressing() — 向上 按鈕

buttonLeft

controller_1.buttonLeft.pressing() — 左按鈕

buttonRight

controller_1.buttonRight.pressing() — 右按鈕

buttonL1

controller_1.buttonL1.pressing() — L1 按鈕

buttonL2

controller_1.buttonL2.pressing() — L2 按鈕

buttonR1

controller_1.buttonR1.pressing() — R1 按鈕

buttonR2

controller_1.buttonR2.pressing() — R2 按鈕

參數

描述

此方法沒有參數。

# Turn right while L1 is pressed
while True:
    if controller_1.buttonL1.pressing():
        drivetrain.turn(RIGHT)
    else:
        drivetrain.stop()

    wait(20, MSEC)

.position#

.position 返回操縱桿軸的位置,數值範圍為 -100 到 100。

必須在特定的軸物件上呼叫此方法,例如 controller_1.axis1。

用法:
此方法可以使用其中一個可用的軸物件:

一個 VEX V5 控制器,搖臂周圍的軸已標註。軸 1 和軸 2 位於右側搖桿周圍,軸 3 和軸 4 位於左側搖桿周圍。

軸

命令

axis1

controller_1.axis1.position() — 右搖桿的水平軸

axis2

controller_1.axis2.position() — 右搖桿的垂直軸

axis3

controller_1.axis3.position() — 左搖桿的垂直軸

axis4

controller_1.axis4.position() — 左搖桿的水平軸

參數

描述

此方法沒有參數。

# Turn with the left joystick
remote_control_code_enabled = False

while True:
    if controller_1.axis4.position() > 10:
        drivetrain.turn(RIGHT)
    elif controller_1.axis4.position() < -10:
        drivetrain.turn(LEFT)
    else:
        drivetrain.stop()

    wait(20, MSEC)

回呼函數#

.pressed#

.pressed 表示當指定的按鈕被按下時執行一個函數。一旦使用,每次按下該按鈕時,函數都會自動運行。

用法:
此方法可以使用其中一個可用的按鈕物件:

V5 控制器的正面和背面,按鈕以黃色高亮顯示。控制器正面左側有上、下、左、右方向鍵,右側從 12 點鐘方向順時針方向依序為 X、A、B、Y 鍵。控制器背面左側和右側分別是 L1、L2、R1 和 R2 鍵。

按鈕

命令

buttonA

controller_1.buttonA.pressed(callback, arg) — A 按鈕

buttonB

controller_1.buttonB.pressed(callback, arg) — B 按鈕

buttonX

controller_1.buttonX.pressed(callback, arg) — X 按鈕

buttonY

controller_1.buttonY.pressed(callback, arg) — Y 按鈕

buttonDown

controller_1.buttonDown.pressed(callback, arg) — 向下按鈕

buttonUp

controller_1.buttonUp.pressed(callback, arg) — 向上 按鈕

buttonLeft

controller_1.buttonLeft.pressed(callback, arg) — 左按鈕

buttonRight

controller_1.buttonRight.pressed(callback, arg) — 右按鈕

buttonL1

controller_1.buttonL1.pressed(callback, arg) — L1 按鈕

buttonL2

controller_1.buttonL2.pressed(callback, arg) — L2 按鈕

buttonR1

controller_1.buttonR1.pressed(callback, arg) — R1 按鈕

buttonR2

controller_1.buttonR2.pressed(callback, arg) — R2 按鈕

範圍

描述

callback

預先定義的 函數,每次按下指定的按鈕時執行。

arg

可選。包含要傳遞給回呼函數的參數的元組。有關更多信息,請參閱使用帶參數的事件。

# Drive forward when A is pressed
def drive_forward():
    drivetrain.drive_for(FORWARD, 100, MM)

controller_1.buttonA.pressed(drive_forward)

.released#

.released 會在指定的按鈕被釋放時運行一個函數。一旦使用,每次釋放該按鈕時,函數都會自動運行。

用法:
此方法可以使用其中一個可用的按鈕物件:

V5 控制器的正面和背面,按鈕以黃色高亮顯示。控制器正面左側有上、下、左、右方向鍵,右側從 12 點鐘方向順時針方向依序為 X、A、B、Y 鍵。控制器背面左側和右側分別是 L1、L2、R1 和 R2 鍵。

按鈕

命令

buttonA

controller_1.buttonA.released(callback, arg) — A 按鈕

buttonB

controller_1.buttonB.released(callback, arg) — B 按鈕

buttonX

controller_1.buttonX.released(callback, arg) — X 按鈕

buttonY

controller_1.buttonY.released(callback, arg) — Y 按鈕

buttonDown

controller_1.buttonDown.released(callback, arg) — 向下按鈕

buttonUp

controller_1.buttonUp.released(callback, arg) — 向上按鈕

buttonLeft

controller_1.buttonLeft.released(callback, arg) — 左按鈕

buttonRight

controller_1.buttonRight.released(callback, arg) — 右按鈕

buttonL1

controller_1.buttonL1.released(callback, arg) — L1 按鈕

buttonL2

controller_1.buttonL2.released(callback, arg) — L2 按鈕

buttonR1

controller_1.buttonR1.released(callback, arg) — R1 按鈕

buttonR2

controller_1.buttonR2.released(callback, arg) — R2 按鈕

範圍

描述

callback

預先定義的 函數,每次釋放指定的按鈕時都會執行。

arg

可選。包含要傳遞給回呼函數的參數的元組。有關更多信息,請參閱使用帶參數的事件。

# Drive backward when A is released
def back_up():
    drivetrain.drive_for(REVERSE, 100, MM)

controller_1.buttonA.released(back_up)

.changed#

.changed 會在操縱桿沿著指定軸的位置改變時執行一個函數。一旦啟用,函數會在每次操縱桿沿著該軸的位置變化時自動運行。

用法:
此方法可以使用其中一個可用的軸物件:

一個 VEX V5 控制器,搖臂周圍的軸已標註。軸 1 和軸 2 位於右側搖桿周圍,軸 3 和軸 4 位於左側搖桿周圍。

軸

命令

axis1

controller_1.axis1.changed(callback, arg) — 右搖桿的水平軸

axis2

controller_1.axis2.changed(callback, arg) — 右搖桿的垂直軸

axis3

controller_1.axis3.changed(callback, arg) — 左搖桿的垂直軸

axis4

controller_1.axis4.changed(callback, arg) — 左搖桿的水平軸

範圍

描述

callback

預先定義的 函數,每次操縱桿的位置沿著指定軸變化時運行。

arg

可選。包含要傳遞給回呼函數的參數的元組。有關更多信息,請參閱使用帶參數的事件。

# Play a sound when the left joystick moves
def tada_sound():
    brain.play_sound(SoundType.TADA)

controller_1.axis4.changed(tada_sound)

建構函數#

Controller#

Controller 建立一個控制器物件。只有在 VEXcode 之外配置控制器時才需要手動建立控制器物件。

Usage:
Controller(controller_type)

範圍

描述

controller_type

可選。要建立的控制器類型: PRIMARY 表示連接到大腦的主控制器,或 PARTNER 表示連接到大腦的第二個控制器。

# Create the primary Controller
controller_1 = Controller(PRIMARY)

# Create a partner Controller
controller_2 = Controller(PARTNER)