传动系统#
介绍#
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();
参数#
范围 |
类型 |
描述 |
|---|---|---|
|
|
左侧和右侧电机或电机组。两个参数必须使用相同的类型:要么是两个 |
|
|
传动车轮的周长。默认值为 |
|
|
左右车轮之间的距离。默认值为 |
|
|
前轮和后轮之间的距离。默认值为 |
|
|
The unit for |
|
|
如果使用齿轮,则用于调整驱动距离的齿轮比。默认值为 |
例子#
// 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 使机器人无限前进或后退。机器人将持续移动,直到收到其他指令,例如转向或停止。
1 - 使用当前驱动速度进行驱动。
void drive( directionType dir );
Parameters2 - 以指定速度行驶。
void drive( directionType dir, double velocity, velocityUnits units );
范围 |
类型 |
描述 |
|---|---|---|
|
|
机器人移动方向: |
|
|
使用 |
|
|
速度单位: |
此函数不返回值。
Notesdrive是非等待的。项目将立即运行下一行代码。机器人将继续移动,直到调用
stop或运行另一个驱动系统运动功能。isDone和isMoving不与drive一起使用,因为drive没有目标距离。
// Drive forward and back.
myDrivetrain.drive(forward);
wait(2, seconds);
myDrivetrain.drive(reverse, 25, rpm);
wait(2, seconds);
myDrivetrain.stop();
驱动#
driveFor 指令使机器人前进或后退指定距离。程序会等待机器人完成移动后才执行下一行代码。
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 );
范围 |
类型 |
描述 |
|---|---|---|
|
|
机器人移动方向: |
|
|
机器人行驶的距离。 |
|
|
距离单位: |
|
|
使用 |
|
|
速度单位: |
|
|
|
返回机器人是否达到目标距离,以布尔值表示。
true- 机器人已达到目标距离。false- 机器人没有完成移动,或者函数在移动完成之前返回,因为waitForCompletion设置为false。
// Drive 200 mm, then back 200 mm.
myDrivetrain.driveFor(forward, 200, mm);
myDrivetrain.driveFor(reverse, 200, mm);
转动#
turn 表示机器人无限向左或向右转向。机器人将持续转向,直到收到其他指令,例如行驶或停止。
1 - 使用当前转弯速度转弯。
void turn( turnType dir );
Parameters2 - 以指定速度转弯。
void turn( turnType dir, double velocity, velocityUnits units );
范围 |
类型 |
描述 |
|---|---|---|
|
|
机器人转向的方向: |
|
|
当使用 |
|
|
速度单位: |
此函数不返回值。
Notesturn行是非等待的。项目将立即运行下一行代码。机器人将继续旋转,直到调用
stop或运行另一个驱动系统运动功能。isDone和isMoving不与turn一起使用,因为turn没有目标角度。
// Turn right, then left, then stop.
myDrivetrain.turn(right);
wait(2, seconds);
myDrivetrain.turn(left);
wait(2, seconds);
myDrivetrain.stop();
转弯#
turnFor 参数用于控制机器人向左或向右旋转指定的角度、圈数或单位。旋转角度相对于机器人的当前位置。程序会等待机器人完成旋转后才执行下一行代码。
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 );
范围 |
类型 |
描述 |
|---|---|---|
|
|
机器人转向的方向: |
|
|
机器人的转弯角度。 |
|
|
旋转单位: |
|
|
当使用 |
|
|
速度单位: |
|
|
|
返回机器人是否达到目标角度,以布尔值表示。
true- 机器人达到目标角度。false- 机器人没有完成回合,或者函数在回合完成之前返回,因为waitForCompletion设置为false。
// Turn right, then left.
myDrivetrain.turnFor(right, 90, degrees);
wait(1, seconds);
myDrivetrain.turnFor(left, 90, degrees);
停止#
stop 停止机器人的运动。如果提供了停止模式,则控制机器人在此函数调用中如何停止。
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 告诉机器人行驶速度。百分比越高,机器人行驶速度越快;百分比越低,机器人行驶速度越慢。
每个项目开始时,机器人默认以 50% 的速度行驶。
注意:速度越高,机器人行驶速度越快,但精度可能越低;速度越低,机器人行驶速度越慢,但精度可能越高。
Available Functionsvoid setDriveVelocity(
double velocity,
velocityUnits units );
范围 |
类型 |
描述 |
|---|---|---|
|
|
使用 |
|
|
速度单位: |
此函数不返回值。
Notes除非在函数调用中指定了特定速度,否则驱动速度将被后续的传动系统运动函数使用。
// Drive forward, then coast to a stop.
myDrivetrain.setDriveVelocity(100, percent);
myDrivetrain.drive(forward);
wait(2, seconds);
myDrivetrain.stop(coast);
设置转弯速度#
setTurnVelocity 告诉机器人转弯的速度。百分比越高,机器人转弯越快;百分比越低,机器人转弯越慢。
每个项目开始时,机器人默认以 50% 的速度旋转。
注意:速度越高,机器人转弯越快,但精度可能越低;速度越低,机器人转弯越慢,但精度可能越高。
Available Functionsvoid setTurnVelocity(
double velocity,
velocityUnits units );
范围 |
类型 |
描述 |
|---|---|---|
|
|
使用 |
|
|
速度单位: |
此函数不返回值。
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 设置机器人停止移动的方式:通过刹车、滑行或保持。
void setStopping(
brakeType mode );
范围 |
类型 |
描述 |
|---|---|---|
|
|
How the robot will stop:
|
此函数不返回值。
Notes除非在函数调用中提供特定的停止模式,否则后续的
stop调用将使用停止模式。如果未使用此功能,机器人停止时将使用
brake。
设置超时#
setTimeout 设置机器人尝试完成一个动作的超时时间。如果机器人无法在规定时间内完成,它将停止尝试并执行下一行代码。这可以防止机器人卡在某个动作上。
void setTimeout(
int32_t time,
timeUnits units );
范围 |
类型 |
描述 |
|---|---|---|
|
|
机器人尝试完成一次动作的时间。该值应为正整数。 |
|
|
时间单位: |
此函数不返回值。
Notes Examples// Turn right after driving forward.
myDrivetrain.setTimeout(1, seconds);
myDrivetrain.driveFor(forward, 25, inches);
myDrivetrain.turnFor(right, 90, degrees);
获取器#
已完成#
isDone 返回机器人是否已完成移动,结果为布尔值。这可用于根据机器人的移动状态控制其他行为的执行时间。
bool isDone( void );
此函数不接受任何参数。
Return Valuestrue- 机器人已停止移动。false- 机器人仍在移动。
正在移动#
isMoving 返回机器人是否正在移动,结果为布尔值。这可用于根据机器人的移动状态控制其他行为的执行时间。
virtual bool isMoving( void );
此函数不接受任何参数。
Return Valuestrue- 机器人正在移动。false- 机器人没有移动。
速度#
velocity 返回机器人行驶的速度。
它可以以百分比、 rpm或 dps的形式返回速度。正值表示机器人向前行驶,负值表示机器人向后行驶。
double velocity(
velocityUnits units );
范围 |
类型 |
描述 |
|---|---|---|
|
|
返回速度的单位: |
返回所选单位的传动系统速度,数值为 double (双精度浮点数)。
// 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 Functions1 - 返回传动系统的电流(以安培为单位)。
double current( currentUnits units = currentUnits::amp );
Parameters2 - 返回传动系统的电流百分比。
double current( percentUnits units );
范围 |
类型 |
描述 |
|---|---|---|
|
|
返回电流的单位: |
|
|
返回电流的单位: |
返回所选单位的传动系统电流,数值为 double。
力量#
power 返回传动系统正在使用的功率,单位为瓦特。功率值表示传动系统消耗能量的速度。
功率值越高,意味着驱动系统消耗能量的速度越快。这种情况可能发生在机器人推挤物体或被卡住后试图移动时。
这可以用来比较不同的运动轨迹,或者检查传动系统是否运转吃力。如果功率持续偏高,传动系统可能会过热或能量利用效率降低。
Available Functionsdouble power(
powerUnits units = powerUnits::watt );
范围 |
类型 |
描述 |
|---|---|---|
|
|
功率单位: |
以瓦特为单位返回传动系统功率( double。
扭矩#
扭矩表示传动系统在车轮旋转时能够施加多大的推力或拉力。
torque 返回传动系统使用的扭矩大小,以牛顿米 (Nm) 或英寸磅 (InLb) 为单位测量。
扭矩值越高,意味着传动系统需要更大的推力或拉力。这种情况可能发生在机器人推挤物体或被卡住后试图移动时。
这可以用来检查传动系统是否运转不畅,或者比较不同动作所需的推力大小。
Available Functionsdouble torque(
torqueUnits units = torqueUnits::nm );
范围 |
类型 |
描述 |
|---|---|---|
|
|
返回扭矩的单位: |
返回所选单位的传动系统扭矩,数值为 double。
效率#
efficiency 返回动力传动系统使用动力的效率,以百分比表示,范围从 0% 到 100%。
效率值反映了传动系统有多少动力被用于运动。效率值越高,意味着传动系统用于运动的动力越多。当传动系统高负荷运转但运动量不大时,例如机器人被卡住或抵挡物体时,效率值会降低。
这可以用来比较运动情况,或者检查传动系统是否浪费动力而不是将其用于运动。
Available Functionsdouble efficiency(
percentUnits units = percentUnits::pct );
范围 |
类型 |
描述 |
|---|---|---|
|
|
单位返回效率为: |
返回所选单位的传动系统效率,数值为 double。
温度#
temperature 返回传动系统的平均温度,以百分比表示,范围从 0% 到 100%。
温度显示传动系统电机的温度。温度越高,意味着电机在工作过程中温度越高。为了保持最佳性能,电机温度应保持在 55°C 以下。
如果电机过热,它们会降低最大电流以保护自身。当温度达到 70°C 时,电机将停止运转,直到冷却下来。
这可以用来检查传动系统在重复运动、长时间运行或推动物体时是否过热。
Available Functionsdouble temperature(
percentUnits units );
范围 |
类型 |
描述 |
|---|---|---|
|
|
返回温度的单位: |
返回所选单位的平均传动系统温度,数值为 double。
返回值为
50%表示平均电机温度约为45°C(113°F)。传动系统的典型工作温度范围约为
20°C(68°F) 至70°C(158°F)。
电压#
voltage 返回传动系统的电压。
电压是指供给传动系统电机的电压力。结合电流和功率值,我们可以了解传动系统如何利用电能。
Available Functionsdouble voltage(
voltageUnits units = voltageUnits::volt );
范围 |
类型 |
描述 |
|---|---|---|
|
|
返回电压的单位: |
返回所选单位的传动系统电压,数值为 double。