陀螺仪传感器#
介绍#
The gyro class is used with a Gyro Sensor to measure rotational movement around a single axis. It can track which direction the robot is facing and how far the robot has turned.
类构造函数#
gyro(
int32_t index );
类析构函数#
Destroys the gyro object and releases associated resources.
virtual ~gyro();
参数#
范围 |
类型 |
描述 |
|---|---|---|
|
|
The Smart Port that the Gyro Sensor is connected to, written as |
例子#
// Create a gyro instance in Port 1
gyro Gyro1 = gyro(PORT1);
成员功能#
The gyro class includes the following member functions:
calibrate— Calibrates the Gyro Sensor.setHeading— Sets the Gyro Sensor’s current heading to a new heading value.setRotation— Sets the Gyro Sensor’s current rotation to a new rotation value.heading— Returns the direction the Gyro Sensor is facing.rotation— Returns how far the Gyro Sensor has turned.rate— Returns how fast the Gyro Sensor is rotating.
Before calling any gyro member functions, a gyro instance must be created, as shown below:
/* 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 1
gyro Gyro1 = gyro(PORT1);
calibrate#
校准陀螺仪传感器。
Available Functionsvoid calibrate();
此函数不接受任何参数。
Return Values此函数不返回值。
Notes校准有助于陀螺仪传感器正确测量转弯角度。校准过程中请保持传感器静止。如果在校准过程中传感器移动,则航向、旋转和速率值可能无法正确测量转弯角度。
// Calibrate the Gyro Sensor before turning.
Gyro1.calibrate();
setHeading#
将陀螺仪传感器的当前航向设置为特定值。
航向角是指陀螺仪传感器所朝向的方向,测量范围为 0 到 359.99 度。设置航向角会改变传感器的当前方向参考。
Available Functionsvoid setHeading(
double value,
rotationUnits units );
参数 |
类型 |
描述 |
|---|---|---|
|
|
新标题要使用的值。 |
|
|
The unit that represents the heading:
|
此函数不返回值。
setRotation#
将陀螺仪传感器的当前旋转角度设置为特定值。
旋转角度是指陀螺仪传感器转动的幅度。与航向不同,旋转角度可以超过 359.99 度,也可以低于 0 度。
Available Functionsvoid setRotation(
double value,
rotationUnits units );
参数 |
类型 |
描述 |
|---|---|---|
|
|
要用于新旋转值的值。 |
|
|
The unit that represents the rotation value:
|
此函数不返回值。
heading#
返回陀螺仪传感器的当前航向。
航向角是指陀螺仪传感器所朝向的方向,测量范围为 0 到 359.99 度。如果传感器旋转超过 359.99 度,航向角将回落至 0 度。
Available Functionsdouble heading(
rotationUnits units = degrees );
范围 |
类型 |
描述 |
|---|---|---|
|
|
The unit that represents the heading:
|
Returns a double representing the current heading of the Gyro Sensor in the specified units, defaulting to degrees.
// Get the current heading for the Gyro.
double value = Gyro1.heading();
Brain.Screen.print(value);
rotation#
返回陀螺仪传感器的当前旋转角度。
旋转角度是指陀螺仪传感器转动的程度。与航向不同,旋转角度可以超过 359.99 度,也可以低于 0 度。例如,向右旋转两圈,旋转角度为 720 度。从 0 度向左旋转一圈,旋转角度为 -360 度。
Available Functionsdouble rotation(
rotationUnits units = degrees );
范围 |
类型 |
描述 |
|---|---|---|
|
|
The unit that represents the rotation value:
|
Returns a double representing the current rotation value of the Gyro Sensor in the specified units, defaulting to degrees.
// Get the current rotation for the Gyro.
double value = Gyro1.rotation();
Brain.Screen.print(value);
rate#
返回陀螺仪传感器的旋转速率。
转速是指陀螺仪传感器旋转的速度。该值可以是正数也可以是负数,具体取决于传感器的旋转方向。
Available Functionsdouble rate(
rateUnits units = rateUnits::dps );
| Parameter | Type | Description |
| :—————-: | :————————————– |
| units | rateUnits | The unit that represents the rate:
rateUnits::dps(default) — degrees per secondrateUnits::rpm— rotations per minute
Returns a double representing the current rate of change of the Gyro Sensor in the specified units, defaulting to rateUnits::dps.