|
| 1 | +package com.stuypulse.robot.commands.swerve; |
| 2 | + |
| 3 | +import com.stuypulse.robot.Robot; |
| 4 | +import com.stuypulse.robot.constants.Field; |
| 5 | +import com.stuypulse.robot.subsystems.conveyor.Conveyor; |
| 6 | +import com.stuypulse.robot.subsystems.intake.Intake; |
| 7 | +import com.stuypulse.robot.subsystems.odometry.Odometry; |
| 8 | +import com.stuypulse.robot.subsystems.shooter.Shooter; |
| 9 | +import com.stuypulse.robot.subsystems.swerve.SwerveDrive; |
| 10 | +import com.stuypulse.stuylib.control.angle.AngleController; |
| 11 | +import com.stuypulse.stuylib.control.angle.feedback.AnglePIDController; |
| 12 | +import com.stuypulse.stuylib.math.Angle; |
| 13 | +import com.stuypulse.stuylib.math.Vector2D; |
| 14 | +import com.stuypulse.stuylib.streams.booleans.BStream; |
| 15 | +import com.stuypulse.stuylib.streams.booleans.filters.BDebounce; |
| 16 | +import com.stuypulse.stuylib.streams.numbers.IStream; |
| 17 | +import com.stuypulse.stuylib.streams.numbers.filters.LowPassFilter; |
| 18 | +import com.stuypulse.stuylib.util.AngleVelocity; |
| 19 | +import com.stuypulse.robot.constants.Settings; |
| 20 | +import com.stuypulse.robot.constants.Settings.Swerve.Assist; |
| 21 | + |
| 22 | +import edu.wpi.first.math.geometry.Rotation2d; |
| 23 | +import edu.wpi.first.math.geometry.Translation2d; |
| 24 | +import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard; |
| 25 | +import edu.wpi.first.wpilibj2.command.Command; |
| 26 | + |
| 27 | + |
| 28 | +public class SwerveDriveFerryFromCurrentPosition extends Command { |
| 29 | + |
| 30 | + private final SwerveDrive swerve; |
| 31 | + private final Odometry odometry; |
| 32 | + private final Shooter shooter; |
| 33 | + private final Conveyor conveyor; |
| 34 | + private final Intake intake; |
| 35 | + |
| 36 | + private final AngleController controller; |
| 37 | + private final IStream angleVelocity; |
| 38 | + |
| 39 | + private final BStream shoot; |
| 40 | + |
| 41 | + public SwerveDriveFerryFromCurrentPosition() { |
| 42 | + swerve = SwerveDrive.getInstance(); |
| 43 | + shooter = Shooter.getInstance(); |
| 44 | + odometry = Odometry.getInstance(); |
| 45 | + conveyor = Conveyor.getInstance(); |
| 46 | + intake = Intake.getInstance(); |
| 47 | + |
| 48 | + controller = new AnglePIDController(Assist.kP, Assist.kI, Assist.kD) |
| 49 | + .setOutputFilter(x -> -x); |
| 50 | + |
| 51 | + AngleVelocity derivative = new AngleVelocity(); |
| 52 | + |
| 53 | + angleVelocity = IStream.create(() -> derivative.get(Angle.fromRotation2d(getTargetAngle()))) |
| 54 | + .filtered(new LowPassFilter(Assist.ANGLE_DERIV_RC)) |
| 55 | + // make angleVelocity contribute less once distance is less than REDUCED_FF_DIST |
| 56 | + // so that angular velocity doesn't oscillate |
| 57 | + .filtered(x -> x * Math.min(1, getDistanceToTarget() / Assist.REDUCED_FF_DIST)) |
| 58 | + .filtered(x -> -x); |
| 59 | + |
| 60 | + shoot = BStream.create(() -> Math.abs(controller.getError().toDegrees()) < getAngleTolerance()) |
| 61 | + .filtered(new BDebounce.Falling(0.4)); |
| 62 | + |
| 63 | + addRequirements(swerve, shooter, odometry, conveyor, intake); |
| 64 | + } |
| 65 | + |
| 66 | + // returns pose of close amp corner |
| 67 | + private Translation2d getTargetPose() { |
| 68 | + |
| 69 | + Translation2d pose = Robot.isBlue() |
| 70 | + ? new Translation2d(0, Field.WIDTH - 1.5) |
| 71 | + : new Translation2d(0, 1.5); |
| 72 | + |
| 73 | + return pose; |
| 74 | + |
| 75 | + // Translation2d offset = odometry.getRobotVelocity() |
| 76 | + // .times(NOTE_TRAVEL_TIME); |
| 77 | + |
| 78 | + // return pose.minus(offset); |
| 79 | + } |
| 80 | + |
| 81 | + private Rotation2d getTargetAngle() { |
| 82 | + return odometry.getPose().getTranslation().minus(getTargetPose()).getAngle(); |
| 83 | + } |
| 84 | + |
| 85 | + private double getDistanceToTarget() { |
| 86 | + return odometry.getPose().getTranslation().getDistance(getTargetPose()); |
| 87 | + } |
| 88 | + |
| 89 | + private double getAngleTolerance() { |
| 90 | + double distance = getDistanceToTarget(); |
| 91 | + final double SHOT_LANDING_TOLERANCE = 1.0; |
| 92 | + |
| 93 | + return Math.toDegrees(Math.atan(SHOT_LANDING_TOLERANCE / distance)); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public void initialize() { |
| 98 | + swerve.stop(); |
| 99 | + shooter.setTargetSpeeds(Settings.Shooter.FERRY); |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public void execute() { |
| 104 | + double omega = angleVelocity.get() + controller.update( |
| 105 | + Angle.fromRotation2d(getTargetAngle()), |
| 106 | + Angle.fromRotation2d(odometry.getPose().getRotation())); |
| 107 | + |
| 108 | + SmartDashboard.putNumber("Ferry/Angle Tolerance", getAngleTolerance()); |
| 109 | + |
| 110 | + swerve.drive(new Vector2D(new Translation2d(0, 0)), omega); |
| 111 | + |
| 112 | + if (shoot.get()) { |
| 113 | + intake.acquire(); |
| 114 | + conveyor.toShooter(); |
| 115 | + } else { |
| 116 | + intake.stop(); |
| 117 | + conveyor.stop(); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public void end(boolean interrupted) { |
| 123 | + intake.stop(); |
| 124 | + conveyor.stop(); |
| 125 | + shooter.setTargetSpeeds(Settings.Shooter.PODIUM_SHOT); |
| 126 | + } |
| 127 | +} |
0 commit comments