RobotDrive

class wpilib.RobotDrive(*args, **kwargs)[source]

Bases: wpilib.MotorSafety

Operations on a robot drivetrain based on a definition of the motor configuration.

The robot drive class handles basic driving for a robot. Currently, 2 and 4 motor tank and mecanum drive trains are supported. In the future other drive types like swerve might be implemented. Motor channel numbers are passed supplied on creation of the class. Those are used for either the drive function (intended for hand created drive code, such as autonomous) or with the Tank/Arcade functions intended to be used for Operator Control driving.

Constructor for RobotDrive.

Either 2 or 4 motors can be passed to the constructor to implement a two or four wheel drive system, respectively.

When positional arguments are used, these are the two accepted orders:

  • leftMotor, rightMotor
  • frontLeftMotor, rearLeftMotor, frontRightMotor, rearRightMotor

Alternatively, the above names can be used as keyword arguments.

Either channel numbers or motor controllers can be passed (determined by whether the passed object has a set function). If channel numbers are passed, the motorController keyword argument, if present, is the motor controller class to use; if unspecified, Talon is used.

class MotorType[source]

Bases: object

The location of a motor on the robot for the purpose of driving.

kFrontLeft = 0

Front left

kFrontRight = 1

Front right

kRearLeft = 2

Rear left

kRearRight = 3

Rear right

RobotDrive.arcadeDrive(*args, **kwargs)[source]

Provide tank steering using the stored robot configuration.

Either one or two joysticks (with optional specified axis) or two raw values may be passed positionally, along with an optional squaredInputs boolean. The valid positional combinations are:

  • stick
  • stick, squaredInputs
  • moveStick, moveAxis, rotateStick, rotateAxis
  • moveStick, moveAxis, rotateStick, rotateAxis, squaredInputs
  • moveValue, rotateValue
  • moveValue, rotateValue, squaredInputs

Alternatively, the above names can be used as keyword arguments. The behavior of mixes of keyword arguments in other than the combinations above is undefined.

If specified positionally, the value and joystick versions are disambiguated by looking for a getY function on the stick.

Parameters:
  • stick – The joystick to use for Arcade single-stick driving. The Y-axis will be selected for forwards/backwards and the X-axis will be selected for rotation rate.
  • moveStick – The Joystick object that represents the forward/backward direction.
  • moveAxis – The axis on the moveStick object to use for forwards/backwards (typically Y_AXIS).
  • rotateStick – The Joystick object that represents the rotation value.
  • rotateAxis – The axis on the rotation object to use for the rotate right/left (typically X_AXIS).
  • moveValue – The value to use for forwards/backwards.
  • rotateValue – The value to use for the rotate right/left.
  • squaredInputs – Setting this parameter to True decreases the sensitivity at lower speeds. Defaults to True if unspecified.
RobotDrive.drive(outputMagnitude, curve)[source]

Drive the motors at “outputMagnitude” and “curve”.

Both outputMagnitude and curve are -1.0 to +1.0 values, where 0.0 represents stopped and not turning. curve < 0 will turn left and curve > 0 will turn right.

The algorithm for steering provides a constant turn radius for any normal speed range, both forward and backward. Increasing m_sensitivity causes sharper turns for fixed values of curve.

This function will most likely be used in an autonomous routine.

Parameters:
  • outputMagnitude – The speed setting for the outside wheel in a turn, forward or backwards, +1 to -1.
  • curve – The rate of turn, constant for different forward speeds. Set curve < 0 for left turn or curve > 0 for right turn.

Set curve = e^(-r/w) to get a turn radius r for wheelbase w of your robot. Conversely, turn radius r = -ln(curve)*w for a given value of curve and wheelbase w.

RobotDrive.free()[source]
RobotDrive.getDescription()[source]
RobotDrive.getNumMotors()[source]
RobotDrive.holonomicDrive(magnitude, direction, rotation)[source]

Holonomic Drive method for Mecanum wheeled robots.

This is an alias to mecanumDrive_Polar() for backward compatibility.

Parameters:
  • magnitude – The speed that the robot should drive in a given direction. [-1.0..1.0]
  • direction – The direction the robot should drive. The direction and magnitude are independent of the rotation rate.
  • rotation – The rate of rotation for the robot that is completely independent of the magnitude or direction. [-1.0..1.0]
RobotDrive.kArcadeRatioCurve_Reported = False
RobotDrive.kArcadeStandard_Reported = False
RobotDrive.kDefaultExpirationTime = 0.1
RobotDrive.kDefaultMaxOutput = 1.0
RobotDrive.kDefaultSensitivity = 0.5
RobotDrive.kMaxNumberOfMotors = 4
RobotDrive.kMecanumCartesian_Reported = False
RobotDrive.kMecanumPolar_Reported = False
RobotDrive.kTank_Reported = False
static RobotDrive.limit(num)[source]

