Skip to content

Commit 924256f

Browse files
committed
autoalign works a bit better :D
1 parent 4742ec3 commit 924256f

File tree

1 file changed

+53
-7
lines changed

1 file changed

+53
-7
lines changed

src/main/java/frc/robot/subsystems/drivetrain/DriveSubsystem.java

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.ctre.phoenix6.swerve.SwerveModule.DriveRequestType;
1818
import com.ctre.phoenix6.swerve.SwerveModule.SteerRequestType;
1919
import com.ctre.phoenix6.swerve.SwerveRequest;
20+
import com.ctre.phoenix6.swerve.SwerveDrivetrain.SwerveDriveState;
2021
import com.ctre.phoenix6.swerve.SwerveRequest.ForwardPerspectiveValue;
2122

2223
import edu.wpi.first.math.VecBuilder;
@@ -141,8 +142,10 @@ public void execute() {
141142
Math.abs(s_headingController.calculate(s_drivetrain.getState().Pose.getRotation().getRadians())), s_autoAlignTarget.getRotation().getRadians()
142143
);
143144

144-
var xComponent = outputVelocity * directionOfTravel.getCos();
145-
var yComponent = outputVelocity * directionOfTravel.getSin();
145+
var angleToRotate =s_drivetrain.getState().Pose.getRotation().getRadians() - s_autoAlignTarget.getRotation().getRadians();
146+
147+
var xComponent = -outputVelocity * directionOfTravel.getCos();
148+
var yComponent = -outputVelocity * directionOfTravel.getSin();
146149

147150
s_drivetrain.setControl(
148151
s_driveRobotCentric
@@ -154,7 +157,8 @@ public void execute() {
154157
if(distance <= 0.05) {
155158
s_shouldAutoAlign = false;
156159
}
157-
160+
161+
158162
Logger.recordOutput("DriveSubsystem/autoAlign/isClose", s_isClose);
159163
Logger.recordOutput(
160164
"DriveSubsystem/autoAlign/closeTime", System.currentTimeMillis() - m_closeTime);
@@ -199,14 +203,30 @@ public void end(boolean interrupted) {
199203

200204
private static CommandSwerveDrivetrain s_drivetrain;
201205
private static SwerveRequest.FieldCentric s_drive;
202-
private static SwerveRequest.RobotCentric s_driveRobotCentric;
206+
private static SwerveRequest.FieldCentric s_driveRobotCentric;
203207
private static PIDController s_autoDrive;
204208
private static PIDController s_headingController;
205209
private static QuestNav m_quest;
206210
private Transform2d ROBOT_TO_QUEST;
207211
private Transform2d OAKD_TO_ROBOT;
208212
private StructEntry<Pose3d> oakd_pose_entry;
209213

214+
215+
// temp vars for loggiong
216+
Translation2d newPosition;
217+
SwerveDriveState drivetrain_state;
218+
Pose2d drivetrain_pose;
219+
double distance;
220+
221+
Rotation2d directionOfTravel;
222+
double outputVelocity;
223+
224+
double rotationRate;
225+
226+
double angleToRotate;
227+
double xComponent;
228+
double yComponent;
229+
210230
private static DoubleSupplier s_driveRequest = () -> 0;
211231
private static DoubleSupplier s_strafeRequest = () -> 0;
212232
private static DoubleSupplier s_rotateRequest = () -> 0;
@@ -247,15 +267,15 @@ public DriveSubsystem(Hardware driveHardware, Telemetry logger) {
247267
.withForwardPerspective(ForwardPerspectiveValue.OperatorPerspective);
248268

249269
s_driveRobotCentric =
250-
new SwerveRequest.RobotCentric()
270+
new SwerveRequest.FieldCentric()
251271
.withDriveRequestType(DriveRequestType.Velocity)
252272
.withSteerRequestType(SteerRequestType.MotionMagicExpo)
253273
.withDeadband(0)
254274
.withRotationalDeadband(0);
255275

256-
s_autoDrive = new PIDController(0.6, 0.2, 0.2);
276+
s_autoDrive = new PIDController(.1, 0.0, 0.1);
257277

258-
s_headingController = new PIDController(0.6, 0.2, 0.2);
278+
s_headingController = new PIDController(.1, 0.0, 0.1);
259279

260280
s_drivetrain.registerTelemetry(logger::telemeterize);
261281

@@ -556,6 +576,26 @@ public void periodic() {
556576
Logger.recordOutput(getName() + "/settingOperatorPerspective", false);
557577
}
558578

579+
newPosition = s_autoAlignTarget.getTranslation().minus(s_drivetrain.getState().Pose.getTranslation());
580+
drivetrain_state = s_drivetrain.getState();
581+
drivetrain_pose = drivetrain_state.Pose;
582+
distance =
583+
drivetrain_pose.getTranslation().getDistance(s_autoAlignTarget.getTranslation());
584+
585+
directionOfTravel = newPosition.getAngle();
586+
outputVelocity = Math.min(
587+
Math.abs(s_autoDrive.calculate(distance, 0.0)), Constants.Drive.MAX_SPEED.magnitude()
588+
);
589+
590+
rotationRate = Math.min(
591+
Math.abs(s_headingController.calculate(s_drivetrain.getState().Pose.getRotation().getRadians())), s_autoAlignTarget.getRotation().getRadians()
592+
);
593+
594+
angleToRotate =s_drivetrain.getState().Pose.getRotation().getRadians() - s_autoAlignTarget.getRotation().getRadians();
595+
596+
xComponent = outputVelocity * directionOfTravel.getCos();
597+
yComponent = outputVelocity * directionOfTravel.getSin();
598+
559599
double cameraTime = 0;
560600
double configTime = 0;
561601
double getPoseEstimateTime = 0;
@@ -564,6 +604,12 @@ public void periodic() {
564604

565605
m_quest.commandPeriodic();
566606

607+
Logger.recordOutput("DriveSubsystem/autoAlign/angle_to_rotate", angleToRotate);
608+
Logger.recordOutput("DriveSubsystem/autoAlign/direction_of_travel", directionOfTravel);
609+
Logger.recordOutput("DriveSubsystem/autoAlign/xComponent", xComponent);
610+
Logger.recordOutput("DriveSubsystem/autoAlign/yComponent", yComponent);
611+
612+
567613

568614
Logger.recordOutput(getName() + "/cameraTimes/config", configTime);
569615
Logger.recordOutput(getName() + "/cameraTimes/getPoseEstimate", getPoseEstimateTime);

0 commit comments

Comments
 (0)