惯性传感器#
介绍#
The inertial class is used to control and access data from the V5 Inertial Sensor. This sensor provides heading, rotation, and acceleration information to help your robot track orientation and movement.
类构造函数#
inertial(
int32_t index,
turnType dir = turnType::right );
类析构函数#
Destroys the inertial object and releases associated resources.
~inertial();
参数#
范围 |
类型 |
描述 |
|---|---|---|
|
|
The Smart Port that the Inertial Sensor is connected to, written as |
|
|
The direction that will return a positive value when turning:
|
示例#
// Create the inertial instance
inertial myInertial = inertial(
PORT1, // Smart Port 1
right); // dir
成员功能#
The inertial class includes the following member functions:
heading— Returns the current heading value.rotation— Returns the current rotation value.setHeading— Sets the sensor’s heading to a specified value.setRotation— Sets the sensor’s rotation to a specified value.calibrate— Calibrates the sensor for accurate heading tracking.resetHeading— Resets the heading to 0.resetRotation— Resets the rotation to 0.isCalibrating— Returns whether the sensor is currently calibrating.changed— Registers a callback function that runs when the heading changes.installed— Returns whether the sensor is installed.acceleration— Returns linear acceleration along the specified axis.gyroRate— Returns the angular velocity for the specified axis.orientation— Returns the orientation for the specified axis.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 heading values.
Before calling any inertial member functions, a inertial instance must be created, as shown below:
/* 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 the inertial instance
inertial myInertial = inertial(
PORT1); // Smart Port 1
heading#
返回惯性传感器的当前航向(偏航角)。
Available Functionsdouble heading( rotationUnits units = degrees );
范围 |
类型 |
描述 |
|---|---|---|
|
|
The unit of measurement for the returned heading value:
|
返回一个双精度浮点数,表示传感器的当前航向,单位为指定单位。
NotesThe heading value is wrapped between 0 and 360 degrees. Use
rotationto access the continuous (unbounded) rotation value.
// Turn, then show the current heading
Drivetrain.turnFor(right, 450, degrees);
Brain.Screen.print("%f", myInertial.heading(degrees));
rotation#
返回惯性传感器的绝对旋转角度(无限制的偏航角)。
Available Functionsdouble rotation( rotationUnits units = degrees );
范围 |
类型 |
描述 |
|---|---|---|
|
|
The unit of measurement for the returned rotation value:
|
Returns a double representing the current rotation in the specified units.
The rotation value is continuous and does not wrap between 0 and 360 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#
将惯性传感器的航向设置为指定值。
Available Functionsvoid setHeading( double value, rotationUnits units );
范围 |
类型 |
描述 |
|---|---|---|
|
|
要分配给传感器的新航向值。 |
|
|
The unit of measurement for the heading value:
|
此函数不返回值。
Examples// Turn the robot around
myInertial.setHeading(180, degrees);
Drivetrain.turnToHeading(0, degrees);
setRotation#
将惯性传感器的绝对旋转角度设置为指定值。
Available Functionsvoid setRotation( double value, rotationUnits units );
范围 |
类型 |
描述 |
|---|---|---|
|
|
要分配给传感器的新绝对旋转值。 |
|
|
The unit of measurement for the rotation value:
|
此函数不返回值。
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。
Available Functionsvoid resetHeading();
此函数不接受任何参数。
Return Values此函数不返回值。
resetRotation#
将惯性传感器的旋转角度设置为 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 |
此函数不返回值。
Examples// Called 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());
}
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#
返回惯性传感器沿指定轴测得的线性加速度。
Available Functionsdouble acceleration( axisType axis );
范围 |
类型 |
描述 |
|---|---|---|
|
|
The axis for which acceleration will be returned:
|
Returns a double representing the acceleration along the specified axis in G (gravitational units).
加速度值以 G 为单位报告,其中 1 G 等于地球表面的重力加速度。
gyroRate#
返回惯性传感器沿指定轴测量的角速度。
Available Functionsdouble gyroRate( axisType axis, velocityUnits units );
范围 |
类型 |
描述 |
|---|---|---|
|
|
The axis for which angular velocity will be returned:
|
|
|
The unit of measurement for the returned angular velocity:
|
Returns a double representing the angular velocity 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(
axisType axis,
rotationUnits units );
范围 |
类型 |
描述 |
|---|---|---|
|
|
The axis for which orientation will be returned:
|
|
|
The unit of measurement for the returned orientation value:
|
Returns a double representing the orientation of the specified axis in the selected 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(xaxis, 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. |
此函数不返回值。
Examples// Called when the inertial sensor's detects a collision
void onCollision(axisType axis, double x, double y, double z) {
Brain.Screen.print("Collision detected!");
}
// Firmly move the robot by hand briefly to simulate
// a collision and call the onCollision function
myInertial.collision(onCollision);
setTurnType#
设置转向方向,返回正航向值。
Available Functionsvoid setTurnType( turnType dir );
范围 |
类型 |
描述 |
|---|---|---|
|
|
The direction that will return positive heading 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.