汽車和汽車集團#
介紹#
馬達和電機組控制機器人各部分的運動。一個 motor 物件控制一個已配置的 IQ 智慧型電機,而一個 motor_group 物件則同時控制多個已配置的 IQ 智慧型電機,使它們協同運動。馬達和馬達組可用於抬起機械手臂、轉動機械爪、旋轉車輪或移動機器人的其他部件。兩個馬達可以協同工作,組成一個驅動系統,從而驅動整個機器人運動和轉向。
每個馬達或馬達組都在「設備」視窗中進行配置。根據系統版本不同,馬達和馬達組的名稱可能會有所變化。本頁使用 Motor1 和 MotorGroup1 作為範例名稱。您可以根據需要將其替換為您自己配置的名稱。
預設情況下, forward 使馬達逆時針旋轉, reverse 使馬達順時針旋轉。如果馬達設定為反轉,則旋轉方向會互換。
引擎#
Class Constructors#
1 — 為連接到指定智慧型連接埠的 IQ 智慧型馬達建立一個
motor物件。馬達自動使用偵測到的內部gearSetting。motor( int32_t index );
2 — 在指定的智慧型連接埠上建立一個
motor對象,並可選擇反轉其方向。馬達自動使用偵測到的內部gearSetting。motor( int32_t index, bool reverse );
3 — 在指定的智慧型端口上建立一個
motor對象,並指定gearSetting。motor( int32_t index, gearSetting gears );
4 — 在指定的智慧型端口上建立一個
motor對象,具有指定的gearSetting和可選的反向方向。motor( int32_t index, gearSetting gears, bool reverse );
Class Destructor#
銷毀 motor 物件並釋放相關資源。
~motor();
Parameters#
範圍 |
類型 |
描述 |
|---|---|---|
|
|
馬達連接的智慧型端口,寫成 |
|
|
Sets whether the motor’s spin direction is reversed:
|
|
|
Specifies the internal gear cartridge ratio.
|
Notes#
齒輪設定應與馬達中安裝的實體卡盤相符。
Example#
// Create the motor instance in Smart Port 1
motor LeftMotor = motor(PORT1);
// Creates the motor instance with different settings
motor RightMotor = motor(
PORT2, // Smart Port 2
ratio2_1, // Gear ratio is 2:1
true);
汽車集團#
Class Constructors#
motor_group( motor &m1, Args&... m2 );
Class Destructor#
銷毀 motor_group 物件並釋放相關資源。
~motor_group();
Parameters#
範圍 |
類型 |
描述 |
|---|---|---|
|
|
集團新增的第一台馬達。 |
|
|
該電機組新增了馬達。一個馬達組最多可包含四個馬達。 |
Example#
// Create individual motor instances
motor LeftMotor = motor(PORT1);
motor RightMotor = motor(PORT2);
// Create a motor group with both motors
motor_group DriveMotors = motor_group(LeftMotor, RightMotor);
成員功能#
電機和電機組的編碼方式有很多種。以下列出了所有 motor 馬達和 motor_group 成員函數:
操作-停止和旋轉馬達及馬達組。
spin— 使馬達或馬達組永遠正轉或反轉。spinFor— 使馬達或馬達組旋轉指定的距離或時間。spinToPosition— 將馬達或馬達組旋轉到特定位置。stop— 停止馬達或馬達組旋轉。
變異器 — 調整馬達和馬達組設定。
setVelocity— 告訴馬達或馬達組旋轉的速度。setMaxTorque— 設定馬達或馬達組在旋轉時允許施加的最大扭力。setPosition— 將馬達或馬達組的目前位置改為新值。setStopping— 設定馬達或馬達組停止移動的方式:透過煞車、滑行或保持。setTimeout— 設定馬達或馬達組嘗試完成運動的時間。
Getters — 檢查馬達和馬達組狀態。
isDone— 返回馬達或馬達組是否已完成移動,以布林值表示。isSpinning— 返回馬達或馬達組是否正在旋轉,以布林值表示。position— 返回馬達或馬達組的目前位置。velocity— 傳回馬達或馬達組的旋轉速度。current— 傳回馬達或馬達組正在使用的電流大小。power— 返回馬達或馬達組正在使用的功率。torque— 返回馬達或馬達組正在使用的扭矩大小。efficiency— 返回馬達或馬達組使用功率的效率。temperature— 返回馬達或馬達組的溫度。voltage— 傳回提供給馬達或馬達組的電壓。direction— 返回馬達或馬達組的旋轉方向。installed— 返回馬達或馬達組是否連接到大腦。count— 傳回馬達組中的馬達數量。
在呼叫任何 motor 或 motor_group 成員函數之前,必須建立一個實例,如下所示:
/* This constructor is required when using VS Code.
Motor and Motor Group configuration is generated automatically
in VEXcode using the Device Menu. Replace the values
as needed. */
// Create the motor instance in Smart Port 1
motor Motor1 = motor(PORT1);
行動#
spin#
spin 表示馬達或馬達組正轉或反轉永不停歇。馬達或馬達組將持續旋轉,直到收到其他指令,例如改變旋轉方向或停止。
1 — 使用目前配置的馬達速度旋轉馬達或馬達組。
void spin( directionType dir );
2 — 使馬達或馬達組以指定的速度旋轉。
void spin( directionType dir, double velocity, velocityUnits units );
Parameters3 — 使用指定的電壓驅動馬達或馬達組旋轉。
void spin( directionType dir, double voltage, voltageUnits units );
範圍 |
類型 |
描述 |
|---|---|---|
|
|
馬達或馬達組的旋轉方向: |
|
|
當使用 |
|
|
速度單位:
|
|
|
旋轉電壓。負值表示與給定的 |
|
|
電壓單位:
|
此函數不傳回值。
Notes函數不等待,呼叫後項目將立即繼續執行。
馬達或馬達組將繼續旋轉,直到呼叫
stop或執行另一個運動功能。諸如
isDone和isSpinning之類的函數不適用於spin,因為它不使用基於目標的移動(沒有rotation或time參數)。
// Spin forward, then stop
Motor1.spin(forward);
wait(1, seconds);
Motor1.stop();
spinFor#
spinFor 函數會使馬達或馬達組旋轉指定的距離或時間。旋轉方向是相對於馬達或馬達組的當前位置而言的。預設情況下,程式會等待馬達或馬達群組旋轉完畢後再執行下一行程式碼。
1 — 以函數呼叫中提供的速度旋轉特定距離。
bool spinFor( double rotation, rotationUnits units, double velocity, velocityUnits units_v, bool waitForCompletion = true );
2 — 接受一個方向參數,並以函數呼叫中提供的速度旋轉指定的距離。
bool spinFor( directionType dir, double rotation, rotationUnits units, double velocity, velocityUnits units_v, bool waitForCompletion = true );
3 — 使用 目前配置的馬達速度 旋轉特定距離。
bool spinFor( double rotation, rotationUnits units, bool waitForCompletion = true );
4 — 接受一個方向參數,並使用目前配置的馬達速度旋轉特定距離。
bool spinFor( directionType dir, double rotation, rotationUnits units, bool waitForCompletion = true );
5 — 以函數呼叫中提供的速度旋轉特定時間。
bool spinFor( double time, timeUnits units, double velocity, velocityUnits units_v );
6 — 接受一個方向參數,並以函數呼叫中提供的速度旋轉特定的時間。
bool spinFor( directionType dir, double time, timeUnits units, double velocity, velocityUnits units_v );
7 — 使用目前配置的馬達速度旋轉特定時間。
bool spinFor( double time, timeUnits units );
Parameters8 — 接受一個方向參數,並使用目前配置的馬達速度旋轉特定的時間。
bool spinFor( directionType dir, double time, timeUnits units );
範圍 |
類型 |
描述 |
|---|---|---|
|
|
馬達或馬達組的旋轉方向: |
|
|
馬達或馬達組的旋轉距離。 |
|
|
The rotation unit:
|
|
|
馬達或馬達組旋轉的時間長度。 |
|
|
The unit of time:
|
|
|
當使用 |
|
|
用於表示速度的單位:
|
|
|
|
傳回布林值,表示馬達或馬達組是否達到目標值。
true— The motor or motor group reached the target value.false— The motor or motor group was not able to reach the target value orwaitForCompletionis set tofalse.
因為
spinFor是基於目標的(使用rotation或time參數),所以諸如isDone和isSpinning之類的函數可以與spinFor一起使用。
// Spin 1 turn fast, then 1 turn slow
Motor1.spinFor(forward, 1, turns, true);
Motor1.spinFor(reverse, 1, turns, 20, velocityUnits::pct, true);
spinToPosition#
spinToPosition 將馬達或馬達組旋轉到特定位置。
馬達或馬達組的位置是指其旋轉的距離,以 degrees 或 turns來衡量。一圈等於 360 度。在專案開始時,馬達或馬達組的位置設定為 0 度。也可以使用 setPosition 函數來設定馬達或馬達組的位置。
位置值是絕對值。這意味著旋轉方向取決於馬達或馬達組的當前位置。
例如,如果馬達或馬達組從 0 度開始旋轉到 720 度,它將正轉兩圈。如果之後它旋轉到 360 度,它將反轉一圈,因為 360 小於 720。
Available Functions1 — 以函數呼叫中提供的速度旋轉到特定位置。
bool spinToPosition( double rotation, rotationUnits units, double velocity, velocityUnits units_v, bool waitForCompletion = true );
Parameters2 — 使用 目前配置的馬達速度 旋轉到特定位置。
bool spinToPosition( double rotation, rotationUnits units, bool waitForCompletion = true );
範圍 |
類型 |
描述 |
|---|---|---|
|
|
馬達或馬達組將旋轉到的位置值。 |
|
|
The rotation unit:
|
|
|
當使用 |
|
|
用來表示速度的單位:
|
|
|
|
傳回布林值,表示馬達或馬達組是否已開始請求的基於目標的運動。
true— The motor or motor group begins the requested target-based movement.false— The call fails orwaitForCompletionis set tofalse.
當
spinToPosition正在進行時,執行另一個馬達運動功能(例如spin或spinFor)將中斷目前運動。因為
spinToPosition是基於目標的(使用rotation參數),所以諸如isDone和isSpinning之類的函數可以與spinToPosition一起使用。
// Spin forward, then go to 90°
Motor1.spin(forward);
wait(1, seconds);
Motor1.spinToPosition(90, degrees);
stop#
stop 停止馬達或馬達組旋轉。
1 — 使用目前配置的煞車模式 停止馬達或馬達組。
void stop();
Parameters2 — 使用函數呼叫中提供的煞車模式停止馬達或馬達組。
void stop( brakeType mode );
範圍 |
類型 |
描述 |
|---|---|---|
|
|
Optional. How the motor or motor group will stop:
|
此函數不傳回值。
Notes呼叫
stop會立即停止任何目前馬達或馬達組的運動。如果沒有指定煞車模式,則馬達或馬達組使用目前配置的煞車模式。
stop會中斷任何基於目標的活動移動(例如spinFor或spinToPosition)。
// Spin and coast to a stop
Motor1.setVelocity(100, percent);
Motor1.spin(forward);
wait(1, seconds);
Motor1.stop(coast);
突變體#
setVelocity#
setVelocity 指示馬達或馬達組的旋轉速度。百分比越高,馬達或馬達組的旋轉速度越快;百分比越低,馬達或馬達組的旋轉速度越慢。
每個項目開始時,每個馬達或馬達組預設以 50% 的速度旋轉。
注意:更高的速度會使馬達或馬達組旋轉得更快,但精度可能會降低。更低的速度會使馬達或馬達組旋轉得更慢,但精度可能會更高。
Available Functionsvoid setVelocity(
double velocity,
velocityUnits units );
範圍 |
類型 |
描述 |
|---|---|---|
|
|
當使用 |
|
|
用於表示速度的單位:
|
此函數不傳回值。
Notes任何後續的運動函數(例如
spin、spinFor或spinToPosition)如果未明確指定速度,則將使用此值。
// Try default, then slow, then fast
Motor1.spinFor(forward, 180, degrees);
wait(1, seconds);
Motor1.setVelocity(20, percent);
Motor1.spinFor(reverse, 180, degrees);
wait(1, seconds);
Motor1.setVelocity(100, percent);
Motor1.spinFor(forward, 180, degrees);
setMaxTorque#
扭矩表示馬達或馬達組在旋轉時能夠產生的推力或拉力的大小。
setMaxTorque 設定馬達或馬達組允許使用的最大扭力。
較高的百分比可以讓馬達或馬達組施加更大的推力,例如在舉起重物時。較低的百分比則會限制馬達或馬達組的推力。這有助於在馬達或馬達組卡住或達到其運動極限時保護機器人。
每個項目開始時,每個馬達或馬達組的扭力預設為 50%。
Available Functions1 — 將最大扭力設定為馬達或馬達組額定扭力的百分比。
void setMaxTorque( double value, percentUnits units );
2 — 使用實體扭力單位設定最大扭力。
void setMaxTorque( double value, torqueUnits units );
Parameters3 — 透過限制馬達電流來設定最大扭力。
void setMaxTorque( double value, currentUnits units );
範圍 |
類型 |
描述 |
|---|---|---|
|
|
馬達或馬達組可使用的最大扭力。 |
|
|
以百分比形式指定扭力: |
|
|
以物理單位指定扭矩,例如:
|
|
|
透過限制馬達電流來指定扭力:
|
此函數不傳回值。
Notes當使用
currentUnits,扭矩可透過限制馬達或馬達組可以使用的電流大小來限制。
setPosition#
A motor or motor group’s position is how far it has spun, measured in degrees or turns. One turn is equal to 360 degrees. setPosition changes the motor or motor group’s current position to a new value.
例如,如果馬達或馬達組旋轉了 180 度,將其位置設為 0 度會將該位置從 180 度重設為 0 度。然後,馬達或馬達組可以根據該新值旋轉到對應位置。
Available Functionsvoid setPosition(
double value,
rotationUnits units );
範圍 |
類型 |
描述 |
|---|---|---|
|
|
要為馬達或馬達組設定的位置值。 |
|
|
The position unit:
|
此函數不傳回值。
Notes呼叫
setPosition後,後續spinToPosition的呼叫將使用更新後的位置作為參考。
setStopping#
setStopping 設定馬達或馬達組停止運動的方式:透過煞車、滑行或保持。
void setStopping(
brakeType mode );
範圍 |
類型 |
描述 |
|---|---|---|
|
|
How the motor or motor group will stop:
|
此函數不傳回值。
Notes任何後續對
stop的調用,如果沒有指定煞車模式,將使用此值。
setTimeout#
setTimeout 設定馬達或馬達組嘗試完成一次運動的逾時時間。如果馬達或馬達組無法在規定時間內完成,則會停止嘗試並執行下一行程式碼。這樣可以防止馬達或馬達組卡在某個運動中。
void setTimeout(
int32_t time,
timeUnits units );
範圍 |
類型 |
描述 |
|---|---|---|
|
|
電機或電機組嘗試完成一次動作的時間。該值必須為正整數。 |
|
|
時間單位:
|
傳回值
此函數不傳回值。
Notes馬達或馬達組逾時用於防止未到達目標位置的運動停止堆疊中其他命令的執行。
如果馬達或馬達組在逾時時間到期前未到達目標位置,它將停止嘗試並繼續下一行程式碼。
逾時適用於具有目標值的函數,例如
spinFor和spinToPosition。
// Stop a long move after 1 second
Motor1.setTimeout(1, seconds);
Motor1.spinFor(forward, 2, turns);
Motor1.spinToPosition(0, degrees);
獲取器#
isDone#
isDone 傳回馬達或馬達組是否已完成運動,傳回值為布林值。此值可用於根據馬達或馬達組的運動狀態來控制其他行為的執行時間。
true— 馬達或馬達組停止運轉。false— 馬達或馬達組仍在運轉。
此函數與以下具有 waitForCompletion 參數的 Motion 函數搭配使用:spinFor 和 spinToPosition。
bool isDone();
此函數不接受任何參數。
Return Values返回馬達或電機組是否停止運動,以布林值表示。
true— The motor or motor group is finished moving.false— The motor or motor group is still moving.
isDone與以下具有waitForCompletion參數的 Motion 函數配合使用:spinFor和spinToPosition。
// Spin forward until the motor is done spinning
Motor1.spinFor(forward, 200, degrees, false);
while (true) {
if (Motor1.isDone()) {
Drivetrain.stop();
} else {
Drivetrain.drive(forward);
}
}
isSpinning#
isSpinning 傳回馬達或馬達群組是否正在旋轉,傳回值為布林值。這可用於根據馬達或電機組的運動狀態來控制其他行為的執行時間。
true— 馬達或馬達組正在旋轉。false— 馬達或馬達組不旋轉。
此函數與 Motion 移動函數搭配使用,例如 spin、spinFor 和 spinToPosition。
bool isSpinning();
此函數不接受任何參數。
Return Values返回馬達或馬達組是否正在旋轉,以布林值表示。
true— The motor or motor group is spinning.false— The motor or motor group is not spinning.
isSpinning與 Motion 運動函數搭配使用,例如spin、spinFor和spinToPosition。
// Spin forward until the motor is done spinning
Motor1.spinFor(forward, 200, degrees, false);
while (true) {
if (Motor1.isSpinning()) {
Drivetrain.drive(forward);
} else {
Drivetrain.stop();
}
}
position#
A motor or motor group’s position is how far it has spun, measured in degrees or turns. One turn is equal to 360 degrees. position returns the motor or motor group’s current position.
專案開始時,馬達或馬達組的位置設定為 0 度。如果馬達或馬達組正轉一圈,則位置為 360 度或 1 圈。如果馬達或馬達組反向旋轉,則位置為負值。
Available Functionsdouble position(
rotationUnits units );
範圍 |
類型 |
描述 |
|---|---|---|
|
|
The unit to return the motor or motor group position in:
|
傳回 double ,表示馬達或馬達組在指定單位中的目前位置。
呼叫
setPosition會改變此值的參考點。返回值反映的是當前編碼器讀數,並不表示馬達或馬達組是否正在運作。
當對
motor_group呼叫此函數時,函數會傳回群組中第一個馬達的位置。
// Spin, then show the final position
Motor1.spin(forward);
wait(1, seconds);
Motor1.stop();
Brain.Screen.print("Pos: %f", Motor1.position(degrees));
velocity#
velocity 返回馬達或馬達組的旋轉速度。
正值表示馬達或馬達組正轉,負值表示馬達或馬達組反轉。
Available Functionsdouble velocity(
velocityUnits units );
範圍 |
類型 |
描述 |
|---|---|---|
|
|
速度單位:
|
返回 double 表示馬達或馬達組的旋轉速度(以指定單位表示)。
條筆記
當對
motor_group呼叫此函數時,該函數會傳回群組中第一個馬達的速度。
current#
current 返回馬達或馬達組的電流大小。電流是指流過馬達或馬達組的電量。以安培為單位返回時,電流值範圍為 0.0 至 1.2 A。
電流值越高,表示馬達或馬達組消耗的電流越大。這種情況可能發生在馬達或馬達組提升重物、推動物體或試圖移動卡住的物體時。
這可以用來檢查馬達或馬達組在運動過程中是否運作吃力。如果電流持續過高,馬達可能會發熱或功率利用率降低。
Available Functions1 — 返回馬達或馬達組的電流(以安培為單位)。
double current( currentUnits units = amp );
Parameters2 — 返回馬達或馬達組的電流佔最大額定電流的百分比。
double current( percentUnits units );
範圍 |
類型 |
描述 |
|---|---|---|
|
|
電流單位: |
|
|
表示目前電流佔馬達最大額定電流的百分比: |
傳回一個 double 表示馬達或馬達組以指定單位消耗的電流大小。
條筆記
當對
motor_group呼叫此函數時,函數會傳回該群組中所有馬達的總電流。
power#
power 返回馬達或馬達組的功率(單位為瓦特)。功率表示馬達或馬達組消耗能量的速度。
功率值越高,代表馬達或馬達組的能耗速度越快。這種情況可能發生在馬達或馬達組提升重物、推動物體或試圖在卡住時移動時。
這可以用來比較運動情況,或檢查馬達或馬達組是否運轉吃力。如果功率持續偏高,馬達可能會發熱或能量利用效率降低。
Available Functionsdouble power(
powerUnits units = watt );
參數
範圍 |
類型 |
描述 |
|---|---|---|
|
|
馬達功率單位:
|
傳回值
返回 double 表示馬達或馬達組正在使用的功率(單位為瓦特)。
條筆記
當對
motor_group呼叫此函數時,函數會傳回群組中第一個馬達的電功率。
torque#
扭矩表示馬達或馬達組在旋轉時扭轉、推動或拉動的力度。
torque 返回馬達或馬達組正在使用的扭矩大小。
扭力值越高,代表馬達或馬達組的推拉力越大。這種情況可能發生在馬達或馬達組提升重物、推動物體或試圖移動卡住的物體時。
這可以用來檢查運動系統或運動系統是否有困難,或比較不同動作需要多少推力。
若要設定馬達或馬達組的扭矩,請使用 setMaxTorque。
double torque(
torqueUnits units = Nm );
參數
範圍 |
類型 |
描述 |
|---|---|---|
|
|
馬達扭力的單位:
|
傳回值
傳回一個 double 的值,表示馬達或馬達組在指定單位下所使用的扭力大小。
條筆記
當對
motor_group呼叫此函數時,該函數會傳回群組中第一個馬達的機械輸出扭力。
efficiency#
efficiency 返回馬達或馬達組使用功率的效率,以 0% 到 100% 的百分比表示。
效率值表示馬達或馬達組的功率有多少用於運動。效率值越高,表示馬達或馬達組用於運動的功率越多。當馬達或馬達組高負載運轉但運動量很小時,例如卡住或抵住障礙物時,效率值就會降低。
這可以用來比較運動情況,或者檢查電機或電機組是否浪費電力而不是將其用於運動。
Available Functionsdouble efficiency(
percentUnits units = percent );
參數
範圍 |
類型 |
描述 |
|---|---|---|
|
|
用於表示馬達效率的單位:
|
傳回值
返回 double 表示馬達或馬達組的功率使用效率(以百分比表示)。
條筆記
當對
motor_group呼叫此函數時,函數會傳回群組中第一個馬達的效率。
temperature#
temperature 返回馬達或馬達組的溫度。
馬達溫度顯示馬達或馬達組的溫度。溫度越高,表示馬達或馬達組在工作過程中溫度越高。為確保馬達始終以最佳性能運行,溫度應保持在 55°C。
如果馬達或馬達組過熱,它會降低最大電流以保護自身。當 70°C°C 時,馬達將停止運行,直到冷卻。
這可以用來檢查馬達或馬達組在重複運動、長時間運轉或推動物體時是否過熱。
Available Functions1 — 返回馬達或馬達組溫度佔最大工作溫度的百分比。
double temperature( percentUnits units = percent );
2 — 以物理溫度單位返回馬達或馬達組溫度。
double temperature( temperatureUnits units );
參數
範圍 |
類型 |
描述 |
|---|---|---|
|
|
用於表示馬達或馬達組溫度的百分比單位:
|
|
|
表示馬達或馬達組溫度的單位(以攝氏度為單位):
|
傳回值
傳回一個 double 表示馬達或馬達組的溫度,單位為指定單位。
條筆記
馬達的工作溫度範圍約為
20°C(68°F) 至70°C(158°F)。當對
motor_group呼叫此函數時,函數會傳回群組中第一個馬達的溫度。
voltage#
voltage 返回提供給馬達或馬達組的電壓。
double voltage(
voltageUnits units = volt );
參數
範圍 |
類型 |
描述 |
|---|---|---|
|
|
電壓單位:
|
傳回值
傳回一個 double 表示以指定單位提供給馬達或馬達組的電壓。
條筆記
當對
motor_group呼叫此函數時,該函數會傳回群組中第一個馬達的電壓。
direction#
direction 返回馬達或馬達組的旋轉方向。
directionType direction();
參數
此函數不接受任何參數。
傳回值
返回 directionType ,表示馬達或馬達組的當前方向:
forward— The motor or motor group is spinning forward.reverse— The motor or motor group is spinning in reverse.
installed#
installed 返回馬達或馬達組是否連接到 IQ(第一代)大腦。
bool installed();
參數
此函數不接受任何參數。
傳回值
傳回 bool 布林值,指示馬達或馬達組是否已連接。
true— The motor or motor group is connected.false— The motor or motor group is not connected.
count#
count 返回馬達組中的馬達數量。
int32_t count();
參數
此函數不接受任何參數。
傳回值
傳回 int32_t ,表示馬達組中的馬達數量。