SmartDrive#

Initializing the SmartDrive Class#

A SmartDrive is created by using the following constructor:

SmartDrive(lm, rm, g, wheelTravel, trackWidth, wheelBase, units, externalGearRatio)

This constructor uses eight parameters:

Parameter

Description

lm

The name of the Left Motor or Motor Group.

rm

The name of the Right Motor or Motor Group.

g

The name of the Inertial Sensor or Gyro Sensor.

wheelTravel

Optional. The circumference of the driven wheels. The default is 300 mm.

trackWidth

Optional. The track width of the Drivetrain. The default is 320 mm.

wheelBase

Optional. The wheel base of the Drivetrain. The default is 320 mm.

units

Optional. A valid DistanceUnit for the units that wheelTravel, trackWidth and wheelBase are specified in. The default is MM.

externalGearRatio

Optional. The gear ratio used to compensate drive distances if gearing is used.

Motors, Motor Groups, Inertial Sensors, and/or Gyro Sensors must be created first before they can be used to create an object with the SmartDrive Class constructor.

# Create the Inertial Sensor.
brain_inertial = Inertial()
# Create the Motors.
left_motor = Motor(Ports.PORT1, GearSetting.RATIO_18_1, False)
right_motor = Motor(Ports.PORT2, GearSetting.RATIO_18_1, True)
# Construct a 2-Motor Smart Drivetrain "drivetrain" with the
# DriveTrain class.
smartdrive = SmartDrive(left_motor, right_motor, brain_inertial, 319.19, 295, 40, MM, 1)

If making a Smart Drivetrain with multiple motors, you need to create the Motors separately before grouping them into a Motor Group.

# Create the Inertial Sensor.
brain_inertial = Inertial()
# Create the left Motors and group them under the
# MotorGroup "left_motors".
left_motor_a = Motor(Ports.PORT1, GearSetting.RATIO_18_1, False)
left_motor_b = Motor(Ports.PORT2, GearSetting.RATIO_18_1, False)
left_motors = MotorGroup(left_motor_a, left_motor_b)
# Create the right Motors and group them under the
# MotorGroup "right_motors".
right_motor_a = Motor(Ports.PORT3, GearSetting.RATIO_18_1, True)
right_motor_b = Motor(Ports.PORT4, GearSetting.RATIO_18_1, True)
right_motors = MotorGroup(right_motor_a, right_motor_b)
# Construct a 4-Motor Drivetrain "drivetrain" with the
# DriveTrain class.
smartdrive = SmartDrive(left_motors, right_motors, brain_inertial, 319.19, 295, 40, MM, 1)

This smartdrive object will be used in all subsequent examples throughout this API documentation when referring to SmartDrive class methods.

To create a DriveTrain object without an Inertial Sensor or Gyro Sensor, use the DriveTrain Class constructor.

Class Methods#

drive()#

The drive(direction, velocity, units) method is used to drive the Smart Drivetrain in the specified direction forever, until another Drivetrain method is used, or the project is stopped.

This is a non-waiting method and allows the next method to run without delay.

Parameters

Description

direction

A valid DirectionType.

velocity

Optional. The velocity at which the Drivetrain will move. The default velocity set by The drivetrain.set_velocity() method will be used.

units

Optional. A valid VelocityUnits type. The default is RPM.

Returns: None.

# Drive the Smart Drivetrain forward.
smartdrive.drive(FORWARD)

# Drive the Smart Drivetrain in reverse for 100 percent velocity.
smartdrive.drive(REVERSE, 100, PERCENT)

drive_for()#

The drive_for(direction, distance, units, velocity, units_v, wait) method is used to drive the Smart Drivetrain a given distance.

This can be a waiting or non-waiting method depending on if the wait parameter is used.

Parameters

Description

direction

A valid DirectionType.

distance

The distance for the Drivetrain to move.

units

Optional. A valid DistanceUnits type. The default is INCHES.

velocity

Optional. The velocity the Drivetrain will move with. The default velocity set by The drivetrain.set_drive_velocity() method will be used if not provided.

units_v

Optional. A valid VelocityUnits type. The default is RPM.

wait

Optional. The wait parameter determines whether the method will block subsequent method (wait=True) or allow immediate execution (wait=False). If unspecified, the default for The wait parameter is wait=True.

Returns: None.

# Drive forward for 10 inches.
smartdrive.drive_for(FORWARD, 10)

turn()#

The turn(direction, velocity, units) method is used to turn the Smart Drivetrain either left or right.

This is a non-waiting method and allows the next method to run without delay.

Parameters

Description

direction

A valid TurnType.

velocity

Optional. The velocity at which the Drivetrain will turn. The default velocity set by The drivetrain.set_turn_velocity() method will be used if not provided.

