智能驱动#

介绍#

The smartdrive class controls how a robot drives and turns using motors, wheels, and a Gyro Sensor. A smartdrive uses a connected Gyro Sensor to track heading and rotation.

smartdrive 使用陀螺仪传感器来帮助机器人精确移动和转向。在每个项目开始时,驱动系统会自动校准陀螺仪传感器。校准期间,请保持机器人静止约 2 秒钟,以便机器人能够正确移动和转向。

smartdrive 类基于 drivetrain 类。5 smartdrive 可以使用与 drivetrain 相同的驱动和转向功能,并且还添加了基于传感器的功能,如 turnToHeading 和 turnToRotation。

本页面使用 myDrivetrain 作为示例智能驱动名称。请根据需要将其替换为您自己配置的名称。

类构造函数#

1 — 使用现有的 motor_group 对象(左右两侧)以及陀螺仪传感器创建一个智能驱动器。

smartdrive(
    motor_group     &l,
    motor_group     &r,
    gyro            &g,
    double          wheelTravel = 320,
    double          trackWidth  = 320,
    double          wheelBase   = 130,
    distanceUnits   unit = mm,
    double          externalGearRatio = 1.0 );

2 — 使用现有的 motor 对象(左右两侧)以及陀螺仪传感器创建一个智能驱动器。

smartdrive(
    motor           &l,
    motor           &r,
    gyro            &g,
    double          wheelTravel = 320,
    double          trackWidth  = 320,
    double          wheelBase   = 130,
    distanceUnits   unit = mm,
    double          externalGearRatio = 1.0 );

类析构函数#

销毁 smartdrive 对象并释放相关资源。

~smartdrive();

参数#

范围

类型

描述

&l / &r

motor_group / motor

左侧和右侧电机或电机组。两个参数必须使用相同的类型:要么是两个 motor_group 对象,要么是两个 motor 对象。

&g

gyro

智能驱动使用的陀螺仪传感器。

wheelTravel

double

传动车轮的周长。默认值为 320。

trackWidth

double

左右车轮之间的距离。默认值为 320。

wheelBase

double

前轮和后轮之间的距离。默认值为 130。

unit

distanceUnits

The unit for wheelTravel, trackWidth, and wheelBase: mm (default) or inches.

externalGearRatio

double

如果使用齿轮,则用于调整驱动距离的齿轮比。默认值为 1.0。

例子#

