传动系统#
介绍#
The drivetrain class controls how a robot drives and turns. A drivetrain is made of motors and wheels that work together to move the robot.
A drivetrain does not use a Gyro Sensor or Inertial Sensor for heading-based or rotation-based turns. It can drive forward or reverse and turn left or right, but it cannot turn to a specific heading or rotation. For heading-based and rotation-based turns, use the smartdrive class.
This page uses myDrivetrain as the example drivetrain name. Replace it with your own configured name as needed.
派生类#
The drivetrain class serves as a base class for the following derived class:
smartdrive- Creates a drivetrain with Gyro Sensor or Inertial Sensor support.
类构造函数#
1 - Creates a drivetrain using existing
motor_groupobjects for the left and right sides.drivetrain( motor_group &l, motor_group &r, double wheelTravel = 320, double trackWidth = 320, double wheelBase = 130, distanceUnits unit = mm, double externalGearRatio = 1.0 );
2 - Creates a drivetrain using existing
motorobjects for the left and right sides.drivetrain( motor &l, motor &r, double wheelTravel = 320, double trackWidth = 320, double wheelBase = 130, distanceUnits unit = mm, double externalGearRatio = 1.0 );
类析构函数#
Destroys the drivetrain object and releases associated resources.
virtual ~drivetrain();
参数#
范围 |
类型 |
描述 |
|---|---|---|
|
|
The left and right motors or motor groups. Both parameters must use the same type: either two |
|
|
The circumference of the drivetrain wheels. The default is |
|
|
The distance between the left and right wheels. The default is |
|
|
The distance between the front and back wheels. The default is |
|
|
The unit for |
|
|
The gear ratio used to adjust drive distances if gears are used. The default is |
例子#
// 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
成员功能#
The drivetrain class includes the following member functions:
操作 - 移动和转向机器人。
drive- Moves the robot forward or reverse forever.driveFor- Moves the robot forward or reverse for a specific distance.turn- Turns the robot left or right forever.turnFor- Turns the robot left or right for a specific number of degrees or revolutions.stop- Stops the robot’s movement.
设置 - 调整传动系统设置。
setDriveVelocity- Tells the robot how fast to drive.setTurnVelocity- Tells the robot how fast to turn.setStopping- Tells how the robot will stop moving: by braking, coasting, or holding.setTimeout- Sets how long the robot will try to finish a movement.
数值 - 检查移动状态。
isDone- Returns whether the robot is finished moving, as a Boolean value.isMoving- Returns whether the robot is moving, as a Boolean value.velocity- Returns how fast the robot is driving.current- Returns how much electrical current the drivetrain is using.power- Returns how quickly the drivetrain is using energy.torque- Returns how much torque the drivetrain is using.efficiency- Returns how efficiently the drivetrain is using power.temperature- Returns how warm the drivetrain is.voltage- Returns the electrical voltage of the drivetrain.
Before calling any drivetrain member functions, a drivetrain object must be created.
/* 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 moves the robot forward or reverse forever. The robot will continue to move until it is given another action, like turning or stopping.
1 - 使用当前驱动速度进行驱动。
void drive( directionType dir );
Parameters2 - 以指定速度行驶。
void drive( directionType dir, double velocity, velocityUnits units );
范围 |
类型 |
描述 |
|---|---|---|
|
|
The direction the robot moves: |
|
|
The velocity to drive with from 0% to 100% when using |
|
|
The velocity unit: |
此函数不返回值。
Notesdriveis non-waiting. The project will run the next line of code right away.The robot will continue to move until
stopis called or another drivetrain movement function runs.isDoneandisMovingare not used withdrivebecausedrivedoes not have a target distance.
// Drive forward and back.
myDrivetrain.drive(forward);
wait(2, seconds);
myDrivetrain.drive(reverse, 25, rpm);
wait(2, seconds);
myDrivetrain.stop();
驱动#
driveFor moves the robot forward or reverse for a specific distance. The project will wait until the robot is done moving before the next line of code runs.
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 );
Parameters4 - 以指定的速度沿指定方向行驶指定距离。
bool driveFor( directionType dir, double distance, distanceUnits units, double velocity, velocityUnits units_v, bool waitForCompletion = true );
范围 |
类型 |
描述 |
|---|---|---|
|
|
The direction the robot moves: |
|
|
机器人行驶的距离。 |
|
|
The distance unit: |
|
|
The velocity to drive with from 0% to 100% when using |
|
|
The velocity unit: |
|
|
|
返回机器人是否达到目标距离,以布尔值表示。
true- The robot reached the target distance.false- The robot did not finish the movement, or the function returned before the movement completed becausewaitForCompletionwas set tofalse.
Running another drivetrain movement function while
driveForis running will interrupt the current movement.driveForuses a target distance, soisDoneandisMovingcan be used with it.
// Drive 200 mm, then back 200 mm.
myDrivetrain.driveFor(forward, 200, mm);
myDrivetrain.driveFor(reverse, 200, mm);
转动#
turn turns the robot left or right forever. The robot will continue to turn until it is given another action, like driving or stopping.
1 - 使用当前转弯速度转弯。
void turn( turnType dir );
Parameters2 - 以指定速度转弯。
void turn( turnType dir, double velocity, velocityUnits units );
范围 |
类型 |
描述 |
|---|---|---|
|
|
The direction the robot turns: |
|
|
The velocity to turn with from 0% to 100% when using |
|
|
The velocity unit: |
此函数不返回值。
Notesturnis non-waiting. The project will run the next line of code right away.The robot will continue turning until
stopis called or another drivetrain movement function runs.isDoneandisMovingare not used withturnbecauseturndoes not have a target angle.
// Turn right, then left, then stop.
myDrivetrain.turn(right);
wait(2, seconds);
myDrivetrain.turn(left);
wait(2, seconds);
myDrivetrain.stop();
转向#
turnFor turns the robot left or right for a specific number of degrees, revolutions, or raw units. The turn is relative to the current position of the robot. The project will wait until the robot is done turning before the next line of code runs.
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 );
Parameters4 - 以指定的速度沿指定方向转弯指定角度。
bool turnFor( turnType dir, double angle, rotationUnits units, double velocity, velocityUnits units_v, bool waitForCompletion = true );
范围 |
类型 |
描述 |
|---|---|---|
|
|
The direction the robot turns: |
|
|
机器人的转弯角度。 |
|
|
The turn unit: |
|
|
The velocity to turn with from 0% to 100% when using |
|
|
The velocity unit: |
|
|
|
返回机器人是否达到目标角度,以布尔值表示。
true- The robot reached the target angle.false- The robot did not finish the turn, or the function returned before the turn completed becausewaitForCompletionwas set tofalse.
Running another drivetrain movement function while
turnForis running will interrupt the current movement.turnForuses a target angle, soisDoneandisMovingcan be used with it.
// Turn right, then left.
myDrivetrain.turnFor(right, 90, degrees);
wait(1, seconds);
myDrivetrain.turnFor(left, 90, degrees);
停止#
stop stops the robot’s movement. If a stopping mode is provided, it controls how the robot stops for this function call.
1 - 使用当前停止模式停止传动系统。
void stop();
Parameters2 - 使用指定的停止模式停止传动系统。
void stop( brakeType mode );
范围 |
类型 |
描述 |
|---|---|---|
|
|
How the robot will stop:
|
此函数不返回值。
Examples// Drive forward, then coast to a stop.
myDrivetrain.setDriveVelocity(100, percent);
myDrivetrain.drive(forward);
wait(2, seconds);
myDrivetrain.stop(coast);
变异体#
设置驱动速度#
setDriveVelocity tells the robot how fast to drive. A higher percentage makes the robot drive faster and a lower percentage makes the robot drive slower.
每个项目开始时,机器人默认以 50% 的速度行驶。
**注意:**速度越高,机器人行驶速度越快,但精度可能越低;速度越低,机器人行驶速度越慢,但精度可能越高。
Available Functionsvoid setDriveVelocity(
double velocity,
velocityUnits units );
范围 |
类型 |
描述 |
|---|---|---|
|
|
The velocity to drive with from 0% to 100% when using |
|
|
The velocity unit: |
此函数不返回值。
Notes除非在函数调用中指定了特定速度,否则驱动速度将被后续的传动系统运动函数使用。
// Drive forward, then coast to a stop.
myDrivetrain.setDriveVelocity(100, percent);
myDrivetrain.drive(forward);
wait(2, seconds);
myDrivetrain.stop(coast);
设置转弯速度#
setTurnVelocity tells the robot how fast to turn. A higher percentage makes the robot turn faster and a lower percentage makes the robot turn slower.
每个项目开始时,机器人默认以 50% 的速度旋转。
**注意:**速度越高,机器人转弯越快,但精度可能越低;速度越低,机器人转弯越慢,但精度可能越高。
Available Functionsvoid setTurnVelocity(
double velocity,
velocityUnits units );
范围 |
类型 |
描述 |
|---|---|---|
|
|
The velocity to turn with from 0% to 100% when using |
|
|
The velocity unit: |
此函数不返回值。
Notes除非在函数调用中提供特定速度,否则后续的传动系统转向功能将使用转向速度。
// 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 sets how the robot will stop moving: by braking, coasting, or holding.
void setStopping(
brakeType mode );
范围 |
类型 |
描述 |
|---|---|---|
|
|
How the robot will stop:
|
此函数不返回值。
NotesThe stopping mode is used by later
stopcalls unless a specific stopping mode is provided in the function call.If this function is not used, the robot will use
brakewhen stopping.
设置超时#
setTimeout sets how long the robot will try to finish a movement. If the robot cannot finish in that time, it will stop trying and move on to the next line of code. This keeps the robot from getting stuck on a movement.
void setTimeout(
int32_t time,
timeUnits units );
范围 |
类型 |
描述 |
|---|---|---|
|
|
机器人尝试完成一次动作的时间。该值应为正整数。 |
|
|
The unit of time: |
此函数不返回值。
NotesThe timeout only applies to drivetrain functions that move to a target, such as
driveForandturnFor.
// Turn right after driving forward.
myDrivetrain.setTimeout(1, seconds);
myDrivetrain.driveFor(forward, 25, inches);
myDrivetrain.turnFor(right, 90, degrees);
获取器#
已完成#
isDone returns whether the robot is finished moving, as a Boolean value. This can be used to control the timing of other behaviors based on the robot’s movement.
bool isDone( void );
此函数不接受任何参数。
Return Valuestrue- The robot is finished moving.false- The robot is still moving.
正在移动#
isMoving returns whether the robot is moving, as a Boolean value. This can be used to control the timing of other behaviors based on the robot’s movement.
virtual bool isMoving( void );
此函数不接受任何参数。
Return Valuestrue- The robot is moving.false- The robot is not moving.
速度#
velocity returns how fast the robot is driving.
It can return velocity as a percentage, in rpm, or in dps. A positive value means the robot is driving forward. A negative value means the robot is driving in reverse.
double velocity(
velocityUnits units );
范围 |
类型 |
描述 |
|---|---|---|
|
|
The unit to return velocity in: |
Returns the drivetrain velocity as a double in the selected unit.
// 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 returns how much electrical current the drivetrain is using. Current is the amount of electricity flowing through the drivetrain. It can be returned in amps (amperes) or as a percentage, depending on the unit.
电流值越高,意味着驱动系统消耗的电流越大。这种情况可能发生在机器人推挤物体或被卡住后试图移动时。
这可以用来检查传动系统在运动过程中是否运转吃力。如果电流持续偏高,传动系统可能会发热或动力利用效率降低。
Available Functions1 - 返回传动系统的电流(以安培为单位)。
double current( currentUnits units = currentUnits::amp );
Parameters2 - 返回传动系统的电流百分比。
double current( percentUnits units );
范围 |
类型 |
描述 |
|---|---|---|
|
|
The unit to return current in: |
|
|
The unit to return current in: |
Returns the drivetrain current as a double in the selected unit.
力量#
power returns how much power the drivetrain is using, measured in watts. Power shows how quickly the drivetrain is using energy.
功率值越高,意味着驱动系统消耗能量的速度越快。这种情况可能发生在机器人推挤物体或被卡住后试图移动时。
这可以用来比较不同的运动轨迹,或者检查传动系统是否运转吃力。如果功率持续偏高,传动系统可能会过热或能量利用效率降低。
Available Functionsdouble power(
powerUnits units = powerUnits::watt );
范围 |
类型 |
描述 |
|---|---|---|
|
|
The power unit: |
Returns drivetrain power as a double in watts.
扭矩#
扭矩表示传动系统在车轮旋转时能够施加多大的推力或拉力。
torque returns how much torque the drivetrain is using, measured in Newton-meters (Nm) or inch-pounds (InLb).
扭矩值越高,意味着传动系统需要更大的推力或拉力。这种情况可能发生在机器人推挤物体或被卡住后试图移动时。
这可以用来检查传动系统是否运转不畅,或者比较不同动作所需的推力大小。
Available Functionsdouble torque(
torqueUnits units = torqueUnits::nm );
范围 |
类型 |
描述 |
|---|---|---|
|
|
The unit to return torque in: |
Returns drivetrain torque as a double in the selected unit.
效率#
efficiency returns how efficiently the drivetrain is using power, as a percentage from 0% to 100%.
效率值反映了传动系统有多少动力被用于运动。效率值越高,意味着传动系统用于运动的动力越多。当传动系统高负荷运转但运动量不大时,例如机器人被卡住或抵挡物体时,效率值会降低。
这可以用来比较运动情况,或者检查传动系统是否浪费动力而不是将其用于运动。
Available Functionsdouble efficiency(
percentUnits units = percentUnits::pct );
范围 |
类型 |
描述 |
|---|---|---|
|
|
The unit to return efficiency in: |
Returns drivetrain efficiency as a double in the selected unit.
温度#
temperature returns the average temperature of the drivetrain as a percentage from 0% to 100%.
温度显示传动系统电机的温度。温度越高,意味着电机在工作过程中温度越高。为了保持最佳性能,电机温度应保持在 55°C 以下。
如果电机过热,它们会降低最大电流以保护自身。当温度达到 70°C 时,电机将停止运转,直到冷却下来。
这可以用来检查传动系统在重复运动、长时间运行或推动物体时是否过热。
Available Functionsdouble temperature(
percentUnits units );
范围 |
类型 |
描述 |
|---|---|---|
|
|
The unit to return temperature in: |
Returns the average drivetrain temperature as a double in the selected unit.
A returned value of
50%means the average motor temperature is about45°C(113°F).The typical operating temperature range for the drivetrain is about
20°C(68°F) to70°C(158°F).
电压#
voltage returns the electrical voltage of the drivetrain.
电压是指供给传动系统电机的电压力。结合电流和功率值,我们可以了解传动系统如何利用电能。
Available Functionsdouble voltage(
voltageUnits units = voltageUnits::volt );
范围 |
类型 |
描述 |
|---|---|---|
|
|
The unit to return voltage in: |
Returns drivetrain voltage as a double in the selected unit.