Limit motor values to the -1.0 to +1.0 range.

RobotDrive.mecanumDrive_Cartesian(x, y, rotation, gyroAngle)[source]

Drive method for Mecanum wheeled robots.

A method for driving with Mecanum wheeled robots. There are 4 wheels on the robot, arranged so that the front and back wheels are toed in 45 degrees. When looking at the wheels from the top, the roller axles should form an X across the robot.

This is designed to be directly driven by joystick axes.

Parameters:
  • x – The speed that the robot should drive in the X direction. [-1.0..1.0]
  • y – The speed that the robot should drive in the Y direction. This input is inverted to match the forward == -1.0 that joysticks produce. [-1.0..1.0]
  • rotation – The rate of rotation for the robot that is completely independent of the translation. [-1.0..1.0]
  • gyroAngle – The current angle reading from the gyro. Use this to implement field-oriented controls.
RobotDrive.mecanumDrive_Polar(magnitude, direction, rotation)[source]

Drive method for Mecanum wheeled robots.

A method for driving with Mecanum wheeled robots. There are 4 wheels on the robot, arranged so that the front and back wheels are toed in 45 degrees. When looking at the wheels from the top, the roller axles should form an X across the robot.

Parameters:
  • magnitude – The speed that the robot should drive in a given direction.
  • direction – The direction the robot should drive in degrees. The direction and maginitute are independent of the rotation rate.
  • rotation – The rate of rotation for the robot that is completely independent of the magnitute or direction. [-1.0..1.0]
static RobotDrive.normalize(wheelSpeeds)[source]

Normalize all wheel speeds if the magnitude of any wheel is greater than 1.0.

static RobotDrive.rotateVector(x, y, angle)[source]

Rotate a vector in Cartesian space.

RobotDrive.setInvertedMotor(motor, isInverted)[source]

Invert a motor direction.

This is used when a motor should run in the opposite direction as the drive code would normally run it. Motors that are direct drive would be inverted, the drive code assumes that the motors are geared with one reversal.

Parameters:
  • motor – The motor index to invert.
  • isInverted – True if the motor should be inverted when operated.
RobotDrive.setLeftRightMotorOutputs(leftOutput, rightOutput)[source]

Set the speed of the right and left motors.

This is used once an appropriate drive setup function is called such as twoWheelDrive(). The motors are set to “leftSpeed” and “rightSpeed” and includes flipping the direction of one side for opposing motors.

Parameters:
  • leftOutput – The speed to send to the left side of the robot.
  • rightOutput – The speed to send to the right side of the robot.
RobotDrive.setMaxOutput(maxOutput)[source]

Configure the scaling factor for using RobotDrive with motor controllers in a mode other than PercentVbus.

Parameters:maxOutput – Multiplied with the output percentage computed by the drive functions.
RobotDrive.setSensitivity(sensitivity)[source]

Set the turning sensitivity.

This only impacts the drive() entry-point.

Parameters:sensitivity – Effectively sets the turning sensitivity (or turn radius for a given value)
RobotDrive.stopMotor()[source]
RobotDrive.tankDrive(*args, **kwargs)[source]

Provide tank steering using the stored robot configuration.

Either two joysticks (with optional specified axis) or two raw values may be passed positionally, along with an optional squaredInputs boolean. The valid positional combinations are:

  • leftStick, rightStick
  • leftStick, rightStick, squaredInputs
  • leftStick, leftAxis, rightStick, rightAxis
  • leftStick, leftAxis, rightStick, rightAxis, squaredInputs
  • leftValue, rightValue
  • leftValue, rightValue, squaredInputs

Alternatively, the above names can be used as keyword arguments. The behavior of mixes of keyword arguments in other than the combinations above is undefined.

If specified positionally, the value and joystick versions are disambiguated by looking for a getY function.

Parameters:
  • leftStick – The joystick to control the left side of the robot.
  • leftAxis – The axis to select on the left side Joystick object (defaults to the Y axis if unspecified).
  • rightStick – The joystick to control the right side of the robot.
  • rightAxis – The axis to select on the right side Joystick object (defaults to the Y axis if unspecified).
  • leftValue – The value to control the left side of the robot.
  • rightValue – The value to control the right side of the robot.
  • squaredInputs – Setting this parameter to True decreases the sensitivity at lower speeds. Defaults to True if unspecified.