generated from StuyPulse/Phil
-
Notifications
You must be signed in to change notification settings - Fork 8
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
Se/swerve commands #11
Merged
Merged
Changes from 4 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
e2f3432
Add SwerveDriveToPose.java and prep test it in sim
Keobkeig 84b300e
Add SwerveCommands
Keobkeig d4446b3
fix SwerveDriveDrive turn not working, but now wheels always pointing…
Keobkeig ad98395
add commands
Keobkeig 6078a63
SwerveDriveToShoot works
Keobkeig 7476a2d
Fix XMode and DriveToShoot
Keobkeig 25bf3d9
Fixed merge conflicts for pr
Keobkeig 03a238b
Remove testing button bindings
BenG49 b1e6b64
Remove gyro feedback
BenG49 e48a26b
Merge branch 'main' of github.com:StuyPulse/BigWang into se/swerve-co…
BenG49 28c4c83
Comment Field.java
BenG49 c938312
Make x mode states constant
BenG49 a567d16
Update swerve logging
BenG49 f848abc
Remove Gyro constant from alignment settings
BenG49 f4d3f5a
Slight changes
BenG49 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
74 changes: 74 additions & 0 deletions
74
src/main/java/com/stuypulse/robot/commands/swerve/SwerveDriveToPose.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,74 @@ | ||
package com.stuypulse.robot.commands.swerve; | ||
|
||
import com.stuypulse.robot.constants.Settings.Alignment; | ||
import com.stuypulse.robot.constants.Settings.Alignment.Rotation; | ||
import com.stuypulse.robot.constants.Settings.Alignment.Translation; | ||
import com.stuypulse.robot.subsystems.odometry.Odometry; | ||
import com.stuypulse.robot.subsystems.swerve.SwerveDrive; | ||
import com.stuypulse.robot.util.HolonomicController; | ||
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 edu.wpi.first.math.geometry.Pose2d; | ||
import edu.wpi.first.math.geometry.Rotation2d; | ||
import edu.wpi.first.wpilibj.smartdashboard.FieldObject2d; | ||
import edu.wpi.first.wpilibj2.command.Command; | ||
|
||
public class SwerveDriveToPose extends Command { | ||
private final SwerveDrive swerve; | ||
private Pose2d targetPose; | ||
|
||
private final HolonomicController controller; | ||
|
||
private final BStream isAligned; | ||
|
||
private final FieldObject2d targetPose2d; | ||
|
||
public SwerveDriveToPose(Pose2d targetPose) { | ||
swerve = SwerveDrive.getInstance(); | ||
this.targetPose = targetPose; | ||
|
||
targetPose2d = Odometry.getInstance().getField().getObject("Target Pose"); | ||
|
||
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) | ||
); | ||
|
||
isAligned = BStream.create(this::isAligned) | ||
.filtered(new BDebounceRC.Both(Alignment.DEBOUNCE_TIME.get())); | ||
|
||
addRequirements(swerve); | ||
} | ||
|
||
private boolean isAligned() { | ||
return controller.isDone(Alignment.X_TOLERANCE.get(), Alignment.Y_TOLERANCE.get(), Alignment.ANGLE_TOLERANCE.get()); | ||
} | ||
|
||
@Override | ||
public void initialize() {} | ||
|
||
@Override | ||
public void execute() { | ||
Pose2d currentPose = Odometry.getInstance().getPose(); | ||
targetPose2d.setPose(targetPose); | ||
|
||
controller.update(targetPose, currentPose); | ||
swerve.setChassisSpeeds(controller.getOutput()); | ||
} | ||
|
||
@Override | ||
public boolean isFinished() { | ||
return isAligned.get(); | ||
} | ||
|
||
@Override | ||
public void end(boolean interrupted) { | ||
swerve.stop(); | ||
targetPose2d.setPose(Double.NaN, Double.NaN, new Rotation2d(Double.NaN)); | ||
} | ||
|
||
} |
113 changes: 113 additions & 0 deletions
113
src/main/java/com/stuypulse/robot/commands/swerve/SwerveDriveToShoot.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,113 @@ | ||
package com.stuypulse.robot.commands.swerve; | ||
|
||
import com.stuypulse.robot.constants.Settings.Alignment; | ||
import com.stuypulse.robot.constants.Settings.Alignment.Rotation; | ||
import com.stuypulse.robot.constants.Settings.Alignment.Translation; | ||
import com.stuypulse.robot.subsystems.odometry.Odometry; | ||
import com.stuypulse.robot.subsystems.swerve.SwerveDrive; | ||
import com.stuypulse.robot.util.HolonomicController; | ||
import com.stuypulse.stuylib.control.angle.feedback.AnglePIDController; | ||
import com.stuypulse.stuylib.control.feedback.PIDController; | ||
import com.stuypulse.stuylib.math.Angle; | ||
import com.stuypulse.stuylib.math.Vector2D; | ||
|
||
import edu.wpi.first.math.geometry.Pose2d; | ||
import edu.wpi.first.math.geometry.Rotation2d; | ||
import edu.wpi.first.math.util.Units; | ||
import edu.wpi.first.wpilibj.smartdashboard.FieldObject2d; | ||
import edu.wpi.first.wpilibj2.command.Command; | ||
|
||
public class SwerveDriveToShoot extends Command { | ||
/* | ||
* swerve | ||
* holonomi controller | ||
* targetpose pose2d | ||
* targetPose2d as a field object 2d | ||
* | ||
* contrusctor (init them, add requirements) | ||
* initialize (do the math to get the target pose) | ||
* execute (set the target pose to the controller and then setChassisSpeeds to it) | ||
* isFinished (check if the controller is done within the tolerances) | ||
* end (stop the swerve) | ||
* | ||
*/ | ||
private final SwerveDrive swerve; | ||
private Pose2d targetPose; | ||
private final HolonomicController controller; | ||
private final FieldObject2d targetPose2d; | ||
|
||
public SwerveDriveToShoot(Pose2d targetPose) { | ||
swerve = SwerveDrive.getInstance(); | ||
this.targetPose = targetPose; | ||
|
||
this.targetPose2d = Odometry.getInstance().getField().getObject("Target Pose"); | ||
|
||
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) | ||
); | ||
|
||
addRequirements(swerve); | ||
} | ||
|
||
private double linearInterpolate(double lowerY, double upperY, double lowerBound, double upperBound, double input) { | ||
if (input == lowerBound) { | ||
return lowerY; | ||
} else if (input == upperBound) { | ||
return upperY; | ||
} else if (input < lowerBound || input > upperBound) { | ||
throw new IllegalArgumentException("Input must be between lowerBound and upperBound"); | ||
} | ||
else { | ||
return (upperY - lowerY) * (input - lowerBound) / (upperBound - lowerBound) + lowerY; | ||
} | ||
} | ||
|
||
//TODO: Make this work lmao | ||
private Pose2d getSpeakerTargetPose(Rotation2d angleToSpeaker) { | ||
//Everything under is from aiming at center of speaker | ||
Vector2D robotPose = new Vector2D(Odometry.getInstance().getPose().getTranslation()); | ||
Vector2D targetPose = new Vector2D(this.targetPose.getTranslation()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interpolate between target poses +- certain y distance from the center of the speaker to adjust the point which the robot will aim at for shooting. |
||
Vector2D targetVector = robotPose.add(robotPose.sub(targetPose).normalize().mul(Alignment.TARGET_DISTANCE_IN.get())); | ||
//Rotation2d angleToSpeaker = new Rotation2d(Math.atan2(targetPose.y - robotPose.y, targetPose.x - robotPose.x)); | ||
|
||
double speakerOpeningLength = Units.inchesToMeters(41.625); | ||
Rotation2d speakerTargetAngle = Rotation2d.fromDegrees(linearInterpolate(-speakerOpeningLength / 2, speakerOpeningLength / 2, -70, 70, angleToSpeaker.getDegrees())); | ||
|
||
return new Pose2d(targetVector.x, targetVector.y, speakerTargetAngle); | ||
} | ||
|
||
@Override | ||
public void initialize() { | ||
Vector2D robotPose = new Vector2D(Odometry.getInstance().getPose().getTranslation()); | ||
Vector2D targetPose = new Vector2D(this.targetPose.getTranslation()); | ||
Rotation2d angleToSpeaker = new Rotation2d(Math.atan2(targetPose.y - robotPose.y, targetPose.x - robotPose.x)); | ||
Vector2D targetVector = robotPose.add(robotPose.sub(targetPose).normalize().mul(Alignment.TARGET_DISTANCE_IN.get())); | ||
//this.targetPose = getSpeakerTargetPose(angleToSpeaker); | ||
|
||
this.targetPose = new Pose2d(targetVector.x, targetVector.y, angleToSpeaker); | ||
} | ||
|
||
@Override | ||
public void execute() { | ||
Pose2d currentPose = Odometry.getInstance().getPose(); | ||
targetPose2d.setPose(targetPose); | ||
|
||
controller.update(targetPose, currentPose); | ||
swerve.setChassisSpeeds(controller.getOutput()); | ||
} | ||
|
||
|
||
@Override | ||
public boolean isFinished() { | ||
return controller.isDone(Alignment.X_TOLERANCE.get(), Alignment.Y_TOLERANCE.get(), Alignment.ANGLE_TOLERANCE.get()); | ||
} | ||
|
||
@Override | ||
public void end(boolean interrupted) { | ||
swerve.stop(); | ||
targetPose2d.setPose(Double.NaN, Double.NaN, new Rotation2d(Double.NaN)); | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/com/stuypulse/robot/commands/swerve/SwerveDriveXMode.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,32 @@ | ||
package com.stuypulse.robot.commands.swerve; | ||
|
||
import com.stuypulse.robot.subsystems.swerve.SwerveDrive; | ||
|
||
import edu.wpi.first.math.geometry.Rotation2d; | ||
import edu.wpi.first.math.kinematics.SwerveModuleState; | ||
import edu.wpi.first.wpilibj2.command.Command; | ||
import edu.wpi.first.wpilibj2.command.InstantCommand; | ||
|
||
public class SwerveDriveXMode extends Command { | ||
private final SwerveDrive swerve; | ||
|
||
public SwerveDriveXMode() { | ||
swerve = SwerveDrive.getInstance(); | ||
addRequirements(swerve); | ||
} | ||
|
||
@Override | ||
public void initialize() { | ||
} | ||
|
||
@Override | ||
public void execute() { | ||
SwerveModuleState[] states = new SwerveModuleState[] { | ||
new SwerveModuleState(0,Rotation2d.fromDegrees(135)), | ||
new SwerveModuleState(0,Rotation2d.fromDegrees(45)), | ||
new SwerveModuleState(0,Rotation2d.fromDegrees(225)), | ||
new SwerveModuleState(0,Rotation2d.fromDegrees(315)) | ||
}; | ||
swerve.setModuleStates(states); | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove unused targetPose param
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This command is supposed to align from any point on the field to a certain shooting distance away from the speaker and to point at the speaker target position. then shoot.