智慧驅動#
介紹#
smartdrive 類透過馬達、輪子和感測器控制機器人的行駛和轉向。 2 smartdrive 可以使用慣性感測器或陀螺儀感測器來追蹤航向和旋轉。
smartdrive 使用感測器輔助機器人精確移動和轉向。在每個專案開始時,驅動系統會自動校準感測器。校準期間,請保持機器人靜止約 2 秒鐘,以便機器人能夠正確移動和轉向。
smartdrive 類別基於 drivetrain 類別。它可以使用與 drivetrain 相同的驅動和轉向功能,並且還添加了基於感測器的功能,如 turnToHeading 和 turnToRotation。
類別建構函數#
1 — 使用現有的
motor_group物件為左側 (&l) 和右側 (&r) 建立智慧型驅動器,以及慣性感測器或陀螺儀感測器。smartdrive( motor_group &l, motor_group &r, inertial &g, double wheelTravel = 320, double trackWidth = 320, double wheelBase = 130, distanceUnits unit = mm, double externalGearRatio = 1.0 );
2 — 使用現有的
motor物件為左側 (&l) 和右側 (&r) 建立智慧型驅動器,以及慣性感測器或陀螺儀感測器。smartdrive( motor &l, motor &r, inertial &g, double wheelTravel = 320, double trackWidth = 320, double wheelBase = 130, distanceUnits unit = mm, double externalGearRatio = 1.0 );
類別析構函數#
銷毀 smartdrive 物件並釋放相關資源。
~smartdrive();
參數#
感測器參數可以是 inertial 或 gyro 物體。
範圍 |
類型 |
描述 |
|---|---|---|
|
|
引用現有的 |
|
|
參考現有的 |
|
|
傳動車輪的周長。預設值為 |
|
|
左右車輪之間的距離。預設值為 |
|
|
前輪和後輪之間的距離。預設值為 |
|
|
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 instances
motor leftMotor = motor(PORT1, false);
motor rightMotor = motor(PORT9, true);
// Create the inertial instance
inertial myInertial = inertial(PORT3);
// Create the smartdrive instance
smartdrive myDrivetrain = smartdrive(
leftMotor, // left motor object
rightMotor, // right motor object
myInertial, // inertial object
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 instances
motor leftMotor = motor(PORT1, false);
motor rightMotor = motor(PORT9, true);
// Create the inertial instance
inertial myInertial = inertial(PORT3);
// Create the smartdrive instance
smartdrive myDrivetrain = smartdrive(
leftMotor, // left motor object
rightMotor, // right motor object
myInertial, // inertial object
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。
當
turnToHeading正在進行時,執行另一個傳動系統運動功能(例如turn或drive)將中斷目前的運動。因為
turnToHeading是基於目標的(使用angle參數),所以諸如isDone和isMoving之類的函數可以與turnToHeading一起使用。
// 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。
當
turnToRotation正在進行時,執行另一個傳動系統運動功能(例如turn或drive)將中斷目前的運動。因為
turnToRotation是基於目標的(使用angle參數),所以諸如isDone和isMoving之類的函數可以與turnToRotation一起使用。
// Spin counterclockwise for 2 turns, then face forward
myDrivetrain.setRotation(720, degrees);
myDrivetrain.turnToRotation(0, degrees);
calibrateDrivetrain#
calibrateDrivetrain 校準與智慧驅動系統配置的慣性感測器或陀螺儀感測器。
校準有助於感測器正確測量運動。校準期間,請保持機器人靜止約 2 秒鐘。如果機器人在校準期間移動,感測器可能無法正確測量運動。
程式將等待校準完成後再執行下一行程式碼。
當傳動系統配置了慣性感測器或陀螺儀感測器時,VEXcode 會自動產生calibrateDrivetrain 函數。此函數包含在專案原始碼中,並非 VEX EXP C++ API 的一部分。
void calibrateDrivetrain();
此函數不接受任何參數。
Return Values此函數不傳回值。
Notes機器人會在每個專案開始時自動進行校準。
校準過程中,保持機器人靜止約 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("Inertial");
DrivetrainInertial.calibrate();
while (DrivetrainInertial.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();