units

Optional. A valid VelocityUnits type. The default is RPM.

Returns: None.

# Turn the Smart Drivetrain to the right.
smartdrive.turn(RIGHT)

# Turn the Smart Drivetrain to the left at 25% velocity.
smartdrive.turn(LEFT, 25, PERCENT)

turn_to_heading()#

The turn_to_heading(angle, units, velocity, units_v, wait) method turns the Smart Drivetrain to a specific heading.

This can be a waiting or non-waiting method depending on if the wait parameter is used.

Parameters

Description

angle

The heading angle to turn to.

units

Optional. A valid RotationUnits type. The default is DEGREES.

velocity

Optional. Spin the motors of the smartdrive using this velocity.

units_v

Optional. A valid VelocityUnits type. The default is RPM.

wait

Optional. The wait parameter determines whether the method will block subsequent method (wait=True) or allow immediate execution (wait=False). If unspecified, the default for the wait parameter is wait==True.

Returns: None.

# Turn to heading 180 degrees.
smartdrive.turn_to_heading(180)

turn_to_rotation()#

The turn_to_rotation(angle, units, velocity, units_v, wait) method turns the Smart Drivetrain to a specific rotation.

This can be a waiting or non-waiting method depending on if the wait parameter is used.

Parameters

Description

angle

The rotation angle to turn to.

units

Optional. A valid RotationUnits type. The default is DEGREES.

velocity

Optional. Spin the motors of the Smart Drivetrain using this velocity.

units_v

Optional. A valid VelocityUnits type. The default is RPM.

wait

Optional. The wait parameter determines whether the method will block subsequent method (wait=True) or allow immediate execution (wait=False). If unspecified, the default for the wait parameter is wait==True.

Returns: None.

# Turn to rotation 180 degrees.
smartdrive.turn_to_rotation(180)

turn_for()#

The turn_for(angle, units, velocity, units_v, wait) method turns the Smart Drivetrain for a specified angle.

This can be a waiting or non-waiting method depending on if the wait parameter is used.

Parameters

Description

angle

The angle to turn to.

units

Optional. A valid RotationUnits type. The default is DEGREES.

velocity

Optional. Spin the motors of the Smart Drivetrain using this velocity.

units_v

Optional. A valid VelocityUnits type. The default is RPM.

wait

Optional. The wait parameter determines whether the method will block subsequent method (wait=True) or allow immediate execution (wait=False). If unspecified, the default for the wait parameter is wait==True.

Returns: None.

# Turn for 180 degrees.
smartdrive.turn_for(180)

stop()#

The stop() method is used to stop the Smart Drivetrain, setting it to 0 velocity and configuring the current stopping mode.

Returns: None.

# Stop the Smart Drivetrain.
smartdrive.stop()

set_drive_velocity()#

The set_drive_velocity(velocity, units) method is used to set the default velocity for the Smart Drivetrain. This velocity setting affects all subsequent Drivetrain methods unless a specific velocity is provided in those method.

Parameters

Description

velocity

The new velocity to set as default for the Smart Drivetrain.

units

Optional. A valid VelocityUnits type. The default is RPM.

Returns: None.

# Set the Smart Drivetrain to drive at a velocity of
# 200 RPM.
smartdrive.set_drive_velocity(200)

# Set the Smart Drivetrain to drive at a velocity of
# 100 PERCENT.
smartdrive.set_drive_velocity(100, PERCENT)

set_turn_velocity()#

The set_turn_velocity(velocity, units) method is used to set the default velocity for turning maneuvers in the Smart Drivetrain. This setting specifies the speed at which the Smart Drivetrain will execute turning method unless overridden by a specific velocity in those method.

Parameters

Description

velocity

The new velocity to set as default for turning maneuvers.

units

Optional. A valid VelocityUnits type. The default is RPM, unless previously changed using The drivetrain.set_turn_velocity() method.

Returns: None.

# Set the Smart Drivetrain to turn at a velocity of
# 200 RPM.
smartdrive.set_turn_velocity(200)

# Set the Smart Drivetrain to turn at a velocity of
# 100 PERCENT.
smartdrive.set_turn_velocity(100, PERCENT)

set_stopping()#

The set_stopping(mode) method is used to set the stopping mode for all Motors on the Smart Drivetrain. This setting determines the behavior of the Motors when they receive a stop method or when the velocity is set to zero.

Parameters

Description

mode

A valid BrakeType. The default is COAST, unless previously changed using the drivetrain.set_stopping() method.

Returns: None.

# Set the stopping mode to BRAKE.
smartdrive.set_stopping(BRAKE)

set_turn_threshold()#

