GPS传感器#
介绍#
gps 类用于控制和访问 V5 GPS(游戏定位系统™)传感器的数据。该传感器可为 VEX 场地上的机器人提供精确的位置和姿态跟踪。它利用场地周边的 GPS 场地代码实时计算机器人的 X 和 Y 坐标以及航向。
GPS 传感器使用在“设备”窗口中配置 GPS 传感器时输入的偏移量自动计算出的参考点来返回机器人的位置。
有关 GPS 传感器的更多信息,请阅读 VEX 库中的 将 GPS 传感器与 VEX V5 一起使用。
参考点作为 GPS 传感器自身的 (0, 0) 位置,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();
参数#
范围 |
类型 |
描述 |
|---|---|---|
|
|
GPS 传感器连接的智能端口,写成 |
|
|
原点的X坐标。 |
|
|
原点的Y坐标。 |
|
|
|
|
|
GPS传感器的旋转偏移量(以度为单位),范围从0.00到359.99。默认值为0。 |
|
|
返回正航向值的方向:
|
示例#
// 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 Functionsvoid calibrate();
此函数不接受任何参数。
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 Functions1 — 将原点设置为指定的 X 和 Y 位置。
void setOrigin( double x, double y, distanceUnits units = mm );
Parameters2 — 将原点设置为传感器的当前位置。
void setOrigin();
范围 |
类型 |
描述 |
|---|---|---|
|
|
新原点位置的 X 坐标。 |
|
|
新原点位置的Y坐标。 |
|
|
用于
|
此函数不返回值。
NotessetLocation#
设置当前 GPS 位置和航向。
Available Functions1 — 使用指定的距离和旋转单位设置当前位置。
void setLocation( double x, double y, distanceUnits units = mm, double angle = 0, rotationUnits units_r = degrees );
Parameters2 — 使用毫米和度设置当前位置。
void setLocation( double x, double y, double angle );
范围 |
类型 |
描述 |
|---|---|---|
|
|
当前位置的X坐标。 |
|
|
当前位置的Y坐标。 |
|
|
用于
|
|
|
当前位置的航向值。 |
|
|
角度的计量单位:
|
此函数不返回值。
NotesxPosition#
返回 GPS 传感器的当前 X 坐标。
Available Functionsdouble xPosition( distanceUnits units = distanceUnits::mm );
范围 |
类型 |
描述 |
|---|---|---|
|
|
返回的 X 坐标的计量单位:
|
返回一个 double 的双精度浮点数,表示以指定单位表示的 x 坐标相对于原点的偏移量。
X 坐标是相对于当前原点测量的。
可以使用
setOrigin更改原点。
// 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 Functionsdouble yPosition( distanceUnits units = distanceUnits::mm );
范围 |
类型 |
描述 |
|---|---|---|
|
|
返回的 Y 坐标的计量单位:
|
返回一个 double 的双精度浮点数,表示以指定单位表示的 Y 轴位置相对于原点的偏移量。
Y轴位置是相对于当前原点测量的。
可以使用
setOrigin()更改原点。
// 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 Functionsdouble heading( rotationUnits units = rotationUnits::deg );
范围 |
类型 |
描述 |
|---|---|---|
|
|
返回的航向值的计量单位:
|
返回一个 double 的值,表示以指定单位表示的当前航向。
航向值介于 0 度到 360 度之间。
正旋转方向由构造过程中指定的
dir参数 决定。
// 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 Functionsdouble acceleration(
axisType axis );
范围 |
类型 |
描述 |
|---|---|---|
|
|
返回加速度的轴:
|
返回一个 double 表示沿指定轴的加速度,单位为 G(重力单位)。
加速度值以 G 为单位报告,其中 1 G 等于地球表面的重力加速度。
gyroRate#
返回 GPS 传感器沿指定轴测量的角速度。
Available Functionsdouble gyroRate(
axisType axis,
velocityUnits units );
范围 |
类型 |
描述 |
|---|---|---|
|
|
返回角速度的轴:
|
|
|
返回角速度的计量单位:
|
返回一个 double ,表示沿指定轴的角速度,单位为所选单位。
orientation#
返回 GPS 传感器在指定轴上的方向角。
Available Functionsdouble orientation(
orientationType axis,
rotationUnits units );
范围 |
类型 |
描述 |
|---|---|---|
|
|
要检索的姿态轴:
|
|
|
返回的方向值的计量单位:
|
返回一个 double ,表示所选单位中指定轴的方向。
quality#
返回感知到的GPS信号质量。
Available Functionsint32_t quality();
此函数不接受任何参数。
Return Values返回一个 int32_t ,表示 GPS 信号质量的百分比:
可能的值 |
描述 |
|---|---|
|
GPS传感器报告的所有位置和航向数据均有效。 |
|
位置数据不再直接从 GPS 字段代码计算,而是通过其他方式估算。 |
|
只有航向值有效。如果GPS场码扫描不足,随着时间的推移,信号质量会持续下降,直至达到 |
changed#
注册一个回调函数,当 GPS 航向值发生变化时运行。
Available Functionsvoid changed(
void (* callback)(void) );
范围 |
类型 |
描述 |
|---|---|---|
|
|
指向一个函数的指针,该函数将在 GPS 航向值改变时被调用。该函数不能接受任何参数,并且返回 |
此函数不返回值。