έξυπνος δίσκος#

Εισαγωγή#

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.

A smartdrive uses the Gyro Sensor to help the robot move and turn precisely. At the start of each project, the drivetrain calibrates the Gyro 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. A smartdrive 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.

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.

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

Παράμετροι#

Παράμετρος

Τύπος

Περιγραφή

&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

Ο Γυροσκοπικός Αισθητήρας που χρησιμοποιείται από το smartdrive.

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

Συναρτήσεις Μελών#

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 Gyro 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 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

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#

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#

calibrateDrivetrain calibrates the Gyro Sensor configured with the smartdrive.

Η βαθμονόμηση βοηθά τον γυροσκοπικό αισθητήρα να μετρήσει σωστά την κίνηση. Κρατήστε το ρομπότ ακίνητο για περίπου 2 δευτερόλεπτα κατά τη βαθμονόμηση. Εάν το ρομπότ κινηθεί κατά τη βαθμονόμηση, ο γυροσκοπικός αισθητήρας ενδέχεται να μην μετρήσει σωστά την κίνηση.

Το έργο θα περιμένει μέχρι να ολοκληρωθεί η βαθμονόμηση πριν εκτελεστεί η επόμενη γραμμή κώδικα.

calibrateDrivetrain is generated automatically by VEXcode when a drivetrain is configured with a Gyro 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
  • Το ρομπότ βαθμονομείται αυτόματα στην αρχή κάθε έργου, όταν έχει διαμορφωθεί ένας smartdrive στο 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("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#

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

setRotation#

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

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

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#

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