智能驱动#

介绍#

The smartdrive class is derived from the drivetrain base class and incorporates an Inertial Sensor, Gyro Sensor, or GPS Sensor for heading and rotation-based functions.

类构造函数#

1 Creates a smartdrive using existing motor_group objects for the left (&l) and right (&r) sides, along with a guido sensor.

smartdrive(
    motor_group     &l,
    motor_group     &r,
    guido           &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 (&l) and right (&r) sides, along with a guido sensor.

smartdrive(
    motor           &l,
    motor           &r,
    guido           &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();

参数#

范围

类型

描述

&l / &r

motor_group / motor

References to existing motor or motor_group instances. Both parameters must be of the same type: either two motor_group instances or two motor instances. The &l parameter refers to the left motor or motor group, and &r refers to the right motor or motor group.

&g

guido

A reference to an existing gps, gyro, or inertial instance.

wheelTravel

double

The wheel’s circumference. Default is 320, expressed in millimeters by default or in the units specified by the unit parameter.

trackWidth

double

The distance between the left and right wheels. Default is 320, expressed in millimeters by default or in the units specified by the unit parameter.

wheelBase

double

The width of the wheel base. Default is 130, expressed in millimeters by default or in the units specified by the unit parameter.

unit

distanceUnits

Units for the wheelTravel, trackWidth, and wheelBase parameters:

  • mm (default) — millimeters
  • inches

externalGearRatio

double

The gear ration compensation factor. 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 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

成员功能#

The smartdrive class includes the following member functions:

Before calling any smartdrive member functions, a smartdrive instance must be created, as shown below:

/* 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

转向航向#

将传动系统旋转到指定方向。

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

传动系统将转向的方向。

units

rotationUnits

The units representing the angle:

  • deg — degrees
  • rev — revolutions
  • raw — raw units

velocity

double

传动系统转速,以双精度浮点数表示。如果未指定转速或转速之前设置则默认转速为 50%。

units_v

velocityUnits

The unit to represent the velocity:

  • velocityUnits::pct — percent
  • rpm — rotations per minute
  • dps — degrees per second

waitForCompletion

bool

Optional.

  • true (default) — The project waits until the function finishes before executing the next line of code.
  • false — The project starts the action and moves on to the next line of code right away, without waiting for the function to finish.

Return Values

Returns a Boolean indicating whether the drivetrain reached the target heading parameter value:

  • true — The drivetrain successfully reaches the target heading parameter value.
  • false — The drivetrain does not complete the movement or if the waitForCompletion parameter is set to false.

Notes
  • Executing another drivetrain movement function (such as turn or drive) while turnToHeading is in progress will interrupt the current movement.

  • Because turnToHeading is target-based (uses an angle parameter), functions such as isDone and isMoving work with turnToHeading.

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

转向旋转#

将传动系统旋转到指定的旋转值。

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 units representing the angle:

  • deg — degrees
  • rev — revolutions
  • raw — raw units

velocity

double

传动系统转速,以双精度浮点数表示。如果未指定转速或转速之前设置则默认转速为 50%。

units_v

velocityUnits

The unit to represent the velocity:

  • velocityUnits::pct — percent
  • rpm — rotations per minute
  • dps — degrees per second

waitForCompletion

bool

Optional.

  • true (default) — The project waits until the function finishes before executing the next line of code.
  • false — The project starts the action and moves on to the next line of code right away, without waiting for the function to finish.

Return Values

Returns a Boolean indicating whether the drivetrain reached the target rotation parameter value:

  • true — The drivetrain successfully reaches the target rotation parameter value.
  • false — The drivetrain does not complete the movement or if the waitForCompletion parameter is set to false.

Notes
  • Executing another drivetrain movement function (such as turn or drive) while turnToRotation is in progress will interrupt the current movement.

  • Because turnToRotation is target-based (uses an angle parameter), functions such as isDone and isMoving work with turnToRotation.

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

校准传动系统#

校准传动系统。

calibrateDrivetrain is a helper function generated automatically by VEXcode when a drivetrain is configured with an Inertial Sensor, GPS Sensor, or Gyro Sensor. It is provided as part of the project source and is not part of the VEX V5 C++ API.

Available Functions
calibrateDrivetrain();

Parameters

此函数不接受任何参数。

Return Values

此函数不返回值。

Notes
  • 机器人会在每个项目开始时自动进行校准。

  • 校准过程中,确保机器人位于平坦的表面上并保持静止。

Function Implementation

The following is 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);
}

设置标题#

设定传动系统的运行方向。

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

Parameters

范围

类型

描述

value

double

传动系统当前所处的航向。

units

rotationUnits

The units representing the heading:

  • deg — degrees
  • rev — revolutions
  • raw — raw units

Return Values

此函数不返回值。

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

设置旋转#

设置传动系统的旋转方向。

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

Parameters

范围

类型

描述

value

double

传动系统当前所处的旋转方向。

units

rotationUnits

The units representing the heading:

  • deg — degrees
  • rev — revolutions
  • raw — raw units

Return Values

此函数不返回值。

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

标题#

返回传动系统的方向。

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

Parameters

范围

类型

描述

units

rotationUnits

Optional. The units representing the heading:

  • degrees / deg (default) — degrees
  • rev — revolutions
  • raw — raw units

Return Values

Returns a double indicating the heading of the drivetrain in the units specified by the units parameter.

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

旋转#

返回传动系统的旋转方向。

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

Parameters

范围

类型

描述

units

rotationUnits

Optional. The units representing the heading:

  • degrees / deg (default) — degrees
  • rev — revolutions
  • raw — raw units

Return Values

Returns a double indicating the rotation of the drivetrain in the units specified by the units parameter.

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