Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix joystick #15

Merged
merged 2 commits into from
Feb 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/main/java/frc/robot/commands/DriveWithController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import edu.wpi.first.math.MathUtil;
import edu.wpi.first.math.filter.SlewRateLimiter;
import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.math.geometry.Translation2d;
import edu.wpi.first.math.kinematics.SwerveModuleState;
import edu.wpi.first.util.CircularBuffer;
import edu.wpi.first.wpilibj.XboxController;
Expand Down Expand Up @@ -101,10 +102,18 @@ public void execute() {
}

if (moving) {
xMove = Math.copySign(xMove * xMove, xMove);
yMove = Math.copySign(yMove * yMove, yMove);
// xMove = Math.copySign(xMove * xMove, xMove);
// yMove = Math.copySign(yMove * yMove, yMove);
rotate = Math.copySign(rotate * rotate, rotate);

Translation2d moveTranslation = new Translation2d(xMove, yMove);
double moveSpeed = moveTranslation.getNorm() * moveTranslation.getNorm();
Rotation2d angle = moveTranslation.getAngle();

xMove = moveSpeed * angle.getCos();
yMove = moveSpeed * angle.getSin();


if (drive.getSlowMode() || controller.getXButton()){
xMove *= 0.3;
yMove *= 0.3;
Expand Down
38 changes: 14 additions & 24 deletions src/main/java/frc/robot/subsystems/Drivetrain.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import edu.wpi.first.math.kinematics.SwerveDriveOdometry;
import edu.wpi.first.math.kinematics.SwerveModulePosition;
import edu.wpi.first.math.kinematics.SwerveModuleState;
import edu.wpi.first.math.trajectory.TrapezoidProfile.Constraints;
// import edu.wpi.first.math.trajectory.TrapezoidProfile.Constraints;
import edu.wpi.first.math.util.Units;
import edu.wpi.first.wpilibj.shuffleboard.BuiltInLayouts;
import edu.wpi.first.wpilibj.shuffleboard.Shuffleboard;
Expand Down Expand Up @@ -60,49 +60,43 @@ public Drivetrain() {
( "Pitch", () -> m_imu.getPitch() );
imuShuffleboard.addNumber
( "Roll", () -> m_imu.getRoll() );



Mk4ModuleConfiguration moduleConfig = Mk4ModuleConfiguration.getDefaultSteerNEO();
moduleConfig.setNominalVoltage(Constants.Drive.kDriveVoltageCompensation);

//: Swerve setup
this.m_swerve_modules[0] = this.createModule(
Constants.Drive.kFrontLeft,
moduleConfig, tab
Constants.Drive.kFrontLeft, moduleConfig, tab
);
this.m_swerve_modules[1] = this.createModule(
Constants.Drive.kFrontRight,
moduleConfig, tab
Constants.Drive.kFrontRight, moduleConfig, tab
);
this.m_swerve_modules[2] = this.createModule(
Constants.Drive.kBackLeft,
moduleConfig, tab
Constants.Drive.kBackLeft,moduleConfig, tab
);
this.m_swerve_modules[3] = this.createModule(
Constants.Drive.kBackRight,
moduleConfig, tab
Constants.Drive.kBackRight, moduleConfig, tab
);

// Setup odometry
m_odometry = new SwerveDriveOdometry(
Constants.Drive.kDriveKinematics,
getIMUHeading(),
getModulePositions()
getIMUHeading(), getModulePositions()
);

var odometryTab = tab.getLayout("Odometry", BuiltInLayouts.kList)
.withSize(2,2)
.withPosition(10,0);

odometryTab.addNumber("X (inches)", ()->Units.metersToInches(m_odometry.getPoseMeters().getX()));
odometryTab.addNumber("Y (inches)", ()->Units.metersToInches(m_odometry.getPoseMeters().getY()));
odometryTab.addNumber("Theta (degrees)", ()->m_odometry.getPoseMeters().getRotation().getDegrees());
odometryTab.addNumber("X (inches)", ()->Units.metersToInches(m_odometry.getPoseMeters().getX()));
odometryTab.addNumber("Y (inches)", ()->Units.metersToInches(m_odometry.getPoseMeters().getY()));
odometryTab.addNumber("Theta (degrees)", ()->m_odometry.getPoseMeters().getRotation().getDegrees());

tab.add("Field", m_field)
.withSize(5,4)
.withPosition(8,2);
}
tab.add("Field", m_field)
.withSize(5,4)
.withPosition(8,2);
}

private SwerveModule createModule(ModuleConfig config, Mk4ModuleConfiguration moduleConfig, ShuffleboardTab tab) {
return Mk4SwerveModuleHelper.createNeo(
Expand Down Expand Up @@ -179,12 +173,9 @@ public void stop() {
drive(0, 0, 0, false);
}

@Override
public void periodic() {

@Override public void periodic() {
// Upldate robote pose
m_odometry.update(getIMUHeading(), getModulePositions());

m_field.setRobotPose(m_odometry.getPoseMeters());

SmartDashboard.putBoolean("Slow Mode", m_slowMode);
Expand Down Expand Up @@ -224,5 +215,4 @@ public Pose2d getPose() {
public void resetOdometry(Pose2d pose) {
m_odometry.resetPosition(getIMUHeading(), getModulePositions(), pose);
}

}
Loading