Skip to content

Turret Oscillation Prevention - #20

Open
Daniel-Tsur wants to merge 4 commits into
mainfrom
feature/turret_oscillation_prevention
Open

Turret Oscillation Prevention#20
Daniel-Tsur wants to merge 4 commits into
mainfrom
feature/turret_oscillation_prevention

Conversation

@Daniel-Tsur

Copy link
Copy Markdown
Contributor

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

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
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;
}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldnt be done inside LaunchCalculator, instead inside of ShootToTargetCmd and should affect whether or not the runAngleDirect method is run on the turret

@Daniel-Tsur
Daniel-Tsur requested a review from DanPeled June 9, 2026 16:00

@DanPeled DanPeled left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did it myself already, too slow too bad

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants