-
Notifications
You must be signed in to change notification settings - Fork 3
22 move to action #25
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2ccda2e
add Angles for angle helpers such as angleDifference, make direction …
liambridgers 368e7b3
remove unused imports, remove redundant public modifier in Localizer
liambridgers 3d11f8a
make Angles class final and uninstantiatable, remove unused toPose2D …
1fdc23c
remove PIDCoefficients as one is already supplied by robotcore, chang…
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 hidden or 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,82 @@ | ||
| package codebase.actions; | ||
|
|
||
| import com.qualcomm.robotcore.hardware.PIDCoefficients; | ||
|
|
||
| import codebase.geometry.Angles; | ||
| import codebase.geometry.FieldPosition; | ||
| import codebase.geometry.MovementVector; | ||
| import codebase.movement.mecanum.MecanumDriver; | ||
| import codebase.pathing.Localizer; | ||
|
|
||
| public class MoveToAction implements Action { | ||
|
|
||
| private static final PIDCoefficients MOVEMENT_PID_COEFFICIENTS = new PIDCoefficients(1, 0, 0); | ||
| private static final PIDCoefficients DIRECTION_PID_COEFFICIENTS = new PIDCoefficients(1, 0, 0); | ||
| private final MecanumDriver driver; | ||
| private final Localizer localizer; | ||
|
|
||
| private final FieldPosition destination; | ||
|
|
||
| /** | ||
| * The speed to move horizontally/vertically or some combination of the two in inches/sec | ||
| */ | ||
| private final double movementSpeed; | ||
|
|
||
| /** | ||
| * The max rotational speed of the robot in radians/sec | ||
| */ | ||
| private final double rotationalSpeed; | ||
|
|
||
| private final double maxDistanceError; | ||
| private final double maxRotationalError; | ||
|
|
||
| private final PIDController xPID; | ||
| private final PIDController yPID; | ||
| private final PIDController directionPID; | ||
|
|
||
| public MoveToAction(MecanumDriver driver, Localizer localizer, FieldPosition destination, double movementSpeed, double rotationalSpeed, double maxDistanceError, double maxRotationalError) { | ||
| this.driver = driver; | ||
| this.localizer = localizer; | ||
| this.destination = destination; | ||
| this.movementSpeed = movementSpeed; | ||
| this.rotationalSpeed = rotationalSpeed; | ||
| this.maxDistanceError = maxDistanceError; | ||
| this.maxRotationalError = maxRotationalError; | ||
|
|
||
| this.xPID = new PIDController(MOVEMENT_PID_COEFFICIENTS.p, MOVEMENT_PID_COEFFICIENTS.i, MOVEMENT_PID_COEFFICIENTS.d, () -> localizer.getCurrentPosition().x, () -> destination.x); | ||
| this.yPID = new PIDController(MOVEMENT_PID_COEFFICIENTS.p, MOVEMENT_PID_COEFFICIENTS.i, MOVEMENT_PID_COEFFICIENTS.d, () -> localizer.getCurrentPosition().y, () -> destination.y); | ||
| this.directionPID = new PIDController( | ||
| DIRECTION_PID_COEFFICIENTS.p, DIRECTION_PID_COEFFICIENTS.i, DIRECTION_PID_COEFFICIENTS.d, | ||
| () -> localizer.getCurrentPosition().direction, | ||
| () -> destination.direction, | ||
| () -> Angles.angleDifference(localizer.getCurrentPosition().direction, destination.direction) | ||
| ); | ||
| } | ||
|
|
||
| @Override | ||
| public void init() { | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public void loop() { | ||
| double powerX = xPID.getPower(); | ||
| double powerY = yPID.getPower(); | ||
| double powerRotational = directionPID.getPower(); | ||
|
|
||
| MovementVector vector = new MovementVector( | ||
| movementSpeed * powerY, | ||
| movementSpeed * powerX, | ||
| rotationalSpeed * powerRotational); | ||
|
|
||
| this.driver.setAbsoluteVelocity(localizer.getCurrentPosition(), vector); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isComplete() { | ||
| double distanceError = Math.sqrt(Math.pow(localizer.getCurrentPosition().x - destination.x, 2) + Math.pow(localizer.getCurrentPosition().y - destination.y, 2)); | ||
| double rotationalError = Angles.angleDifference(localizer.getCurrentPosition().direction, destination.direction); | ||
|
|
||
| return (distanceError <= maxDistanceError) && (rotationalError <= maxRotationalError); | ||
| } | ||
| } |
This file contains hidden or 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,21 @@ | ||
| package codebase.geometry; | ||
|
|
||
| public final class Angles { | ||
| public static double angleDifference(double fromAngle, double toAngle) { | ||
| double diff = toAngle - fromAngle; | ||
|
|
||
| // Normalize to [-2PI, 2PI] | ||
| diff = diff % (Math.PI * 2); | ||
|
|
||
| // Convert to [-PI, PI] range | ||
| if (diff > Math.PI) { | ||
| diff -= (Math.PI * 2); | ||
| } else if (diff <= -Math.PI) { | ||
| diff += (Math.PI * 2); | ||
| } | ||
|
|
||
| return diff; | ||
| } | ||
|
|
||
| private Angles() {} | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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,11 @@ | ||
| package codebase.pathing; | ||
|
|
||
| import codebase.Loop; | ||
| import codebase.geometry.FieldPosition; | ||
|
|
||
| public interface Localizer extends Loop { | ||
|
|
||
| FieldPosition getCurrentPosition(); | ||
|
|
||
| void init(FieldPosition initialPosition); | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.