陀螺仪传感器#

介绍#

gyro 类用于测量机器人的航向和旋转运动,从而可以跟踪其随时间推移的姿态。

类构造函数#

gyro(
  triport::port &port);

类析构函数#

销毁 gyro 对象并释放相关资源。

virtual ~gyro();

参数#

范围

类型

描述

port

triport::port &

陀螺仪传感器连接的 3 线端口,写成 Brain.ThreeWirePort.X 或 ExpanderName.X,其中 X 是端口字母(例如, Brain.ThreeWirePort.A 或 Expander1.A)。

例子#

// Create a gyro instance in Port A
gyro GyroA = gyro(Brain.ThreeWirePort.A);

成员功能#

gyro 类包含以下成员函数:

  • calibrate — 校准陀螺仪以实现稳定的航向跟踪。

  • setHeading — 设置陀螺仪的航向。

  • setRotation — 设置陀螺仪的当前旋转值。

  • heading — 返回陀螺仪的航向。

  • rotation — 返回陀螺仪的旋转角度。

在调用任何 gyro 成员函数之前,必须先创建一个 gyro 实例,如下所示:

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

// Create a gyro instance in Port A
gyro GyroA = gyro(Brain.ThreeWirePort.A);

calibrate#

校准陀螺仪传感器。

Available Functions
void calibrate(
  int32_t value = 0 );

Parameters

参数

类型

描述

value

int32_t

校准所需时间(秒)。默认值为 0。

Return Values

此函数不返回值。

Notes
  • calibrate 仅应在传感器不移动时调用。

Examples
// Start calibration with the calibration time set to 3 seconds.
GyroA.calibrate(3);

setHeading#

将陀螺仪传感器的航向设置为特定值。

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

参数

类型

描述

value

double

新标题要使用的值。

units

rotationUnits

表示航向的单位:

  • degrees
  • turns
Return Values

此函数不返回值。

setRotation#

将陀螺仪传感器的旋转角度设置为特定值。

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

Parameters

参数

类型

描述

value

double

要用于新旋转值的值。

units

rotationUnits

表示旋转值的单位:

  • degrees
  • turns
Return Values

此函数不返回值。

heading#

返回陀螺仪传感器的当前航向。

Available Functions
double heading(
  rotationUnits units = degrees );

Parameters

范围

类型

描述

units

rotationUnits

表示航向的单位:

  • degrees (默认)
  • turns
Return Values

返回一个 double 表示陀螺仪传感器的当前航向,单位为指定值,默认为 degrees。

Examples
// Get the current heading for the Gyro.
double value = GyroA.heading();
Brain.Screen.print(value);

rotation#

返回陀螺仪传感器的当前旋转角度。

Available Functions
double rotation(
  rotationUnits units = degrees );

Parameters

范围

类型

描述

units

rotationUnits

表示旋转值的单位:

  • degrees (默认)
  • turns
Return Values

返回一个 double 表示陀螺仪传感器的当前旋转值,单位为指定值,默认为 degrees。

Examples
// Get the current rotation for the Gyro.
double value = GyroA.rotation();
Brain.Screen.print(value);