智能驱动#

介绍#

The smartdrive class controls how a robot drives and turns using motors, wheels, and a sensor. A smartdrive can use a connected Gyro Sensor or the Brain’s built-in Inertial Sensor.

A smartdrive uses the sensor to help the robot move and turn precisely. At the start of each project, the drivetrain calibrates the sensor automatically. Keep the robot still for about 2 seconds during calibration, so the robot can move and turn correctly.

The smartdrive class is based on the drivetrain class. It can use the same drive and turn functions as drivetrain, and it also adds sensor-based functions like turnToHeading and turnToRotation.

This page uses myDrivetrain as the example smartdrive name. Replace it with your own configured name as needed.

类构造函数#

1 Creates a smartdrive using existing motor_group objects for the left and right sides, along with a Gyro Sensor or Inertial Sensor.

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 Creates a smartdrive using existing motor objects for the left and right sides, along with a Gyro Sensor or Inertial Sensor.

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

类析构函数#

Destroys the smartdrive object and releases associated resources.

~smartdrive();

参数#

The sensor parameter can be a gyro or inertial object.

范围

类型

描述

&l / &r

motor_group / motor

The left and right motors or motor groups. Both parameters must use the same type: either two motor_group objects or two motor objects.

&g

gyro / inertial

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

wheelTravel

double

The circumference of the drivetrain wheels. The default is 320.

trackWidth

double

The distance between the left and right wheels. The default is 320.

wheelBase

double

The distance between the front and back wheels. The default is 130.

unit

distanceUnits

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

externalGearRatio

double

The gear ratio used to adjust drive distances if gears are used. The default is 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 Inertial Sensor object.
inertial myInertial = inertial(PORT3);

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

成员功能#

The smartdrive class includes the following member functions:

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

  • 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.

  • turnToRotation — Turns the robot to a specific rotation.

  • calibrateDrivetrain — Calibrates the sensor configured with the smartdrive.

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

  • setHeading — Changes the robot’s current heading to a new heading.

  • setRotation — Changes the robot’s current rotation to a new rotation.

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

  • heading — Returns the robot’s current heading from 0 to 359 degrees.

  • rotation — Returns the robot’s current rotation.

A smartdrive can also use the inherited member functions from drivetrain, such as drive, driveFor, turn, turnFor, stop, isDone, and isMoving.

Before calling any smartdrive member functions, a smartdrive 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 Inertial Sensor object.
inertial myInertial = inertial(PORT3);

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

转向航向#

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

The direction the robot should face, in degrees. This can be from -359 to 359 when using deg or degrees.

units

rotationUnits

The heading unit: deg (degrees), rev (revolutions), or raw (raw units). One revolution is equal to 360 degrees.

velocity

double

The velocity to turn with from 0% to 100% when using velocityUnits::pct, from 0 to 120 rpm when using rpm, or from 0 to 720 degrees per second when using dps. If no velocity is provided, the robot turns at the current turn velocity.

units_v

velocityUnits

The velocity unit: velocityUnits::pct (percent), rpm (rotations per minute), or dps (degrees per second).

waitForCompletion

bool

true (default) makes the project wait until the robot is done turning before the next line of code runs. false makes the next line of code run right away.

Return Values

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

  • true — The robot reached the target heading.

  • false — The robot did not finish the turn, or the function returned before the turn completed because waitForCompletion was set to false.

Notes
  • Running another drivetrain movement function while turnToHeading is running will interrupt the current movement.

  • turnToHeading uses a target heading, so isDone and isMoving can be used with it.

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

转向旋转#

turnToRotation turns the robot to a specific rotation.

Rotation is how much the robot has turned, measured in degrees or revolutions. At the beginning of a project, the rotation value is set to 0 degrees. Rotation can also be set using the setRotation function.

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

例如,如果机器人从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

The rotation unit: deg (degrees), rev (revolutions), or raw (raw units). One revolution is equal to 360 degrees.

velocity

double

The velocity to turn with from 0% to 100% when using velocityUnits::pct, from 0 to 120 rpm when using rpm, or from 0 to 720 degrees per second when using dps. If no velocity is provided, the robot turns at the current turn velocity.

units_v

velocityUnits

The velocity unit: velocityUnits::pct (percent), rpm (rotations per minute), or dps (degrees per second).

waitForCompletion

bool

true (default) makes the project wait until the robot is done turning before the next line of code runs. false makes the next line of code run right away.

Return Values

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

  • true — The robot reached the target rotation.

  • false — The robot did not finish the turn, or the function returned before the turn completed because waitForCompletion was set to false.

Notes
  • Running another drivetrain movement function while turnToRotation is running will interrupt the current movement.

  • turnToRotation uses a target rotation, so isDone and isMoving can be used with it.

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

校准传动系统#

calibrateDrivetrain calibrates the connected Gyro Sensor or the Brain’s built-in Inertial Sensor configured with the smartdrive.

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

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

Note: calibrateDrivetrain is generated automatically by VEXcode when a drivetrain is configured with a Gyro Sensor or Inertial Sensor. It is provided as part of the project source and is not part of the VEX IQ C++ API.

Available Functions
void calibrateDrivetrain();

Parameters

此函数不接受任何参数。

Return Values

此函数不返回值。

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

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

Function Implementation

The following is an example of the auto-generated implementation of calibrateDrivetrain created by VEXcode:

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);
}

设置标题#

A heading is the direction the robot is facing, measured in degrees. setHeading changes the robot’s current heading to a new heading value.

例如,如果机器人转向右侧,将航向设置为 0 度,则该右侧位置将成为新的 0 度航向。然后,机器人可以根据新的航向转向其他位置。

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

Parameters

范围

类型

描述

value

double

The heading value to set for the robot. This can be from -359 to 359 when using deg or degrees.

units

rotationUnits

The heading unit: deg (degrees), rev (revolutions), or raw (raw units). One revolution is equal to 360 degrees.

Return Values

此函数不返回值。

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

设置旋转#

Rotation is how much the robot has turned, measured in degrees or revolutions. At the beginning of a project, the rotation value is set to 0 degrees. setRotation changes the robot’s current rotation to a new value.

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

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

Parameters

范围

类型

描述

value

double

要设置的机器人旋转值。

units

rotationUnits

The rotation unit: deg (degrees), rev (revolutions), or raw (raw units). One revolution is equal to 360 degrees.

Return Values

此函数不返回值。

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

标题#

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

Optional. The unit to return heading in: degrees, deg (default), rev, or raw. One revolution is equal to 360 degrees.

Return Values

Returns the robot’s current heading as a double in the selected unit.

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 is how much the robot has turned, measured in degrees or revolutions. At the beginning of a project, the rotation value is set to 0 degrees. rotation returns the robot’s current rotation.

向右转会增加旋转角度,向左转会减少旋转角度。例如,向右转两圈将得到 720 度的旋转角度。

Available Functions
double rotation( rotationUnits units = rotationUnits::deg );

Parameters

范围

类型

描述

units

rotationUnits

Optional. The unit to return rotation in: degrees, deg (default), rev, or raw. One revolution is equal to 360 degrees.

Return Values

Returns the robot’s current rotation as a double in the selected unit.

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