// Account for imparted velocity by robot (turret) to offset
double timeOfFlight;
Pose2d lookaheadPose = turretPosition;
- double lookaheadTurretToTargetDistance = turretToTargetDistance;
+ //double lookaheadTurretToTargetDistance = turretToTargetDistance;
// Loop (20) until lookahreadTurretToTargetDistance converges
for (int i = 0; i < 20; i++) {
target3d.minus(lookahead3d),
8.0);
- timeOfFlight = ShotInterpolation.timeOfFlightMap.get(lookaheadTurretToTargetDistance);
+ timeOfFlight = goalState.timeOfFlight(); // TODO: Change this to get it from shooter physics
double offsetX = turretVelocityX * timeOfFlight;
double offsetY = turretVelocityY * timeOfFlight;
lookaheadPose =
new Pose2d(
turretPosition.getTranslation().plus(new Translation2d(offsetX, offsetY)),
turretPosition.getRotation());
- lookaheadTurretToTargetDistance = target.getDistance(lookaheadPose.getTranslation());
+ //lookaheadTurretToTargetDistance = target.getDistance(lookaheadPose.getTranslation());
}
turretAngle = target.minus(lookaheadPose.getTranslation()).getAngle();
if (lastTurretAngle == null) {
public class ShooterPhysics {
// pitch in radians, going up from the horizontal
// exit velocity speed in m/s
- public record TurretState(Rotation2d yaw, double pitch, double exitVel, double height) {
+ public record TurretState(Rotation2d yaw, double pitch, double exitVel, double height, double timeOfFlight) {
public boolean satisfies(Constraints constraints) {
if (height < constraints.height())
return false;
*/
public static TurretState getShotParams(Translation2d robotVelocity, Translation3d robotToTarget,
double height) {
+ double zExitVel = Math.sqrt(2 * height * Constants.GRAVITY_ACCELERATION);
+ double t = (-zExitVel - Math.sqrt(Math.pow(zExitVel, 2) - 2 * Constants.GRAVITY_ACCELERATION * robotToTarget.getZ())) / -Constants.GRAVITY_ACCELERATION;
Translation3d exitVel = getRequiredExitVelocity(robotVelocity, robotToTarget, height);
- return cvtShot(exitVel, height);
+ return cvtShot(exitVel, height, t);
}
public static TurretState getShotParams(Translation3d robotToTarget, double height) {
if (withMinPitch.isPresent()) {
minHeight = Math.min(minHeight, withMinPitch.get().height());
}
- TurretState withMinHeight = cvtShot(getRequiredExitVelocity(robotVelocity, robotToTarget, minHeight),
- minHeight);
+ Translation3d minVel = getRequiredExitVelocity(robotVelocity, robotToTarget, minHeight);
+ double minT = (-minVel.getZ() - Math.sqrt(Math.pow(minVel.getZ(), 2) - 2 * Constants.GRAVITY_ACCELERATION * robotToTarget.getZ())) / -Constants.GRAVITY_ACCELERATION;
+ TurretState withMinHeight = cvtShot(minVel, minHeight, minT);
if (withMinHeight.satisfies(constraints))
return Optional.of(withMinHeight);
// use a 5cm tolerance
while (Math.abs(newRange.getFirst().height() - newRange.getSecond().height()) < .05) {
double avgHeight = (newRange.getFirst().height() + newRange.getSecond().height()) / 2;
- TurretState guess = cvtShot(getRequiredExitVelocity(robotVelocity, robotToTarget, avgHeight), avgHeight);
+ Translation3d guessVel = getRequiredExitVelocity(robotVelocity, robotToTarget, avgHeight);
+ double guessT = (-guessVel.getZ() - Math.sqrt(Math.pow(guessVel.getZ(), 2) - 2 * Constants.GRAVITY_ACCELERATION * robotToTarget.getZ())) / -Constants.GRAVITY_ACCELERATION;
+ TurretState guess = cvtShot(guessVel, avgHeight, guessT);
if (guess.satisfies(constraints)) {
if (guess.height() < lastValid.height()) lastValid = guess;
newRange = new Pair<TurretState, TurretState>(guess, newRange.getSecond());
return Optional.of(lastValid);
}
- public static TurretState cvtShot(Translation3d velocity, double height) {
+ public static TurretState cvtShot(Translation3d velocity, double height, double timeOfFlight) {
Translation2d onGround = velocity.toTranslation2d();
Rotation2d yaw = onGround.getAngle();
double magnitude2d = onGround.getNorm();
pitch %= Math.PI * 2;
double speed = velocity.getDistance(Translation3d.kZero);
- return new TurretState(yaw, pitch, speed, height);
+ return new TurretState(yaw, pitch, speed, height, timeOfFlight);
}
/**
guess = target.getZ();
Translation3d guessVelocity = getRequiredExitVelocity(initialVelocity, target, guess);
+ double guessT = (-guessVelocity.getZ() - Math.sqrt(Math.pow(guessVelocity.getZ(), 2) - 2 * Constants.GRAVITY_ACCELERATION * target.getZ())) / -Constants.GRAVITY_ACCELERATION;
double guessSpeed = guessVelocity.getNorm();
double difference = minSpeed - guessSpeed;
throw new RuntimeException("Incorrect minimum speed calculation in ShooterPhysics.java");
if (Math.abs(difference) <= tolerance)
- return cvtShot(guessVelocity, guess);
+ return cvtShot(guessVelocity, guess, guessT);
guess += difference * 1.7; // experimentally determined value
}
guess = target.getZ();
Translation3d guessVelocity = getRequiredExitVelocity(initialVelocity, target, guess);
- TurretState polar = cvtShot(guessVelocity, guess);
+ double guessT = (-guessVelocity.getZ() - Math.sqrt(Math.pow(guessVelocity.getZ(), 2) - 2 * Constants.GRAVITY_ACCELERATION * target.getZ())) / -Constants.GRAVITY_ACCELERATION;
+ TurretState polar = cvtShot(guessVelocity, guess, guessT);
double difference = pitch - polar.pitch();
// we've already hit minimum height and are trying to go lower