SmartDrive#
Introducción#
The smartdrive class controls how a robot drives and turns using motors, wheels, and a sensor. A smartdrive can use an Inertial Sensor or Gyro Sensor to track heading and rotation.
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.
Constructores de clases#
1 — Creates a smartdrive using existing
motor_groupobjects for the left (&l) and right (&r) sides, along with an Inertial Sensor or Gyro Sensor.smartdrive( motor_group &l, motor_group &r, inertial &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 an Inertial Sensor or Gyro Sensor.smartdrive( motor &l, motor &r, inertial &g, double wheelTravel = 320, double trackWidth = 320, double wheelBase = 130, distanceUnits unit = mm, double externalGearRatio = 1.0 );
Instructor de clase#
Destroys the smartdrive object and releases associated resources.
~smartdrive();
Parámetros#
The sensor parameter can be an inertial or gyro object.
Parámetro |
Tipo |
Descripción |
|---|---|---|
|
|
References to existing |
|
|
A reference to an existing |
|
|
The circumference of the drivetrain wheels. The default is |
|
|
The distance between the left and right wheels. The default is |
|
|
The distance between the front and back wheels. The default is |
|
|
The unit for |
|
|
The gear ratio used to adjust drive distances if gears are used. The default is |
Ejemplo#
/* 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
Funciones de los miembros#
The smartdrive class includes the following member functions:
Acciones: Gire el robot utilizando el control de dirección o la rotación.
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 drivetrain’s configured Inertial Sensor or Gyro Sensor.
Ajustes: Ajustar los valores de rumbo y rotación.
setHeading— Changes the robot’s current heading to a new heading.setRotation— Changes the robot’s current rotation to a new rotation.
Valores: Compruebe el rumbo y la rotación.
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 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
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.
La dirección inicial del robot es de 0 grados.
El proyecto esperará a que el robot termine de girar antes de ejecutar la siguiente línea de código.
Available Functions1 — Gira el tren motriz hacia la dirección especificada utilizando la velocidad de giro configurada.
bool turnToHeading( double angle, rotationUnits units, bool waitForCompletion = true );
Parameters2 — Gira el tren de transmisión hacia la dirección especificada a la velocidad especificada.
bool turnToHeading( double angle, rotationUnits units, double velocity, velocityUnits units_v, bool waitForCompletion = true );
Parámetro |
Tipo |
Descripción |
|---|---|---|
|
|
The direction the robot should face, in degrees. This can be from -359 to 359 when using |
|
|
The rotation unit: |
|
|
The velocity to turn with from 0% to 100% when using |
|
|
The velocity unit: |
|
|
|
Devuelve, como valor booleano, si el robot alcanzó la dirección objetivo.
true— The robot reached the target heading.false— The robot did not finish the turn, or the function returned before the turn completed becausewaitForCompletionwas 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);
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.
Los valores de rotación son absolutos. Esto significa que la dirección del giro depende de la rotación actual del robot. Girar a la derecha aumenta la rotación, y girar a la izquierda la disminuye.
Por ejemplo, si el robot comienza en 0 grados y se le da una rotación de 720 grados, girará dos veces a la derecha. Si luego se le da una rotación de 360 grados, girará una vez a la izquierda, ya que 360 es menor que 720.
El proyecto esperará a que el robot termine de girar antes de ejecutar la siguiente línea de código.
Available Functions1 — Gira el tren de transmisión a la rotación especificada utilizando la velocidad de giro configurada.
bool turnToRotation( double angle, rotationUnits units, bool waitForCompletion = true );
Parameters2 — Gira el tren de transmisión a la rotación especificada a la velocidad especificada.
bool turnToRotation( double angle, rotationUnits units, double velocity, velocityUnits units_v, bool waitForCompletion = true );
Parámetro |
Tipo |
Descripción |
|---|---|---|
|
|
El valor de rotación al que girará el robot. |
|
|
The rotation unit: |
|
|
The velocity to turn with from 0% to 100% when using |
|
|
The velocity unit: |
|
|
|
Devuelve, como valor booleano, si el robot alcanzó la rotación objetivo.
true— The robot reached the target rotation.false— The robot did not finish the turn, or the function returned before the turn completed becausewaitForCompletionwas 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#
calibrateDrivetrain calibrates the Inertial Sensor or Gyro Sensor configured with the smartdrive.
La calibración ayuda al sensor a medir el movimiento correctamente. Mantenga el robot quieto durante aproximadamente 2 segundos durante la calibración. Si el robot se mueve durante la calibración, es posible que el sensor no mida el movimiento correctamente.
El proyecto esperará a que finalice la calibración antes de ejecutar la siguiente línea de código.
calibrateDrivetrain is generated automatically by VEXcode when a drivetrain is configured with an Inertial Sensor or Gyro Sensor. It is provided as part of the project source and is not part of the VEX EXP C++ API.
void calibrateDrivetrain();
Esta función no acepta ningún parámetro.
Return ValuesEsta función no devuelve ningún valor.
NotesEl robot se calibra automáticamente al inicio de cada proyecto.
Mantenga el robot inmóvil durante aproximadamente 2 segundos durante la calibración.
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);
}
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.
Por ejemplo, si el robot ha girado para mirar hacia la derecha, al establecer la orientación a 0 grados, esa posición mirando hacia la derecha se convierte en la nueva posición de 0 grados. Entonces, el robot puede girar a otras posiciones en función de esa nueva orientación.
Available Functionsvoid setHeading(
double value,
rotationUnits units);
Parámetro |
Tipo |
Descripción |
|---|---|---|
|
|
The heading value to set for the robot. This can be from -359 to 359 when using |
|
|
The heading unit: |
Esta función no devuelve ningún valor.
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.
Por ejemplo, si el robot ha dado dos vueltas completas a la derecha, su valor de rotación será de 720 grados. Si se establece la rotación en 0 grados, esta volverá a su valor original. A partir de ahí, el robot podrá girar según ese nuevo valor.
Available Functionsvoid setRotation(
double value,
rotationUnits units);
Parámetro |
Tipo |
Descripción |
|---|---|---|
|
|
El valor de rotación que se debe establecer para el robot. |
|
|
The rotation unit: |
Esta función no devuelve ningún valor.
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.
La dirección inicial del robot es de 0 grados.
Available Functionsdouble heading( rotationUnits units = rotationUnits::deg );
Parámetro |
Tipo |
Descripción |
|---|---|---|
|
|
Optional. The unit to return heading in: |
Returns the robot’s current heading as a double in the selected unit.
// 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.
Girar a la derecha aumenta la rotación, y girar a la izquierda la disminuye. Por ejemplo, dar dos vueltas completas a la derecha produce una rotación de 720 grados.
Available Functionsdouble rotation( rotationUnits units = rotationUnits::deg );
Parámetro |
Tipo |
Descripción |
|---|---|---|
|
|
Optional. The unit to return heading in: |
Returns the robot’s current rotation as a double in the selected unit.
// 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();