diff --git a/wpilibcExamples/src/main/cpp/examples/SwerveBot/cpp/SwerveModule.cpp b/wpilibcExamples/src/main/cpp/examples/SwerveBot/cpp/SwerveModule.cpp index 8783385e91..fb07c15050 100644 --- a/wpilibcExamples/src/main/cpp/examples/SwerveBot/cpp/SwerveModule.cpp +++ b/wpilibcExamples/src/main/cpp/examples/SwerveBot/cpp/SwerveModule.cpp @@ -8,8 +8,15 @@ #include SwerveModule::SwerveModule(const int driveMotorChannel, - const int turningMotorChannel) - : m_driveMotor(driveMotorChannel), m_turningMotor(turningMotorChannel) { + const int turningMotorChannel, + const int driveEncoderChannelA, + const int driveEncoderChannelB, + const int turningEncoderChannelA, + const int turningEncoderChannelB) + : m_driveMotor(driveMotorChannel), + m_turningMotor(turningMotorChannel), + m_driveEncoder(driveEncoderChannelA, driveEncoderChannelB), + m_turningEncoder(turningEncoderChannelA, turningEncoderChannelB) { // Set the distance per pulse for the drive encoder. We can simply use the // distance traveled for one rotation of the wheel divided by the encoder // resolution. diff --git a/wpilibcExamples/src/main/cpp/examples/SwerveBot/include/Drivetrain.h b/wpilibcExamples/src/main/cpp/examples/SwerveBot/include/Drivetrain.h index 6a60b974b2..496ba7f39c 100644 --- a/wpilibcExamples/src/main/cpp/examples/SwerveBot/include/Drivetrain.h +++ b/wpilibcExamples/src/main/cpp/examples/SwerveBot/include/Drivetrain.h @@ -35,10 +35,10 @@ class Drivetrain { frc::Translation2d m_backLeftLocation{-0.381_m, +0.381_m}; frc::Translation2d m_backRightLocation{-0.381_m, -0.381_m}; - SwerveModule m_frontLeft{1, 2}; - SwerveModule m_frontRight{2, 3}; - SwerveModule m_backLeft{5, 6}; - SwerveModule m_backRight{7, 8}; + SwerveModule m_frontLeft{1, 2, 0, 1, 2, 3}; + SwerveModule m_frontRight{2, 3, 4, 5, 6, 7}; + SwerveModule m_backLeft{5, 6, 8, 9, 10, 11}; + SwerveModule m_backRight{7, 8, 12, 13, 14, 15}; frc::AnalogGyro m_gyro{0}; diff --git a/wpilibcExamples/src/main/cpp/examples/SwerveBot/include/SwerveModule.h b/wpilibcExamples/src/main/cpp/examples/SwerveBot/include/SwerveModule.h index 3c3afa697a..6c6db533d1 100644 --- a/wpilibcExamples/src/main/cpp/examples/SwerveBot/include/SwerveModule.h +++ b/wpilibcExamples/src/main/cpp/examples/SwerveBot/include/SwerveModule.h @@ -18,7 +18,9 @@ class SwerveModule { public: - SwerveModule(int driveMotorChannel, int turningMotorChannel); + SwerveModule(int driveMotorChannel, int turningMotorChannel, + int driveEncoderChannelA, int driveEncoderChannelB, + int turningEncoderChannelA, int turningEncoderChannelB); frc::SwerveModuleState GetState() const; void SetDesiredState(const frc::SwerveModuleState& state); diff --git a/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/swervebot/Drivetrain.java b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/swervebot/Drivetrain.java index acfff2112e..3575266217 100644 --- a/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/swervebot/Drivetrain.java +++ b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/swervebot/Drivetrain.java @@ -20,10 +20,10 @@ public class Drivetrain { private final Translation2d m_backLeftLocation = new Translation2d(-0.381, 0.381); private final Translation2d m_backRightLocation = new Translation2d(-0.381, -0.381); - private final SwerveModule m_frontLeft = new SwerveModule(1, 2); - private final SwerveModule m_frontRight = new SwerveModule(3, 4); - private final SwerveModule m_backLeft = new SwerveModule(5, 6); - private final SwerveModule m_backRight = new SwerveModule(7, 8); + private final SwerveModule m_frontLeft = new SwerveModule(1, 2, 0, 1, 2, 3); + private final SwerveModule m_frontRight = new SwerveModule(3, 4, 4, 5, 6, 7); + private final SwerveModule m_backLeft = new SwerveModule(5, 6, 8, 9, 10, 11); + private final SwerveModule m_backRight = new SwerveModule(7, 8, 12, 13, 14, 15); private final AnalogGyro m_gyro = new AnalogGyro(0); diff --git a/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/swervebot/SwerveModule.java b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/swervebot/SwerveModule.java index 9fcf669d23..98c636fd2f 100644 --- a/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/swervebot/SwerveModule.java +++ b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/swervebot/SwerveModule.java @@ -25,11 +25,13 @@ public class SwerveModule { private final MotorController m_driveMotor; private final MotorController m_turningMotor; - private final Encoder m_driveEncoder = new Encoder(0, 1); - private final Encoder m_turningEncoder = new Encoder(2, 3); + private final Encoder m_driveEncoder; + private final Encoder m_turningEncoder; + // Gains are for example purposes only - must be determined for your own robot! private final PIDController m_drivePIDController = new PIDController(1, 0, 0); + // Gains are for example purposes only - must be determined for your own robot! private final ProfiledPIDController m_turningPIDController = new ProfiledPIDController( 1, @@ -43,15 +45,28 @@ public class SwerveModule { private final SimpleMotorFeedforward m_turnFeedforward = new SimpleMotorFeedforward(1, 0.5); /** - * Constructs a SwerveModule. + * Constructs a SwerveModule with a drive motor, turning motor, drive encoder and turning encoder. * - * @param driveMotorChannel ID for the drive motor. - * @param turningMotorChannel ID for the turning motor. + * @param driveMotorChannel PWM output for the drive motor. + * @param turningMotorChannel PWM output for the turning motor. + * @param driveEncoderChannelA DIO input for the drive encoder channel A + * @param driveEncoderChannelB DIO input for the drive encoder channel B + * @param turningEncoderChannelA DIO input for the turning encoder channel A + * @param turningEncoderChannelB DIO input for the turning encoder channel B */ - public SwerveModule(int driveMotorChannel, int turningMotorChannel) { + public SwerveModule( + int driveMotorChannel, + int turningMotorChannel, + int driveEncoderChannelA, + int driveEncoderChannelB, + int turningEncoderChannelA, + int turningEncoderChannelB) { m_driveMotor = new PWMSparkMax(driveMotorChannel); m_turningMotor = new PWMSparkMax(turningMotorChannel); + m_driveEncoder = new Encoder(driveEncoderChannelA, driveEncoderChannelB); + m_turningEncoder = new Encoder(turningEncoderChannelA, turningEncoderChannelB); + // Set the distance per pulse for the drive encoder. We can simply use the // distance traveled for one rotation of the wheel divided by the encoder // resolution.