傳動系統#

介紹#

drivetrain 類控制機器人的行駛與轉向方式。驅動系統由馬達和輪子組成,它們協同工作以驅動機器人移動。

drivetrain 不使用 陀螺儀感測器 進行基於航向或旋轉的轉向。它可以前進或後退,也可以左轉或右轉,但無法轉向特定的航向或旋轉角度。對於基於航向和旋轉的轉向,請使用 smartdrive 類別。

本頁面使用 myDrivetrain 作為範例傳動系統名稱。請根據需要將其替換為您自己配置的名稱。

衍生類別#

drivetrain 類別作為以下衍生類別的基底類別:

  • smartdrive - 建立支援陀螺儀感應器的傳動系統。

類別建構函數#

1 - 使用現有的 motor_group 物件為左側和右側建立傳動系統。

drivetrain(
    motor_group      &l,
    motor_group      &r,
    double           wheelTravel = 320,
    double           trackWidth = 320,
    double           wheelBase = 130,
    distanceUnits    unit = mm,
    double           externalGearRatio = 1.0 );

2 - 使用現有的 motor 物件為左側和右側建立傳動系統。

drivetrain(
    motor            &l,
    motor            &r,
    double           wheelTravel = 320,
    double           trackWidth = 320,
    double           wheelBase = 130,
    distanceUnits    unit = mm,
    double           externalGearRatio = 1.0 );

類別析構函數#

銷毀 drivetrain 物件並釋放相關資源。

virtual ~drivetrain();

參數#

範圍

類型

描述

&l / &r

motor_group / motor

左側和右側馬達或馬達組。兩個參數必須使用相同的類型:要么是兩個 motor_group 對象,要么是兩個 motor 對象。

wheelTravel

double

傳動車輪的周長。預設值為 320。

trackWidth

double

左右車輪之間的距離。預設值為 320。

wheelBase

double

前輪和後輪之間的距離。預設值為 130。

unit

distanceUnits

The unit for wheelTravel, trackWidth, and wheelBase: mm (default) or inches.

externalGearRatio

double

如果使用齒輪,則用於調整驅動距離的齒輪比。預設值為 1.0。

例子#

// Create the left and right motor objects.
motor leftMotor = motor(PORT1, false);
motor rightMotor = motor(PORT9, true);

// Create the drivetrain object.
drivetrain myDrivetrain = drivetrain(
    leftMotor,  // left motor
    rightMotor, // right motor
    259.34,     // wheelTravel
    320,        // trackWidth
    40,         // wheelBase
    mm,         // unit
    1.0 );      // externalGearRatio

成員功能#

drivetrain 類別包含以下成員函數:

操作 - 移動和轉向機器人。

  • drive - 使機器人無限向前或向後移動。

  • driveFor - 使機器人向前或向後移動指定的距離。

  • turn - 讓機器人永遠向左或向右轉。

  • turnFor - 使機器人向左或向右旋轉指定的角度或圈數。

  • stop - 停止機器人的移動。

設定 - 調整傳動系統設定。

  • setDriveVelocity - 告訴機器人要以多快的速度行駛。

  • setTurnVelocity - 告訴機器人轉彎的速度。

  • setStopping - 說明機器人如何停止移動:透過煞車、滑行或保持。

  • setTimeout - 設定機器人嘗試完成一次動作的時間。

數值 - 檢查移動狀態。

  • isDone - 返回機器人是否已完成移動,以布林值表示。

  • isMoving - 傳回機器人是否正在移動,以布林值表示。

  • velocity - 返回機器人行駛的速度。

  • current - 傳回傳動系統正在使用的電流大小。

  • power - 返回傳動系統使用能量的速度。

  • torque - 返回傳動系統正在使用的扭矩大小。

  • efficiency - 返回傳動系統使用動力的效率。

  • temperature - 傳回傳動系統的溫度。

  • voltage - 返回傳動系統的電壓。

在呼叫任何 drivetrain 成員函數之前,必須建立一個 drivetrain 物件。

