make lidar align theoretically work

This commit is contained in:
C4llSiqn
2025-01-29 17:06:55 -07:00
parent 2bc1d6eb9c
commit 42e7896f5f
3 changed files with 65 additions and 4 deletions
@@ -227,6 +227,7 @@ public final class Constants {
.withKS(0).withKV(0.124); .withKS(0).withKV(0.124);
public static final Gains DRIFT_CORRECTION_GAINS = new Gains(2.5, 0, 0.1); public static final Gains DRIFT_CORRECTION_GAINS = new Gains(2.5, 0, 0.1);
public static final Gains RELATIVE_LOCKED_ANGLE_GAINS = new Gains(5, 0, 0.1); // TODO: TEST
} }
public static final class AutoConstants { public static final class AutoConstants {
@@ -4,6 +4,8 @@
package frc4388.robot.commands; package frc4388.robot.commands;
import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.math.geometry.Translation2d;
import edu.wpi.first.wpilibj2.command.Command; import edu.wpi.first.wpilibj2.command.Command;
import frc4388.robot.subsystems.Lidar; import frc4388.robot.subsystems.Lidar;
import frc4388.robot.subsystems.SwerveDrive; import frc4388.robot.subsystems.SwerveDrive;
@@ -17,6 +19,7 @@ public class LidarAlign extends Command {
// private int tickFoundPipe; // private int tickFoundPipe;
private boolean foundReef; private boolean foundReef;
private boolean headedRight; private boolean headedRight;
private double speed;
private final boolean constructedHeadedRight; private final boolean constructedHeadedRight;
/** Creates a new LidarAlign. */ /** Creates a new LidarAlign. */
@@ -26,12 +29,15 @@ public class LidarAlign extends Command {
this.swerveDrive = swerveDrive; this.swerveDrive = swerveDrive;
this.lidar = lidar; this.lidar = lidar;
addRequirements(swerveDrive, lidar);
} }
// Called when the command is initially scheduled. // Called when the command is initially scheduled.
@Override @Override
public void initialize() { public void initialize() {
this.currentFinderTick = 0; this.currentFinderTick = 0;
this.speed = 0.05; // TODO: find good speed for this
this.foundReef = false; this.foundReef = false;
this.headedRight = constructedHeadedRight; this.headedRight = constructedHeadedRight;
} }
@@ -40,18 +46,47 @@ public class LidarAlign extends Command {
// Called every time the scheduler runs while the command is scheduled. // Called every time the scheduler runs while the command is scheduled.
@Override @Override
public void execute() { public void execute() {
double speed = 0.05; // TODO: find good speed for this if (lidar.withinDistance()) {
swerveDrive.softStop();
foundReef = true;
return;
}
if (currentFinderTick > 100) { //arbutrary threshhold for now.
headedRight = !headedRight;
currentFinderTick *= -1;
}
double relAngle = Math.round(swerveDrive.getGyroAngle() / 60.d) * 60; // Relative driving to the side of the reef double relAngle = Math.round(swerveDrive.getGyroAngle() / 60.d) * 60; // Relative driving to the side of the reef
if (!headedRight) {
swerveDrive.driveRelativeLockedAngle(new Translation2d(0, speed), Rotation2d.fromDegrees(relAngle));
} else {
swerveDrive.driveRelativeLockedAngle(new Translation2d(0, -speed), Rotation2d.fromDegrees(relAngle));
}
currentFinderTick++;
} }
// Called once the command ends or is interrupted. // Called once the command ends or is interrupted.
@Override @Override
public void end(boolean interrupted) {} public void end(boolean interrupted) {
}
// Returns true when the command should end. // Returns true when the command should end.
@Override @Override
public boolean isFinished() { public boolean isFinished() {
return false; if (foundReef && lidar.withinDistance()) {
swerveDrive.stopModules();
return true;
} else if (foundReef && !lidar.withinDistance()) {
speed = speed / 2;
headedRight = !headedRight;
currentFinderTick = 0;
foundReef = false;
return false;
} else {
return false;
}
} }
} }
@@ -210,6 +210,21 @@ public class SwerveDrive extends Subsystem {
.withTargetDirection(rightStick.getAngle())); .withTargetDirection(rightStick.getAngle()));
} }
public void driveRelativeLockedAngle(Translation2d leftStick, Rotation2d heading) {
leftStick = leftStick.rotateBy(heading);
var ctrl = new SwerveRequest.FieldCentricFacingAngle()
.withVelocityX(leftStick.getX() * speedAdjust)
.withVelocityY(leftStick.getY() * speedAdjust)
.withTargetDirection(Rotation2d.fromDegrees(rotTarget));
ctrl.HeadingController.setPID(
SwerveDriveConstants.PIDConstants.RELATIVE_LOCKED_ANGLE_GAINS.kP,
SwerveDriveConstants.PIDConstants.RELATIVE_LOCKED_ANGLE_GAINS.kI,
SwerveDriveConstants.PIDConstants.RELATIVE_LOCKED_ANGLE_GAINS.kD
);
swerveDriveTrain.setControl(ctrl);
}
public boolean rotateToTarget(double angle) { public boolean rotateToTarget(double angle) {
swerveDriveTrain.setControl(new SwerveRequest.FieldCentricFacingAngle() swerveDriveTrain.setControl(new SwerveRequest.FieldCentricFacingAngle()
.withVelocityX(0) .withVelocityX(0)
@@ -248,6 +263,16 @@ public class SwerveDrive extends Subsystem {
swerveDriveTrain.tareEverything(); swerveDriveTrain.tareEverything();
} }
public void softStop() {
stopped = true;
swerveDriveTrain.setControl(new SwerveRequest.FieldCentric()
.withVelocityX(0)
.withVelocityY(0)
.withRotationalRate(0)
); // stop the modules without breaking
}
public void stopModules() { public void stopModules() {
stopped = true; stopped = true;
swerveDriveTrain.setControl(new SwerveRequest.SwerveDriveBrake()); swerveDriveTrain.setControl(new SwerveRequest.SwerveDriveBrake());