2022-01-11 11:54:44 -07:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2021-11-15 17:52:28 -07:00
|
|
|
|
|
|
|
|
package frc4388.robot.subsystems;
|
|
|
|
|
|
2022-01-15 13:12:31 -07:00
|
|
|
import java.nio.ByteBuffer;
|
2022-01-14 21:02:15 -07:00
|
|
|
import java.util.Arrays;
|
|
|
|
|
|
2021-11-29 17:18:31 -07:00
|
|
|
import com.ctre.phoenix.motorcontrol.can.WPI_TalonFX;
|
2021-12-02 17:24:41 -07:00
|
|
|
import com.ctre.phoenix.sensors.CANCoder;
|
2022-01-14 17:48:53 -07:00
|
|
|
import com.ctre.phoenix.sensors.WPI_PigeonIMU;
|
2021-11-29 17:18:31 -07:00
|
|
|
|
2022-01-14 21:02:15 -07:00
|
|
|
import edu.wpi.first.hal.HAL;
|
2022-01-11 11:05:52 -07:00
|
|
|
import edu.wpi.first.math.geometry.Rotation2d;
|
|
|
|
|
import edu.wpi.first.math.geometry.Translation2d;
|
|
|
|
|
import edu.wpi.first.math.kinematics.ChassisSpeeds;
|
|
|
|
|
import edu.wpi.first.math.kinematics.SwerveDriveKinematics;
|
2022-01-15 14:55:07 -07:00
|
|
|
import edu.wpi.first.math.kinematics.SwerveDriveOdometry;
|
2022-01-11 11:05:52 -07:00
|
|
|
import edu.wpi.first.math.kinematics.SwerveModuleState;
|
|
|
|
|
import edu.wpi.first.math.util.Units;
|
|
|
|
|
import edu.wpi.first.wpilibj.interfaces.Gyro;
|
2022-01-11 19:57:50 -07:00
|
|
|
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
|
2021-12-02 17:51:06 -07:00
|
|
|
import edu.wpi.first.wpilibj2.command.SubsystemBase;
|
2021-11-15 17:52:28 -07:00
|
|
|
import frc4388.robot.Constants.SwerveDriveConstants;
|
2021-12-16 16:22:36 -07:00
|
|
|
import frc4388.utility.Gains;
|
2022-01-15 11:31:15 -07:00
|
|
|
import frc4388.utility.controller.XboxController;
|
2022-01-11 11:05:52 -07:00
|
|
|
|
|
|
|
|
public class SwerveDrive extends SubsystemBase {
|
|
|
|
|
SwerveDriveKinematics m_kinematics;
|
|
|
|
|
private WPI_TalonFX m_leftFrontSteerMotor;
|
|
|
|
|
private WPI_TalonFX m_leftFrontWheelMotor;
|
|
|
|
|
private WPI_TalonFX m_rightFrontSteerMotor;
|
|
|
|
|
private WPI_TalonFX m_rightFrontWheelMotor;
|
|
|
|
|
private WPI_TalonFX m_leftBackSteerMotor;
|
|
|
|
|
private WPI_TalonFX m_leftBackWheelMotor;
|
|
|
|
|
private WPI_TalonFX m_rightBackSteerMotor;
|
|
|
|
|
private WPI_TalonFX m_rightBackWheelMotor;
|
2022-01-11 19:57:50 -07:00
|
|
|
private CANCoder m_leftFrontEncoder;
|
2022-01-11 11:05:52 -07:00
|
|
|
private CANCoder m_rightFrontEncoder;
|
|
|
|
|
private CANCoder m_leftBackEncoder;
|
2022-01-11 19:57:50 -07:00
|
|
|
private CANCoder m_rightBackEncoder;
|
2022-01-11 11:05:52 -07:00
|
|
|
double halfWidth = SwerveDriveConstants.WIDTH / 2.d;
|
|
|
|
|
double halfHeight = SwerveDriveConstants.HEIGHT / 2.d;
|
|
|
|
|
public static Gains m_swerveGains = SwerveDriveConstants.SWERVE_GAINS;
|
|
|
|
|
|
|
|
|
|
|
2022-01-12 17:04:58 -07:00
|
|
|
Translation2d m_frontLeftLocation = new Translation2d(Units.inchesToMeters(halfHeight), Units.inchesToMeters(halfWidth));
|
|
|
|
|
Translation2d m_frontRightLocation = new Translation2d(Units.inchesToMeters(halfHeight), Units.inchesToMeters(-halfWidth));
|
|
|
|
|
Translation2d m_backLeftLocation = new Translation2d(Units.inchesToMeters(-halfHeight), Units.inchesToMeters(halfWidth));
|
|
|
|
|
Translation2d m_backRightLocation = new Translation2d(Units.inchesToMeters(-halfHeight), Units.inchesToMeters(-halfWidth));
|
|
|
|
|
// setSwerveGains();
|
2022-01-11 19:57:50 -07:00
|
|
|
|
2022-01-11 11:05:52 -07:00
|
|
|
private SwerveDriveKinematics kinematics = new SwerveDriveKinematics(m_frontLeftLocation, m_frontRightLocation, m_backLeftLocation, m_backRightLocation);
|
|
|
|
|
public SwerveModule[] modules;
|
2022-01-14 17:48:53 -07:00
|
|
|
public WPI_PigeonIMU m_gyro; //TODO Add Gyro Lol
|
2022-01-11 19:57:50 -07:00
|
|
|
|
2022-01-15 14:55:07 -07:00
|
|
|
public double speedAdjust = SwerveDriveConstants.JOYSTICK_TO_METERS_PER_SECOND_SLOW;
|
|
|
|
|
public boolean ignoreAngles;
|
|
|
|
|
|
2022-01-11 19:57:50 -07:00
|
|
|
public SwerveDrive(WPI_TalonFX leftFrontSteerMotor,WPI_TalonFX leftFrontWheelMotor,WPI_TalonFX rightFrontSteerMotor,WPI_TalonFX rightFrontWheelMotor,
|
|
|
|
|
WPI_TalonFX leftBackSteerMotor,WPI_TalonFX leftBackWheelMotor,WPI_TalonFX rightBackSteerMotor,WPI_TalonFX rightBackWheelMotor, CANCoder leftFrontEncoder,
|
|
|
|
|
CANCoder rightFrontEncoder,
|
|
|
|
|
CANCoder leftBackEncoder,
|
2022-01-14 17:48:53 -07:00
|
|
|
CANCoder rightBackEncoder, WPI_PigeonIMU gyro)
|
2022-01-11 19:57:50 -07:00
|
|
|
{
|
|
|
|
|
m_leftFrontSteerMotor = leftFrontSteerMotor;
|
|
|
|
|
m_leftFrontWheelMotor = leftFrontWheelMotor;
|
|
|
|
|
m_rightFrontSteerMotor = rightFrontSteerMotor;
|
|
|
|
|
m_rightFrontWheelMotor = rightFrontWheelMotor;
|
|
|
|
|
m_leftBackSteerMotor = leftBackSteerMotor;
|
|
|
|
|
m_leftBackWheelMotor = leftBackWheelMotor;
|
|
|
|
|
m_rightBackSteerMotor = rightBackSteerMotor;
|
|
|
|
|
m_rightBackWheelMotor = rightBackWheelMotor;
|
|
|
|
|
m_leftFrontEncoder = leftFrontEncoder;
|
|
|
|
|
m_rightFrontEncoder = rightFrontEncoder;
|
|
|
|
|
m_leftBackEncoder = leftBackEncoder;
|
|
|
|
|
m_rightBackEncoder = rightBackEncoder;
|
2022-01-14 17:48:53 -07:00
|
|
|
m_gyro = gyro;
|
2022-01-11 19:57:50 -07:00
|
|
|
|
|
|
|
|
modules = new SwerveModule[] {
|
|
|
|
|
new SwerveModule(m_leftFrontWheelMotor, m_leftFrontSteerMotor, m_leftFrontEncoder, SwerveDriveConstants.LEFT_FRONT_ENCODER_OFFSET), // Front Left
|
|
|
|
|
new SwerveModule(m_rightFrontWheelMotor, m_rightFrontSteerMotor, m_rightFrontEncoder, SwerveDriveConstants.RIGHT_FRONT_ENCODER_OFFSET), // Front Right
|
|
|
|
|
new SwerveModule(m_leftBackWheelMotor, m_leftBackSteerMotor, m_leftBackEncoder, SwerveDriveConstants.LEFT_BACK_ENCODER_OFFSET), // Back Left
|
|
|
|
|
new SwerveModule(m_rightBackWheelMotor, m_rightBackSteerMotor, m_rightBackEncoder, SwerveDriveConstants.RIGHT_BACK_ENCODER_OFFSET) // Back Right
|
|
|
|
|
};
|
2022-01-14 17:48:53 -07:00
|
|
|
m_gyro.reset();
|
2022-01-11 11:05:52 -07:00
|
|
|
}
|
2022-01-11 19:57:50 -07:00
|
|
|
//https://github.com/ZachOrr/MK3-Swerve-Example
|
|
|
|
|
/**
|
|
|
|
|
* Method to drive the robot using joystick info.
|
|
|
|
|
*
|
2022-01-14 17:48:53 -07:00
|
|
|
* @param speeds[0] Speed of the robot in the x direction (forward).
|
|
|
|
|
* @param speeds[1] Speed of the robot in the y direction (sideways).
|
2022-01-11 19:57:50 -07:00
|
|
|
* @param rot Angular rate of the robot.
|
|
|
|
|
* @param fieldRelative Whether the provided x and y speeds are relative to the field.
|
|
|
|
|
*/
|
2022-01-14 17:48:53 -07:00
|
|
|
public void driveWithInput(double[] speeds, double rot, boolean fieldRelative)
|
2022-01-11 19:57:50 -07:00
|
|
|
{
|
2022-01-15 15:52:38 -07:00
|
|
|
if (speeds[0] == 0 && speeds[1] == 0 && rot == 0) ignoreAngles = true;
|
2022-01-15 14:55:07 -07:00
|
|
|
else ignoreAngles = false;
|
2022-01-15 15:52:38 -07:00
|
|
|
speeds[0] *= speeds[0] * speeds[0];
|
|
|
|
|
speeds[1] *= speeds[1] * speeds[1];
|
|
|
|
|
// // Temporary janky raw joysticks
|
|
|
|
|
// float[] driveController = new float[HAL.kMaxJoystickAxes];
|
|
|
|
|
// HAL.getJoystickAxes((byte) 0, driveController);
|
|
|
|
|
// // ByteBuffer buttons = new
|
|
|
|
|
// ByteBuffer num_buttons_buffer = ByteBuffer.allocateDirect(1);
|
|
|
|
|
// int buttons_int = HAL.getJoystickButtons((byte) 0, num_buttons_buffer);
|
|
|
|
|
// int num_buttons = num_buttons_buffer.get(0);
|
|
|
|
|
// boolean[] buttons = new boolean[num_buttons];
|
|
|
|
|
// for (int i = 0; i < num_buttons; i++) {
|
|
|
|
|
// buttons[i] = (buttons_int & 1 << i) > 0;
|
|
|
|
|
// }
|
|
|
|
|
// if (buttons[0])
|
|
|
|
|
// m_gyro.reset();
|
|
|
|
|
// float leftXAxis = driveController[0];
|
|
|
|
|
// float leftYAxis = driveController[1];
|
|
|
|
|
// float rightXAxis = driveController[4];
|
|
|
|
|
// leftXAxis = XboxController.inDeadZone(leftYAxis) ? 0.f : leftXAxis;
|
|
|
|
|
// leftYAxis = XboxController.inDeadZone(leftYAxis) ? 0.f : leftYAxis;
|
|
|
|
|
// rightXAxis = XboxController.inDeadZone(rightXAxis) ? 0.f : rightXAxis;
|
|
|
|
|
// leftXAxis *= leftXAxis * leftXAxis;
|
|
|
|
|
// leftYAxis *= leftYAxis * leftYAxis;
|
|
|
|
|
// rightXAxis *= rightXAxis * rightXAxis;
|
|
|
|
|
// double[] dashboardNums = new double[HAL.kMaxJoystickAxes];
|
|
|
|
|
// for (int i = 0; i < HAL.kMaxJoystickAxes; i++) {
|
|
|
|
|
// dashboardNums[i] = (double)((int)(driveController[i] * 100.f)) / 100.d;
|
|
|
|
|
// }
|
|
|
|
|
// SmartDashboard.putNumberArray("axes", dashboardNums);
|
|
|
|
|
// speeds = XboxController.ClampJoystickAxis(leftXAxis, leftYAxis);
|
|
|
|
|
// rot = -rightXAxis;
|
2022-01-15 11:31:15 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
// System.out.println("Inputx: " + speeds[0] + " Inputy: " + speeds[1]);
|
|
|
|
|
/*var speeds = new ChassisSpeeds(strafeX, strafeY, rotate * SwerveDriveConstants.ROTATION_SPEED //in rad/s );
|
|
|
|
|
driveFromSpeeds(speeds);*/
|
2022-01-15 14:55:07 -07:00
|
|
|
double xSpeedMetersPerSecond = -speeds[0] * speedAdjust;
|
|
|
|
|
double ySpeedMetersPerSecond = speeds[1] * speedAdjust;
|
2022-01-15 11:31:15 -07:00
|
|
|
SwerveModuleState[] states =
|
|
|
|
|
kinematics.toSwerveModuleStates(
|
|
|
|
|
fieldRelative
|
2022-01-15 14:55:07 -07:00
|
|
|
? ChassisSpeeds.fromFieldRelativeSpeeds(xSpeedMetersPerSecond, ySpeedMetersPerSecond, rot * SwerveDriveConstants.ROTATION_SPEED, m_gyro.getRotation2d())
|
|
|
|
|
: new ChassisSpeeds(xSpeedMetersPerSecond, ySpeedMetersPerSecond, rot * SwerveDriveConstants.ROTATION_SPEED));
|
2022-01-15 11:31:15 -07:00
|
|
|
SwerveDriveKinematics.desaturateWheelSpeeds(states, Units.feetToMeters(SwerveDriveConstants.MAX_SPEED_FEET_PER_SEC));
|
|
|
|
|
for (int i = 0; i < states.length; i++) {
|
|
|
|
|
SwerveModule module = modules[i];
|
|
|
|
|
SwerveModuleState state = states[i];
|
2022-01-15 15:52:38 -07:00
|
|
|
module.setDesiredState(state, ignoreAngles);
|
2022-01-14 17:48:53 -07:00
|
|
|
}
|
2022-01-11 11:05:52 -07:00
|
|
|
}
|
|
|
|
|
|
2022-01-14 21:02:15 -07:00
|
|
|
@Override
|
|
|
|
|
public void periodic() {
|
2022-01-15 11:31:15 -07:00
|
|
|
System.err.println(m_gyro.getFusedHeading() +" aaa");
|
2022-01-14 21:02:15 -07:00
|
|
|
// m_gyro.setFusedHeadingToCompass();
|
|
|
|
|
// m_gyro.setYawToCompass();
|
|
|
|
|
// m_gyro.getRotation2d();
|
|
|
|
|
super.periodic();
|
|
|
|
|
}
|
2022-01-11 11:05:52 -07:00
|
|
|
|
2022-01-15 14:55:07 -07:00
|
|
|
public void highSpeed(boolean shift){
|
|
|
|
|
if (shift){
|
|
|
|
|
speedAdjust = SwerveDriveConstants.JOYSTICK_TO_METERS_PER_SECOND_FAST;
|
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
speedAdjust = SwerveDriveConstants.JOYSTICK_TO_METERS_PER_SECOND_SLOW;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-11-15 17:27:14 -07:00
|
|
|
}
|