智能驱动#
介绍#
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_groupobjects for the left (&l) and right (&r) sides, along with aguidosensor.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
motorobjects for the left (&l) and right (&r) sides, along with aguidosensor.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();
参数#
范围 |
类型 |
描述 |
|---|---|---|
|
|
References to existing |
|
|
A reference to an existing |
|
|
The wheel’s circumference. Default is |
|
|
The distance between the left and right wheels. Default is |
|
|
The width of the wheel base. Default is |
|
|
Units for the
|
|
|
The gear ration compensation factor. Default is |
例子#
/* 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:
turnToHeading— Rotates the robot to face a specific absolute heading.turnToRotation— Rotates the robot to reach a specific cumulative rotation.calibrateDrivetrain— Calibrates the drivetrain’s configured Inertial Sensor, Gyro Sensor, or GPS Sensor.setHeading— Manually sets the robot’s heading value.setRotation— Manually sets the robot’s cumulative rotation value.heading— Returns the drivetrain’s heading angle.rotation— Returns how much the drivetrain has turned since the project started.
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 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 );
范围 |
类型 |
描述 |
|---|---|---|
|
|
传动系统将转向的方向。 |
|
|
The units representing the angle:
|
|
|
传动系统转速,以双精度浮点数表示。如果未指定转速或转速之前设置则默认转速为 50%。 |
|
|
The unit to represent the velocity:
|
|
|
Optional.
|
Returns a Boolean indicating whether the drivetrain reached the target heading parameter value:
true— The drivetrain successfully reaches the targetheadingparameter value.false— The drivetrain does not complete the movement or if thewaitForCompletionparameter is set tofalse.
Executing another drivetrain movement function (such as
turnordrive) whileturnToHeadingis in progress will interrupt the current movement.Because
turnToHeadingis target-based (uses anangleparameter), functions such asisDoneandisMovingwork withturnToHeading.
// Face the new 0 degrees
myDrivetrain.setHeading(90, degrees);
myDrivetrain.turnToHeading(0, degrees);
转向旋转#
将传动系统旋转到指定的旋转值。
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 );
范围 |
类型 |
描述 |
|---|---|---|
|
|
传动系统将达到的旋转值。 |
|
|
The units representing the angle:
|
|
|
传动系统转速,以双精度浮点数表示。如果未指定转速或转速之前设置则默认转速为 50%。 |
|
|
The unit to represent the velocity:
|
|
|
Optional.
|
Returns a Boolean indicating whether the drivetrain reached the target rotation parameter value:
true— The drivetrain successfully reaches the targetrotationparameter value.false— The drivetrain does not complete the movement or if thewaitForCompletionparameter is set tofalse.
Executing another drivetrain movement function (such as
turnordrive) whileturnToRotationis in progress will interrupt the current movement.Because
turnToRotationis target-based (uses anangleparameter), functions such asisDoneandisMovingwork withturnToRotation.
// 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.
calibrateDrivetrain();
此函数不接受任何参数。
Return Values此函数不返回值。
Notes机器人会在每个项目开始时自动进行校准。
校准过程中,确保机器人位于平坦的表面上并保持静止。
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 Functionsvoid setHeading(
double value,
rotationUnits units);
范围 |
类型 |
描述 |
|---|---|---|
|
|
传动系统当前所处的航向。 |
|
|
The units representing the heading:
|
此函数不返回值。
Examples// Face the new 0 degrees
myDrivetrain.setHeading(90, degrees);
DrivemyDrivetraintrain.turnToHeading(0, degrees);
设置旋转#
设置传动系统的旋转方向。
Available Functionsvoid setRotation(
double value,
rotationUnits units);
范围 |
类型 |
描述 |
|---|---|---|
|
|
传动系统当前所处的旋转方向。 |
|
|
The units representing the heading:
|
此函数不返回值。
Examples// Spin counterclockwise for 2 turns, then face forward
myDrivetrain.setRotation(720, degrees);
myDrivetrain.turnToRotation(0, degrees);
标题#
返回传动系统的方向。
Available Functionsdouble heading( rotationUnits units = rotationUnits::deg );
范围 |
类型 |
描述 |
|---|---|---|
|
|
Optional. The units representing the heading:
|
Returns a double indicating the heading of the drivetrain in the units specified by the units parameter.
// 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 Functionsdouble rotation( rotationUnits units = rotationUnits::deg );
范围 |
类型 |
描述 |
|---|---|---|
|
|
Optional. The units representing the heading:
|
Returns a double indicating the rotation of the drivetrain in the units specified by the units parameter.
// 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();