惯性传感器#

介绍#

inertial 类用于控制和访问连接到智能端口的 V5 惯性传感器的数据。

V5惯性传感器测量机器人的运动和转向情况。陀螺仪测量转向参数,例如航向、旋转、陀螺仪速率和方向。加速度计测量运动变化,例如沿x轴、y轴和z轴的加速或减速。

类构造函数#

inertial(
  int32_t  index,
  turnType dir = turnType::right );

类析构函数#

摧毁惯性为 inertial 物体并释放相关资源。

~inertial();

参数#

范围

类型

描述

index

int32_t

惯性传感器连接的智能端口,记为 PORTx,其中 x 是端口号(例如, PORT1)。

dir

turnType

Optional. The direction that returns positive values:

  • right (default) — Right turns return positive values.
  • left — Left turns return positive values.

示例#

// Create an inertial instance for the V5 Inertial Sensor
inertial myInertial = inertial(
    PORT1,      // Smart Port 1
    right);     // dir

成员功能#

inertial 类包含以下成员函数:

  • heading — 返回传感器朝向的方向。

  • rotation — 返回传感器旋转了多远。

  • setHeading — 将传感器的当前航向设置为新的航向值。

  • setRotation — 将传感器的当前旋转设置为新的旋转值。

  • calibrate — 校准惯性传感器。

  • resetHeading — 将传感器的当前航向重置为 0 度。

  • resetRotation — 将传感器的当前旋转角度重置为 0 度。

  • isCalibrating — 返回惯性传感器当前是否正在校准。

  • changed — 注册一个在标题更改时运行的回调函数。

  • installed — 返回惯性传感器是否已安装。

  • acceleration — 返回传感器在选定轴上加速或减速的速度。

  • gyroRate — 返回传感器在选定轴上的旋转速度。

  • orientation — 返回惯性传感器的横滚角、俯仰角或偏航角。

  • collision — 注册一个在检测到碰撞时运行的回调函数。

  • setTurnType — 设置哪个转弯方向返回正值。

  • getTurnType — 返回哪个转弯方向产生正值。

在调用任何 inertial 函数之前,必须先创建一个 inertial ,如下所示:

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

// Create an inertial instance for the V5 Inertial Sensor connected to Port 1.
inertial myInertial = inertial(PORT1);

heading#

航向角是指传感器朝向的方向,以度为单位,范围从 0 到 359.99。此函数返回传感器的当前航向角。

初始航向为0度。如果传感器旋转超过359.99度,航向将回落至0度。

Available Functions
double heading( rotationUnits units = degrees );

Parameters

范围

类型

描述

units

rotationUnits

The heading unit:

  • deg / degrees (default) — degrees
  • turns / rev — revolutions

Return Values

返回一个 double ,表示传感器当前方向的指定单位。

Notes
  • 航向角的值介于 0 到 359.99 度之间。使用 rotation 可以访问连续的旋转值。

Examples
// Turn, then show the current heading
Drivetrain.turnFor(right, 450, degrees);
Brain.Screen.print("%f", myInertial.heading(degrees));

rotation#

返回惯性传感器的当前旋转角度。

旋转角度是指传感器转动的程度。与航向不同,旋转角度可以超过 359.99 度,也可以低于 0 度。例如,向右旋转两圈,旋转角度为 720 度。从 0 度向左旋转一圈,旋转角度为 -360 度。

Available Functions
double rotation( rotationUnits units = degrees );

Parameters

范围

类型

描述

units

rotationUnits

旋转单位:

  • deg / degrees (默认)— 度
  • turns / rev — 圈数

Return Values

返回一个 double 的双精度浮点数,表示以指定单位表示的当前旋转角度。

Notes
  • 旋转值是连续的,在 0 到 359.99 度之间不会循环。使用 heading 可以访问循环后的航向值。

Examples
// Turn, then show the current rotation
Drivetrain.turnFor(right, 450, degrees);
Brain.Screen.print("%f", myInertial.rotation(degrees));

setHeading#

航向角是指传感器朝向的方向,以度为单位,范围从 0 到 359.99。此功能将惯性传感器的当前航向角更改为新的航向角值。

例如,如果传感器已转向右侧,将航向设置为 0 度,则该右侧位置将成为新的 0 度。然后,传感器可以根据该新方向跟踪其他航向。

Available Functions
void setHeading( double value, rotationUnits units );

Parameters

范围

类型

描述

value

double

要设置的传感器航向值,单位为度。该值可以介于 0 到 359.99 之间。

units

rotationUnits

The heading unit:

  • deg / degrees — degrees
  • turns / rev — revolutions

Return Values

此函数不返回值。

Examples
// Turn the robot around
myInertial.setHeading(180, degrees);
Drivetrain.turnToHeading(0, degrees);

setRotation#

旋转角度是指传感器转动的程度,以度为单位。与航向不同,旋转角度可以超过 359.99 度,也可以低于 0 度。项目开始时,旋转角度值设置为 0 度。此函数会将惯性传感器的当前旋转角度更改为新值。

例如,如果传感器向右旋转了两圈,其旋转角度将为 720 度。将旋转角度设置为 0 度会将旋转角度从 720 度重置为 0 度。然后,传感器可以根据这个新值跟踪旋转。

Available Functions
void setRotation( double value, rotationUnits units );

Parameters

范围

类型

描述

value

double

要设置的传感器旋转值,以度为单位。可以是整数或小数。

units

rotationUnits

The rotation unit:

  • deg / degrees — degrees
  • turns / rev — revolutions

Return Values

此函数不返回值。

Examples
// Turn the robot around
myInertial.setRotation(-180, degrees);
Drivetrain.turnToRotation(0, degrees);

calibrate#

校准惯性传感器。

校准有助于传感器正确测量转弯角度。校准过程中请保持传感器静止。如果在校准过程中传感器移动,则航向、旋转、陀螺仪速率和方向值可能无法正确测量。

Available Functions
void calibrate( );

Parameters

此函数不接受任何参数。

Return Values

此函数不返回值。

Notes
  • 校准过程中,传感器应保持静止。

  • 使用 isCalibrating 来确定校准何时完成。

resetHeading#

将惯性传感器的当前航向重置为 0 度。

使用此功能后,传感器的当前方向将变为新的 0 度航向。

Available Functions
void resetHeading();

Parameters

此函数不接受任何参数。

Return Values

此函数不返回值。

resetRotation#

将惯性传感器的当前旋转角度重置为 0 度。

使用此功能后,传感器将从新的 0 度旋转值开始跟踪未来的旋转。

Available Functions
void resetRotation();

Parameters

此函数不接受任何参数。

Return Values

此函数不返回值。

isCalibrating#

返回惯性传感器当前是否正在校准。

Available Functions
bool isCalibrating();

Parameters

此函数不接受任何参数。

Return Values

返回一个布尔值,指示惯性传感器是否正在校准:

  • true — The sensor is currently calibrating.
  • false — The sensor is not currently calibrating.

changed#

注册一个回调函数,当惯性传感器的航向值发生变化时,该函数将运行。

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

Parameters

范围

类型

描述

callback

void (*)(void)

指向一个函数的指针,该函数将在航向值改变时调用。该函数不能接受任何参数,并且返回 void

Return Values

此函数不返回值。

Examples

定义回调函数(在 int main()之外)

// Display the heading when the Inertial Sensor's heading value changes.
void onHeadingChanged() {
  Brain.Screen.clearScreen();
  Brain.Screen.setCursor(1, 1);
  Brain.Screen.print("Heading: %.2f", myInertial.heading());
}

在 int main()中注册回调函数

int main() {
  // Run onHeadingChanged when the Inertial Sensor's heading value changes.
  myInertial.changed(onHeadingChanged);
}

installed#

返回惯性传感器是否已安装并连接。

Available Functions
bool installed();

Parameters

此函数不接受任何参数。

Return Values

返回一个布尔值,指示惯性传感器是否已安装并连接:

  • true — The sensor is connected and responding.
  • false — The sensor is not connected or not detected.

acceleration#

加速度是指传感器加速或减速的快慢。此函数返回惯性传感器在所选轴上的加速度,范围从 -4.0 G 到 4.0 G。

G 相当于你静止坐着时感受到的重力加速度。该值可以是正数也可以是负数,具体取决于所选轴上的加速度方向。

Available Functions
double acceleration( axisType axis );

Parameters

范围

类型

描述

axis

axisType

测量加速度的轴:

  • xaxis
  • yaxis
  • zaxis

Return Values

返回一个 double 表示沿指定轴的加速度,单位为 G(重力单位)。

Notes
  • 加速度值以 G为单位报告。

gyroRate#

陀螺仪转速是指惯性传感器旋转的速度。此函数返回惯性传感器在所选轴上的当前旋转速度。

该值可以是正数也可以是负数,具体取决于传感器绕该轴旋转的方向。

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

Parameters

范围

类型

描述

axis

axisType

返回陀螺仪速率的轴:

  • xaxis
  • yaxis
  • zaxis

units

velocityUnits

陀螺仪速率单位:

  • dps — 度/秒
  • rpm — 转/分钟

Return Values

返回一个 double 表示以所选单位表示的沿指定轴的当前旋转速度。

Examples
// Display the rate of change of rotation while turning in dps
Drivetrain.turn(right);
wait(1, seconds);

Brain.Screen.print(myInertial.gyroRate(zaxis, dps));

Drivetrain.stop();

orientation#

方向是指惯性传感器在选定轴上的当前角度。此函数返回惯性传感器在指定轴上的当前方向。

所选的 x 轴、y 轴或 z 轴对应于传感器的其中一个方向轴。

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

Parameters

范围

类型

描述

axis

axisType

返回方向的轴:

  • xaxis
  • yaxis
  • zaxis

units

rotationUnits

The orientation unit:

  • deg / degrees — degrees
  • turns / rev — revolutions

Return Values

返回一个 double 表示以指定单位表示的选定方向角。

Examples
while (true) {
  Brain.Screen.clearScreen();
  Brain.Screen.setCursor(1,1);
  // Show the Inertial Sensor's orientation value
  // for roll in degrees
  double orient = myInertial.orientation(xaxis, deg);
  Brain.Screen.print("%f", orient);
  wait(0.1, seconds);
}

collision#

注册一个回调函数,当惯性传感器检测到突然冲击或碰撞时,该函数将运行。

Available Functions
void collision( void (* callback)(axisType, double, double, double) );

Parameters

范围

类型

描述

callback

void (*)(axisType, double, double, double)

A pointer to a function that will be called when a collision is detected. The callback function must accept the following parameters:

  • axisType — The axis on which the collision was detected.
  • Acceleration values at the time of impact, in order:
    • double — Acceleration along the x-axis (in G).
    • double — Acceleration along the y-axis (in G).
    • double — Acceleration along the z-axis (in G).
The function must return void.

Return Values

此函数不返回值。

Examples

定义回调函数(在 int main()之外)

// Display a message when the Inertial Sensor detects a collision.
void onCollision(axisType axis, double x, double y, double z) {
  Brain.Screen.print("Collision detected!");
}

在 int main()中注册回调函数

int main() {
  // Run onCollision when the Inertial Sensor detects a collision.
  myInertial.collision(onCollision);
}

setTurnType#

设置转向方向返回正值。

Available Functions
void setTurnType( turnType dir );

Parameters

范围

类型

描述

dir

turnType

返回正值的方向:

  • right
  • left

Return Values

此函数不返回值。

Notes

getTurnType#

返回转向方向产生正值的结果。

Available Functions
turnType getTurnType();

Parameters

此函数不接受任何参数。

Return Values

返回 turnType ,表示哪个方向产生正值:

  • right — Clockwise rotation returns positive values.
  • left — Counterclockwise rotation returns positive values.
Notes