generated from StuyPulse/Phil
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add swerve to starting auto position command
- Loading branch information
Showing
3 changed files
with
99 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
src/main/java/com/stuypulse/robot/commands/swerve/SwerveDriveToAutoStart.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/************************ PROJECT JON *************************/ | ||
/* Copyright (c) 2024 StuyPulse Robotics. All rights reserved.*/ | ||
/* This work is licensed under the terms of the MIT license. */ | ||
/**************************************************************/ | ||
|
||
package com.stuypulse.robot.commands.swerve; | ||
|
||
import com.stuypulse.stuylib.control.angle.feedback.AnglePIDController; | ||
import com.stuypulse.stuylib.control.feedback.PIDController; | ||
import com.stuypulse.stuylib.streams.booleans.BStream; | ||
import com.stuypulse.stuylib.streams.booleans.filters.BDebounceRC; | ||
|
||
import static com.stuypulse.robot.constants.Settings.Alignment.*; | ||
|
||
import java.util.function.Supplier; | ||
|
||
import com.pathplanner.lib.commands.PathPlannerAuto; | ||
import com.stuypulse.robot.subsystems.odometry.AbstractOdometry; | ||
import com.stuypulse.robot.subsystems.odometry.Odometry; | ||
import com.stuypulse.robot.subsystems.swerve.SwerveDrive; | ||
import com.stuypulse.robot.util.HolonomicController; | ||
|
||
import edu.wpi.first.math.geometry.Pose2d; | ||
import edu.wpi.first.math.geometry.Rotation2d; | ||
import edu.wpi.first.wpilibj.smartdashboard.FieldObject2d; | ||
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard; | ||
import edu.wpi.first.wpilibj2.command.Command; | ||
|
||
|
||
public class SwerveDriveToAutoStart extends Command { | ||
private final SwerveDrive swerve; | ||
private Pose2d targetPose; | ||
|
||
// Holonomic control | ||
private final HolonomicController controller; | ||
private final BStream aligned; | ||
|
||
private final FieldObject2d targetPose2d; | ||
|
||
private final Supplier<String> name; | ||
|
||
public SwerveDriveToAutoStart(Supplier<String> name) { | ||
this.swerve = SwerveDrive.getInstance(); | ||
this.name = name; | ||
|
||
controller = new HolonomicController( | ||
new PIDController(Translation.P,Translation.I,Translation.D), | ||
new PIDController(Translation.P, Translation.I, Translation.D), | ||
new AnglePIDController(Rotation.P, Rotation.I, Rotation.D)); | ||
|
||
SmartDashboard.putData("Alignment/Controller", controller); | ||
|
||
aligned = BStream.create(this::isAligned).filtered(new BDebounceRC.Rising(DEBOUNCE_TIME)); | ||
|
||
targetPose2d = AbstractOdometry.getInstance().getField().getObject("Target Pose"); | ||
addRequirements(swerve); | ||
} | ||
|
||
private boolean isAligned() { | ||
return controller.isDone(X_TOLERANCE.get(), Y_TOLERANCE.get(), ANGLE_TOLERANCE.get()); | ||
} | ||
|
||
@Override | ||
public void initialize() { | ||
try { | ||
targetPose = PathPlannerAuto.getStaringPoseFromAutoFile(name.get()); | ||
} catch (Exception e) { | ||
targetPose = Odometry.getInstance().getPose(); | ||
} | ||
} | ||
|
||
@Override | ||
public void execute() { | ||
Pose2d currentPose = AbstractOdometry.getInstance().getPose(); | ||
|
||
targetPose2d.setPose(targetPose); | ||
|
||
controller.update(targetPose, currentPose); | ||
swerve.setChassisSpeeds(controller.getOutput()); | ||
} | ||
|
||
@Override | ||
public boolean isFinished(){ | ||
return aligned.get(); | ||
} | ||
|
||
public void end(boolean interupted) { | ||
swerve.stop(); | ||
targetPose2d.setPose(Double.NaN, Double.NaN, new Rotation2d(Double.NaN)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters