-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added functionality to VisionSubsystem to determine if we can see the…
… inner target, modified VisionRotateToTargetCommand to get the proper angle to the target.
- Loading branch information
Showing
2 changed files
with
57 additions
and
18 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
63 changes: 50 additions & 13 deletions
63
src/main/java/org/frcteam2910/c2020/subsystems/VisionSubsystem.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 |
---|---|---|
@@ -1,53 +1,90 @@ | ||
package org.frcteam2910.c2020.subsystems; | ||
|
||
import edu.wpi.first.networktables.NetworkTableEntry; | ||
import edu.wpi.first.networktables.NetworkTableInstance; | ||
import edu.wpi.first.wpilibj.shuffleboard.Shuffleboard; | ||
import edu.wpi.first.wpilibj.shuffleboard.ShuffleboardTab; | ||
import edu.wpi.first.wpilibj2.command.Subsystem; | ||
import org.frcteam2910.common.math.MathUtils; | ||
import org.frcteam2910.common.math.Vector2; | ||
import org.frcteam2910.common.robot.drivers.Limelight; | ||
|
||
import java.util.OptionalDouble; | ||
|
||
public class VisionSubsystem implements Subsystem { | ||
private static final double TARGET_HEIGHT = 98.25; | ||
// TODO: Put the actual height of the Limelight | ||
private static final double LIMELIGHT_HEIGHT = 20.0; | ||
// TODO: Put the actual angle the Limelight is mounted at | ||
private static final double LIMELIGHT_MOUNTING_ANGLE = Math.toRadians(20.0); | ||
private static final double TARGET_HEIGHT = 46.25; | ||
private static final double LIMELIGHT_HEIGHT = 5.5; | ||
|
||
private final Limelight limelight = new Limelight(); | ||
private static final double INNER_TARGET_DEPTH = 29.25; | ||
|
||
private static final double LIMELIGHT_MOUNTING_ANGLE = Math.toRadians(32.2); | ||
private static final double INNER_TARGET_RANGE_ANGLE = Math.toRadians(16.2); | ||
|
||
private static final Limelight LIMELIGHT = new Limelight(); | ||
|
||
private final NetworkTableEntry distanceToTargetEntry; | ||
private final NetworkTableEntry canSeeInnerTargetEntry; | ||
|
||
private OptionalDouble distanceToTarget; | ||
private OptionalDouble angleToTarget; | ||
private boolean canSeeInnerTarget; | ||
|
||
public VisionSubsystem() { | ||
ShuffleboardTab tab = Shuffleboard.getTab("Vision"); | ||
distanceToTargetEntry = tab.add("distance to target", 0.0) | ||
.withPosition(0, 0) | ||
.withSize(1, 1) | ||
.getEntry(); | ||
canSeeInnerTargetEntry = tab.add("can see inner target", false) | ||
.withPosition(0, 0) | ||
.withSize(1, 1) | ||
.getEntry(); | ||
} | ||
|
||
public OptionalDouble getDistanceToTarget() { | ||
return distanceToTarget; | ||
} | ||
|
||
public OptionalDouble getAngleToTarget() { | ||
return angleToTarget; | ||
} | ||
|
||
public boolean isInnerTargetVisible() { | ||
return canSeeInnerTarget; | ||
} | ||
|
||
@Override | ||
public void periodic() { | ||
// Determine whether the Limelight has a target or not | ||
if (limelight.hasTarget()) { | ||
// If so, get the target position and calculate the distance to it | ||
Vector2 targetPosition = limelight.getTargetPosition(); | ||
if (LIMELIGHT.hasTarget()) { | ||
// Calculate the distance to the outer target | ||
Vector2 targetPosition = LIMELIGHT.getTargetPosition(); | ||
double theta = LIMELIGHT_MOUNTING_ANGLE + targetPosition.y; | ||
distanceToTarget = OptionalDouble.of((TARGET_HEIGHT - LIMELIGHT_HEIGHT) / Math.tan(theta)); | ||
distanceToTargetEntry.setDouble(distanceToTarget.getAsDouble()); | ||
double distanceToOuterTarget = (TARGET_HEIGHT - LIMELIGHT_HEIGHT) / Math.tan(theta); | ||
double dYOuter = distanceToOuterTarget * Math.sin(targetPosition.x); | ||
double dXOuter = Math.sqrt(Math.pow(distanceToOuterTarget, 2) - Math.pow(dYOuter, 2)); | ||
|
||
// Calculate the distance to the inner target | ||
double dXInner = dXOuter + INNER_TARGET_DEPTH; | ||
double distanceToInnerTarget = Math.hypot(dXInner, dYOuter); | ||
double alpha = Math.asin(dYOuter / dXInner); | ||
|
||
// Bring to light whether we can see the inner target | ||
canSeeInnerTarget = MathUtils.isInRange(-INNER_TARGET_RANGE_ANGLE, INNER_TARGET_RANGE_ANGLE, alpha); | ||
if (canSeeInnerTarget) { | ||
distanceToTarget = OptionalDouble.of(distanceToInnerTarget); | ||
angleToTarget = OptionalDouble.of(alpha); | ||
} else { | ||
distanceToTarget = OptionalDouble.of(distanceToOuterTarget); | ||
angleToTarget = OptionalDouble.of(targetPosition.x); | ||
} | ||
} else { | ||
// Else, set distanceToTarget to an empty OptionalDouble | ||
distanceToTarget = OptionalDouble.empty(); | ||
angleToTarget = OptionalDouble.empty(); | ||
canSeeInnerTarget = false; | ||
|
||
// Update shuffleboard | ||
distanceToTargetEntry.setDouble(-1.0); | ||
canSeeInnerTargetEntry.setBoolean(canSeeInnerTarget); | ||
} | ||
} | ||
} |