Package frc.robot.subsystems.swervedrive
Class SwerveSubsystem
java.lang.Object
edu.wpi.first.wpilibj2.command.SubsystemBase
frc.robot.subsystems.swervedrive.SwerveSubsystem
- All Implemented Interfaces:
edu.wpi.first.util.sendable.Sendable,edu.wpi.first.wpilibj2.command.Subsystem
public class SwerveSubsystem
extends edu.wpi.first.wpilibj2.command.SubsystemBase
-
Field Summary
FieldsModifier and TypeFieldDescriptiondoubleMaximum speed of the robot in meters per second, used to limit acceleration. -
Constructor Summary
ConstructorsConstructorDescriptionSwerveSubsystem(File directory) InitializeSwerveDrivewith the directory provided.SwerveSubsystem(SwerveDriveConfiguration driveCfg, SwerveControllerConfiguration controllerCfg) Construct the swerve drive. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdd a fake vision reading for testing purposes.voiddrive(edu.wpi.first.math.geometry.Translation2d translation, double rotation, boolean fieldRelative) The primary method for controlling the drivebase.voiddrive(edu.wpi.first.math.kinematics.ChassisSpeeds velocity) Drive according to the chassis robot oriented velocity.voiddriveFieldOriented(edu.wpi.first.math.kinematics.ChassisSpeeds velocity) Drive the robot given a chassis field oriented velocity.edu.wpi.first.wpilibj2.command.CommandgetAutonomousCommand(String pathName, boolean setOdomToStart) Get the path follower with events.edu.wpi.first.math.kinematics.ChassisSpeedsGets the current field-relative velocity (x, y and omega) of the robotedu.wpi.first.math.geometry.Rotation2dGets the current yaw angle of the robot, as reported by the imu.edu.wpi.first.math.kinematics.SwerveDriveKinematicsGet the swerve drive kinematics object.edu.wpi.first.math.geometry.Rotation2dgetPitch()Gets the current pitch angle of the robot, as reported by the imu.edu.wpi.first.math.geometry.Pose2dgetPose()Gets the current pose (position and rotation) of the robot, as reported by odometry.edu.wpi.first.math.kinematics.ChassisSpeedsGets the current velocity (x, y and omega) of the robotGet theSwerveControllerin the swerve drive.Get theSwerveDriveConfigurationobject.edu.wpi.first.math.kinematics.ChassisSpeedsgetTargetSpeeds(double xInput, double yInput, double headingX, double headingY) Get the chassis speeds based on controller input of 2 joysticks.edu.wpi.first.math.kinematics.ChassisSpeedsgetTargetSpeeds(double xInput, double yInput, edu.wpi.first.math.geometry.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(edu.wpi.first.math.trajectory.Trajectory trajectory) Post the trajectory to the field.voidresetOdometry(edu.wpi.first.math.geometry.Pose2d initialHolonomicPose) Resets odometry to the given pose.voidsetChassisSpeeds(edu.wpi.first.math.kinematics.ChassisSpeeds chassisSpeeds) Set chassis speeds with closed-loop velocity control.voidsetMotorBrake(boolean brake) Sets the drive motors to brake/coast mode.voidSetup AutoBuilder for PathPlanner.voidvoidzeroGyro()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
-
Field Details
-
maximumSpeed
public double maximumSpeedMaximum speed of the robot in meters per second, used to limit acceleration.
-
-
Constructor Details
-
SwerveSubsystem
InitializeSwerveDrivewith the directory provided.- Parameters:
directory- Directory of swerve drive config files.
-
SwerveSubsystem
public SwerveSubsystem(SwerveDriveConfiguration driveCfg, SwerveControllerConfiguration controllerCfg) Construct the swerve drive.- Parameters:
driveCfg- SwerveDriveConfiguration for the swerve.controllerCfg- Swerve Controller.
-
-
Method Details
-
setupPathPlanner
public void setupPathPlanner()Setup AutoBuilder for PathPlanner. -
getAutonomousCommand
public edu.wpi.first.wpilibj2.command.Command getAutonomousCommand(String pathName, boolean setOdomToStart) 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.
-
drive
public void drive(edu.wpi.first.math.geometry.Translation2d translation, double rotation, boolean fieldRelative) 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
public void driveFieldOriented(edu.wpi.first.math.kinematics.ChassisSpeeds velocity) Drive the robot given a chassis field oriented velocity.- Parameters:
velocity- Velocity according to the field.
-
drive
public void drive(edu.wpi.first.math.kinematics.ChassisSpeeds velocity) Drive according to the chassis robot oriented velocity.- Parameters:
velocity- Robot orientedChassisSpeeds
-
periodic
public void periodic() -
simulationPeriodic
public void simulationPeriodic() -
getKinematics
public edu.wpi.first.math.kinematics.SwerveDriveKinematics getKinematics()Get the swerve drive kinematics object.- Returns:
SwerveDriveKinematicsof the swerve drive.
-
resetOdometry
public void resetOdometry(edu.wpi.first.math.geometry.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
-
getPose
public edu.wpi.first.math.geometry.Pose2d getPose()Gets the current pose (position and rotation) of the robot, as reported by odometry.- Returns:
- The robot's pose
-
setChassisSpeeds
public void setChassisSpeeds(edu.wpi.first.math.kinematics.ChassisSpeeds chassisSpeeds) Set chassis speeds with closed-loop velocity control.- Parameters:
chassisSpeeds- Chassis Speeds to set.
-
postTrajectory
public void postTrajectory(edu.wpi.first.math.trajectory.Trajectory trajectory) Post the trajectory to the field.- Parameters:
trajectory- The trajectory to post.
-
zeroGyro
public void zeroGyro()Resets the gyro angle to zero and resets odometry to the same position, but facing toward 0. -
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.
-
getHeading
public edu.wpi.first.math.geometry.Rotation2d getHeading()Gets the current yaw angle of the robot, as reported by the imu. CCW positive, not wrapped.- Returns:
- The yaw angle
-
getTargetSpeeds
public edu.wpi.first.math.kinematics.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.
-
getTargetSpeeds
public edu.wpi.first.math.kinematics.ChassisSpeeds getTargetSpeeds(double xInput, double yInput, edu.wpi.first.math.geometry.Rotation2d angle) Get the chassis speeds based on controller input of 1 joystick and one angle.- 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
public edu.wpi.first.math.kinematics.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
-
getRobotVelocity
public edu.wpi.first.math.kinematics.ChassisSpeeds getRobotVelocity()Gets the current velocity (x, y and omega) of the robot- Returns:
- A
ChassisSpeedsobject of the current velocity
-
getSwerveController
Get theSwerveControllerin the swerve drive.- Returns:
SwerveControllerfrom theSwerveDrive.
-
getSwerveDriveConfiguration
Get theSwerveDriveConfigurationobject.- Returns:
- The
SwerveDriveConfigurationfpr the current drive.
-
lock
public void lock()Lock the swerve drive to prevent it from moving. -
getPitch
public edu.wpi.first.math.geometry.Rotation2d getPitch()Gets the current pitch angle of the robot, as reported by the imu.- Returns:
- The heading as a
Rotation2dangle
-
addFakeVisionReading
public void addFakeVisionReading()Add a fake vision reading for testing purposes.
-