GPS感應器#

介紹#

gps 類用於控制和存取 V5 GPS(遊戲定位系統™)感測器的資料。此感測器可為 VEX 場地上的機器人提供精確的位置和姿態追蹤。它利用場地週邊的 GPS 場地程式碼即時計算機器人的 X 和 Y 座標以及航向。

GPS 感測器使用在「設備」視窗中配置 GPS 感測器時輸入的偏移量自動計算出的參考點來返回機器人的位置。

有關 GPS 感測器的更多信息,請閱讀 VEX 庫中的 將 GPS 感測器與 VEX V5 一起使用。

參考點作為 GPS 感測器本身的 (0, 0) 位置,GPS 方法傳回的所有座標、航向和運動都相對於此配置點。

VEX V5 GPS 感測器。

類別建構函數#

1 — 使用預設原點 (0, 0) 在指定的智慧型連接埠上建立一個 gps 物件。

gps(
  int32_t  index,
  double   heading_offset = 0,
  turnType dir = right );

2 — 在指定的智慧型連接埠上建立一個 gps 對象,並定義一個自訂原點位置。

gps(
  int32_t       index,
  double        ox,
  double        oy,
  distanceUnits units = mm,
  double        heading_offset = 0,
  turnType      dir = right );

類別析構函數#

銷毀 gps 物件並釋放相關資源。

~gps();

參數#

範圍

類型

描述

index

int32_t

GPS 感測器連接的智慧型端口,寫成 PORTx,其中 x 是端口號(例如, PORT1)。

ox

double

原點的X座標。

oy

double

原點的Y座標。

units

distanceUnits

ox 和 oy:

  • mm (默认)— 毫米
  • distanceUnits::in / inches — 英寸
  • distanceUnits::cm — 厘米

heading_offset

double

GPS感測器的旋轉偏移量(以度為單位),範圍從0.00到359.99。預設值為0。

dir

turnType

返回正航向值的方向:

  • right (預設)
  • left

範例#

// Create the gps instance
gps GPS1 = gps(
    PORT1,      // Smart Port 1
    180);       // heading_offset


// Create the gps instance
gps GPS1 = gps(
    PORT1,   // Smart Port 1
    50.00,   // ox
    100.00,  // oy
    mm,      // units
    180 );   // heading_offset

成員功能#

gps 類別包含以下成員函數:

  • calibrate — 校準 GPS 感測器。

  • setOrigin — 設定 GPS 座標系的原點位置。

  • setLocation — 設定 GPS 感測器的目前位置和方向。

  • xPosition — 傳回目前 X 位置。

  • yPosition — 傳回目前 Y 位置。

  • heading — 傳回目前標題。

  • acceleration — 傳回沿指定軸的線性加速度。

  • gyroRate — 傳回指定軸的角速度。

  • orientation — 傳回指定軸的方向。

  • quality — 傳回目前 GPS 訊號品質。

  • changed — 註冊一個在 GPS 資料變更時執行的回呼函數。

在呼叫任何 gps 成員函數之前,必須建立一個 gps 實例,如下所示:

/* This constructor is required when using VS Code.
GPS Sensor configuration is generated automatically
in VEXcode using the Device Menu. Replace the values
as needed. */

// Create the gps instance in Smart Port 1
gps GPS1 = gps(PORT1);

類別方法#

calibrate#

校準 GPS 感測器。這將把 GPS 配置的航向(根據其角度偏移)設定為傳動系統的目前航向。

在校準過程中,GPS感測器必須保持靜止。

Available Functions
void calibrate();

Parameters

此函數不接受任何參數。

Return Values

此函數不傳回值。

Examples
// Turn to face the lower Field wall using data from the GPS Sensor.
GPS1.calibrate();
Drivetrain.turnToHeading(180, degrees)

setOrigin#

設定GPS座標系的原點。

Available Functions

1 — 將原點設定為指定的 X 和 Y 位置。

void setOrigin( 
  double        x, 
  double        y, 
  distanceUnits units = mm );

2 — 將原點設定為感測器的目前位置。

void setOrigin();

Parameters

範圍

類型

描述

x

double

新原點位置的 X 座標。

y

double

新原點位置的Y座標。

units

distanceUnits

用於 x 和 y測量單位:

  • mm (預設)— mm8
  • distanceUnits::distanceUnits::cm — 公分
  • distanceUnits::in — 吋

Return Values

此函數不傳回值。

Notes

setLocation#

設定目前 GPS 位置和航向。

Available Functions

1 — 使用指定的距離和旋轉單位設定目前位置。

void setLocation(
  double        x,
  double        y,
  distanceUnits units = mm,
  double        angle = 0,
  rotationUnits units_r = degrees );

2 — 使用毫米和度設定目前位置。

void setLocation( double x, double y, double angle );

Parameters

範圍

類型

描述

x

double

當前位置的X座標。

y

double

當前位置的Y座標。

units

distanceUnits

用于 x 和 y测量单位:

  • mm (默认)— 毫米
  • distanceUnits::in / inches — 英寸
  • distanceUnits::cm — 厘米

angle

double

目前位置的航向值。

units_r

rotationUnits

