|
| 1 | +// Copyright (c) FIRST and other WPILib contributors. |
| 2 | +// Open Source Software; you can modify and/or share it under the terms of |
| 3 | +// the WPILib BSD license file in the root directory of this project. |
| 4 | + |
| 5 | +package frc.robot.subsystems; |
| 6 | + |
| 7 | +import java.lang.reflect.Array; |
| 8 | + |
| 9 | +import com.ctre.phoenix6.hardware.CANcoder; |
| 10 | +import com.ctre.phoenix6.hardware.Pigeon2; |
| 11 | +import com.ctre.phoenix6.hardware.TalonFX; |
| 12 | + |
| 13 | +import edu.wpi.first.math.kinematics.ChassisSpeeds; |
| 14 | +import edu.wpi.first.math.kinematics.SwerveDriveKinematics; |
| 15 | +import edu.wpi.first.math.kinematics.SwerveModuleState; |
| 16 | +import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard; |
| 17 | +import edu.wpi.first.wpilibj2.command.SubsystemBase; |
| 18 | +import frc.robot.constants.ConstantsEverything; |
| 19 | + |
| 20 | +public class SwerveBase extends SubsystemBase { |
| 21 | + /** Creates a new SwerveBase. */ |
| 22 | + private final SwerveModule rightFront; |
| 23 | + private final SwerveModule leftFront; |
| 24 | + private final SwerveModule rightBack; |
| 25 | + private final SwerveModule leftBack; |
| 26 | + private final Pigeon2 gyroscope; |
| 27 | + |
| 28 | + public SwerveBase() { |
| 29 | + rightFront = new SwerveModule(new TalonFX(ConstantsEverything.rightFrontMoveMotor), new TalonFX(ConstantsEverything.rightFrontTurnMotor), new CANcoder(ConstantsEverything.rightFrontCanCoder), 0); |
| 30 | + leftFront = new SwerveModule(new TalonFX(ConstantsEverything.leftFrontMoveMotor), new TalonFX(ConstantsEverything.leftFrontTurnMotor), new CANcoder(ConstantsEverything.leftFrontCanCoder), 0); |
| 31 | + rightBack = new SwerveModule(new TalonFX(ConstantsEverything.rightBackMoveMotor), new TalonFX(ConstantsEverything.rightBackTurnMotor), new CANcoder(ConstantsEverything.rightBackCanCoder), 0); |
| 32 | + leftBack = new SwerveModule(new TalonFX(ConstantsEverything.leftBackMoveMotor), new TalonFX(ConstantsEverything.leftBackTurnMotor), new CANcoder(ConstantsEverything.leftBackCanCoder), 0); |
| 33 | + |
| 34 | + gyroscope = new Pigeon2(ConstantsEverything.gyroscopeCanID); |
| 35 | + |
| 36 | + } |
| 37 | + public void coast(){ |
| 38 | + rightFront.coast(); |
| 39 | + leftFront.coast(); |
| 40 | + rightBack.coast(); |
| 41 | + leftBack.coast(); |
| 42 | + } |
| 43 | + |
| 44 | + public void brake(){ |
| 45 | + rightFront.brake(); |
| 46 | + leftFront.brake(); |
| 47 | + rightBack.brake(); |
| 48 | + leftBack.brake(); |
| 49 | + } |
| 50 | + |
| 51 | + public void setSwerveModulesStates(SwerveModuleState[] swerveModuleStates){ |
| 52 | + rightFront.setSwerveState(swerveModuleStates[0]); |
| 53 | + leftFront.setSwerveState(swerveModuleStates[1]); |
| 54 | + rightBack.setSwerveState(swerveModuleStates[2]); |
| 55 | + leftBack.setSwerveState(swerveModuleStates[3]); |
| 56 | + } |
| 57 | + |
| 58 | + public void setSwerveBaseSpeed(ChassisSpeeds chassisSpeeds){ |
| 59 | + setSwerveModulesStates(new SwerveDriveKinematics().toSwerveModuleStates(chassisSpeeds)); |
| 60 | + } |
| 61 | + |
| 62 | + public void setSwerveDrive(double velocityY, double velocityX, double rX){ |
| 63 | + setSwerveBaseSpeed(ChassisSpeeds.fromFieldRelativeSpeeds(velocityX, velocityY, rX, gyroscope.getRotation2d())); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public void periodic() { |
| 68 | + // This method will be called once per scheduler run |
| 69 | + |
| 70 | + |
| 71 | + SmartDashboard.putNumber("Right Front Speed", rightFront.getVelocity()); |
| 72 | + SmartDashboard.putNumber("Left Front Speed", leftFront.getVelocity()); |
| 73 | + SmartDashboard.putNumber("Right Back Speed", rightBack.getVelocity()); |
| 74 | + SmartDashboard.putNumber("Left Back Speed", leftBack.getVelocity()); |
| 75 | + |
| 76 | + |
| 77 | + SmartDashboard.putNumber("Right Front Angle", rightFront.getAngle()); |
| 78 | + SmartDashboard.putNumber("Left Front Angle", leftFront.getAngle()); |
| 79 | + SmartDashboard.putNumber("Right Back Angle", rightBack.getAngle()); |
| 80 | + SmartDashboard.putNumber("Left Back Angle", leftBack.getAngle()); |
| 81 | + } |
| 82 | +} |
0 commit comments