|
| 1 | +package com.flash3388.flashlib.frc.robot.systems; |
| 2 | + |
| 3 | +import com.flash3388.flashlib.frc.robot.RunningFrcRobot; |
| 4 | +import com.flash3388.flashlib.frc.robot.simulation.SimTankDriveSystem; |
| 5 | +import com.flash3388.flashlib.frc.robot.simulation.TankDriveSim; |
| 6 | +import com.flash3388.flashlib.io.devices.Encoder; |
| 7 | +import com.flash3388.flashlib.io.devices.Gyro; |
| 8 | +import com.flash3388.flashlib.io.devices.SpeedController; |
| 9 | +import com.flash3388.flashlib.scheduling.actions.ActionFlag; |
| 10 | +import com.flash3388.flashlib.scheduling.actions.Actions; |
| 11 | +import edu.wpi.first.math.geometry.Pose2d; |
| 12 | +import edu.wpi.first.math.geometry.Rotation2d; |
| 13 | +import edu.wpi.first.math.kinematics.DifferentialDriveOdometry; |
| 14 | + |
| 15 | +public class FrcTankDriveSystem extends SimTankDriveSystem { |
| 16 | + |
| 17 | + private final Encoder mRightEncoder; |
| 18 | + private final Encoder mLeftEncoder; |
| 19 | + private final Gyro mGyro; |
| 20 | + private final DifferentialDriveOdometry mOdometry; |
| 21 | + |
| 22 | + public FrcTankDriveSystem(SpeedController rightController, |
| 23 | + SpeedController leftController, |
| 24 | + Encoder rightEncoder, |
| 25 | + Encoder leftEncoder, |
| 26 | + Gyro gyro, |
| 27 | + TankDriveSim driveSim) { |
| 28 | + super(rightController, leftController, driveSim); |
| 29 | + |
| 30 | + mRightEncoder = rightEncoder; |
| 31 | + mLeftEncoder = leftEncoder; |
| 32 | + mGyro = gyro; |
| 33 | + |
| 34 | + if (!RunningFrcRobot.getControl().isRuntimeSimulation()) { |
| 35 | + mOdometry = new DifferentialDriveOdometry(Rotation2d.fromDegrees(0), 0, 0); |
| 36 | + |
| 37 | + // this action will update our odometry data |
| 38 | + Actions.fromRunnable(this::update) |
| 39 | + .configure() |
| 40 | + .setName("FrcTankDriveSystem-Updater") |
| 41 | + .addFlags(ActionFlag.RUN_ON_DISABLED) |
| 42 | + .save() |
| 43 | + .start(); |
| 44 | + } else { |
| 45 | + mOdometry = null; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + public Pose2d getPose() { |
| 50 | + if (RunningFrcRobot.getControl().isRuntimeSimulation()) { |
| 51 | + return getSimPose(); |
| 52 | + } |
| 53 | + |
| 54 | + return mOdometry.getPoseMeters(); |
| 55 | + } |
| 56 | + |
| 57 | + public double getLeftPositionMeters() { |
| 58 | + if (RunningFrcRobot.getControl().isRuntimeSimulation()) { |
| 59 | + return getSimLeftPositionMeters(); |
| 60 | + } |
| 61 | + |
| 62 | + return mLeftEncoder.getDistance(); |
| 63 | + } |
| 64 | + |
| 65 | + public double getRightPositionMeters() { |
| 66 | + if (RunningFrcRobot.getControl().isRuntimeSimulation()) { |
| 67 | + return getSimRightPositionMeters(); |
| 68 | + } |
| 69 | + |
| 70 | + return mRightEncoder.getDistance(); |
| 71 | + } |
| 72 | + |
| 73 | + public double getAngleDegrees() { |
| 74 | + if (RunningFrcRobot.getControl().isRuntimeSimulation()) { |
| 75 | + return getSimAngleDegrees(); |
| 76 | + } |
| 77 | + |
| 78 | + return mGyro.getAngle(); |
| 79 | + } |
| 80 | + |
| 81 | + private void update() { |
| 82 | + mOdometry.update( |
| 83 | + Rotation2d.fromDegrees(getAngleDegrees()), |
| 84 | + getLeftPositionMeters(), |
| 85 | + getRightPositionMeters() |
| 86 | + ); |
| 87 | + } |
| 88 | +} |
0 commit comments