スマートドライブ#
導入#
smartdrive クラスは、モーター、ホイール、センサーを使用してロボットの走行と旋回を制御します smartdrive 、接続された ジャイロセンサー またはブレインに内蔵された 慣性センサー を使用できます。
smartdrive センサーを使用して、ロボットの正確な移動と旋回を支援します。各プロジェクトの開始時に、ドライブトレインはセンサーを自動的にキャリブレーションします。キャリブレーション中はロボットを約2秒間静止させてください。そうすることで、ロボットが正しく移動および旋回できるようになります。
smartdrive クラスは drivetrain クラスに基づいています。 drivetrain と同じドライブおよびターン機能を使用でき、 turnToHeading や turnToRotationのようなセンサーベースの機能も追加されています。
このページでは、例として myDrivetrain というスマートドライブ名を使用しています。必要に応じて、ご自身で設定した名前に置き換えてください。
クラスコンストラクタ#
1 — 既存の
motor_groupオブジェクトを左右に使用し、ジャイロセンサーまたは慣性センサーとともにスマートドライブを作成します。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 — 既存の
motorオブジェクトを左右に使用し、ジャイロセンサーまたは慣性センサーとともにスマートドライブを作成します。smartdrive( motor &l, motor &r, gyro &g, double wheelTravel = 320, double trackWidth = 320, double wheelBase = 130, distanceUnits unit = mm, double externalGearRatio = 1.0 );
クラスデストラクタ#
smartdrive オブジェクトを破棄し、関連するリソースを解放します。
~smartdrive();
パラメータ#
センサーパラメータは、 gyro または inertial オブジェクトのいずれかです。
パラメータ |
タイプ |
説明 |
|---|---|---|
|
|
左右のモーターまたはモーターグループ。両方のパラメータは同じタイプを使用する必要があります。つまり、2つの |
|
|
|
|
|
駆動輪の円周。デフォルト値は |
|
|
左右の車輪間の距離。デフォルト値は |
|
|
前輪と後輪の間の距離。デフォルト値は |
|
|
The unit for |
|
|
ギアを使用する場合に、駆動距離を調整するために使用されるギア比。デフォルト値は |
例#
/* 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
メンバーの機能#
smartdrive クラスには、以下のメンバ関数が含まれています。
操作 — 方位角または回転を使用してロボットを旋回させる。
turnToHeading— ロボットを-359度から359度までの特定の方向に向けて旋回させます。ロボットは目標の方向に到達するために最短の方向に旋回します。turnToRotation— ロボットを特定の回転方向に回転させます。calibrateDrivetrain— スマートドライブで構成されたセンサーをキャリブレーションします。
設定 — 方位と回転の値を調整します。
setHeading— ロボットの現在の進行方向を新しい進行方向に変更します。setRotation— ロボットの現在の回転を新しい回転に変更します。
値 — 方位と回転を確認してください。
smartdrive 、drivetrain から継承したメンバ関数、例えば drive、 driveFor、 turn、 turnFor、 stop、 isDone、および isMovingを使用することもできます。
smartdrive メンバー関数を呼び出す前に、 smartdrive オブジェクトを作成する必要があります。
/* 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
turnToHeading#
方位とは、ロボットが向いている方向のことで、度数で表されます。0 turnToHeading は ロボットを-359度から359度までの特定の方位に向けます。ロボットは、目標の方位に到達するために最短の方向へ旋回します。
ロボットの初期進行方向は0度です。
プロジェクトは、ロボットの旋回が完了するまで待機し、その後次のコードが実行されます。
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 );
パラメータ |
タイプ |
説明 |
|---|---|---|
|
|
ロボットが向くべき方向(度数)。0 |
|
|
見出しの単位は、 |
|
|
回転速度は、 |
|
|
速度の単位: |
|
|
|
ロボットが目標の進行方向に到達したかどうかをブール値で返します。
true— ロボットは目標の進行方向に到達しました。false— ロボットが旋回を完了しなかったか、falseがwaitForCompletionに設定されたため、旋回が完了する前に関数が戻りました。
turnToHeadingが実行中に別の駆動系移動機能を実行すると、現在の移動が中断されます。turnToHeadingは目標の見出しを使用するため、isDoneおよびisMovingを一緒に使用できます。
// Face the new 0 degrees.
myDrivetrain.setHeading(90, degrees);
myDrivetrain.turnToHeading(0, degrees);
turnToRotation#
turnToRotation はロボットを特定の回転方向に回転させます。
回転とは、ロボットがどれだけ回転したかを示すもので、度または回転数で測定されます。プロジェクトの開始時には、回転値は0度に設定されます。回転は、setRotation 関数を使用して設定することもできます。
回転値は絶対値です。つまり、旋回方向はロボットの現在の回転角度によって決まります。右に旋回すると回転角度が大きくなり、左に旋回すると回転角度が小さくなります。
例えば、ロボットが0度の位置から始まり、720度回転させると、右に2回転します。次に360度回転させると、360度は720度より小さいため、左に1回転します。
プロジェクトは、ロボットの旋回が完了するまで待機し、その後次のコードが実行されます。
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 );
パラメータ |
タイプ |
説明 |
|---|---|---|
|
|
ロボットが回転する回転値。 |
|
|
回転単位: |
|
|
回転速度は、 |
|
|
速度の単位: |
|
|
|
ロボットが目標の回転位置に到達したかどうかをブール値で返します。
true— ロボットは目標の回転位置に到達しました。false— ロボットが旋回を完了しなかったか、falseがwaitForCompletionに設定されたため、旋回が完了する前に関数が戻りました。
turnToRotationが実行中に別の駆動系動作機能を実行すると、現在の動作が中断されます。
// Spin counterclockwise for 2 turns, then face forward.
myDrivetrain.setRotation(720, degrees);
myDrivetrain.turnToRotation(0, degrees);
calibrateDrivetrain#
calibrateDrivetrain は、スマートドライブで構成された接続されたジャイロセンサーまたはブレイン内蔵の慣性センサーを較正します。
キャリブレーションは、センサーが動きを正確に測定するために役立ちます。キャリブレーション中は、ロボットを約2秒間静止させてください。キャリブレーション中にロボットが動くと、センサーが動きを正確に測定できない場合があります。
プロジェクトは、キャリブレーションが完了するまで次のコード行の実行を待ちます。
注: calibrateDrivetrain 、ドライブトレインがジャイロセンサーまたは慣性センサーで構成されている場合に VEXcode によって自動的に生成されます。これはプロジェクトソースの一部として提供され、VEX IQ C++ API の一部ではありません。
void calibrateDrivetrain();
この関数は引数を受け付けません。
Return Valuesこの関数は値を返しません。
NotesVEXcodeでスマートドライブが設定されている場合、ロボットは各プロジェクトの開始時に自動的にキャリブレーションを行います。
キャリブレーション中は、ロボットを約2秒間静止させてください。
以下は、VEXcodeによって自動生成された calibrateDrivetrain の実装例です。
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#
方位とは、ロボットが向いている方向のことで、度数で表されます。0 setHeading は ロボットの現在の方位を新しい方位値に変更します。
例えば、ロボットが右を向いた場合、方位を0度に設定すると、その右向きの位置が新たな0度になります。その後、ロボットはその新たな方位に基づいて他の位置に旋回することができます。
Available Functionsvoid setHeading(
double value,
rotationUnits units);
パラメータ |
タイプ |
説明 |
|---|---|---|
|
|
ロボットに設定する方位値。0 |
|
|
見出しの単位は、 |
この関数は値を返しません。
Examples// Face the new 0 degrees.
myDrivetrain.setHeading(90, degrees);
myDrivetrain.turnToHeading(0, degrees);
setRotation#
回転とは、ロボットがどれだけ回転したかを示すもので、度数または回転数で表されます。プロジェクトの開始時には、回転値は0度に設定されます。setRotation を実行すると、ロボットの現在の回転値が新しい値に変更されます。
例えば、ロボットが右に2回転した場合、回転角度は720度になります。回転角度を0度に設定すると、720度から0度にリセットされます。その後、ロボットはその新しい値に基づいて回転することができます。
Available Functionsvoid setRotation(
double value,
rotationUnits units);
パラメータ |
タイプ |
説明 |
|---|---|---|
|
|
ロボットに設定する回転値。 |
|
|
回転単位: |
この関数は値を返しません。
Examples// Spin counterclockwise for 2 turns, then face forward.
myDrivetrain.setRotation(720, degrees);
myDrivetrain.turnToRotation(0, degrees);
heading#
方位とは、ロボットが向いている方向のことで、度数で表されます。0 heading ロボットの現在の方位を0度から359度の範囲で返します。
ロボットの初期進行方向は0度です。
Available Functionsdouble heading( rotationUnits units = rotationUnits::deg );
パラメータ |
タイプ |
説明 |
|---|---|---|
|
|
オプション。方位を返す単位: |
ロボットの現在の進行方向を、選択された単位で double 倍数として返します。
// 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.
右に回すと回転角度が大きくなり、左に回すと回転角度が小さくなります。例えば、右に2回転すると720度回転します。
Available Functionsdouble rotation( rotationUnits units = rotationUnits::deg );
パラメータ |
タイプ |
説明 |
|---|---|---|
|
|
オプション。回転を返す単位: |
選択した単位で、ロボットの現在の回転を double 倍数として返します。
// 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();