全球定位系统#
To make gps commands appear in VEXcode V5, a GPS (Game Positioning System™) Sensor must be configured in the Devices window.
有关详细信息,请参阅以下文章:
初始化 gps 类#
这个“gps”构造函数在指定的端口中创建具有指定旋转偏移量的 gps 类的对象。
范围 |
描述 |
---|---|
|
GPS 传感器连接到的有效 智能端口。 |
|
GPS 传感器的旋转偏移量(以度为单位),从 0.00 到 359.99。 |
// Construct a GPS Sensor "GPS1" with the gps class, using
// a 180 degree offset.
gps GPS1 = gps(PORT1, 180);
这个 gps
构造函数在指定的端口中创建一个具有指定原点位置和旋转偏移的 gps 类的对象。
范围 |
描述 |
---|---|
|
GPS 传感器连接到的有效 智能端口。 |
“牛” |
GPS 传感器相对于机器人原点(也称为参考点)的 X 位置。 |
“哎呀” |
GPS 传感器相对于机器人原点(也称为参考点)的 Y 位置。 |
|
有效的 distanceUnit。 |
|
GPS 传感器的旋转偏移量(以度为单位),从 0.00 到 359.99。 |
// Construct a GPS Sensor "GPS1" with the gps class, with
// the origin at (10, 10) and using a 180 degree offset.
gps GPS1 = gps(PORT1, 10.00, 10.00, mm, 180);
当引用 gps 类方法时,此“GPS1”对象将在整个 API 文档的所有后续示例中使用。
类方法#
校准()#
当传感器配置为 V5 传动系统的一部分时,calibrate()
方法会校准 GPS 传感器。这会将 GPS 的配置航向(根据其角度偏移)设置为传动系统的当前航向。
在校准过程中,GPS 传感器和传动系统必须保持静止。
在这个例子中,GPS 传感器将进行校准,然后使用传感器的航向将机器人转到场地的 180 度航向。
// Turn to face the lower Field wall using data from the GPS Sensor.
DrivetrainGPS.calibrate()
Drivetrain.turnToHeading(180, degrees)
**返回:**无。
正在校准()#
当 GPS 传感器执行请求的重新校准时,isCalibrating()
方法返回 true,重新校准完成后变为 false
。
**返回:**如果 GPS 传感器仍在校准,则返回“true”。
重置标题()#
resetHeading()
方法将 GPS 传感器的航向设置为 0。
**返回:**无。
重置旋转()#
resetRotation()
方法将 GPS 传感器的旋转角度设置为 0。
**返回:**无。
设置标题()#
setHeading(value, units)
方法将 GPS 传感器的航向设置为新值。
参数 |
描述 |
---|---|
价值 |
要设置的旋转值。 |
单位 |
**可选。**有效的 rotationUnit。默认值为“度”。 |
**返回:**无。
设置旋转()#
setRotation(value, units)
方法将 GPS 传感器的旋转设置为新值。
参数 |
描述 |
---|---|
价值 |
要设置的旋转值。 |
单位 |
**可选。**有效的 rotationUnit。默认值为“度”。 |
**返回:**无。
卷()#
roll(units)
方法返回 GPS 传感器的滚动。
参数 |
描述 |
---|---|
单位 |
**可选。**有效的 rotationUnit。默认值为“度”。 |
返回: GPS 传感器以指定单位滚动。
沥青()#
pitch(units)
方法返回 GPS 传感器的俯仰角。
参数 |
描述 |
---|---|
单位 |
**可选。**有效的 rotationUnit。默认值为“度”。 |
**返回:**指定单位的 GPS 传感器的俯仰角。
偏航()#
yaw(units)
方法返回 GPS 传感器的偏航角。
参数 |
描述 |
---|---|
单位 |
**可选。**有效的 rotationUnit。默认值为“度”。 |
**返回:**以指定单位表示的 GPS 传感器偏航角。
x位置()#
xPosition(units)
方法返回现场 GPS 传感器的当前 x 坐标。
参数 |
描述 |
---|---|
单位 |
**可选。**有效的 distanceUnit。默认值为 |
默认情况下,“xPosition()”方法返回 GPS 传感器的 X 坐标位置。
如果在配置 GPS 传感器时添加了偏移量,则报告的 X 位置将是机器人上的参考点。
**返回:**指定单位的 GPS 传感器的 x 坐标。
在本例中,机器人(从场地右侧出发)向中心移动,直到 X 轴位置报告小于 0 毫米。然后机器人停止移动。
// Drive slowly towards the center of the Field.
Drivetrain.setDriveVelocity(20.0, percent);
Drivetrain.drive(forward);
// Stop driving when the GPS reports an x position near the center of the Field.
waitUntil((GPS20.xPosition(mm) < 0.0));
Drivetrain.stop();
y位置()#
yPosition(units)
方法返回现场 GPS 传感器的当前 y 坐标。
参数 |
描述 |
---|---|
单位 |
**可选。**有效的 distanceUnit。默认值为 |
默认情况下,“yPosition()”方法返回 GPS 传感器的 Y 坐标位置。
如果在配置 GPS 传感器时添加了偏移量,则报告的 Y 位置将是机器人上的参考点。
**返回:**指定单位的 GPS 传感器的 y 坐标。
// Get the current y coordinate for the GPS Sensor.
yPos = GPS1.yPosition();
在此示例中,机器人(从场地顶部出发)向中心移动,直到 Y 位置报告小于 0 毫米。然后机器人停止移动。
// 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((GPS20.yPosition(mm) < 0.0));
Drivetrain.stop();
方向()#
orientation(axis, units)
方法返回 gps 一个轴的方向。
参数 |
描述 |
---|---|
轴 |
有效的 axisType。 |
单位 |
**可选。**有效的 rotationUnit。默认值为“度”。 |
x 或 roll 轴报告 -180 到 +180 度之间的值,表示 GPS 传感器向左或向右倾斜时的方向。
y 或 pitch 轴报告 -90 到 +90 度之间的值,表示 GPS 传感器向前或向后倾斜时的方向。
z、yaw 轴报告一个介于 -180 到 +180 度之间的值,表示 GPS 传感器绕垂直轴旋转时的方向。
**返回:**指定单位的轴方向值。
// Get the orientation of the x axis on the GPS Sensor.
pitch = GPS1.orientation(xaxis);
标题()#
heading(units)
方法根据传感器从 GPS 字段代码读取的数据返回字段上 GPS 传感器的航向。
参数 |
描述 |
---|---|
单位 |
**可选。**有效的 rotationUnit。默认值为“度”。 |
heading
方法报告的范围是从 0.00 到 359.99 度。
航向对应于场方向,范围为顺时针方向 0º 至 359.9º。0º 位于 12 点钟位置。
默认情况下,“heading”方法报告 GPS 传感器的航向。
如果在配置 GPS 传感器时添加了角度偏移,则报告的航向将是机器人上的参考点。
**返回:**以指定单位表示的 GPS 传感器的当前航向。
在这个例子中,机器人(起始方向面向场地的12点钟方向)转向场地右侧墙壁,直到航向报告大于90度。然后机器人停止行驶。
// Turn slowly to face the right wall.
Drivetrain.setTurnVelocity(20.0, percent);
Drivetrain.turn(right);
// Stop turningwhen the GPS reports a heading greater than 90 degrees.
waitUntil((GPS20.heading() > 90.0));
Drivetrain.stop();
旋转()#
rotation(units)
方法返回 GPS 传感器的当前旋转。
参数 |
描述 |
---|---|
单位 |
**可选。**有效的 rotationUnit。默认值为“度”。 |
返回: GPS 传感器以指定单位表示的当前旋转。
// Get the current rotation for the GPS Sensor.
rotation = GPS1.rotation();
陀螺仪速率()#
gyroRate(axis, units)
方法返回 GPS 传感器一个轴的陀螺仪速率。
参数 |
描述 |
---|---|
轴 |
有效的 AxisType。 |
单位 |
**可选。**有效的 velocityUnit。默认值为 |
**返回:**以指定单位表示的轴陀螺仪速率值。
// Get the gyro rate for the z axis of the GPS Sensor.
gyroRate = GPS1.gyroRate(zaxis);
加速度()#
acceleration(axis)
方法返回 GPS 传感器轴的加速度。
参数 |
描述 |
---|---|
轴 |
有效的 axisType。 |
**返回:**以重力单位表示的轴加速度值。
// Get the acceleration for the Zz axis of the GPS Sensor.
accel = GPS1.acceleration(zaxis);
质量()#
quality()
方法报告 0-100 之间的数值,表示 GPS 传感器的当前信号质量。
报告值 |
描述 |
---|---|
100 |
GPS 传感器报告的所有位置和航向数据均有效。 |
~90 |
任何位置数据不再通过捕获 VEX 字段代码中的信息来计算,而是通过其他方式。 |
0 - 80 |
只有 GPS 传感器航向值有效,但随着时间的推移,GPS 传感器扫描的 VEX 字段代码不足以准确确定位置和航向信息,报告的信号质量将持续下降直至为 0,此时任何 GPS 传感器数据都会被有效冻结且不再有效。 |
返回: GPS 传感器的质量在 0 - 100 范围内。
在此代码片段中,由于 while (true)
循环的存在,信号质量将不断被检查。只有当 GPS 传感器的信号质量报告值高于 90 时,TRUE 分支才会运行,以确保项目中使用的 GPS 传感器数据有效。
while (true) {
if (GPS20.quality() > 90.0) {} else {}
wait(5, msec);
}
return 0;
设置原点()#
setOrigin(x, y, units)
方法设置 GPS 传感器的原点。
参数 |
描述 |
---|---|
x |
GPS 相对于机器人原点的 X 位置。 |
y |
GPS 相对于机器人原点的 Y 位置。 |
单位 |
**可选。**有效的 distanceUnit。默认值为 |
**返回:**无。
// Set the origin of the GPS Sensor.
GPS1.setOrigin(6, -6, inches);
设置位置()#
setLocation(x, y, units, angle, units_r)
方法会在项目开始时将 GPS 传感器的 X、Y 坐标和航向设置为已知值。这样做有助于减少项目启动时由于传感器靠近场地墙壁而导致的 GPS 数据误差。
参数 |
描述 |
---|---|
x |
初始 x 坐标。 |
y |
初始 y 坐标。 |
单位 |
**可选。**有效的 distanceUnit。默认值为 |
角度 |
**可选。**机器人的初始航向。 |
单位 |
**可选。**有效的 rotationUnit。默认值为“度”。 |
当 GPS 传感器距离场地墙壁太近而无法读取场地代码时(例如,当机器人处于场地上的比赛起始位置时),如果代码依赖于 GPS 的坐标和航向数据,则经常会出现意外行为。
x
和 y
参数分别是机器人在场地上已知位置的参考点的 X 和 Y 坐标。机器人上的参考点与配置 GPS 传感器时用于计算偏移量的位置相同。
heading
参数是机器人参考点的航向。使用上面的示例图,GPS 传感器安装在机器人后方,而参考点位于机器人手臂的末端。由于参考点朝向右侧墙壁(航向为 90 度),因此应输入的航向值为 90 度。
**返回:**无。
此处显示的示例设置了机器人(手臂)上的参考点的位置,以匹配机器人在场地上的位置。
// Wait so the GPS has time to initialize.
wait(0.5, seconds)
// Set the starting position to the bottom left corner of the Field facing the right wall.
DrivetrainGPS.setLocation(-1200, -1200, mm, 90)
已更改()#
changed(callback)
方法注册一个回调函数,用于 GPS 航向值发生变化时回调。
参数 |
描述 |
---|---|
打回来 |
当 GPS 航向改变时调用的回调函数。 |
**返回:**事件类的一个实例。
// Define the headingChanged function with a void return
// type, showing it doesn't return a value.
void headingChanged() {
// The Brain will print that the GPS Sensor's heading
// changed on the Brain's screen.
Brain.Screen.print("GPS heading changed");
}
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Turn the robot right.
Drivetrain.turn(right);
// Run headingChanged when the value of the GPS Sensor's
// heading changes.
GPS1.changed(headingChanged);
}
时间戳()#
timestamp()
方法请求 GPS 传感器最后接收到的状态包的时间戳。
**返回:**最后一个状态包的时间戳,以毫秒为单位的无符号 32 位整数。
安装()#
installed()
方法检查 GPS 传感器是否已安装。
**返回:**如果安装了 GPS 传感器,则返回 true
。如果未安装,则返回 false
。