/* 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 Gyro Sensor object.
gyro myGyro = gyro(PORT3);

// Create the smartdrive object.
smartdrive myDrivetrain = smartdrive(
    leftMotor,  // left motor
    rightMotor, // right motor
    myGyro,     // Gyro Sensor
    259.34,     // wheelTravel
    320,        // trackWidth
    40,         // wheelBase
    mm,         // unit
    1.0 );      // externalGearRatio

成员功能#

smartdrive 类包含 成员函数:

动作——使用航向或旋转控制机器人转向。

  • turnToHeading — 将机器人转向 -359 度到 359 度之间的特定航向。机器人将沿最短路径转向以达到目标航向。

  • turnToRotation — 将机器人旋转到指定的方向。

  • calibrateDrivetrain — 校准与智能驱动配置的陀螺仪传感器。

设置 — 调整航向和旋转值。

  • setHeading — 将机器人的当前航向更改为新的航向。

  • setRotation — 将机器人的当前旋转更改为新的旋转。

数值——检查方向和旋转角度。

  • heading — 返回机器人的当前航向,范围从 0 到 359 度。

  • rotation — 返回机器人的当前旋转角度。

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 objects.
motor leftMotor = motor(PORT1, false);
motor rightMotor = motor(PORT9, true);

// Create the Gyro Sensor object.
gyro myGyro = gyro(PORT3);

// Create the smartdrive object.
smartdrive myDrivetrain = smartdrive(
    leftMotor,  // left motor
    rightMotor, // right motor
    myGyro,     // Gyro Sensor
    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 Functions

1 — 使用当前转弯速度将机器人转向指定的航向。

bool turnToHeading(
    double        angle,
    rotationUnits units,
    bool          waitForCompletion = true );

2 — 使机器人以指定的速度转向指定的航向。

bool turnToHeading(
    double        angle,
    rotationUnits units,
    double        velocity,
    velocityUnits units_v,
    bool          waitForCompletion = true );

Parameters

范围

类型

描述

angle

double

机器人应面向的方向,以度为单位。当使用 deg 或 degrees ,该值可以从 -359 到 359。

units

rotationUnits

航向单位: deg (度), rev (转),或 raw (原始单位)。一转等于 360 度。

velocity

double

当使用 velocityUnits::pct,转弯速度为 0% 到 100%;当使用 rpm时,转弯速度为 0 到 120 rpm;当使用 dps时,转弯速度为 0 到 720 度/秒。如果未提供速度,则机器人以当前转弯速度转弯。

units_v

velocityUnits

速度单位: velocityUnits::pct (百分比), rpm (每分钟转数),或 dps (每秒度数)。

waitForCompletion

bool

true (默认值)使项目等待机器人完成转向后才运行下一行代码。2 false 使下一行代码立即运行。

Return Values

返回机器人是否达到目标航向,以布尔值表示。

  • true — 机器人已到达目标航向。

  • false — 机器人未完成回合,或者函数在回合完成之前返回,因为 waitForCompletion 设置为 false。

Notes
  • 当 turnToHeading 运行时,运行另一个传动系统运动功能将中断当前运动。

  • turnToHeading 使用目标航向,因此可以与 isDone 和 isMoving 一起使用。

Examples
// Face the new 0 degrees.
myDrivetrain.setHeading(90, degrees);
myDrivetrain.turnToHeading(0, degrees);

turnToRotation#

turnToRotation 将机器人旋转到指定角度。

旋转角度是指机器人转动的程度,以度或转数衡量。在项目开始时,旋转角度值设置为 0 度。也可以使用 setRotation 函数设置旋转角度。

旋转角度是绝对值。这意味着转弯方向取决于机器人当前的旋转角度。向右转弯会增加旋转角度,向左转弯会减少旋转角度。

例如,如果机器人从0度开始,你将其旋转720度,它会向右转两次。如果你再将其旋转360度,它会向左转一次,因为360小于720。

程序会等到机器人完成转向后才运行下一行代码。

Available Functions

1 — 使用当前转弯速度将机器人转到指定的旋转方向。

bool turnToRotation(
    double        angle,
    rotationUnits units,
    bool          waitForCompletion = true );

2 — 使机器人以指定的速度旋转到指定的方向。

bool turnToRotation(
    double        angle,
    rotationUnits units,
    double        velocity,
    velocityUnits units_v,
    bool          waitForCompletion = true );

Parameters

范围

类型

描述

angle

double

机器人将转向的旋转值。

units

rotationUnits

旋转单位: deg (度), rev (转),或 raw (原始单位)。一转等于 360 度。

velocity

double

当使用 velocityUnits::pct,转弯速度为 0% 到 100%;当使用 rpm时,转弯速度为 0 到 120 rpm;当使用 dps时,转弯速度为 0 到 720 度/秒。如果未提供速度,则机器人以当前转弯速度转弯。

units_v

velocityUnits

速度单位: velocityUnits::pct (百分比), rpm (每分钟转数),或 dps (每秒度数)。

waitForCompletion

bool

true (默认值)使项目等待机器人完成转向后才运行下一行代码。2 false 使下一行代码立即运行。

Return Values

返回机器人是否达到目标旋转角度,以布尔值表示。

  • true — 机器人达到目标旋转角度。

  • false — 机器人未完成回合,或者函数在回合完成之前返回,因为 waitForCompletion 设置为 false。

Notes
  • 当 turnToRotation 运行时,运行另一个传动系统运动函数会中断当前的运动。

  • turnToRotation 使用目标旋转,因此 isDone 和 isMoving 可以与它​​一起使用。

Examples
// Spin counterclockwise for 2 turns, then face forward.
myDrivetrain.setRotation(720, degrees);
myDrivetrain.turnToRotation(0, degrees);

calibrateDrivetrain#

calibrateDrivetrain 校准与智能驱动系统配置的陀螺仪传感器。

校准有助于陀螺仪传感器正确测量运动。校准期间,请保持机器人静止约 2 秒钟。如果机器人在校准期间移动,陀螺仪传感器可能无法正确测量运动。

程序将等待校准完成后再运行下一行代码。

当传动系统配置了陀螺仪传感器时,VEXcode 会自动生成calibrateDrivetrain 它是项目源代码的一部分,并非 VEX IQ C++ API 的一部分。

Available Functions
void calibrateDrivetrain();

Parameters

此函数不接受任何参数。

Return Values

此函数不返回值。

Notes
  • 当在 VEXcode 中配置智能驱动器时,机器人会在每个项目开始时自动校准。

  • 校准过程中,请保持机器人静止约 2 秒钟。

Function Implementation

以下是 VEXcode 自动生成的 calibrateDrivetrain 实现示例:

bool vexcode_initial_drivetrain_calibration_completed = false;
void calibrateDrivetrain() {
  wait(200, msec);
  Brain.Screen.print("Calibrating");
  Brain.Screen.newLine();
  Brain.Screen.print("Gyro");
  DrivetrainGyro.calibrate();
  while (DrivetrainGyro.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 Functions
void setHeading(
  double value,
  rotationUnits units);

Parameters

范围

类型

描述

value

double

要设置的机器人航向值。当使用 deg 或 degrees ,该值可以介于 -359 到 359 之间。

units

rotationUnits

航向单位: deg (度), rev (转),或 raw (原始单位)。一转等于 360 度。

Return Values

此函数不返回值。

Examples
// Face the new 0 degrees.
myDrivetrain.setHeading(90, degrees);
myDrivetrain.turnToHeading(0, degrees);

setRotation#

旋转角度是指机器人转动的程度,以度或转数为单位。项目开始时,旋转角度值设置为 0 度。`setRotation 会将机器人的当前旋转角度更改为新值。

例如,如果机器人向右转了两圈,它的旋转角度将为 720 度。将旋转角度设置为 0 度会将旋转角度从 720 度重置为 0 度。然后,机器人可以根据这个新的旋转角度进行旋转。

Available Functions
void setRotation(
  double value,
  rotationUnits units);

Parameters

范围

类型

描述

value

double

要设置的机器人旋转值。

units

rotationUnits

旋转单位: deg (度), rev (转),或 raw (原始单位)。一转等于 360 度。

Return Values

此函数不返回值。

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 Functions
double heading( rotationUnits units = rotationUnits::deg );

Parameters

范围

类型

描述

units

rotationUnits

可选。返回航向的单位: degrees deg (默认), rev,或 raw一转等于 360 度。

Return Values

返回机器人当前航向,数值为 double 单位为选定的单位。

Examples
// 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 Functions
double rotation( rotationUnits units = rotationUnits::deg );

Parameters

范围

类型

描述

units

rotationUnits

可选。旋转单位: degrees deg (默认), rev,或 raw一转等于 360 度。

Return Values

返回机器人当前旋转方向,数值为 double 单位为选定的双精度浮点数。

Examples
// 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();