角度的計量單位: angle:

  • deg (預設)— 度
  • rev — 轉數

Return Values

此函數不傳回值。

Notes

xPosition#

返回 GPS 感測器的目前 X 座標。

Available Functions
double xPosition( distanceUnits units = distanceUnits::mm );

Parameters

範圍

類型

描述

units

distanceUnits

傳回的 X 座標的計量單位:

  • mm (預設)— mm4
  • distanceUnits::distanceUnits::cm — 公分
  • distanceUnits::in — 吋

Return Values

傳回一個 double 的雙精確度浮點數,表示以指定單位表示的 x 座標相對於原點的偏移量。

Notes
  • X 座標是相對於當前原點測量的。

  • 可以使用 setOrigin 來變更原點。

Examples
// Drive slowly towards the center of the Field
Drivetrain.setDriveVelocity(20.0, percent);
Drivetrain.drive(forward);
// Stop driving when the GPS returns an x position
// near the center of the Field
waitUntil((GPS1.xPosition(mm) < 0.0));
Drivetrain.stop();

yPosition#

返回 GPS 感測器的目前 Y 座標。

Available Functions
double yPosition( distanceUnits units = distanceUnits::mm );

Parameters

範圍

類型

描述

units

distanceUnits

傳回的 Y 座標的計量單位:

  • mm (預設)— mm4
  • distanceUnits::distanceUnits::cm — 公分
  • distanceUnits::in — 吋

Return Values

傳回一個 double 雙精度浮點數,表示以指定單位表示的 Y 軸位置相對於原點的偏移量。

Notes
  • Y軸位置是相對於目前原點測量的。

  • 可以使用 setOrigin()來變更原點。

Examples
// Drive slowly towards the center of the Field
Drivetrain.setDriveVelocity(20.0, percent);
Drivetrain.drive(forward);
// Stop driving when the GPS reports an y position
// near the center of the Field
waitUntil((GPS1.yPosition(mm) < 0.0));
Drivetrain.stop();

heading#

返回 GPS 感測器的目前航向(偏航角)。

Available Functions
double heading( rotationUnits units = rotationUnits::deg );

Parameters

範圍

類型

描述

units

rotationUnits

傳回的航向值的計量單位:

  • deg (預設)— 度
  • rev — 轉

Return Values

傳回一個 double 之間的雙精度浮點數,表示以指定單位表示的目前航向。

Notes
  • 航向值介於 0 度到 360 度之間。

  • 正旋轉方向由構造過程中指定的 dir 參數 決定。

Examples
// Turn slowly to face the right wall
Drivetrain.setTurnVelocity(20.0, percent);
Drivetrain.turn(right);
// Stop turning when the GPS reports a heading 
// greater than 90 degrees
waitUntil((GPS1.heading() > 90.0));
Drivetrain.stop();

acceleration#

返回 GPS 感測器沿指定軸測量的線性加速度。

Available Functions
double acceleration( 
  axisType axis );

Parameters

範圍

類型

描述

axis

axisType

返回加速度的軸:

  • xaxis
  • yaxis
  • zaxis

Return Values

傳回一個 double 表示沿著指定軸的加速度,單位為 G(重力單位)。

Notes
  • 加速度值以 G 為單位報告,其中 1 G 等於地球表面的重力加速度。

gyroRate#

返回 GPS 感測器沿指定軸測量的角速度。

Available Functions
double gyroRate( 
  axisType      axis, 
  velocityUnits units );

Parameters

範圍

類型

描述

axis

axisType

返回角速度的軸:

  • xaxis
  • yaxis
  • zaxis

units

velocityUnits

返回角速度的計量單位:

  • dps — 度/秒
  • rpm — 轉/分

Return Values

傳回一個 double ,表示沿指定軸的角速度,單位為所選單位。

orientation#

返回 GPS 感知器在指定軸上的方向角。

Available Functions
double orientation( 
  orientationType axis, 
  rotationUnits   units );

Parameters

範圍

類型

描述

axis

orientationType

要檢索的姿態軸:

  • roll
  • pitch
  • yaw

units

rotationUnits

傳回的方向值的計量單位:

  • deg — 度
  • rev — 轉數

Return Values

傳回一個 double ,表示所選單位中指定軸的方向。

quality#

返回感知到的GPS訊號品質。

Available Functions
int32_t quality();

Parameters

此函數不接受任何參數。

Return Values

傳回一個 int32_t ,表示 GPS 訊號品質的百分比:

可能的值

描述

100

GPS感測器報告的所有位置和航向資料均有效。

~90

位置資料不再直接從 GPS 欄位程式碼計算,而是透過其他方式估算。

0 – 80

只有航向值有效。如果GPS場碼掃描不足,隨著時間的推移,訊號品質會持續下降,直到達到 0,此時GPS資料實際上已被凍結,不再有效。

changed#

註冊一個回呼函數,當 GPS 航向值變化時運行。

Available Functions
void changed( 
  void (* callback)(void) );

Parameters

範圍

類型

描述

callback

void (*)(void)

指向一個函數的指針,該函數將在 GPS 航向值改變時被呼叫。函數不能接受任何參數,且傳回 void。

Return Values

此函數不傳回值。