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 (默认)— 毫米
  • 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 (默认)— 毫米
  • 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 (默认)— 毫米
  • 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

此函数不返回值。