@@ -135,6 +135,7 @@ public class DriveSubsystem extends MeasurableSubsystem {
135135 private double autoBalanceStableCounts = 0 ;
136136 private Timer autoBalanceTimer = new Timer ();
137137 private Timer autoBalancePulseTimer = new Timer ();
138+ private Timer autoBalanceRecoveryTimer = new Timer ();
138139 public boolean autoBalanceReadjust = false ;
139140 public boolean isOnAllianceSide ;
140141 public boolean autoBalanceTempStable = false ;
@@ -143,6 +144,9 @@ public class DriveSubsystem extends MeasurableSubsystem {
143144 private double sumRoll = 0 ;
144145 private double avgStartingRoll = 0 ;
145146 private boolean yawAdjustmentActive = false ;
147+ private double recoveryStartingPitch ;
148+ private boolean recoveryStartingPitchSet ;
149+ private double autoBalanceGyroDirection = 0 ;
146150 // private boolean isAutoDriveFinished = false;
147151
148152 public DriveSubsystem (Constants constants ) {
@@ -451,7 +455,7 @@ public void autoBalance(boolean isOnAllianceSide) {
451455 sumRoll = 0 ;
452456 avgStartingRoll = 0 ;
453457 this .isOnAllianceSide = isOnAllianceSide ;
454- tempRoll = Math . abs ( getGyroPitch () );
458+ tempRoll = getGyroPitch ();
455459 logger .info (
456460 "Starting AutoBalance: tempRoll: {}, Alliance: {}, isOnAllianceSide: {}" ,
457461 tempRoll ,
@@ -594,6 +598,7 @@ public void periodic() {
594598 logger .info ("{} -> AUTO_BALANCE_AVERAGE" , currDriveState );
595599 currDriveState = DriveStates .AUTO_BALANCE_AVERAGE ;
596600 autoBalanceTimer .restart ();
601+ autoBalanceGyroDirection = getGyroPitch () - tempRoll ;
597602 }
598603 break ;
599604 case AUTO_BALANCE_AVERAGE :
@@ -605,11 +610,8 @@ public void periodic() {
605610 logger .info ("Average Starting Roll: {}" , avgStartingRoll );
606611 logger .info ("{} -> AUTO_BALANCE" , currDriveState );
607612 currDriveState = DriveStates .AUTO_BALANCE ;
608- if (isOnAllianceSide ) {
609- move (DriveConstants .kAutoBalanceSlowDriveVel , 0 , 0 , false );
610- } else {
611- move (-DriveConstants .kAutoBalanceSlowDriveVel , 0 , 0 , false );
612- }
613+ if (isOnAllianceSide ) move (DriveConstants .kAutoBalanceSlowDriveVel , 0 , 0 , false );
614+ else move (-DriveConstants .kAutoBalanceSlowDriveVel , 0 , 0 , false );
613615 autoBalanceTimer .reset ();
614616 }
615617 break ;
@@ -622,8 +624,58 @@ public void periodic() {
622624 "AutoBalance Stop: Gyro Roll: {}, trigger Difference: {}" ,
623625 getGyroPitch (),
624626 Math .abs (avgStartingRoll ) - Math .abs (getGyroPitch ()));
625- logger .info ("{} -> AUTO_BALANCE_FINISHED" , currDriveState );
626- currDriveState = DriveStates .AUTO_BALANCE_FINISHED ;
627+ logger .info ("{} -> AUTO_BALANCE_RECOVERY" , currDriveState );
628+ currDriveState = DriveStates .AUTO_BALANCE_RECOVERY ;
629+
630+ autoBalanceRecoveryTimer .reset ();
631+ autoBalanceRecoveryTimer .start ();
632+ recoveryStartingPitchSet = false ;
633+ }
634+ break ;
635+ case AUTO_BALANCE_RECOVERY :
636+ if (autoBalanceRecoveryTimer .hasElapsed (DriveConstants .kSettleTime )) {
637+ autoBalanceRecoveryTimer .stop ();
638+ // isOnAllianceSide is true move positive
639+ if (!recoveryStartingPitchSet ) {
640+ recoveryStartingPitch = getGyroPitch ();
641+ recoveryStartingPitchSet = true ;
642+ }
643+ // if (isOnAllianceSide)
644+ if ((Math .abs (tempRoll - getGyroPitch ()) > 2 )
645+ && (Math .abs (recoveryStartingPitch ) - Math .abs (getGyroPitch ()) < 1.5 )) {
646+ // move(-DriveConstants.kAutoBalanceRecoveryDriveVel, 0, 0, false);
647+ // if (((autoBalanceGyroDirection > 0) && (((getGyroPitch() - tempRoll) > 0) &&
648+ // isOnAllianceSide)) || ((autoBalanceGyroDirection < 0) && (((getGyroPitch() -
649+ // tempRoll) < 0) && isOnAllianceSide))) {}
650+ if ((autoBalanceGyroDirection > 0 )) {
651+ // Positive Gyro Direction
652+ if ((getGyroPitch () - tempRoll ) > 0 ) {
653+ if (isOnAllianceSide )
654+ move (DriveConstants .kAutoBalanceRecoveryDriveVel , 0 , 0 , false );
655+ else move (-DriveConstants .kAutoBalanceRecoveryDriveVel , 0 , 0 , false );
656+ } else if ((getGyroPitch () - tempRoll ) < 0 ) {
657+ if (isOnAllianceSide )
658+ move (-DriveConstants .kAutoBalanceRecoveryDriveVel , 0 , 0 , false );
659+ else move (DriveConstants .kAutoBalanceRecoveryDriveVel , 0 , 0 , false );
660+ }
661+ } else if ((autoBalanceGyroDirection < 0 )) {
662+ // negative gyro direction
663+ if ((getGyroPitch () - tempRoll ) > 0 ) {
664+ if (isOnAllianceSide )
665+ move (-DriveConstants .kAutoBalanceRecoveryDriveVel , 0 , 0 , false );
666+ else move (DriveConstants .kAutoBalanceRecoveryDriveVel , 0 , 0 , false );
667+ } else if ((getGyroPitch () - tempRoll ) < 0 ) {
668+ if (isOnAllianceSide )
669+ move (DriveConstants .kAutoBalanceRecoveryDriveVel , 0 , 0 , false );
670+ else move (-DriveConstants .kAutoBalanceRecoveryDriveVel , 0 , 0 , false );
671+ }
672+ } // 9 if pos or like -8 if neg
673+ } else {
674+ drive (0 , 0 , 0 );
675+ xLock ();
676+ logger .info ("{} -> AUTO_BALANCE_FINISHED" , currDriveState );
677+ currDriveState = DriveStates .AUTO_BALANCE_FINISHED ;
678+ }
627679 }
628680 break ;
629681 case AUTO_BALANCE_FINISHED :
@@ -961,6 +1013,7 @@ public enum DriveStates {
9611013 AUTO_BALANCE_DRIVE ,
9621014 AUTO_BALANCE_AVERAGE ,
9631015 AUTO_BALANCE ,
1016+ AUTO_BALANCE_RECOVERY ,
9641017 AUTO_BALANCE_FINISHED ,
9651018 AUTO_BALANCE_PULSE ,
9661019 AUTO_BALANCE_CHECK ,
0 commit comments