Class Drivetrain

java.lang.Object
edu.wpi.first.wpilibj2.command.SubsystemBase
frc.robot.subsystems.drivetrain.Drivetrain
All Implemented Interfaces:
Sendable, Subsystem

public class Drivetrain extends SubsystemBase
Subsystem that controls the drivetrain.
  • Constructor Details

    • Drivetrain

      public Drivetrain(File directory)
      Initialize SwerveDrive with the directory provided.
      Parameters:
      directory - Directory of swerve drive config files.
  • Method Details

    • periodic

      public void periodic()
    • simulationPeriodic

      public void simulationPeriodic()
    • getSwerveDrive

      public SwerveDrive getSwerveDrive()
      Gets the swerve drive object.
      Returns:
      SwerveDrive
    • getSwerveController

      public SwerveController getSwerveController()
      Get the SwerveController in the swerve drive.
      Returns:
      SwerveController from the SwerveDrive.
      See Also:
    • getSwerveDriveConfiguration

      public SwerveDriveConfiguration getSwerveDriveConfiguration()
      Get the SwerveDriveConfiguration object.
      Returns:
      The SwerveDriveConfiguration for the current drive.
      See Also:
    • getKinematics

      public SwerveDriveKinematics getKinematics()
      Get the swerve drive kinematics object.
      Returns:
      SwerveDriveKinematics of the swerve drive.
      See Also:
    • resetOdometry

      public void resetOdometry(Pose2d initialHolonomicPose)
      Resets odometry to the given pose. Gyro angle and module positions do not need to be reset when calling this method. However, if either gyro angle or module position is reset, this must be called in order for odometry to keep working.
      Parameters:
      initialHolonomicPose - The pose to set the odometry to
      See Also:
    • getPose

      public Pose2d getPose()
      Gets the current pose (position and rotation) of the robot, as reported by odometry.
      Returns:
      The robot's pose
      See Also:
    • sysIdDriveMotorCommand

      public Command sysIdDriveMotorCommand()
      Command to characterize the robot drive motors using SysId.
      Returns:
      SysId Drive Command
    • sysIdAngleMotorCommand

      public Command sysIdAngleMotorCommand()
      Command to characterize the robot angle motors using SysId.
      Returns:
      SysId Angle Command
    • centerModulesCommand

      public Command centerModulesCommand()
      Command to center the modules of the SwerveDrive subsystem.
      Returns:
      Command to run
    • postTrajectory

      public void postTrajectory(Trajectory trajectory)
      Post the trajectory to the field.
      Parameters:
      trajectory - The trajectory to post.
      See Also:
    • zeroGyro

      public void zeroGyro()
      Resets the gyro angle to zero and resets odometry to the same position, but facing toward 0.
      See Also:
    • zeroGyroWithAlliance

      public void zeroGyroWithAlliance()
      This will zero (calibrate) the robot to assume the current position is facing forward

      If red alliance rotate the robot 180 after the drviebase zero command

    • setMotorBrake

      public void setMotorBrake(boolean brake)
      Sets the drive motors to brake/coast mode.
      Parameters:
      brake - True to set motors to brake mode, false for coast.
      See Also:
    • getHeading

      public Rotation2d getHeading()
      Gets the current yaw angle of the robot, as reported by the swerve pose estimator in the underlying drivebase. Note, this is not the raw gyro reading, this may be corrected from calls to resetOdometry().
      Returns:
      The yaw angle
    • getFieldVelocity

      public ChassisSpeeds getFieldVelocity()
      Gets the current field-relative velocity (x, y and omega) of the robot.
      Returns:
      A ChassisSpeeds object of the current field-relative velocity
      See Also:
    • getRobotVelocity

      public ChassisSpeeds getRobotVelocity()
      Gets the current velocity (x, y and omega) of the robot.
      Returns:
      A ChassisSpeeds object of the current velocity
      See Also:
    • lock

      public void lock()
      Lock the swerve drive to prevent it from moving.
      See Also:
    • getPitch

      public Rotation2d getPitch()
      Gets the current pitch angle of the robot, as reported by the imu.
      Returns:
      The heading as a Rotation2d angle
      See Also:
    • drive

      public void drive(Translation2d translation, double rotation, boolean fieldRelative)
      The primary method for controlling the drivebase. Takes a Translation2d and a rotation rate, and calculates and commands module states accordingly. Can use either open-loop or closed-loop velocity control for the wheel velocities. Also has field- and robot-relative modes, which affect how the translation vector is used.
      Parameters:
      translation - Translation2d that is the commanded linear velocity of the robot, in meters per second. In robot-relative mode, positive x is torwards the bow (front) and positive y is torwards port (left). In field-relative mode, positive x is away from the alliance wall (field North) and positive y is torwards the left wall when looking through the driver station glass (field West).
      rotation - Robot angular rate, in radians per second. CCW positive. Unaffected by field/robot relativity.
      fieldRelative - Drive mode. True for field-relative, false for robot-relative.
      See Also:
    • resetLastAngleScalar

      public void resetLastAngleScalar()
      The method to reset what the heading control will turn to if no angle is inputed. Used to prevent angle snapback.
      Implementation Note:
      Running this on the Red alliance will cause the robot to flip 180 degrees. Call resetLastAngleScalarByAlliance() instead if that is not the desired behavior.
    • resetLastAngleScalarCommand

      public Command resetLastAngleScalarCommand()
      The command to reset what the heading control will turn to if no angle is inputed. Used to prevent angle snapback.
      Returns:
      Command to run.
      See Also:
    • resetLastAngleScalarInverted

      public void resetLastAngleScalarInverted()
      Inverted method to reset what the heading control will turn to if no angle is inputed. Used to prevent angle snapback.
      See Also:
      Implementation Note:
      Running this on the Blue alliance will cause the robot to flip 180 degrees. Call resetLastAngleScalarByAlliance() instead if that is not the desired behavior.
    • resetLastAngleScalarInvertedCommand

      public Command resetLastAngleScalarInvertedCommand()
      Inverted command to reset what the heading control will turn to if no angle is inputed. Used to prevent angle snapback.
      Returns:
      Command to run.
      See Also:
    • resetLastAngleScalarByAlliance

      public void resetLastAngleScalarByAlliance()
      Method to reset what the heading control will turn to if no angle is inputed. Inverted based on alliance color. Used to prevent angle snapback.
      See Also:
    • resetLastAngleScalarByAllianceCommand

      public Command resetLastAngleScalarByAllianceCommand()
      Command to reset what the heading control will turn to if no angle is inputed. Inverted based on alliance color. Used to prevent angle snapback.
      Returns:
      Command to run.
      See Also:
    • driveToPoseCommand

      public Command driveToPoseCommand(Supplier<Pose2d> pose)
      Use PathPlanner Path finding to go to a point on the field.
      Parameters:
      pose - Target Pose2d to go to.
      Returns:
      PathFinding command
      See Also:
    • driveToNearestPoseCommand

      public Command driveToNearestPoseCommand(List<Pose2d> poseList)
      Drives to the nearest pose out of a list.
      Parameters:
      poseList - List of poses to choose from.
      Returns:
      Command to run.
    • driveWithSetpointGeneratorFieldRelativeCommand

      public Command driveWithSetpointGeneratorFieldRelativeCommand(Supplier<ChassisSpeeds> fieldRelativeSpeeds)
      Drive with 254's Setpoint generator; port written by PathPlanner.
      Parameters:
      fieldRelativeSpeeds - Field-Relative ChassisSpeeds
      Returns:
      Command to drive the robot using the setpoint generator.
      See Also:
      • driveWithSetpointGeneratorCommand(Supplier)
    • driveToDistanceCommand

      public Command driveToDistanceCommand(double distanceInMeters, double speedInMetersPerSecond)
      Command that drives the swerve drive to a specific distance at a given speed.
      Parameters:
      distanceInMeters - the distance to drive in meters
      speedInMetersPerSecond - the speed at which to drive in meters per second
      Returns:
      Command to run
    • driveAngularCommand

      public Command driveAngularCommand(DoubleSupplier translationX, DoubleSupplier translationY, DoubleSupplier angularRotationX)
      Command to drive the robot using translative values and heading as angular velocity.
      Parameters:
      translationX - Translation in the X direction. Cubed for smoother controls.
      translationY - Translation in the Y direction. Cubed for smoother controls.
      angularRotationX - Angular velocity of the robot to set. Cubed for smoother controls.
      Returns:
      Command to run
      See Also:
    • driveHeadingCommand

      public Command driveHeadingCommand(DoubleSupplier translationX, DoubleSupplier translationY, DoubleSupplier headingX, DoubleSupplier headingY)
      Command to drive the robot using translative values and heading as a setpoint.
      Parameters:
      translationX - Translation in the X direction. Cubed for smoother controls.
      translationY - Translation in the Y direction. Cubed for smoother controls.
      headingX - Heading X to calculate angle of the joystick.
      headingY - Heading Y to calculate angle of the joystick.
      Returns:
      Command to run
    • drive

      public void drive(ChassisSpeeds velocity)
      Drive according to the chassis robot oriented velocity.
      Parameters:
      velocity - Robot oriented ChassisSpeeds
      See Also:
    • driveFieldOriented

      public void driveFieldOriented(ChassisSpeeds velocity)
      Drive the robot given a chassis field oriented velocity.
      Parameters:
      velocity - Velocity according to the field.
      See Also:
    • driveFieldOrientedCommand

      public Command driveFieldOrientedCommand(Supplier<ChassisSpeeds> velocity)
      Drive the robot given a chassis field oriented velocity.
      Parameters:
      velocity - Velocity according to the field.
      Returns:
      Command to run
      See Also:
    • driveFieldOrientedAngularVelocityControllerCommand

      public Command driveFieldOrientedAngularVelocityControllerCommand(CommandXboxController controller)
      Parameters:
      controller - Controller to use.
      Returns:
      Command to run.
    • driveFieldOrientedDirectAngleControllerCommand

      public Command driveFieldOrientedDirectAngleControllerCommand(CommandXboxController controller)
      Parameters:
      controller - Controller to use.
      Returns:
      Command to run.
    • driveFieldOrientedDirectAngleSimControllerCommand

      public Command driveFieldOrientedDirectAngleSimControllerCommand(CommandXboxController controller)
      Parameters:
      controller - Controller to use.
      Returns:
      Command to run.
    • setChassisSpeeds

      public void setChassisSpeeds(ChassisSpeeds chassisSpeeds)
      Set chassis speeds with closed-loop velocity control.
      Parameters:
      chassisSpeeds - Chassis Speeds to set.
      See Also: