Skip to content

Commit 44c6057

Browse files
authored
Merge pull request #103 from strykeforce/AutoBalanceRecovery
More Robust Auto Balance
2 parents 41a944f + f92e82f commit 44c6057

File tree

4 files changed

+68
-13
lines changed

4 files changed

+68
-13
lines changed
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
max_velocity = 1.5 #m/s
22
max_acceleration = 2.0 #m/s
33
start_velocity = 0.0 #m/s
4-
end_velocity = 0.0 #m/s
4+
end_velocity = 1.0 #m/s
55
is_reversed = false
66
target_yaw = 0.0 #degrees
77

88
[start_pose]
9-
x = 0.0
9+
x = 0.3
1010
y = 0.0
1111
angle = 180.0
1212
[end_pose]
13-
x = -0.3
13+
x = 0.1
1414
y = 0.0
1515
angle = 180.0
1616

1717
[[internal_points]]
18-
x = -0.15
18+
x = 0.2
1919
y = 0.0

src/main/java/frc/robot/Constants.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ public static Translation2d[] getWheelLocationMeters() {
228228
public static final double kAutoBalanceEnableGyroThresholdDegrees = 8;
229229

230230
public static final double kAutoBalanceSlowDriveVel = 0.35; // 0.6
231+
public static final double kAutoBalanceRecoveryDriveVel = 0.25;
231232
public static final double kAutoBalanceFinalDriveVel = 1.0; // 0.5 0.75
232233
public static final double kAutoBalanceSlowdownTimeSec = 1.0; // 1.3-> 1.15
233234
public static final double kAutoBalanceStopThresholdDegrees = 1.5; // 1 0.6 1.5
@@ -239,6 +240,7 @@ public static Translation2d[] getWheelLocationMeters() {
239240
public static final double kPauseAutoBalanceTime = 1.0;
240241
public static final double kPulseSpeed = 0.5;
241242
public static final double kHoldSpeed = 0.1;
243+
public static final double kSettleTime = 1.0;
242244

243245
public static TalonSRXConfiguration getAzimuthTalonConfig() {
244246
// constructor sets encoder to Quad/CTRE_MagEncoder_Relative

src/main/java/frc/robot/subsystems/DriveSubsystem.java

Lines changed: 61 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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,

src/main/java/frc/robot/subsystems/IntakeSubsystem.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public void zeroIntake() {
128128
}
129129

130130
public void retractIntake(boolean rollersOff) {
131-
logger.info("Retract Intake to: {}", IntakeConstants.kRetractPosTicks);
131+
// logger.info("Retract Intake to: {}", IntakeConstants.kRetractPosTicks);
132132
currIntakeState = IntakeState.RETRACTED;
133133
if (rollersOff) intakeOpenLoop(0);
134134
setToGreaterPos = IntakeConstants.kRetractPosTicks > getPos();

0 commit comments

Comments
 (0)