drivetrain#
Initializing the drivetrain Class#
使用以下构造函数创建动力传动系统:
drivetrain
构造函数创建一个 drivetrain 对象。
范围 |
描述 |
---|---|
|
The name of the Left Motor or Motor Group. |
|
The name of the Right Motor or Motor Group. |
|
驱动轮的周长。默认值为 300 毫米。 |
|
传动系统的轮距。默认值为 320 毫米。 |
|
传动系统的轴距。默认值为 320 毫米。 |
|
对于 wheelTravel、trackWidth 和 wheelBase 指定的单位,有效的 distanceUnit。默认值为“MM”。 |
|
如果使用齿轮传动,则使用齿轮比来补偿驱动距离。 |
Motors and/or Motor Groups must be created first before they can be used to create an object with the drivetrain Class constructor.
// Create the Motors.
motor left_motor = motor(PORT1,false);
motor right_motor = motor(PORT2, true);
// Construct a 2-Motor drivetrain "Drivetrain" with the
// drivetrain class.
drivetrain Drivetrain = drivetrain(left_motor, right_motor, 259.34, 320, 40, mm, 1);
If making a 4-Motor Drivetrain, you need to create the Motors separately before grouping them into a Motor Group.
// Create the left Motors and group them under the
// motor_group "leftMotors".
motor leftMotorA = motor(PORT1, false);
motor leftMotorB = motor(PORT2, false);
motor_group leftMotors = motor_group(leftMotorA, leftMotorB);
// Create the right Motors and group them under the
// MotorGroup "rightMotors".
motor rightMotorA = motor(PORT3, true);
motor rightMotorB = motor(PORT4, true);
motor_group rightMotors = motor_group(rightMotorA, rightMotorB);
// Construct a 4-Motor drivetrain "Drivetrain" with the
// drivetrain class.
drivetrain Drivetrain = drivetrain(leftMotors, rightMotors, 259.34, 320, 40, mm, 1);
当引用传动系统类方法时,此“Drivetrain”对象将在整个 API 文档的所有后续示例中使用。
要在您的传动系统中加入 惯性传感器 或 陀螺仪传感器 以增强转弯功能,您可以在使用 smartdrive 类构造函数创建对象时将其包含在内。
Class Methods#
drive()#
This is a non-waiting method and allows the next method to run without delay.
This method is called in the following ways:
The drive(dir)
method is used to drive the drivetrain in the specified direction forever at the default velocity as specified by the setDriveVelocity()
method, until another drivetrain movement method is used, or the project is stopped.
参数 |
描述 |
---|---|
dir |
A valid directionType. |
Returns: None.
// Drive the drivetrain forward.
Drivetrain.drive(forward);
The drive(dir, velocity, units)
method is used to drive the drivetrain in the specified direction forever at a specified velocity, until another drivetrain movement method is used, or the project is stopped.
参数 |
描述 |
---|---|
dir |
A valid directionType. |
速度 |
The velocity at which the drivetrain will move. |
单位 |
A valid velocityUnit. |
Returns: None.
// Drive the drivetrain forward at 100 rpm.
Drivetrain.drive(forward, 100, rpm);
driveFor()#
This can be a waiting or non-waiting method depending on if the wait
parameter is used.
This method is called in the following ways:
The driveFor(distance, units, waitForCompletion)
method is used to drive the drivetrain for a specified distance at the default velocity.
参数 |
描述 |
---|---|
距离 |
The distance for the drivetrain to move. |
单位 |
A valid distanceUnit. The default is |
waitForCompletion |
Determines whether the command will block subsequent commands ( |
Returns: A boolean value representing whether the drivetrain has reached the target distance.
// Drive the drivetrain for 10 inches.
Drivetrain.driveFor(10, inches);
The driveFor(dir, distance, units, waitForCompletion)
method is used to drive the drivetrain in a specified direction for a specified distance at the default velocity.
参数 |
描述 |
---|---|
dir |
A valid directionType. |
距离 |
The distance for the drivetrain to move. |
单位 |
A valid distanceUnit. The default is |
waitForCompletion |
Determines whether the command will block subsequent commands ( |
Returns: A boolean value representing whether the drivetrain has reached the target distance.
// Drive the drivetrain forward for 10 inches.
Drivetrain.driveFor(forward, 10, inches);
The driveFor(distance, units, velocity, units_v, waitForCompletion)
method is used to drive the drivetrain for a specified distance at the default velocity.
参数 |
描述 |
---|---|
距离 |
The distance for the drivetrain to move. |
单位 |
A valid distanceUnit. The default is |
速度 |
The velocity the drivetrain will move with. The default velocity set by the |
单位 |
A valid velocityUnit. The default is |
waitForCompletion |
Determines whether the command will block subsequent commands ( |
Returns: A boolean value representing whether the drivetrain has reached the target distance.
// Drive the drivetrain for 10 inches at 100 rpm.
Drivetrain.driveFor(10, inches, 100, rpm);
The driveFor(dir, distance, units, velocity, units_v, waitForCompletion)
method is used to drive the drivetrain for a specified distance at the default velocity.
参数 |
描述 |
---|---|
dir |
A valid directionType. |
距离 |
The distance for the drivetrain to move. |
单位 |
A valid distanceUnit. The default is |
速度 |
The velocity the drivetrain will move with. The default velocity set by the |
单位 |
A valid velocityUnit. The default is |
waitForCompletion |
Determines whether the command will block subsequent commands ( |
Returns: A boolean value representing whether the drivetrain has reached the target distance.
// Drive the drivetrain forward for 10 inches at 100 rpm.
Drivetrain.driveFor(forward, 10, inches, 100, rpm);
turn()#
This is a non-waiting method and allows the next method to run without delay.
This method is called in the following ways:
The turn(dir)
method is used to turn the robot in a specified direction at the default velocity as specified by the setTurnVelocity()
method, until another drivetrain movement method is used, or the project is stopped.
参数 |
描述 |
---|---|
dir |
A valid directionType. |
Returns: None.
// Turn the drivetrain right.
Drivetrain.turn(right);
The turn(dir, velocity, units)
method is used to turn the robot in a specified direction at a specified velocity, until another drivetrain movement method is used, or the project is stopped.
参数 |
描述 |
---|---|
dir |
A valid directionType. |
速度 |
The velocity at which the drivetrain will turn. The default velocity set by The |
单位 |
A valid velocityUnit. The default is |
Returns: None.
// Turn the drivetrain right at 100 rpm.
Drivetrain.turn(right, 100, rpm);
turnFor()#
This can be a waiting or non-waiting method depending on if the wait
parameter is used.
This method is called in the following ways:
The turnFor(angle, units, waitForCompletion)
method is used to turn the drivetrain for a specific angle at the default velocity as specified by the setTurnVelocity()
method.
参数 |
描述 |
---|---|
角度 |
The angle for the drivetrain to turn. |
单位 |
A valid rotationUnit. The default is |
waitForCompletion |
Determines whether the command will block subsequent commands ( |
Returns: A boolean value representing whether the drivetrain has turned to the target angle.
// Turn the drivetrain for 90 degrees.
Drivetrain.turnFor(90, degrees);
The turnFor(dir, angle, units, waitForCompletion)
method is used to turn the drivetrain for a specific angle at the default velocity as specified by the setTurnVelocity()
method.
参数 |
描述 |
---|---|
dir |
A valid directionType. |
角度 |
The angle for the drivetrain to turn. |
单位 |
A valid rotationUnit. The default is |
waitForCompletion |
Determines whether the command will block subsequent commands ( |
Returns: A boolean value representing whether the drivetrain has turned to the target angle.
// Turn the drivetrain right for 90 degrees.
Drivetrain.turnFor(right, 90, degrees);
The turnFor(angle, units, velocity, units_v, waitForCompletion)
method is used to turn the drivetrain for a specific angle at a specified velocity.
参数 |
描述 |
---|---|
角度 |
The angle for the drivetrain to turn. |
单位 |
A valid rotationUnit. The default is |
速度 |
The velocity at which the drivetrain will turn. The default velocity set by The |
单位 |
A valid velocityUnit. The default is |
waitForCompletion |
Determines whether the command will block subsequent commands ( |
Returns: A boolean value representing whether the drivetrain has turned to the target angle.
// Turn the drivetrain for 90 degrees at 100 rpm.
Drivetrain.turnFor(90, degrees, 100, rpm);
The turnFor(dir angle, units, velocity, units_v, waitForCompletion)
method is used to turn the drivetrain for a specific angle at a specified velocity.
参数 |
描述 |
---|---|
dir |
A valid directionType. |
角度 |
The angle for the drivetrain to turn. |
单位 |
A valid rotationUnit. The default is |
速度 |
The velocity at which the drivetrain will turn. The default velocity set by The |
单位 |
A valid velocityUnit. The default is |
waitForCompletion |
Determines whether the command will block subsequent commands ( |
Returns: A boolean value representing whether the drivetrain has turned to the target angle.
// Turn the drivetrain right for 90 degrees at 100 rpm.
Drivetrain.turnFor(right, 90, degrees, 100, rpm);
stop()#
This is a non-waiting command and allows the next command to run without delay.
This method is called in the following ways:
The stop()
command is used to stop the drivetrain, setting the drivetrain to 0 velocity and configuring the current stopping mode. The default Brake Type is coast
, unless previously changed using the setStopping()
command.
Returns: None.
// Spin Drivetrain for 2.5 seconds.
Drivetrain.spin(forward);
wait(2.5 seconds);
// Stop Drivetrain.
Drivetrain.stop();
The stop(mode)
command is used to stop the drivetrain using a specific Brake Type.
参数 |
描述 |
---|---|
模式 |
A valid brakeType. |
Returns: None.
// Spin Drivetrain for 2.5 seconds.
Drivetrain.spin(forward);
wait(2.5 seconds);
// Stop Drivetrain using the hold Brake Type.
Drivetrain.stop(hold);
isMoving()#
The isMoving()
command returns if any drivetrain motor is on and rotating to a specific target.
Returns: true
if any drivetrain motor is on and rotating to a target. false
if it they are done rotating to a target.
isDone()#
The isDone()
command returns if all drivetrain motors are done rotating to a specific target.
Returns: true
if the all drivetrain motors are done rotating to a target. false
if any drivetrain motor is on and rotating to a target.
setDriveVelocity()#
The setDriveVelocity(velocity, units)
method is used to set the default velocity for the Drivetrain. This velocity setting affects all subsequent drive method unless a specific velocity is provided in those method.
参数 |
描述 |
---|---|
速度 |
The new velocity to set as default for the Drivetrain. |
单位 |
A valid velocityUnit or |
Returns: None.
// Set the drivetrain to drive at a velocity of 200 rpm.
Drivetrain.setDriveVelocity(200, rpm);
setTurnVelocity()#
The setTurnVelocity(velocity, units)
method is used to set the default velocity for turning maneuvers in the Drivetrain. This setting specifies the speed at which the drivetrain will execute turning method unless overridden by a specific velocity in those method.
参数 |
描述 |
---|---|
速度 |
The new velocity to set as default for turning maneuvers. |
单位 |
A valid velocityUnit or |
Returns: None.
// Set the drivetrain to turn at a velocity of 200 RPM.
Drivetrain.setTurnVelocity(200, rpm);
setStopping()#
The setStopping(mode)
method is used to set the stopping mode for all motors on the Drivetrain. This setting determines the behavior of the motors when they receive a stop method or when the velocity is set to zero.
参数 |
描述 |
---|---|
模式 |
A valid brakeType. |
Returns: None.
// Set the stopping mode to BRAKE.
Drivetrain.setStopping(brake);
setGearRatio()#
The setGearRatio(mode)
method is used to set the gear ratio for all motors on the Drivetrain.
参数 |
描述 |
---|---|
模式 |
A valid gearSetting. |
Returns: None.
setTimeout()#
The setTimeout(time, units)
method is used to set the timeout value for all motors on the Drivetrain. This setting determines how long the drivetrain will attempt to execute driveFor
or turnFor
method before timing out if the motors have not completed their movements.
参数 |
描述 |
---|---|
timeout |
The timeout duration to set as default for drivetrain operations. |
单位 |
A valid timeUnit type. The default is |
Returns: None.
// Set the timeout for the drivetrain to 10 seconds.
Drivetrain.setTimeout(10, seconds);
velocity()#
The velocity(units)
method returns the average velocity of the drivetrain.
参数 |
描述 |
---|---|
单位 |
A valid velocityUnit or |
Returns: A double representing the drivetrain velocity in the specified units.
current()#
The current()
method returns the average current of the drivetrain.
参数 |
描述 |
---|---|
单位 |
The only valid units for current are |
Returns: A double representing the drivetrain current in the specified units.
power()#
The power(units)
method returns the average power of the drivetrain .
参数 |
描述 |
---|---|
单位 |
功率的唯一有效单位是“瓦特”。 |
Returns: A double representing the drivetrain power in the specified units.
torque()#
The torque()
method returns the average torque of the drivetrain.
参数 |
描述 |
---|---|
单位 |
A valid torqueUnit. The default is |
Returns: A double representing the drivetrain torque in the specified units.
efficiency()#
The efficiency()
method returns the average efficiency of the drivetrain.
参数 |
描述 |
---|---|
单位 |
效率的唯一有效单位是“百分比”。 |
Returns: A double representing the drivetrain efficiency in the provided units.