智能驱动#
介绍#
smartdrive 类通过电机、轮子和传感器控制机器人的行驶和转向。2 smartdrive 可以使用惯性传感器、陀螺仪传感器或GPS传感器来跟踪航向和旋转。
smartdrive 使用传感器辅助机器人精确移动和转向。在每个项目开始时,驱动系统会自动校准传感器。校准期间,请保持机器人静止约 2 秒钟,以便机器人能够正确移动和转向。
smartdrive 类基于 drivetrain 类。它可以使用与 drivetrain 相同的驱动和转向功能,并且还添加了基于传感器的功能,如 turnToHeading 和 turnToRotation。
类构造函数#
1 — 使用现有的
motor_group对象为左侧 (&l) 和右侧 (&r) 创建智能驱动器,以及惯性传感器、陀螺仪传感器或 GPS 传感器。smartdrive( motor_group &l, motor_group &r, inertial &g, double wheelTravel = 320, double trackWidth = 320, double wheelBase = 130, distanceUnits unit = mm, double externalGearRatio = 1.0 );
2 — 使用现有的
motor对象为左侧 (&l) 和右侧 (&r) 创建智能驱动器,以及惯性传感器、陀螺仪传感器或 GPS 传感器。smartdrive( motor &l, motor &r, inertial &g, double wheelTravel = 320, double trackWidth = 320, double wheelBase = 130, distanceUnits unit = mm, double externalGearRatio = 1.0 );
类析构函数#
销毁 smartdrive 对象并释放相关资源。
~smartdrive();
参数#
传感器参数可以是 inertial, gyro或 gps 对象。
范围 |
类型 |
描述 |
|---|---|---|
|
|
引用现有的 |
|
|
参考现有的 |
|
|
传动车轮的周长。默认值为 |
|
|
左右车轮之间的距离。默认值为 |
|
|
前轮和后轮之间的距离。默认值为 |
|
|
The unit for |
|
|
如果使用齿轮,则用于调整驱动距离的齿轮比。默认值为 |
例子#
/* 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 instances
motor leftMotor = motor(PORT1, false);
motor rightMotor = motor(PORT9, true);
// Create the inertial instance
inertial myInertial = inertial(PORT3);
// Create the smartdrive instance
smartdrive myDrivetrain = smartdrive(
leftMotor, // left motor object
rightMotor, // right motor object
myInertial, // inertial object
259.34, // wheelTravel
320, // trackWidth
40, // wheelBase
mm, // unit
1.0 ); // externalGearRatio
成员功能#
smartdrive 类包含 成员函数:
动作——使用航向或旋转控制机器人转向。
turnToHeading— 将机器人转向 -359 度到 359 度之间的特定航向。机器人将沿最短路径转向以达到目标航向。turnToRotation— 将机器人旋转到指定的方向。calibrateDrivetrain— 校准传动系统的配置惯性传感器、陀螺仪传感器或GPS传感器。
设置 — 调整航向和旋转值。
setHeading— 将机器人的当前航向更改为新的航向。setRotation— 将机器人的当前旋转更改为新的旋转。
数值——检查方向和旋转角度。
smartdrive 还可以使用 drivetrain 中继承的成员函数,例如 drive, driveFor, turn, turnFor, stop, isDone和 isMoving。
在调用任何 smartdrive 成员函数之前,必须先创建一个 smartdrive 实例,如下所示:
/* 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 instances
motor leftMotor = motor(PORT1, false);
motor rightMotor = motor(PORT9, true);
// Create the inertial instance
inertial myInertial = inertial(PORT3);
// Create the smartdrive instance
smartdrive myDrivetrain = smartdrive(
leftMotor, // left motor object
rightMotor, // right motor object
myInertial, // inertial object
259.34, // wheelTravel
320, // trackWidth
40, // wheelBase
mm, // unit
1.0 ); // externalGearRatio
turnToHeading#
A heading is the direction the robot is facing, measured in degrees. turnToHeading turns the robot to face a specific heading from -359 to 359 degrees. The robot will turn the shortest direction to reach the target heading.
机器人的初始航向角为0度。
程序会等到机器人完成转向后才运行下一行代码。
Available Functions1 — 使用当前配置的转弯速度 将传动系统转向指定的航向。
bool turnToHeading( double angle, rotationUnits units, bool waitForCompletion = true );
Parameters2 — 将传动系统转向指定航向,并达到指定速度。
bool turnToHeading( double angle, rotationUnits units, double velocity, velocityUnits units_v, bool waitForCompletion = true );
范围 |
类型 |
描述 |
|---|---|---|
|
|
机器人应面向的方向,以度为单位。当使用 |
|
|
旋转单位: |
|
|
当使用 |
|
|
速度单位: |
|
|
|
返回机器人是否达到目标航向,以布尔值表示。
true— 机器人已到达目标航向。false— 机器人未完成回合,或者函数在回合完成之前返回,因为waitForCompletion设置为false。
当
turnToHeading正在进行时,执行另一个传动系统运动功能(例如turn或drive)将中断当前的运动。因为
turnToHeading是基于目标的(使用angle参数),所以诸如isDone和isMoving之类的函数可以与turnToHeading一起使用。
// Face the new 0 degrees
myDrivetrain.setHeading(90, degrees);
myDrivetrain.turnToHeading(0, degrees);
turnToRotation#
turnToRotation 将机器人旋转到指定角度。
旋转角度是指机器人转动的程度,以度或转数衡量。在项目开始时,旋转角度值设置为 0 度。也可以使用 setRotation 函数设置旋转角度。
旋转角度是绝对值。这意味着转弯方向取决于机器人当前的旋转角度。向右转弯会增加旋转角度,向左转弯会减少旋转角度。
例如,如果机器人从0度开始,你将其旋转720度,它会向右转两次。如果你再将其旋转360度,它会向左转一次,因为360小于720。
程序会等到机器人完成转向后才运行下一行代码。
Available Functions1 — 使用当前配置的旋转速度 将传动系统旋转到指定的旋转方向。
bool turnToRotation( double angle, rotationUnits units, bool waitForCompletion = true );
Parameters2 — 使传动系统以指定的速度旋转到指定的转速。
bool turnToRotation( double angle, rotationUnits units, double velocity, velocityUnits units_v, bool waitForCompletion = true );
范围 |
类型 |
描述 |
|---|---|---|
|
|
机器人将转向的旋转值。 |
|
|
旋转单位: |
|
|
当使用 |
|
|
速度单位: |
|
|
|
返回机器人是否达到目标旋转角度,以布尔值表示。
true— 机器人达到目标旋转角度。false— 机器人未完成回合,或者函数在回合完成之前返回,因为waitForCompletion设置为false。
当
turnToRotation正在进行时,执行另一个传动系统运动功能(例如turn或drive)将中断当前的运动。因为
turnToRotation是基于目标的(使用angle参数),所以诸如isDone和isMoving之类的函数可以与turnToRotation一起使用。
// Spin counterclockwise for 2 turns, then face forward
myDrivetrain.setRotation(720, degrees);
myDrivetrain.turnToRotation(0, degrees);
calibrateDrivetrain#
calibrateDrivetrain 校准与智能驱动系统配置的惯性传感器、陀螺仪传感器或 GPS 传感器。
校准有助于传感器正确测量运动。校准期间,请保持机器人静止约 2 秒钟。如果机器人在校准期间移动,传感器可能无法正确测量运动。
程序将等待校准完成后再运行下一行代码。
当传动系统配置了惯性传感器、GPS传感器或陀螺仪传感器时,VEXcode会自动生成calibrateDrivetrain 。它作为项目源代码的一部分提供,并非VEX V5 C++ API的一部分。
void calibrateDrivetrain();
此函数不接受任何参数。
Return Values此函数不返回值。
Notes机器人会在每个项目开始时自动进行校准。
校准过程中,请保持机器人静止约 2 秒钟。
以下是 VEXcode 自动生成的 calibrateDrivetrain 的实现:
bool vexcode_initial_drivetrain_calibration_completed = false;
void calibrateDrivetrain() {
wait(200, msec);
Brain.Screen.print("Calibrating");
Brain.Screen.newLine();
Brain.Screen.print("Inertial");
DrivetrainInertial.calibrate();
while (DrivetrainInertial.isCalibrating()) {
wait(25, msec);
}
vexcode_initial_drivetrain_calibration_completed = true;
// Clears the screen and returns the cursor to row 1, column 1.
Brain.Screen.clearScreen();
Brain.Screen.setCursor(1, 1);
}
setHeading#
航向角是指机器人面向的方向,以度为单位。0 setHeading 将机器人的当前航向角更改为新的航向角值。
例如,如果机器人转向右侧,将航向设置为 0 度,则该右侧位置将成为新的 0 度航向。然后,机器人可以根据新的航向转向其他位置。
Available Functionsvoid setHeading(
double value,
rotationUnits units);
范围 |
类型 |
描述 |
|---|---|---|
|
|
要设置的机器人航向值。当使用 |
|
|
航向单位: |
此函数不返回值。
Examples// Face the new 0 degrees
myDrivetrain.setHeading(90, degrees);
myDrivetrain.turnToHeading(0, degrees);
setRotation#
旋转角度是指机器人转动的程度,以度或转数为单位。项目开始时,旋转角度值设置为 0 度。`setRotation 会将机器人的当前旋转角度更改为新值。
例如,如果机器人向右转了两圈,它的旋转角度将为 720 度。将旋转角度设置为 0 度会将旋转角度从 720 度重置为 0 度。然后,机器人可以根据这个新的旋转角度进行旋转。
Available Functionsvoid setRotation(
double value,
rotationUnits units);
范围 |
类型 |
描述 |
|---|---|---|
|
|
要设置的机器人旋转值。 |
|
|
航向单位: |
此函数不返回值。
Examples// Spin counterclockwise for 2 turns, then face forward
myDrivetrain.setRotation(720, degrees);
myDrivetrain.turnToRotation(0, degrees);
heading#
A heading is the direction the robot is facing, measured in degrees. heading returns the robot’s current heading from 0 to 359 degrees.
机器人的初始航向角为0度。
Available Functionsdouble heading( rotationUnits units = rotationUnits::deg );
范围 |
类型 |
描述 |
|---|---|---|
|
|
可选。返回航向的单位: |
返回机器人当前航向,数值为 double 单位为选定的单位。
// Show heading before and after turning
Brain.Screen.print("Before: %f", myDrivetrain.heading(degrees));
myDrivetrain.turnFor(right, 90, degrees);
wait(1, seconds);
Brain.Screen.newLine();
Brain.Screen.print("After: %f", myDrivetrain.heading(degrees));
myDrivetrain.stop();
rotation#
旋转角度是指机器人转动的程度,以度或转数为单位。项目开始时,旋转角度值设置为 rotation 返回机器人当前的旋转角度。
向右转会增加旋转角度,向左转会减少旋转角度。例如,向右转两圈将得到 720 度的旋转角度。
Available Functionsdouble rotation( rotationUnits units = rotationUnits::deg );
范围 |
类型 |
描述 |
|---|---|---|
|
|
可选。返回旋转单位: |
返回机器人当前旋转方向,数值为 double 单位为选定的双精度浮点数。
// Show rotation before and after turning
Brain.Screen.print("Before: %f", myDrivetrain.rotation(degrees));
myDrivetrain.turnFor(right, 90, degrees);
wait(1, seconds);
Brain.Screen.newLine();
Brain.Screen.print("After: %f", myDrivetrain.rotation(degrees));
myDrivetrain.stop();