Package frc.robot.subsystems.swervedrive2.swervelib

## Class SwerveModule

- java.lang.Object
- - frc.robot.subsystems.swervedrive2.swervelib.SwerveModule

-     public class SwerveModule
      extends Object

  The Swerve Module class which represents and controls Swerve Modules for the swerve drive.

- - ### Field Summary

    Fields
    Modifier and Type | Field | Description
    --- | --- | ---
    `private SwerveAbsoluteEncoder` | `absoluteEncoder` | Absolute encoder for swerve drive.
    `double` | `angle` | Current state.
    `private SwerveMotor` | `angleMotor` | Swerve Motors.
    `private double` | `angleOffset` | Angle offset from the absolute encoder.
    `SwerveModuleConfiguration` | `configuration` | Swerve module configuration options.
    `private SwerveMotor` | `driveMotor` | Swerve Motors.
    `double` | `dt` | Current state.
    `double` | `fakePos` | Current state.
    `edu.wpi.first.math.controller.SimpleMotorFeedforward` | `feedforward` | Feedforward for drive motor during closed loop control.
    `double` | `lastAngle` | Last angle set for the swerve module.
    `double` | `lastTime` | Current state.
    `int` | `moduleNumber` | Module number for kinematics, usually 0 to 3.
    `double` | `omega` | Current state.
    `double` | `speed` | Current state.
    `private edu.wpi.first.wpilibj.Timer` | `time` | Timer for simulation.

  - ### Constructor Summary

    Constructors
    Constructor | Description
    --- | ---
    `SwerveModule​(int moduleNumber, SwerveModuleConfiguration moduleConfiguration)` | Construct the swerve module and initialize the swerve module motors and absolute encoder.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method | Description
    --- | --- | ---
    `double` | `getCANCoder()` | Get the CANCoder absolute position.
    `edu.wpi.first.math.kinematics.SwerveModulePosition` | `getPosition()` |
    `double` | `getRelativeEncoder()` | Get the relative encoder angle in degrees.
    `SwerveModuleState2` | `getState()` | Get the Swerve Module state.
    `void` | `setAngle​(double angle)` | Set the angle for the module.
    `void` | `setDesiredState​(SwerveModuleState2 desiredState, boolean isOpenLoop)` | Set the desired state of the swerve module.
    `void` | `setMotorBrake​(boolean brake)` | Set the brake mode.
    `void` | `synchronizeEncoders()` | Synchronize the integrated angle encoder with the absolute encoder.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### configuration

          public final SwerveModuleConfiguration configuration

      Swerve module configuration options.

    - #### angleOffset

          private final double angleOffset

      Angle offset from the absolute encoder.

    - #### angleMotor

          private final SwerveMotor angleMotor

      Swerve Motors.

    - #### driveMotor

          private final SwerveMotor driveMotor

      Swerve Motors.

    - #### absoluteEncoder

          private final SwerveAbsoluteEncoder absoluteEncoder

      Absolute encoder for swerve drive.

    - #### moduleNumber

          public int moduleNumber

      Module number for kinematics, usually 0 to 3. front left -> front right -> back left -> back right.

    - #### feedforward

          public edu.wpi.first.math.controller.SimpleMotorFeedforward feedforward

      Feedforward for drive motor during closed loop control.

    - #### lastAngle

          public double lastAngle

      Last angle set for the swerve module.

    - #### angle

          public double angle

      Current state.

    - #### omega

          public double omega

      Current state.

    - #### speed

          public double speed

      Current state.

    - #### fakePos

          public double fakePos

      Current state.

    - #### lastTime

          public double lastTime

      Current state.

    - #### dt

          public double dt

      Current state.

    - #### time

          private edu.wpi.first.wpilibj.Timer time

      Timer for simulation.

  - ### Constructor Detail

    - #### SwerveModule

          public SwerveModule​(int moduleNumber,
                              SwerveModuleConfiguration moduleConfiguration)

      Construct the swerve module and initialize the swerve module motors and absolute encoder.
      Parameters:
        `moduleNumber` - Module number for kinematics.
        `moduleConfiguration` - Module constants containing CAN ID's and offsets.

  - ### Method Detail

    - #### synchronizeEncoders

          public void synchronizeEncoders()

      Synchronize the integrated angle encoder with the absolute encoder.

    - #### setDesiredState

          public void setDesiredState​(SwerveModuleState2 desiredState,
                                      boolean isOpenLoop)

      Set the desired state of the swerve module.
      Parameters:
        `desiredState` - Desired swerve module state.
        `isOpenLoop` - Whether to use open loop (direct percent) or direct velocity control.

    - #### setAngle

          public void setAngle​(double angle)

      Set the angle for the module.
      Parameters:
        `angle` - Angle in degrees.

    - #### getState

          public SwerveModuleState2 getState()

      Get the Swerve Module state.
      Returns:
        Current SwerveModule state.

    - #### getPosition

          public edu.wpi.first.math.kinematics.SwerveModulePosition getPosition()

    - #### getCANCoder

          public double getCANCoder()

      Get the CANCoder absolute position.
      Returns:
        Absolute encoder angle in degrees.

    - #### getRelativeEncoder

          public double getRelativeEncoder()

      Get the relative encoder angle in degrees.
      Returns:
        Angle in degrees.

    - #### setMotorBrake

          public void setMotorBrake​(boolean brake)

      Set the brake mode.
      Parameters:
        `brake` - Set the brake mode.