汽车和汽车集团#
介绍#
Motors and motor groups control how parts of a robot move. A motor object controls one configured IQ Smart Motor, while a motor_group object controls multiple configured IQ Smart Motors together so they move in tandem. Motors and motor groups can be used to raise an arm, turn a claw, spin a wheel, or move another part of a build. Two motors can work together as a drivetrain to move and turn the whole robot.
Each motor or motor group is configured in the Devices window. Depending on the build, motor and motor group names can change. This page uses Motor1 and MotorGroup1 as example names. Replace them with your own configured names as needed.
By default, forward spins a motor counterclockwise, and reverse spins a motor clockwise. If a motor is set to reverse, those directions will be switched.
发动机#
Class Constructors#
1 — Creates a
motorobject for the IQ Smart Motor connected to the specified Smart Port. The motor automatically uses the detected internalgearSetting.motor( int32_t index );
2 — Creates a
motorobject on the specified Smart Port and optionally reverses its direction. The motor automatically uses the detected internalgearSetting.motor( int32_t index, bool reverse );
3 — Creates a
motorobject on the specified Smart Port with the specifiedgearSetting.motor( int32_t index, gearSetting gears );
4 — Creates a
motorobject on the specified Smart Port with the specifiedgearSettingand optional reversed direction.motor( int32_t index, gearSetting gears, bool reverse );
Class Destructor#
Destroys the motor object and releases associated resources.
~motor();
Parameters#
范围 |
类型 |
描述 |
|---|---|---|
|
|
The Smart Port that the motor is connected to, written as |
|
|
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#
Destroys the motor_group object and releases associated resources.
~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);
成员功能#
There are many ways to code motors and motor groups. Below is a list of all motor and motor_group member functions:
操作——停止和旋转电机及电机组。
spin— Spins a motor or motor group forward or reverse forever.spinFor— Spins a motor or motor group for a specific distance or amount of time.spinToPosition— Spins a motor or motor group to a specific position.stop— Stops a motor or motor group from spinning.
变异器 — 调整电机和电机组设置。
setVelocity— Tells a motor or motor group how fast to spin.setMaxTorque— Sets how hard a motor or motor group is allowed to push while spinning.setPosition— Changes the motor or motor group’s current position to a new value.setStopping— Sets how a motor or motor group will stop moving: by braking, coasting, or holding.setTimeout— Sets how much time a motor or motor group will try to finish a movement.
Getters — 检查电机和电机组状态。
isDone— Returns whether the motor or motor group is finished moving, as a Boolean value.isSpinning— Returns whether the motor or motor group is spinning, as a Boolean value.position— Returns the motor or motor group’s current position.velocity— Returns how fast the motor or motor group is spinning.current— Returns how much electrical current the motor or motor group is using.power— Returns how much power the motor or motor group is using.torque— Returns how much torque the motor or motor group is using.efficiency— Returns how efficiently the motor or motor group is using power.temperature— Returns the temperature of the motor or motor group.voltage— Returns the electrical voltage supplied to the motor or motor group.direction— Returns the direction the motor or motor group is spinning.installed— Returns whether the motor or motor group is connected to the Brain.count— Returns the number of motors in a motor group.
Before calling any motor or motor_group member functions, an instance must be created, as shown below:
/* 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 spins a motor or motor group forward or reverse forever. The motor or motor group will continue to spin until it is given another action, like spinning in a different direction or stopping.
1 — 使用当前配置的电机速度旋转电机或电机组。
void spin( directionType dir );
2 — 使电机或电机组以指定的速度旋转。
void spin( directionType dir, double velocity, velocityUnits units );
Parameters3 — 使用指定的电压驱动电机或电机组旋转。
void spin( directionType dir, double voltage, voltageUnits units );
范围 |
类型 |
描述 |
|---|---|---|
|
|
The direction the motor or motor group spins: |
|
|
The velocity to spin with from 0% to 100% when using |
|
|
The unit used to represent velocity:
|
|
|
The voltage to spin with. A negative value spins opposite the given |
|
|
The unit used to represent voltage:
|
此函数不返回值。
Notes该函数不等待,调用后项目将立即继续执行。
The motor or motor group will continue spinning until
stopis called or another movement function is executed.Functions such as
isDoneandisSpinningare not applicable tospin, since it does not use target-based movement (doesn’t have arotationortimeparameter).
// Spin forward, then stop
Motor1.spin(forward);
wait(1, seconds);
Motor1.stop();
spinFor#
spinFor spins a motor or motor group for a specific distance or amount of time. A rotation-based spin is relative to the current position of the motor or motor group. By default, the project will wait until the motor or motor group is done spinning before the next line of code runs.
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 direction the motor or motor group spins: |
|
|
电机或电机组的旋转距离。 |
|
|
The rotation unit:
|
|
|
电机或电机组旋转的时间长度。 |
|
|
The unit of time:
|
|
|
The velocity to spin with from 0% to 100% when using |
|
|
The unit used to represent
|
|
|
|
返回一个布尔值,表示电机或电机组是否达到目标值。
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.
Executing another motor movement function (such as
spinorstop) whilespinForis in progress will interrupt the current movement.Because
spinForis target-based (uses arotationortimeparameter), functions such asisDoneandisSpinningwork withspinFor.
// 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 spins a motor or motor group to a specific 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. At the beginning of a project, the motor or motor group position is set to 0 degrees. The motor or motor group position can also be set using the setPosition function.
位置值是绝对值。这意味着旋转方向取决于电机或电机组的当前位置。
例如,如果电机或电机组从 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:
|
|
|
The velocity to spin with from 0% to 100% when using |
|
|
The unit used to represent
|
|
|
|
返回一个布尔值,表示电机或电机组是否已开始请求的基于目标的运动。
true— The motor or motor group begins the requested target-based movement.false— The call fails orwaitForCompletionis set tofalse.
Executing another motor movement function (such as
spinorspinFor) whilespinToPositionis in progress will interrupt the current movement.Because
spinToPositionis target-based (uses arotationparameter), functions such asisDoneandisSpinningwork withspinToPosition.
// Spin forward, then go to 90°
Motor1.spin(forward);
wait(1, seconds);
Motor1.spinToPosition(90, degrees);
stop#
stop stops a motor or motor group from spinning.
1 — 使用当前配置的制动模式 停止电机或电机组。
void stop();
Parameters2 — 使用函数调用中提供的制动模式停止电机或电机组。
void stop( brakeType mode );
范围 |
类型 |
描述 |
|---|---|---|
|
|
Optional. How the motor or motor group will stop:
|
此函数不返回值。
NotesCalling
stopimmediately stops any current motor or motor group movement.如果没有指定制动模式,则电机或电机组使用当前配置的制动模式。
stopinterrupts any active target-based movement (such asspinFororspinToPosition).
// Spin and coast to a stop
Motor1.setVelocity(100, percent);
Motor1.spin(forward);
wait(1, seconds);
Motor1.stop(coast);
修改器#
setVelocity#
setVelocity tells a motor or motor group how fast to spin. A higher percentage makes the motor or motor group spin faster and a lower percentage makes the motor or motor group spin slower.
每个项目开始时,每个电机或电机组默认以 50% 的速度旋转。
**注意:**更高的速度会使电机或电机组旋转得更快,但精度可能会降低。更低的速度会使电机或电机组旋转得更慢,但精度可能会更高。
Available Functionsvoid setVelocity(
double velocity,
velocityUnits units );
范围 |
类型 |
描述 |
|---|---|---|
|
|
The velocity to spin with from 0% to 100% when using |
|
|
The unit used to represent
|
此函数不返回值。
NotesAny subsequent motor movement function (such as
spin,spinFor, orspinToPosition) that does not explicitly specify a velocity will use this value.
// 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 sets the most torque a motor or motor group is allowed to use.
较高的百分比可以让电机或电机组施加更大的推力,例如在举起重物时。较低的百分比则会限制电机或电机组的推力。这有助于在电机或电机组卡住或达到其运动极限时保护机器人。
每个项目开始时,每个电机或电机组的扭矩默认设置为 50%。
Available Functions1 — 将最大扭矩设置为电机或电机组额定扭矩的百分比。
void setMaxTorque( double value, percentUnits units );
2 — 使用物理扭矩单位设置最大扭矩。
void setMaxTorque( double value, torqueUnits units );
Parameters3 — 通过限制电机电流来设定最大扭矩。
void setMaxTorque( double value, currentUnits units );
范围 |
类型 |
描述 |
|---|---|---|
|
|
电机或电机组可使用的最大扭矩。 |
|
|
Specifies torque as a percentage: |
|
|
Specifies torque in physical units such as:
|
|
|
Specifies torque by limiting motor current:
|
此函数不返回值。
NotesWhen using
currentUnits, torque is limited by restricting how much electrical current the motor or motor group can use.
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:
|
此函数不返回值。
NotesAfter calling
setPosition, subsequent calls tospinToPositionwill use the updated position as the reference.
setStopping#
setStopping sets how a motor or motor group will stop moving: by braking, coasting, or holding.
void setStopping(
brakeType mode );
范围 |
类型 |
描述 |
|---|---|---|
|
|
How the motor or motor group will stop:
|
此函数不返回值。
NotesAny subsequent call to
stopwithout a specified brake mode will use this value.
setTimeout#
setTimeout sets how much time a motor or motor group will try to finish a movement. If the motor or motor group cannot finish in that time, it will stop trying and move on to the next line of code. This keeps the motor or motor group from getting stuck on a movement.
void setTimeout(
int32_t time,
timeUnits units );
范围 |
类型 |
描述 |
|---|---|---|
|
|
电机或电机组尝试完成一次动作的时间。该值必须为正整数。 |
|
|
The unit of time:
|
返回值
此函数不返回值。
Notes电机或电机组超时用于防止未到达目标位置的运动停止堆栈中其他命令的执行。
如果电机或电机组在超时时间到期前未到达目标位置,它将停止尝试并继续执行下一行代码。
The timeout applies to functions that have a target value such as
spinForandspinToPosition.
// Stop a long move after 1 second
Motor1.setTimeout(1, seconds);
Motor1.spinFor(forward, 2, turns);
Motor1.spinToPosition(0, degrees);
吸气剂#
isDone#
isDone returns whether the motor or motor group is finished moving, as a Boolean value. This can be used to control the timing of other behaviors based on the motor or motor group’s movement.
true— The motor or motor group is finished moving.false— The motor or motor group is still moving.
This function works together with the following Motion functions that have the waitForCompletion parameter: spinFor and spinToPosition.
bool isDone();
此函数不接受任何参数。
Return Values返回电机或电机组是否停止运动,以布尔值表示。
true— The motor or motor group is finished moving.false— The motor or motor group is still moving.
isDoneworks together with the following Motion functions that have thewaitForCompletionparameter:spinForandspinToPosition.
// 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 returns whether the motor or motor group is spinning, as a Boolean value. This can be used to control the timing of other behaviors based on the motor or motor group’s movement.
true— The motor or motor group is spinning.false— The motor or motor group is not spinning.
This function works together with Motion movement functions such as spin, spinFor, and spinToPosition.
bool isSpinning();
此函数不接受任何参数。
Return Values返回电机或电机组是否正在旋转,以布尔值表示。
true— The motor or motor group is spinning.false— The motor or motor group is not spinning.
isSpinningworks with Motion movement functions such asspin,spinFor, andspinToPosition.
// 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:
|
Returns a double representing the motor or motor group’s current position in the specified units.
Calling
setPositionchanges the reference point for this value.返回值反映的是当前编码器读数,并不表示电机或电机组是否正在运行。
When called on a
motor_group, this function returns the position of the first motor in the 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 returns how fast the motor or motor group is spinning.
正值表示电机或电机组正转,负值表示电机或电机组反转。
Available Functionsdouble velocity(
velocityUnits units );
范围 |
类型 |
描述 |
|---|---|---|
|
|
The unit used to represent velocity:
|
Returns a double representing how fast the motor or motor group is spinning in the specified units.
条笔记
When called on a
motor_group, this function returns the velocity of the first motor in the group.
current#
current returns how much electrical current the motor or motor group is using. Current is the amount of electricity flowing through the motor or motor group. When returned in amps, current is reported from 0.0 to 1.2 A.
电流值越高,表示电机或电机组消耗的电流越大。这种情况可能发生在电机或电机组提升重物、推动物体或试图移动卡住的物体时。
这可以用来检查电机或电机组在运动过程中是否运转吃力。如果电流持续过高,电机可能会发热或功率利用率降低。
Available Functions1 — 返回电机或电机组的电流(以安培为单位)。
double current( currentUnits units = amp );
Parameters2 — 返回电机或电机组的电流占最大额定电流的百分比。
double current( percentUnits units );
范围 |
类型 |
描述 |
|---|---|---|
|
|
The unit used to represent current: |
|
|
Represents the current as a percentage of the motor’s maximum rated current: |
Returns a double representing how much electrical current the motor or motor group is drawing in the specified units.
条笔记
When called on a
motor_group, this function returns the total electrical current for all motors in the group.
power#
power returns how much power the motor or motor group is using in watts. Power shows how quickly the motor or motor group is using energy.
功率值越高,意味着电机或电机组的能耗速度越快。这种情况可能发生在电机或电机组提升重物、推动物体或试图在卡住时移动时。
这可以用来比较运动情况,或者检查电机或电机组是否运转吃力。如果功率持续偏高,电机可能会发热或能量利用效率降低。
Available Functionsdouble power(
powerUnits units = watt );
参数
范围 |
类型 |
描述 |
|---|---|---|
|
|
The unit used to represent the motor power:
|
返回值
Returns a double representing how much power the motor or motor group is using in watts.
条笔记
When called on a
motor_group, this function returns the electrical power of the first motor in the group.
torque#
扭矩表示电机或电机组在旋转时扭转、推动或拉动的力度。
torque returns how much torque the motor or motor group is using.
扭矩值越高,意味着电机或电机组的推拉力越大。这种情况可能发生在电机或电机组提升重物、推动物体或试图移动卡住的物体时。
这可以用来检查运动系统或运动系统是否出现困难,或者比较不同动作需要多少推力。
To set the torque of a motor or motor group, use setMaxTorque.
double torque(
torqueUnits units = Nm );
参数
范围 |
类型 |
描述 |
|---|---|---|
|
|
The unit used to represent the motor torque:
|
返回值
Returns a double representing how much torque the motor or motor group is using in the specified units.
条笔记
When called on a
motor_group, this function returns the mechanical output torque of the first motor in the group.
efficiency#
efficiency returns how efficiently the motor or motor group is using power, as a percentage from 0% to 100%.
效率值表示电机或电机组的功率有多少用于运动。效率值越高,意味着电机或电机组用于运动的功率越多。当电机或电机组高负荷运转但运动量很小时,例如被卡住或抵住障碍物时,效率值就会降低。
这可以用来比较运动情况,或者检查电机或电机组是否浪费电力而不是将其用于运动。
Available Functionsdouble efficiency(
percentUnits units = percent );
参数
范围 |
类型 |
描述 |
|---|---|---|
|
|
The unit used to represent motor efficiency:
|
返回值
Returns a double representing how efficiently the motor or motor group is using power as a percentage.
条笔记
When called on a
motor_group, this function returns the efficiency of the first motor in the group.
temperature#
temperature returns the temperature of the motor or motor group.
Motor temperature shows how warm the motor or motor group is. A higher temperature means the motor or motor group is getting warmer while it works. The motor should stay below 55°C to keep working at full performance.
If the motor or motor group gets too hot, it will lower its maximum current to protect itself. At 70°C, a motor will stop running until it cools down.
这可以用来检查电机或电机组在重复运动、长时间运行或推动物体时是否过热。
Available Functions1 — 返回电机或电机组温度占最大工作温度的百分比。
double temperature( percentUnits units = percent );
2 — 以物理温度单位返回电机或电机组温度。
double temperature( temperatureUnits units );
参数
范围 |
类型 |
描述 |
|---|---|---|
|
|
The unit used to represent motor or motor group temperature as a percentage:
|
|
|
The unit used to represent motor or motor group temperature in degrees:
|
返回值
Returns a double representing the temperature of the motor or motor group in the specified units.
条笔记
The operating temperature range for the motor is approximately
20°C(68°F) to70°C(158°F).When called on a
motor_group, this function returns the temperature of the first motor in the group.
voltage#
voltage returns the electrical voltage supplied to the motor or motor group.
double voltage(
voltageUnits units = volt );
参数
范围 |
类型 |
描述 |
|---|---|---|
|
|
The unit to represent the voltage:
|
返回值
Returns a double representing the electrical voltage supplied to the motor or motor group in the specified units.
条笔记
When called on a
motor_group, this function returns the electrical voltage of the first motor in the group.
direction#
direction returns the direction that the motor or motor group is spinning.
directionType direction();
参数
此函数不接受任何参数。
返回值
Returns a directionType indicating the motor’s or motor group’s current direction:
forward— The motor or motor group is spinning forward.reverse— The motor or motor group is spinning in reverse.
installed#
installed returns whether the motor or motor group is connected to the IQ (2nd gen) Brain.
bool installed();
参数
此函数不接受任何参数。
返回值
Returns a bool indicating whether the motor or motor group is connected.
true— The motor or motor group is connected.false— The motor or motor group is not connected.
count#
count returns the number of motors in a motor group.
int32_t count();
参数
此函数不接受任何参数。
返回值
Returns an int32_t representing the number of motors in the motor group.