智慧驅動#
介紹#
The smartdrive class controls how a robot drives and turns using motors, wheels, and a Gyro Sensor. A smartdrive uses a connected Gyro Sensor to track heading and rotation.
smartdrive 使用陀螺儀感測器來幫助機器人精確移動和轉向。在每個專案開始時,驅動系統會自動校準陀螺儀感測器。校準期間,請保持機器人靜止約 2 秒鐘,以便機器人能夠正確移動和轉向。
smartdrive 類別基於 drivetrain 類別。 5 smartdrive 可以使用與 drivetrain 相同的驅動和轉向功能,並且還添加了基於感測器的功能,如 turnToHeading 和 turnToRotation。
本頁面使用 myDrivetrain 作為範例智慧驅動名稱。請根據需要將其替換為您自己配置的名稱。
類別建構函數#
1 — 使用現有的
motor_group物件(左右兩側)以及陀螺儀感測器建立一個智慧型驅動器。smartdrive( motor_group &l, motor_group &r, gyro &g, double wheelTravel = 320, double trackWidth = 320, double wheelBase = 130, distanceUnits unit = mm, double externalGearRatio = 1.0 );
2 — 使用現有的
motor物件(左右兩側)以及陀螺儀感測器來建立一個智慧型驅動器。smartdrive( motor &l, motor &r, gyro &g, double wheelTravel = 320, double trackWidth = 320, double wheelBase = 130, distanceUnits unit = mm, double externalGearRatio = 1.0 );
類別析構函數#
銷毀 smartdrive 物件並釋放相關資源。
~smartdrive();
參數#
範圍 |
類型 |
描述 |
|---|---|---|
|
|
左側和右側馬達或馬達組。兩個參數必須使用相同的類型:要么是兩個 |
|
|
智慧驅動使用的陀螺儀感測器。 |
|
|
傳動車輪的周長。預設值為 |
|
|
左右車輪之間的距離。預設值為 |
|
|
前輪和後輪之間的距離。預設值為 |
|
|
The unit for |
|
|
如果使用齒輪,則用於調整驅動距離的齒輪比。預設值為 |
例子#
/* This constructor is required when using VS Code.
Drivetrain configuration is generated automatically
in VEXcode using the Device Menu. Replace the values
as needed. */
// Create the left and right motor objects.
motor leftMotor = motor(PORT1, false);
motor rightMotor = motor(PORT9, true);
// Create the Gyro Sensor object.
gyro myGyro = gyro(PORT3);
// Create the smartdrive object.
smartdrive myDrivetrain = smartdrive(
leftMotor, // left motor
rightMotor, // right motor
myGyro, // Gyro Sensor
259.34, // wheelTravel
320, // trackWidth
40, // wheelBase
mm, // unit
1.0 ); // externalGearRatio
成員功能#
smartdrive 類別包含 成員函數:
動作-使用航向或旋轉控制機器人轉向。
turnToHeading— 將機器人轉向 -359 度到 359 度之間的特定航向。機器人將沿著最短路徑轉向以達到目標航向。turnToRotation— 將機器人旋轉到指定的方向。calibrateDrivetrain— 校準與智慧驅動配置的陀螺儀感測器。
設定 — 調整航向和旋轉值。
setHeading— 將機器人的目前航向改為新的航向。setRotation— 將機器人的目前旋轉改為新的旋轉。
數值-檢查方向和旋轉角度。
smartdrive 也可以使用 drivetrain 中繼承的成員函數,例如 drive, driveFor, turn, turnFor, stop, isDone和 isMoving。
在呼叫任何 smartdrive 成員函數之前,必須先建立一個 smartdrive 物件。
/* This constructor is required when using VS Code.
Drivetrain configuration is generated automatically
in VEXcode using the Device Menu. Replace the values
as needed. */
// Create the left and right motor objects.
motor leftMotor = motor(PORT1, false);
motor rightMotor = motor(PORT9, true);
// Create the Gyro Sensor object.
gyro myGyro = gyro(PORT3);
// Create the smartdrive object.
smartdrive myDrivetrain = smartdrive(
leftMotor, // left motor
rightMotor, // right motor
myGyro, // Gyro Sensor
259.34, // wheelTravel
320, // trackWidth
40, // wheelBase
mm, // unit
1.0 ); // externalGearRatio
turnToHeading#
A heading is the direction the robot is facing, measured in degrees. turnToHeading turns the robot to face a specific heading from -359 to 359 degrees. The robot will turn the shortest direction to reach the target heading.
機器人的初始航向角為0度。
程式會等到機器人完成轉向後才執行下一行程式碼。
Available Functions1 — 使用當前轉彎速度將機器人轉向指定的航向。
bool turnToHeading( double angle, rotationUnits units, bool waitForCompletion = true );
Parameters2 — 讓機器人以指定的速度轉向指定的航向。
bool turnToHeading( double angle, rotationUnits units, double velocity, velocityUnits units_v, bool waitForCompletion = true );
範圍 |
類型 |
描述 |
|---|---|---|
|
|
機器人應面向的方向,以度為單位。當使用 |
|
|
航向單位: |
|
|
使用 |
|
|
速度單位: |
|
|
|
返回機器人是否達到目標航向,以布林值表示。
true— 機器人已到達目標航向。false— 機器人未完成回合,或函數在回合完成前返回,因為waitForCompletion設定為false。
// Face the new 0 degrees.
myDrivetrain.setHeading(90, degrees);
myDrivetrain.turnToHeading(0, degrees);
turnToRotation#
turnToRotation 將機器人旋轉到指定角度。
旋轉角度是指機器人轉動的程度,以度數或轉數來衡量。在專案開始時,旋轉角度值設定為 0 度。也可以使用 setRotation 函數設定旋轉角度。
旋轉角度是絕對值。這意味著轉彎方向取決於機器人目前的旋轉角度。向右轉會增加旋轉角度,向左轉彎會減少旋轉角度。
例如,如果機器人從0度開始,你將其旋轉720度,它會向右轉兩次。如果你再將其旋轉360度,它會向左轉一次,因為360小於720。
程式會等到機器人完成轉向後才執行下一行程式碼。
Available Functions1 — 使用目前轉彎速度將機器人轉到指定的旋轉方向。
bool turnToRotation( double angle, rotationUnits units, bool waitForCompletion = true );
Parameters2 — 讓機器人以指定的速度旋轉到指定的方向。
bool turnToRotation( double angle, rotationUnits units, double velocity, velocityUnits units_v, bool waitForCompletion = true );
範圍 |
類型 |
描述 |
|---|---|---|
|
|
機器人將轉向的旋轉值。 |
|
|
旋轉單位: |
|
|
使用 |
|
|
速度單位: |
|
|
|
返回機器人是否達到目標旋轉角度,以布林值表示。
true— 機器人達到目標旋轉角度。false— 機器人未完成回合,或函數在回合完成前返回,因為waitForCompletion設定為false。
// Spin counterclockwise for 2 turns, then face forward.
myDrivetrain.setRotation(720, degrees);
myDrivetrain.turnToRotation(0, degrees);
calibrateDrivetrain#
calibrateDrivetrain 校準與智慧驅動系統配置的陀螺儀感測器。
校準有助於陀螺儀感測器正確測量運動。校準期間,請保持機器人靜止約 2 秒鐘。如果機器人在校準期間移動,陀螺儀感測器可能無法正確測量運動。
程式將等待校準完成後再執行下一行程式碼。
當傳動系統配置了陀螺儀感應器時,VEXcode 會自動產生calibrateDrivetrain 它是專案原始碼的一部分,並非 VEX IQ C++ API 的一部分。
void calibrateDrivetrain();
此函數不接受任何參數。
Return Values此函數不傳回值。
Notes當在 VEXcode 中配置智慧型驅動器時,機器人會在每個專案開始時自動校準。
校準過程中,請保持機器人靜止約 2 秒鐘。
以下是 VEXcode 自動產生的 calibrateDrivetrain 實作範例:
bool vexcode_initial_drivetrain_calibration_completed = false;
void calibrateDrivetrain() {
wait(200, msec);
Brain.Screen.print("Calibrating");
Brain.Screen.newLine();
Brain.Screen.print("Gyro");
DrivetrainGyro.calibrate();
while (DrivetrainGyro.isCalibrating()) {
wait(25, msec);
}
vexcode_initial_drivetrain_calibration_completed = true;
// Clears the screen and returns the cursor to row 1, column 1.
Brain.Screen.clearScreen();
Brain.Screen.setCursor(1, 1);
}
setHeading#
航向角是指機器人面向的方向,以度為單位。 0 setHeading 將機器人的目前航向角變更為新的航向角值。
例如,如果機器人轉向右側,將航向設定為 0 度,則該右側位置將成為新的 0 度航向。然後,機器人可以根據新的航向轉向其他位置。
Available Functionsvoid setHeading(
double value,
rotationUnits units);
範圍 |
類型 |
描述 |
|---|---|---|
|
|
要設定的機器人航向值。當使用 |
|
|
航向單位: |
此函數不傳回值。
Examples// Face the new 0 degrees.
myDrivetrain.setHeading(90, degrees);
myDrivetrain.turnToHeading(0, degrees);
setRotation#
旋轉角度是指機器人轉動的程度,以度或轉數為單位。專案開始時,旋轉角度值設定為 0 度。 `setRotation 會將機器人的目前旋轉角度變更為新值。
例如,如果機器人向右轉了兩圈,它的旋轉角度將為 720 度。將旋轉角度設為 0 度會將旋轉角度從 720 度重設為 0 度。然後,機器人可以根據這個新的旋轉角度進行旋轉。
Available Functionsvoid setRotation(
double value,
rotationUnits units);
範圍 |
類型 |
描述 |
|---|---|---|
|
|
要設定的機器人旋轉值。 |
|
|
旋轉單位: |
此函數不傳回值。
Examples// Spin counterclockwise for 2 turns, then face forward.
myDrivetrain.setRotation(720, degrees);
myDrivetrain.turnToRotation(0, degrees);
heading#
A heading is the direction the robot is facing, measured in degrees. heading returns the robot’s current heading from 0 to 359 degrees.
機器人的初始航向角為0度。
Available Functionsdouble heading( rotationUnits units = rotationUnits::deg );
範圍 |
類型 |
描述 |
|---|---|---|
|
|
可選。返回航向的單位: |
返回機器人目前航向,數值為 double 單位為選定的單位。
// Show heading before and after turning.
Brain.Screen.print("Before: %f", myDrivetrain.heading(degrees));
myDrivetrain.turnFor(right, 90, degrees);
wait(1, seconds);
Brain.Screen.newLine();
Brain.Screen.print("After: %f", myDrivetrain.heading(degrees));
myDrivetrain.stop();
rotation#
旋轉角度是指機器人轉動的程度,以度或轉數為單位。項目開始時,旋轉角度值設定為 rotation 回到機器人目前的旋轉角度。
向右轉會增加旋轉角度,向左轉會減少旋轉角度。例如,向右轉兩圈將得到 720 度的旋轉角度。
Available Functionsdouble rotation( rotationUnits units = rotationUnits::deg );
範圍 |
類型 |
描述 |
|---|---|---|
|
|
可選。旋轉單位: |
返回機器人目前旋轉方向,數值為 double 單位為選定的雙精度浮點數。
// Show rotation before and after turning.
Brain.Screen.print("Before: %f", myDrivetrain.rotation(degrees));
myDrivetrain.turnFor(right, 90, degrees);
wait(1, seconds);
Brain.Screen.newLine();
Brain.Screen.print("After: %f", myDrivetrain.rotation(degrees));
myDrivetrain.stop();