Package frc.robot.subsystems
Class Drivetrain
java.lang.Object
edu.wpi.first.wpilibj2.command.SubsystemBase
frc.robot.subsystems.Drivetrain
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoiddoVisionUpdates(boolean doVisionUpdates) voiddrive(Translation2d translation, double rotation, boolean fieldRelative) The primary method for controlling the drivebase.voiddrive(ChassisSpeeds velocity) Drive according to the chassis robot oriented velocity.driveCommand(DoubleSupplier translationX, DoubleSupplier translationY, DoubleSupplier angularRotationX) Command to drive the robot using translative values and heading as angular velocity.driveCommand(DoubleSupplier translationX, DoubleSupplier translationY, DoubleSupplier headingX, DoubleSupplier headingY) Command to drive the robot using translative values and heading as a setpoint.voiddriveFieldOriented(ChassisSpeeds velocity) Drive the robot given a chassis field oriented velocity.driveToPose(Pose2d pose) getAutonomousCommand(String pathName, boolean setOdomToStart) Get the path follower with events.doubleGets the current field-relative velocity (x, y and omega) of the robotGets the current yaw angle of the robot, as reported by the imu.Get the swerve drive kinematics object.doublegetPitch()Gets the current pitch angle of the robot, as reported by the imu.getPose()Gets the current pose (position and rotation) of the robot, as reported by odometry.Gets the current velocity (x, y and omega) of the robotgetRoll()Gets the current roll angle of the robot, as reported by the imu.swervelib.SwerveControllerGet theSwerveControllerin the swerve drive.swervelib.parser.SwerveDriveConfigurationGet theSwerveDriveConfigurationobject.getTargetSpeeds(double xInput, double yInput, double thetaInput) getTargetSpeeds(double xInput, double yInput, double headingX, double headingY) Get the chassis speeds based on controller input of 2 joysticks.getTargetSpeeds(double xInput, double yInput, Rotation2d angle) Get the chassis speeds based on controller input of 1 joystick and one angle.voidlock()Lock the swerve drive to prevent it from moving.voidperiodic()voidpostTrajectory(Trajectory trajectory) Post the trajectory to the field.voidvoidresetOdometry(Pose2d initialHolonomicPose) Resets odometry to the given pose.voidsetChassisSpeeds(ChassisSpeeds chassisSpeeds) Set chassis speeds with closed-loop velocity control.voidsetHeadingCorrection(boolean doHeadingCorrection) voidsetMotorBrake(boolean brake) Sets the drive motors to brake/coast mode.voidSetup AutoBuilder for PathPlanner.simDriveCommand(DoubleSupplier translationX, DoubleSupplier translationY, DoubleSupplier rotation) Command to drive the robot using translative values and heading as a setpoint.voidturnToAngleCommand(Rotation2d angle) voidzeroGyro()Resets the gyro angle to zero and resets odometry to the same position, but facing toward 0.Methods inherited from class edu.wpi.first.wpilibj2.command.SubsystemBase
addChild, getName, getSubsystem, initSendable, setName, setSubsystemMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface edu.wpi.first.wpilibj2.command.Subsystem
defer, getCurrentCommand, getDefaultCommand, register, removeDefaultCommand, run, runEnd, runOnce, setDefaultCommand, startEnd, startRun
-
Constructor Details
-
Drivetrain
public Drivetrain()InitializeSwerveDrivewith the directory provided.
-
-
Method Details
-
setupPathPlanner
public void setupPathPlanner()Setup AutoBuilder for PathPlanner. -
getAutonomousCommand
Get the path follower with events.- Parameters:
pathName- PathPlanner path name.setOdomToStart- Set the odometry position to the start of the path.- Returns:
AutoBuilder.followPath(PathPlannerPath)path command.
-
driveToPose
-
driveCommand
public Command driveCommand(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:
- Drive command.
-
simDriveCommand
public Command simDriveCommand(DoubleSupplier translationX, DoubleSupplier translationY, DoubleSupplier rotation) Command to drive the robot using translative values and heading as a setpoint.- Parameters:
translationX- Translation in the X direction.translationY- Translation in the Y direction.rotation- Rotation as a value between [-1, 1] converted to radians.- Returns:
- Drive command.
-
driveCommand
public Command driveCommand(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:
- Drive command.
-
turnToAngleCommand
-
drive
The primary method for controlling the drivebase. Takes aTranslation2dand 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-Translation2dthat 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.
-
driveFieldOriented
Drive the robot given a chassis field oriented velocity.- Parameters:
velocity- Velocity according to the field.
-
drive
Drive according to the chassis robot oriented velocity.- Parameters:
velocity- Robot orientedChassisSpeeds
-
periodic
public void periodic() -
simulationPeriodic
public void simulationPeriodic() -
getKinematics
Get the swerve drive kinematics object.- Returns:
SwerveDriveKinematicsof the swerve drive.
-
resetOdometry
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
-
getPose
Gets the current pose (position and rotation) of the robot, as reported by odometry.- Returns:
- The robot's pose
-
setChassisSpeeds
Set chassis speeds with closed-loop velocity control.- Parameters:
chassisSpeeds- Chassis Speeds to set.
-
postTrajectory
Post the trajectory to the field.- Parameters:
trajectory- The trajectory to post.
-
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.
-
zeroGyro
public void zeroGyro()Resets the gyro angle to zero and resets odometry to the same position, but facing toward 0. -
resetPose
-
doVisionUpdates
public void doVisionUpdates(boolean doVisionUpdates) -
getTargetSpeeds
public ChassisSpeeds getTargetSpeeds(double xInput, double yInput, double headingX, double headingY) Get the chassis speeds based on controller input of 2 joysticks. One for speeds in which direction. The other for the angle of the robot.- Parameters:
xInput- X joystick input for the robot to move in the X direction.yInput- Y joystick input for the robot to move in the Y direction.headingX- X joystick which controls the angle of the robot.headingY- Y joystick which controls the angle of the robot.- Returns:
ChassisSpeedswhich can be sent to th Swerve Drive.
-
getHeading
Gets the current yaw angle of the robot, as reported by the imu. CCW positive, not wrapped.- Returns:
- The yaw angle
-
setHeadingCorrection
public void setHeadingCorrection(boolean doHeadingCorrection) -
getTargetSpeeds
-
getTargetSpeeds
Get the chassis speeds based on controller input of 1 joystick and one angle. Control the robot at an offset of 90deg.- Parameters:
xInput- X joystick input for the robot to move in the X direction.yInput- Y joystick input for the robot to move in the Y direction.angle- The angle in as aRotation2d.- Returns:
ChassisSpeedswhich can be sent to th Swerve Drive.
-
getFieldVelocity
Gets the current field-relative velocity (x, y and omega) of the robot- Returns:
- A ChassisSpeeds object of the current field-relative velocity
-
getRobotVelocity
Gets the current velocity (x, y and omega) of the robot- Returns:
- A
ChassisSpeedsobject of the current velocity
-
getSwerveController
public swervelib.SwerveController getSwerveController()Get theSwerveControllerin the swerve drive.- Returns:
SwerveControllerfrom theSwerveDrive.
-
getMaximumVelocity
public double getMaximumVelocity() -
getSwerveDriveConfiguration
public swervelib.parser.SwerveDriveConfiguration getSwerveDriveConfiguration()Get theSwerveDriveConfigurationobject.- Returns:
- The
SwerveDriveConfigurationfpr the current drive.
-
lock
public void lock()Lock the swerve drive to prevent it from moving. -
getPitch
Gets the current pitch angle of the robot, as reported by the imu.- Returns:
- The heading as a
Rotation2dangle
-
resetLastAngeScalar
public void resetLastAngeScalar() -
getRoll
Gets the current roll angle of the robot, as reported by the imu.- Returns:
- The heading as a
Rotation2dangle
-
getDistanceToSpeaker
public double getDistanceToSpeaker()
-