Turret Oscillation Prevention - #20
Open
Daniel-Tsur wants to merge 4 commits into
Open
Conversation
Added some code to possible fix turret oscillation when it is very cose to the max and min degrees and its moving forward or back
DanPeled
requested changes
Jun 1, 2026
Comment on lines
44
to
247
| public void setRobotToTurret(Transform2d transform) { | ||
| this.robotToTurret = transform; | ||
| } | ||
|
|
||
| public LaunchCalculator withRobotToTurretOffset(Transform2d transform) { | ||
| this.setRobotToTurret(transform); | ||
| return this; | ||
| } | ||
|
|
||
| public void setTurretAngleFormat(AngleFormat format) { | ||
| m_turretAngleFormat = format; | ||
| } | ||
|
|
||
| public LaunchCalculator withTurretAngleFormat(AngleFormat format) { | ||
| setTurretAngleFormat(format); | ||
| return this; | ||
| } | ||
|
|
||
| private final MeasuredInterpolatingTreeMap<AngleUnit, Angle> launchHoodAngleMap = | ||
| new MeasuredInterpolatingTreeMap<>(Degrees); | ||
|
|
||
| private final MeasuredInterpolatingTreeMap<AngularVelocityUnit, AngularVelocity> | ||
| launchFlywheelSpeedMap = new MeasuredInterpolatingTreeMap<>(RPM); | ||
|
|
||
| private final MeasuredInterpolatingTreeMap<TimeUnit, Time> timeOfFlightMap = | ||
| new MeasuredInterpolatingTreeMap<>(Seconds); | ||
|
|
||
| { | ||
| /* | ||
| // Dan's testing | ||
| launchHoodAngleMap.put(1, Degrees.of(0)); | ||
| launchHoodAngleMap.put(1.5, Degrees.of(4)); | ||
| launchHoodAngleMap.put(3.208, Degrees.of(20)); | ||
| launchHoodAngleMap.put(2.679, Degrees.of(20.0)); | ||
| launchHoodAngleMap.put(2.464, Degrees.of(20.0)); | ||
| launchHoodAngleMap.put(1.887, Degrees.of(10.0)); | ||
| launchHoodAngleMap.put(1.967, Degrees.of(10.0)); | ||
| launchHoodAngleMap.put(3.677, Degrees.of(25.0)); | ||
| launchHoodAngleMap.put(3.0, Degrees.of(22)); | ||
|
|
||
| launchFlywheelSpeedMap.put(1, RPM.of(4500)); | ||
| launchFlywheelSpeedMap.put(3.0, RPM.of(4300.0)); | ||
| launchFlywheelSpeedMap.put(3.208, RPM.of(4700.0)); | ||
| launchFlywheelSpeedMap.put(2.679, RPM.of(4400.0)); | ||
| launchFlywheelSpeedMap.put(2.464, RPM.of(4000.0)); | ||
| launchFlywheelSpeedMap.put(1.887, RPM.of(4000.0)); | ||
| launchFlywheelSpeedMap.put(1.967, RPM.of(4200.0)); | ||
| launchFlywheelSpeedMap.put(3.677, RPM.of(4500.0)); | ||
| launchFlywheelSpeedMap.put(1.5, RPM.of(4500)); | ||
|
|
||
| // Daniel's testing | ||
| launchHoodAngleMap.put(3.11, Degrees.of(21.0)); | ||
| launchHoodAngleMap.put(4.04, Degrees.of(24.0)); | ||
| launchHoodAngleMap.put(4.44, Degrees.of(27.0)); | ||
| launchHoodAngleMap.put(5.16, Degrees.of(29.0)); | ||
|
|
||
| launchFlywheelSpeedMap.put(3.11, RPM.of(4500.0)); | ||
| launchFlywheelSpeedMap.put(4.04, RPM.of(4700.0)); | ||
| launchFlywheelSpeedMap.put(4.44, RPM.of(5000.0)); | ||
| launchFlywheelSpeedMap.put(5.16, RPM.of(5400.0)); | ||
| */ | ||
|
|
||
| double framesPerSecondGali = 30; | ||
| double framesPerSecondHadar = | ||
| 30; // both are supposed to have 30 fps according to google but not for sure | ||
|
|
||
| // timeOfFlightMap.put(2.039, Seconds.of()) | ||
| timeOfFlightMap.put(1, Seconds.of(1.07)); | ||
| timeOfFlightMap.put(1.5, Seconds.of(1.06)); | ||
| timeOfFlightMap.put(2, Seconds.of(1.04)); | ||
| timeOfFlightMap.put(3.01, Seconds.of(1.03)); | ||
| timeOfFlightMap.put(3.11, Seconds.of((93 - 62) / framesPerSecondGali)); | ||
| // timeOfFlightMap.put(3.11, (42 - 9) / framesPerSecondGali); | ||
| timeOfFlightMap.put(4.04, Seconds.of((146 - 115) / framesPerSecondGali)); | ||
| // timeOfFlightMap.put(4.04, (69 - 35) / framesPerSecondGali); | ||
| // timeOfFlightMap.put(4.44, (243 - 275) / framesPerSecondHadar); | ||
| // timeOfFlightMap.put(4.44, (484 - 518) / framesPerSecondHadar); | ||
| // timeOfFlightMap.put(4.44, (609 - 643) / framesPerSecondHadar); | ||
| timeOfFlightMap.put(4.44, Seconds.of((757 - 722) / framesPerSecondHadar)); | ||
| // timeOfFlightMap.put(5.16, (69 - 36) / framesPerSecondHadar); | ||
| // timeOfFlightMap.put(5.16, (189 - 155) / framesPerSecondHadar); | ||
| // timeOfFlightMap.put(5.16, (309 - 274) / framesPerSecondHadar); | ||
| timeOfFlightMap.put(5.16, Seconds.of((421 - 388) / framesPerSecondHadar)); | ||
| // incase I made any mistakes while looking at the videos, | ||
| // (ballBelowTopOfHubHoopFrame-ballOutsideOrTouchingOrangeWheel)/cameraFPS | ||
|
|
||
| launchHoodAngleMap.put(2.1681, Degrees.of(8.0)); | ||
| launchHoodAngleMap.put(1.119, Degrees.of(4.0)); | ||
| launchHoodAngleMap.put(2.8877, Degrees.of(17.0)); | ||
| launchHoodAngleMap.put(3.718, Degrees.of(19.0)); | ||
| launchHoodAngleMap.put(4.226, Degrees.of(21.0)); | ||
| launchHoodAngleMap.put(4.8338, Degrees.of(23.0)); | ||
| launchHoodAngleMap.put(5.215, Degrees.of(23.0)); | ||
| launchHoodAngleMap.put(6.52, Degrees.of(30.0)); | ||
| launchHoodAngleMap.put(2.62, Degrees.of(13.0)); | ||
|
|
||
| launchFlywheelSpeedMap.put(0.0, RPM.of(4000.0)); | ||
| launchFlywheelSpeedMap.put(0.0, RPM.of(3500.0)); | ||
| launchFlywheelSpeedMap.put(0.0, RPM.of(4400.0)); | ||
| launchFlywheelSpeedMap.put(0.0, RPM.of(4500.0)); | ||
| launchFlywheelSpeedMap.put(0.0, RPM.of(4200.0)); | ||
| launchFlywheelSpeedMap.put(0.0, RPM.of(4700.0)); | ||
| launchFlywheelSpeedMap.put(0.0, RPM.of(4700.0)); | ||
| launchFlywheelSpeedMap.put(0.0, RPM.of(5300.0)); | ||
| launchFlywheelSpeedMap.put(0.0, RPM.of(3900.0)); | ||
| } | ||
|
|
||
| public record ShotParams( | ||
| boolean valid, Angle turretAngle, Angle hoodAngle, AngularVelocity flywheelAngularVelocity) {} | ||
|
|
||
| public ShotParams calculate( | ||
| Pose2d robotPose, | ||
| ChassisSpeeds robotVelocityRobotRelative, | ||
| ChassisSpeeds robotVelocityFieldRelative, | ||
| Translation2d target) { | ||
|
|
||
| if (robotToTurret == null) { | ||
| throw new IllegalStateException("robotToTurret must be set before calling calculate()"); | ||
| } | ||
|
|
||
| Pose2d estimatedPose = | ||
| robotPose.exp( | ||
| new Twist2d( | ||
| robotVelocityRobotRelative.vxMetersPerSecond * phaseDelay, | ||
| robotVelocityRobotRelative.vyMetersPerSecond * phaseDelay, | ||
| robotVelocityRobotRelative.omegaRadiansPerSecond * phaseDelay)); | ||
|
|
||
| Transform2d turretOffset = new Transform2d(robotToTurret.getTranslation(), new Rotation2d()); | ||
| Pose2d turretPose = robotPose.transformBy(turretOffset); | ||
|
|
||
| double distance = target.getDistance(turretPose.getTranslation()); | ||
|
|
||
| double robotAngle = estimatedPose.getRotation().getRadians(); | ||
|
|
||
| double turretVelX = | ||
| robotVelocityFieldRelative.vxMetersPerSecond | ||
| + robotVelocityFieldRelative.omegaRadiansPerSecond | ||
| * (robotToTurret.getY() * Math.cos(robotAngle) | ||
| - robotToTurret.getX() * Math.sin(robotAngle)); | ||
|
|
||
| double turretVelY = | ||
| robotVelocityFieldRelative.vyMetersPerSecond | ||
| + robotVelocityFieldRelative.omegaRadiansPerSecond | ||
| * (robotToTurret.getX() * Math.cos(robotAngle) | ||
| - robotToTurret.getY() * Math.sin(robotAngle)); | ||
|
|
||
| Pose2d lookaheadPose = turretPose; | ||
| double lookaheadDistance = distance; | ||
|
|
||
| for (int i = 0; i < 20; i++) { | ||
| double tof = timeOfFlightMap.get(lookaheadDistance).in(Seconds); | ||
| double offsetX = turretVelX * tof; | ||
| double offsetY = turretVelY * tof; | ||
| lookaheadPose = | ||
| new Pose2d( | ||
| turretPose.getTranslation().plus(new Translation2d(offsetX, offsetY)), | ||
| turretPose.getRotation()); | ||
| lookaheadDistance = target.getDistance(lookaheadPose.getTranslation()); | ||
| } | ||
|
|
||
| Logger.recordOutput("LaunchCalculator/LookaheadDistance", lookaheadDistance); | ||
|
|
||
| Translation2d toTarget = target.minus(lookaheadPose.getTranslation()); | ||
| Rotation2d fieldTurretAngle = new Rotation2d(toTarget.getX(), toTarget.getY()); | ||
| Rotation2d turretAngle = fieldTurretAngle.minus(robotPose.getRotation()); | ||
| double deg = turretAngle.getDegrees(); | ||
|
|
||
| if (Math.abs(robotVelocityRobotRelative.vxMetersPerSecond) < turretYRobotHighSpeedCap) { | ||
| if ((deg < turretCloseToEdgeDegrees | ||
| && robotVelocityRobotRelative.vyMetersPerSecond | ||
| < 0) // at left side of hub and moving to the right | ||
| || (360 - deg < turretCloseToEdgeDegrees | ||
| && robotVelocityRobotRelative.vyMetersPerSecond | ||
| > 0)) { // at right side of hub and moving to the left | ||
| turretChangingTolerance = | ||
| turretMaxTolerance | ||
| * (Math.abs(robotVelocityRobotRelative.vxMetersPerSecond) | ||
| / turretYRobotHighSpeedCap); | ||
| } | ||
| } else { | ||
| turretChangingTolerance = 0; | ||
| } | ||
|
|
||
| if (m_turretAngleFormat.isCWPositive()) { | ||
| deg *= -1; // CCW → CW | ||
| } | ||
| if (m_turretAngleFormat.is0To360()) { | ||
| if (deg < 0) { | ||
| deg += 360; | ||
| if (turretChangingTolerance < 0) { | ||
| if (deg < 0) { | ||
| deg += 360; | ||
| } | ||
| deg %= 360; | ||
| } else { | ||
| if (deg < 360 + turretChangingTolerance && deg > 360) { // only if the tolerance is needed | ||
| deg = 360; // set setpoint to max angle | ||
| } else { | ||
| deg %= 360; | ||
| } | ||
| if (deg > 0 - turretChangingTolerance && deg < 0) { // only if the tolerance is needed | ||
| deg = 0; // set setpoint to min angle | ||
| } else { | ||
| deg += 360; | ||
| } | ||
| } |
Member
There was a problem hiding this comment.
Shouldnt be done inside LaunchCalculator, instead inside of ShootToTargetCmd and should affect whether or not the runAngleDirect method is run on the turret
DanPeled
requested changes
Jun 9, 2026
DanPeled
left a comment
Member
There was a problem hiding this comment.
I did it myself already, too slow too bad
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Added some code to possible fix turret oscillation when it is very cose to the max and min degrees and its moving forward or back