慣性感測器#
介紹#
inertial 類用於控制和存取 IQ(第二代)大腦內建慣性感測器的資料。此感測器測量大腦的運動和轉動情況。
慣性感測器由兩部分組成。陀螺儀測量轉向,例如航向、旋轉、陀螺儀速率和方向。加速度計測量運動變化,例如沿 x 軸、y 軸和 z 軸的加速或減速。
類別建構函數#
inertial(
turnType dir = turnType::right );
類別析構函數#
摧毀慣性為 inertial 物體並釋放相關資源。
~inertial();
參數#
範圍 |
類型 |
描述 |
|---|---|---|
|
|
Optional. The direction that returns positive values:
|
範例#
// Create the inertial instance
inertial myInertial = inertial(
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 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:
|
傳回一個 double ,表示感測器目前方向的指定單位。
航向角的值介於 0 到 359.99 度之間。使用
rotation可以存取連續的旋轉值。
// 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 );
範圍 |
類型 |
描述 |
|---|---|---|
|
|
旋轉單位:
|
傳回 double 的雙精度浮點數,表示以指定單位表示的目前旋轉角度。
旋轉值是連續的,在 0 到 359.99 度之間不會循環。使用
heading可以存取循環後的航向值。
// 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校準過程中,感測器應保持靜止。
使用
isCalibrating來確定校準何時完成。
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) );
範圍 |
類型 |
描述 |
|---|---|---|
|
|
指向一個函數的指針,該函數將在航向值改變時調用。函數不能接受任何參數,且傳回 |
此函數不傳回值。
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 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。
G 相當於你靜止坐著時感受到的重力加速度。該值可以是正數也可以是負數,這取決於所選軸上的加速度方向。
double acceleration( axisType axis );
範圍 |
類型 |
描述 |
|---|---|---|
|
|
測量加速度的軸:
|
傳回一個 double 表示沿著指定軸的加速度,單位為 G(重力單位)。
加速度值以
G為單位報告。
gyroRate#
陀螺儀轉速是指慣性感測器旋轉的速度。此函數會傳回慣性感測器在所選軸上的目前旋轉速度。
該值可以是正數也可以是負數,這取決於感測器繞該軸旋轉的方向。
Available Functionsdouble gyroRate( axisType axis,
velocityUnits units );
範圍 |
類型 |
描述 |
|---|---|---|
|
|
返回陀螺儀速率的軸:
|
|
|
陀螺儀速率單位:
|
傳回一個 double 表示以所選單位表示的沿指定軸的目前旋轉速度。
// 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 unit:
|
傳回一個 double 表示以指定單位表示的選定方向角。
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. |
此函數不傳回值。
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 Functionsvoid setTurnType( turnType dir );
範圍 |
類型 |
描述 |
|---|---|---|
|
|
回正值的方向:
|
此函數不傳回值。
Notes呼叫此函數會覆寫在建構
inertial物件時指定的dir值。
getTurnType#
傳回轉向方向產生正值的結果。
Available FunctionsturnType getTurnType();
此函數不接受任何參數。
Return Values返回 turnType ,表示哪個方向產生正值:
-
right— Clockwise rotation returns positive values. -
left— Counterclockwise rotation returns positive values.
該值是在建構
inertial物件 期間設置,或透過呼叫setTurnType來設定。