/* 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 drivetrain object.
drivetrain myDrivetrain = drivetrain(
    leftMotor,  // left motor
    rightMotor, // right motor
    259.34,     // wheelTravel
    320,        // trackWidth
    40,         // wheelBase
    mm,         // unit
    1.0 );      // externalGearRatio

行動#

駕駛#

drive 使機器人無限前進或後退。機器人將持續移動,直到收到其他指令,例如轉向或停止。

Available Functions

1 - 使用目前驅動速度進行驅動。

void drive(
    directionType dir );

2 - 以指定速度行駛。

void drive(
    directionType dir,
    double        velocity,
    velocityUnits units );

Parameters

範圍

類型

描述

dir

directionType

機器人移動方向: forward 或 reverse。

velocity

double

使用 velocityUnits::pct,驅動速度範圍為 0% 至 100%;使用 rpm時,驅動速度範圍為 0 到 120 rpm;使用 dps時,驅動速度範圍為 0 到 720 度每秒。如果未提供速度,則機器人以目前驅動速度行駛。

units

velocityUnits

速度單位: velocityUnits::pct (百分比), rpm (每分鐘轉數),或 dps (每秒度數)。

Return Values

此函數不傳回值。

Notes
  • drive 是非等待的。專案將立即運行下一行程式碼。

  • 機器人將繼續移動,直到呼叫 stop 或運行另一個驅動系統運動功能。

  • isDone 和 isMoving 不與 drive 一起使用,因為 drive 沒有目標距離。

Examples
// Drive forward and back.
myDrivetrain.drive(forward);
wait(2, seconds);
myDrivetrain.drive(reverse, 25, rpm);
wait(2, seconds);
myDrivetrain.stop();

驅動#

driveFor 指令使機器人前進或後退指定距離。程式會等待機器人完成移動後再執行下一行程式碼。

Available Functions

1 - 以目前行駛速度沿指定方向行駛一段距離。

bool driveFor(
    directionType dir,
    double        distance,
    distanceUnits units,
    bool          waitForCompletion = true );

2 - 根據距離值,以當前行駛速度朝特定方向行駛。

bool driveFor(
    double        distance,
    distanceUnits units,
    bool          waitForCompletion = true );

3 - 根據距離值以指定速度朝特定方向行駛。

bool driveFor(
    double        distance,
    distanceUnits units,
    double        velocity,
    velocityUnits units_v,
    bool          waitForCompletion = true );

4 - 以指定的速度沿指定方向行駛指定距離。

bool driveFor(
    directionType dir,
    double        distance,
    distanceUnits units,
    double        velocity,
    velocityUnits units_v,
    bool          waitForCompletion = true );

Parameters

範圍

類型

描述

dir

directionType

機器人移動方向: forward 或 reverse。

distance

double

機器人行駛的距離。

units

distanceUnits

距離單位: mm (mm)、 inches) 或 cm)。

velocity

double

使用 velocityUnits::pct,驅動速度範圍為 0% 至 100%;使用 rpm時,驅動速度範圍為 0 到 120 rpm;使用 dps時,驅動速度範圍為 0 到 720 度每秒。如果未提供速度,則機器人以目前驅動速度行駛。

units_v

velocityUnits

速度單位: velocityUnits::pct (百分比), rpm (每分鐘轉數),或 dps (每秒度數)。

waitForCompletion

bool

true (預設值)使專案等待機器人完成移動後再執行下一行程式碼。 2 false 使下一行程式碼立即運行。

Return Values

返回機器人是否達到目標距離,以布林值表示。

  • true - 機器人已達到目標距離。

  • false - 機器人沒有完成移動,或函數在移動完成之前返回,因為 waitForCompletion 設定為 false。

Notes
  • 當 driveFor 運作時,執行另一個傳動系統運動函數會中斷目前的運動。

  • driveFor 使用目標距離,因此可以與 isDone 和 isMoving 一起使用。

Examples
// Drive 200 mm, then back 200 mm.
myDrivetrain.driveFor(forward, 200, mm);
myDrivetrain.driveFor(reverse, 200, mm);

轉動#

turn 表示機器人無限向左或向右轉向。機器人將持續轉向,直到收到其他指令,例如行駛或停止。

Available Functions

1 - 使用目前轉彎速度轉彎。

void turn(
    turnType dir );

2 - 以指定速度轉彎。

void turn(
    turnType     dir,
    double       velocity,
    velocityUnits units );

Parameters

範圍

類型

描述

dir

turnType

機器人轉向的方向: left 或 right。

velocity

double

使用 velocityUnits::pct,轉彎速度為 0% 到 100%;使用 rpm時,轉彎速度為 0 到 120 rpm;使用 dps時,轉彎速度為 0 到 720 度/秒。如果未提供速度,則機器人以當前轉彎速度轉彎。

units

velocityUnits

速度單位: velocityUnits::pct (百分比), rpm (每分鐘轉數),或 dps (每秒度數)。

Return Values

此函數不傳回值。

Notes
  • turn 行是非等待的。專案將立即運行下一行程式碼。

  • 機器人將繼續旋轉,直到呼叫 stop 或執行另一個驅動系統運動功能。

  • isDone 和 isMoving 不與 turn 一起使用,因為 turn 沒有目標角度。

Examples
// Turn right, then left, then stop.
myDrivetrain.turn(right);
wait(2, seconds);
myDrivetrain.turn(left);
wait(2, seconds);
myDrivetrain.stop();

轉彎#

turnFor 參數用於控制機器人向左或向右旋轉指定的角度、圈數或單位。旋轉角度相對於機器人的當前位置。程式會等待機器人完成旋轉後才執行下一行程式碼。

Available Functions

1 - 使用目前轉彎速度依指定角度轉彎。

bool turnFor(
    double        angle,
    rotationUnits units,
    bool          waitForCompletion = true );

2 - 以目前轉彎速度沿指定方向轉彎指定角度。

bool turnFor(
    turnType      dir,
    double        angle,
    rotationUnits units,
    bool          waitForCompletion = true );

3 - 以指定的速度轉指定的角度。

bool turnFor(
    double        angle,
    rotationUnits units,
    double        velocity,
    velocityUnits units_v,
    bool          waitForCompletion = true );

4 - 以指定的速度沿指定方向轉彎指定角度。

bool turnFor(
    turnType      dir,
    double        angle,
    rotationUnits units,
    double        velocity,
    velocityUnits units_v,
    bool          waitForCompletion = true );

Parameters

範圍

類型

描述

dir

turnType

機器人轉向的方向: left 或 right。

angle

double

機器人的轉彎角度。

units

rotationUnits

旋轉單位: deg (度數), rev (轉),或 raw (單位)。一轉等於 360 度。

velocity

double

使用 velocityUnits::pct,轉彎速度為 0% 到 100%;使用 rpm時,轉彎速度為 0 到 120 rpm;使用 dps時,轉彎速度為 0 到 720 度/秒。如果未提供速度,則機器人以當前轉彎速度轉彎。

units_v

velocityUnits

速度單位: velocityUnits::pct (百分比), rpm (每分鐘轉數),或 dps (每秒度數)。

waitForCompletion

bool

true (預設值)使專案等待機器人完成轉向後才執行下一行程式碼。 2 false 使下一行程式碼立即運行。

Return Values

返回機器人是否達到目標角度,以布林值表示。

  • true - 機器人達到目標角度。

  • false - 機器人沒有完成回合,或函數在回合完成前返回,因為 waitForCompletion 設定為 false。

Notes
  • 當 turnFor 運作時運行另一個傳動系統運動功能會中斷目前的運動。

  • turnFor 使用目標角度,因此可以與 isDone 和 isMoving 一起使用。

Examples
// Turn right, then left.
myDrivetrain.turnFor(right, 90, degrees);
wait(1, seconds);
myDrivetrain.turnFor(left, 90, degrees);

停止#

stop 停止機器人的運動。如果提供了停止模式,則控制機器人在此函數呼叫中如何停止。

Available Functions

1 - 使用目前停止模式停止傳動系統。

void stop();

2 - 使用指定的停止模式停止傳動系統。

void stop(
    brakeType mode );

Parameters

範圍

類型

描述

mode

brakeType

How the robot will stop:

  • brake - Stops immediately.
  • coast - Slows to a stop.
  • hold - Stops immediately and holds the wheels’ position.

Return Values

此函數不傳回值。

Examples
// Drive forward, then coast to a stop.
myDrivetrain.setDriveVelocity(100, percent);
myDrivetrain.drive(forward);
wait(2, seconds);
myDrivetrain.stop(coast);

突變體#

設定驅動速度#

setDriveVelocity 告訴機器人行駛速度。百分比越高,機器人行駛速度越快;百分比越低,機器人行駛速度越慢。

每個專案開始時,機器人預設以 50% 的速度行駛。

注意:速度越高,機器人行駛速度越快,但精度可能越低;速度越低,機器人行駛速度越慢,但精度可能越高。

Available Functions
void setDriveVelocity(
  double velocity,
  velocityUnits units );

Parameters

範圍

類型

描述

velocity

double

使用 velocityUnits::pct時,驅動速度從 0% 到 100%;使用 rpm時,驅動速度從 0 到 120 rpm;使用 dps時,驅動速度從 0 到 720 度每秒。

units

velocityUnits

速度單位: velocityUnits::pct (百分比), rpm (每分鐘轉數),或 dps (每秒度數)。

Return Values

此函數不傳回值。

Notes
  • 除非在函數呼叫中指定了特定速度,否則驅動速度將被後續的傳動系統運動函數使用。

Examples
// Drive forward, then coast to a stop.
myDrivetrain.setDriveVelocity(100, percent);
myDrivetrain.drive(forward);
wait(2, seconds);
myDrivetrain.stop(coast);

設定轉彎速度#

setTurnVelocity 告訴機器人轉彎的速度。百分比越高,機器人轉彎越快;百分比越低,機器人轉彎越慢。

每個專案開始時,機器人預設以 50% 的速度旋轉。

注意:速度越高,機器人轉彎越快,但精度可能越低;速度越低,機器人轉彎越慢,但精度可能越高。

Available Functions
void setTurnVelocity(
  double velocity,
  velocityUnits units );

Parameters

範圍

類型

描述

velocity

double

使用 velocityUnits::pct,轉速從 0% 到 100%;使用 rpm時,轉速從 0 到 120 rpm;使用 dps時,轉速從 0 到 720 度每秒。

units

velocityUnits

速度單位: velocityUnits::pct (百分比), rpm (每分鐘轉數),或 dps (每秒度數)。

Return Values

此函數不傳回值。

Notes
  • 除非在函數呼叫中提供特定速度,否則後續的傳動系統轉向功能將使用轉向速度。

Examples
// Try default, slow, then fast.
myDrivetrain.turnFor(right, 360, degrees);
wait(1, seconds);
myDrivetrain.setTurnVelocity(20, percent);
myDrivetrain.turnFor(right, 360, degrees);
wait(1, seconds);
myDrivetrain.setTurnVelocity(100, percent);
myDrivetrain.turnFor(right, 360, degrees);

設定停止#

setStopping 設定機器人停止移動的方式:透過煞車、滑行或保持。

Available Functions
void setStopping(
  brakeType mode );

Parameters

範圍

類型

描述

mode

brakeType

How the robot will stop:

  • brake - Stops immediately.
  • coast - Slows to a stop.
  • hold - Stops immediately and holds the wheels’ position.

Return Values

此函數不傳回值。

Notes
  • 除非在函數呼叫中提供特定的停止模式,否則後續的 stop 呼叫將使用停止模式。

  • 如果未使用此功能,機器人停止時將使用 brake。

設定超時#

setTimeout 設定機器人嘗試完成一個動作的超時時間。如果機器人無法在規定時間內完成,它將停止嘗試並執行下一行程式碼。這可以防止機器人卡在某個動作上。

Available Functions
void setTimeout(
  int32_t time,
  timeUnits units );

Parameters

範圍

類型

描述

time

int32_t

機器人嘗試完成一次動作的時間。該值應為正整數。

units

timeUnits

時間單位: sec 秒或 msec 秒。

Return Values

此函數不傳回值。

Notes
  • 逾時僅適用於移動到目標的傳動系統函數,例如 driveFor 和 turnFor。

Examples
// Turn right after driving forward.
myDrivetrain.setTimeout(1, seconds);
myDrivetrain.driveFor(forward, 25, inches);
myDrivetrain.turnFor(right, 90, degrees);

獲取器#

已完成#

isDone 返回機器人是否已完成移動,結果為布林值。這可用於根據機器人的移動狀態控制其他行為的執行時間。

Available Functions
bool isDone( void );

Parameters

此函數不接受任何參數。

Return Values
  • true - 機器人已停止移動。

  • false - 機器人仍在移動。

Notes
  • isDone 與基於目標的傳動系統運動功能配合使用,例如 driveFor 和 turnFor。

  • isDone 不與 drive 或 turn 一起使用,因為這些功能不會移動到目標。

正在移動#

isMoving 返回機器人是否正在移動,結果為布林值。這可用於根據機器人的移動狀態控制其他行為的執行時間。

Available Functions
virtual bool isMoving( void );

Parameters

此函數不接受任何參數。

Return Values
  • true - 機器人正在移動。

  • false - 機器人沒有移動。

Notes
  • isMoving 與基於目標的傳動系統運動功能配合使用,例如 driveFor 和 turnFor。

  • isMoving 不與 drive 或 turn 一起使用,因為這些功能不會移動到目標位置。

速度#

velocity 返回機器人行駛的速度。

它可以以百分比、 rpm或 dps的形式返回速度。正值表示機器人向前行駛,負值表示機器人向後行駛。

Available Functions
double velocity(
  velocityUnits units );

Parameters

範圍

類型

描述

units

velocityUnits

返回速度的單位: percent pct rpm或 dps。

Return Values

傳回所選單位的傳動系統速度,數值為 double (雙精度浮點數)。

Examples
// Show velocity before and during motion.
Brain.Screen.print("Resting: %f", myDrivetrain.velocity(percent));
myDrivetrain.drive(forward, 100, velocityUnits::pct);
wait(1, seconds);
Brain.Screen.newLine();
Brain.Screen.print("Moving: %f", myDrivetrain.velocity(percent));
myDrivetrain.stop();

目前的#

current 返回動力傳動系統正在使用的電流大小。電流是指流經動力傳動系統的電量。根據單位不同,電流值可以以安培 (A) 或百分比的形式傳回。

電流值越高,代表驅動系統消耗的電流越大。這種情況可能發生在機器人推擠物體或卡住後試圖移動時。

這可以用來檢查傳動系統在運動過程中是否運作吃力。如果電流持續偏高,傳動系統可能會發熱或動力利用效率降低。

Available Functions

1 - 傳回傳動系統的電流(以安培為單位)。

double current(
    currentUnits units = currentUnits::amp );

2 - 傳回傳動系統的電流百分比。

double current(
    percentUnits units );

Parameters

範圍

類型

描述

units

currentUnits

返回電流的單位: amp。

units

percentUnits

返回電流的單位: percent % 或 pct.

Return Values

返回所選單位的傳動系統電流,數值為 double。

力量#

power 返回傳動系統正在使用的功率,單位為瓦特。功率值表示傳動系統消耗能量的速度。

功率值越高,代表驅動系統消耗能量的速度越快。這種情況可能發生在機器人推擠物體或卡住後試圖移動時。

這可以用來比較不同的運動軌跡,或是檢查傳動系統是否運作吃力。如果功率持續偏高,傳動系統可能會過熱或能量利用效率降低。

Available Functions
double power(
  powerUnits units = powerUnits::watt );

Parameters

範圍

類型

描述

units

powerUnits

功率單位: watt。

Return Values

以瓦特為單位返回傳動系統功率( double。

扭力#

扭力表示傳動系統在車輪旋轉時能夠施加多大的推力或拉力。

torque 返回傳動系統使用的扭矩大小,以牛頓米 (Nm) 或英寸磅 (InLb) 為單位測量。

扭力值越高,表示傳動系統需要更大的推力或拉力。這種情況可能發生在機器人推擠物體或卡住後試圖移動時。

這可以用來檢查傳動系統是否運作不暢,或是比較不同動作所需的推力大小。

Available Functions
double torque(
  torqueUnits units = torqueUnits::nm );

Parameters

範圍

類型

描述

units

torqueUnits

返回扭力的單位: Nm (牛頓米,預設值)或 InLb (英吋磅)。

Return Values

返回所選單位的傳動系統扭矩,數值為 double。

效率#

efficiency 返回動力傳動系統使用動力的效率,以百分比表示,範圍從 0% 到 100%。

效率值反映了傳動系統有多少動力被用於運動。效率值越高,代表傳動系統用於運動的動力越多。當傳動系統高負載運轉但運動量不大時,例如機器人被卡住或抵擋物體時,效率值會降低。

這可以用來比較運動情況,或者檢查傳動系統是否浪費動力而不是將其用於運動。

Available Functions
double efficiency(
  percentUnits units = percentUnits::pct );

Parameters

範圍

類型

描述

units

percentUnits

單位返回效率為: percent 或 pct。

Return Values

傳回所選單位的傳動系統效率,數值為 double。

溫度#

temperature 傳回傳動系統的平均溫度,以百分比表示,範圍從 0% 到 100%。

溫度顯示傳動系統馬達的溫度。溫度越高,代表馬達在工作過程中溫度越高。為了保持最佳性能,馬達溫度應保持在 55°C 以下。

如果馬達過熱,它們會降低最大電流以保護自身。當溫度達到 70°C 時,馬達將停止運轉,直到冷卻。

這可以用來檢查傳動系統在重複運動、長時間運作或推動物體時是否過熱。

Available Functions
double temperature(
  percentUnits units );

Parameters

範圍

類型

描述

units

percentUnits

返回溫度的單位: percent 或 pct。

Return Values

傳回所選單位的平均傳動系統溫度,數值為 double。

Notes
  • 返回值為 50% 表示平均馬達溫度約為 45°C (113°F)。

  • 傳動系統的典型工作溫度範圍約為 20°C (68°F) 至 70°C (158°F)。

電壓#

voltage 返回傳動系統的電壓。

電壓是指輸送至傳動系統馬達的電壓力。結合電流和功率值,我們可以了解傳動系統如何利用電能。

Available Functions
double voltage(
  voltageUnits units = voltageUnits::volt );

Parameters

範圍

類型

描述

units

voltageUnits

回傳電壓的單位: volt (預設)或 voltageUnits::mV. voltageUnits::mV 表示毫伏特。

Return Values

傳回所選單位的傳動系統電壓,數值為 double。