The set_turn_threshold(value) method sets the turn threshold for the Smart Drivetrain. The threshold value is used to determine that turns are complete. If this is too large, then turns will not be accurate. If too small, then turns may not complete.

Parameters

Description

value

The new turn threshold in DEGREES. The default for a Smart Drivetrain is 1 degree.

Returns: None.

set_turn_constant()#

The set_turn_constant(value) method sets the turn constant for the Smart Drivetrain. The Smart Drivetrain uses a simple P controller when doing turns. This constant, generally known as kp, is the gain used in the equation that turns angular error into motor velocity.

Parameters

Description

value

The new turn constant in the range 0.1 - 4.0. The default is 1.0.

Returns: None.

set_turn_direction_reverse()#

The set_turn_direction_reverse(value) method sets the expected turn direction for positive heading change.

Parameters

Description

value

A boolean value to set the direction reversed or not.

Returns: None.

set_heading()#

The set_heading(value, units) method sets the heading for the Smart Drivetrain.

Parameters

Description

value

The new value to use for heading.

units

Optional. A valid RotationUnits type. The default is DEGREES.

Returns: None.

# Set the value of heading to 180 degrees.
smartdrive.set_heading(180)

heading()#

The heading(units) method returns the current heading of the Smart Drivetrain.

Parameters

Description

units

Optional. A valid RotationUnits type. The default is DEGREES.

Returns: The current heading of the Smart Drivetrain in the specified units.

# Get the current heading for the Smart Drivetrain.
value = smartdrive.heading()

set_rotation()#

The set_rotation() method sets the rotation for the Smart Drivetrain.

Parameters

Description

value

The new value to use for rotation.

units

Optional. A valid RotationUnits type. The default is DEGREES.

Returns: None.

# Set the value of rotation to 180 degrees.
smartdrive.set_rotation(180)

rotation()#

The rotation(units) method returns the current rotation of the Smart Drivetrain.

Parameters

Description

units

Optional. A valid RotationUnits type. The default is DEGREES.

Returns: The current rotation of the Smart Drivetrain in the specified units.

# Get the current rotation for the Smart Drivetrain.
value = smartdrive.rotation()

is_turning()#

The is_turning() method checks if the Smart Drivetrain is currently turning.

Returns: True if the Smart Drivetrain is currently turning. False if it is not currently turning.

is_moving()#

The is_moving() method checks if the Smart Drivetrain is currently moving.

Returns: True if the Smart Drivetrain is currently moving. False if it is not currently moving.

is_done()#

The is_done() method checks if the Smart Drivetrain is not currently moving.

Returns: True if the Smart Drivetrain is not currently moving. False if it is currently moving.

set_timeout()#

The set_timeout(timeout, units) method is used to set the timeout value for all motors on the Smart Drivetrain. This setting determines how long the Smart Drivetrain will attempt to execute drive_for and turn_for method before timing out if the motors have not completed their movements.

Parameters

Description

timeout

The timeout duration to set as default for Smart Drivetrain operations.

units

Optional. A valid TimeUnits type. The default is MSEC, unless previously changed using the set_timeout() method.

Returns: None.

# Set the timeout for the Smart Drivetrain to
# 1000 milliseconds.
drivetrain.set_timeout(1000)

get_timeout()#

The smartdrive.get_timeout() method gets the current timeout setting.

Returns: The current time after which a Smart Drivetrain method will timeout.

velocity()#

The velocity() method returns the average velocity of the left and right Motors.

Parameters

Description

units

Optional. A valid VelocityUnits type. The default is RPM.

Returns: The Smart Drivetrain’s velocity in the provided units.

current()#

The current() method returns the average current of the left and right Motors.

Parameters

Description

units

Optional. The only valid unit for current is AMP.

Returns: The Smart Drivetrain current in the provided units.

power()#

The power() method returns the total power all Smart Drivetrain Motors are using.

Parameters

Description

units

Optional. The only valid unit for power is WATT.

Returns: The Smart Drivetrain’s power in the provided units.

torque()#

The torque() method returns the total torque of all Smart Drivetrain motors.

Parameters

Description

units

Optional. A valid TorqueUnits type. The default is NM.

Returns: The Smart Drivetrain’s torque in the provided units.

efficiency()#

The efficiency() method returns the average efficiency of the left and right Motors.

Parameters

Description

units

Optional. The only valid unit for efficiency is PERCENT.

Returns: The Smart Drivetrain’s efficiency in the provided units.

temperature()#

The temperature() method returns the average temperature of the left and right Motors.

Parameters

Description

units

Optional. A valid TemperatureUnits type. The default is CELSIUS.

Returns: The Smart Drivetrain’s temperature in the provided units.