惯性传感器#
介绍#
The inertial class is used to control and access data from the IQ (2nd gen) Brain’s built-in Inertial Sensor. This sensor measures how the Brain is moving and turning.
惯性传感器由两部分组成。陀螺仪测量转向,例如航向、旋转、陀螺仪速率和方向。加速度计测量运动变化,例如沿 x 轴、y 轴和 z 轴的加速或减速。
类构造函数#
inertial(
turnType dir = turnType::right );
类析构函数#
Destroys the inertial object and releases associated resources.
~inertial();
参数#
范围 |
类型 |
描述 |
|---|---|---|
|
|
Optional. The direction that returns positive values:
|
示例#
// Create the inertial instance
inertial myInertial = inertial(
right); // dir
成员功能#
The inertial class includes the following member functions:
heading— Returns the direction the sensor is facing.rotation— Returns how far the sensor has turned.setHeading— Sets the sensor’s current heading to a new heading value.setRotation— Sets the sensor’s current rotation to a new rotation value.calibrate— Calibrates the Inertial Sensor.resetHeading— Resets the sensor’s current heading to 0 degrees.resetRotation— Resets the sensor’s current rotation to 0 degrees.isCalibrating— Returns whether the Inertial Sensor is currently calibrating.changed— Registers a callback function that runs when the heading changes.installed— Returns whether the Inertial Sensor is installed.acceleration— Returns how quickly the sensor is speeding up or slowing down on the selected axis.gyroRate— Returns how fast the sensor is rotating on the selected axis.orientation— Returns the Inertial Sensor’s roll, pitch, or yaw angle.collision— Registers a callback function that runs when a collision is detected.setTurnType— Sets which turn direction returns positive heading values.getTurnType— Returns which turn direction produces positive values.
Before calling any inertial member functions, an inertial instance must be created, as shown below:
/* This constructor is required when using VS Code.
Inertial Sensor configuration is generated automatically
in VEXcode with the IQ (2nd gen) Brain. Replace the values
as needed. */
// Create the inertial instance
inertial myInertial = inertial();
heading#
航向角是指传感器朝向的方向,以度为单位,范围从 0 到 359.99。此函数返回传感器的当前航向角。
初始航向为0度。如果传感器旋转超过359.99度,航向将回落至0度。
Available Functionsdouble heading( rotationUnits units = degrees );
范围 |
类型 |
描述 |
|---|---|---|
|
|
The heading unit:
|
Returns a double representing the current heading of the sensor in the specified units.
The heading value is wrapped between 0 and 359.99 degrees. Use
rotationto access the continuous rotation value.
// 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 Functionsdouble rotation( rotationUnits units = degrees );
范围 |
类型 |
描述 |
|---|---|---|
|
|
The rotation unit:
|
Returns a double representing the current rotation in the specified units.
The rotation value is continuous and does not wrap between 0 and 359.99 degrees. Use
headingto access the wrapped heading value.
// 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 Functionsvoid setHeading( double value,
rotationUnits units );
范围 |
类型 |
描述 |
|---|---|---|
|
|
要设置的传感器航向值,单位为度。该值可以介于 0 到 359.99 之间。 |
|
|
The heading unit:
|
此函数不返回值。
Examples// Turn the robot around
myInertial.setHeading(180, degrees);
Drivetrain.turnToHeading(0, degrees);
setRotation#
将惯性传感器的当前旋转角度设置为指定值。
旋转角度是指传感器转动的幅度。与航向不同,旋转角度可以超过 359.99 度,也可以低于 0 度。
Available Functionsvoid setRotation( double value,
rotationUnits units );
范围 |
类型 |
描述 |
|---|---|---|
|
|
要设置的传感器旋转值,以度为单位。可以是整数或小数。 |
|
|
The rotation unit:
|
此函数不返回值。
Examples// Turn the robot around
myInertial.setRotation(-180, degrees);
Drivetrain.turnToRotation(0, degrees);
calibrate#
校准惯性传感器。
校准有助于传感器正确测量转弯角度。校准过程中请保持传感器静止。如果在校准过程中传感器移动,则航向、旋转、陀螺仪速率和方向值可能无法正确测量。
Available Functionsvoid calibrate( );
此函数不接受任何参数。
Return Values此函数不返回值。
Notes校准过程中,传感器应保持静止。
Use
isCalibratingto determine when calibration has completed.
resetHeading#
将惯性传感器的当前航向重置为 0 度。
使用此功能后,传感器的当前方向将变为新的 0 度航向。
Available Functionsvoid resetHeading();
此函数不接受任何参数。
Return Values此函数不返回值。
resetRotation#
将惯性传感器的当前旋转角度重置为 0 度。
使用此功能后,传感器将从新的 0 度旋转值开始跟踪未来的旋转。
Available Functionsvoid resetRotation();
此函数不接受任何参数。
Return Values此函数不返回值。
isCalibrating#
返回惯性传感器当前是否正在校准。
Available Functionsbool isCalibrating();
此函数不接受任何参数。
Return Values返回一个布尔值,指示惯性传感器是否正在校准:
-
true— The sensor is currently calibrating. -
false— The sensor is not currently calibrating.
changed#
注册一个回调函数,当惯性传感器的航向值发生变化时,该函数将运行。
Available Functionsvoid changed( void (* callback)(void) );
范围 |
类型 |
描述 |
|---|---|---|
|
|
A pointer to a function that will be called when the heading value changes. The function must take no parameters and return |
此函数不返回值。
ExamplesDefine the callback function (outside of
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()); }Register the callback inside
int main()int main() { // Run onHeadingChanged when the Inertial Sensor's heading value changes. myInertial.changed(onHeadingChanged); }
installed#
返回惯性传感器是否已安装并连接。
Available Functionsbool installed();
此函数不接受任何参数。
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。
A G is a unit used to measure acceleration. 1 G is about the acceleration you feel from gravity while sitting still. The value can be positive or negative depending on the direction of acceleration on the selected axis.
double acceleration( axisType axis );
范围 |
类型 |
描述 |
|---|---|---|
|
|
The axis to measure acceleration on:
|
Returns a double representing the acceleration along the specified axis in G (gravitational units).
Acceleration values are reported in units of
G.
gyroRate#
陀螺仪转速是指惯性传感器旋转的速度。此函数返回惯性传感器在所选轴上的当前旋转速度。
该值可以是正数也可以是负数,具体取决于传感器绕该轴旋转的方向。
Available Functionsdouble gyroRate( axisType axis,
velocityUnits units );
范围 |
类型 |
描述 |
|---|---|---|
|
|
The axis to return the gyro rate from:
|
|
|
The gyro rate unit:
|
Returns a double representing the current rotation speed along the specified axis in the selected units.
// 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#
方向是指惯性传感器在选定旋转轴上的当前角度。此函数返回惯性传感器在指定轴上的当前方向。
横滚、俯仰和偏航描述了传感器倾斜或旋转的不同方式。
Available Functionsdouble orientation(
orientationType type,
rotationUnits units );
范围 |
类型 |
描述 |
|---|---|---|
|
|
The orientation angle to return:
|
|
|
The orientation unit:
|
Returns a double representing the selected orientation angle in the specified units.
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(roll, deg);
Brain.Screen.print("%f", orient);
wait(0.1, seconds);
}
collision#
注册一个回调函数,当惯性传感器检测到突然冲击或碰撞时,该函数将运行。
Available Functionsvoid collision( void (* callback)(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:
void. |
此函数不返回值。
ExamplesDefine the callback function (outside of
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!"); }Register the callback inside
int main()int main() { // Run onCollision when the Inertial Sensor detects a collision. myInertial.collision(onCollision); }
setTurnType#
设置转向方向返回正值。
Available Functionsvoid setTurnType( turnType dir );
范围 |
类型 |
描述 |
|---|---|---|
|
|
The direction that returns positive values:
|
此函数不返回值。
NotesThis setting affects how
headingandrotationreport positive and negative values.Calling this function overrides the
dirvalue specified when constructing theinertialobject.
getTurnType#
返回转向方向产生正值的结果。
Available FunctionsturnType getTurnType();
此函数不接受任何参数。
Return ValuesReturns a turnType indicating which direction produces positive values:
-
right— Clockwise rotation returns positive values. -
left— Counterclockwise rotation returns positive values.
This value is set during construction of the
inertialobject or by callingsetTurnType.