Skip to content

Commit 5f88916

Browse files
committed
Reset drivetrain modules when override or timeout
1 parent 120330a commit 5f88916

File tree

9 files changed

+152
-23
lines changed

9 files changed

+152
-23
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ targetCompatibility = JavaVersion.VERSION_11
99
def includeDesktopSupport = true
1010

1111
group 'com.thegongoliers'
12-
version '6.1.1'
12+
version '6.1.2'
1313

1414

1515
publishing {

src/main/java/com/thegongoliers/output/drivetrain/AnchorModule.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
import com.kylecorry.pid.PID;
44
import com.thegongoliers.input.odometry.DistanceSensor;
5+
import com.thegongoliers.utils.Resettable;
56

67
/**
78
* A drivetrain module which will lock the drivetrain in place while a trigger condition is met
89
*/
9-
public class AnchorModule implements DriveModule {
10+
public class AnchorModule implements DriveModule, Resettable {
1011

1112
private DistanceSensor mLeftDistanceSupplier, mRightDistanceSupplier;
1213
private PID mLeftPID, mRightPID;
@@ -88,4 +89,9 @@ public void setPID(PID pid) {
8889
mLeftPID = new PID(pid.getP(), pid.getI(), pid.getD());
8990
mRightPID = new PID(pid.getP(), pid.getI(), pid.getD());
9091
}
92+
93+
@Override
94+
public void reset() {
95+
stopHoldingPosition();
96+
}
9197
}

src/main/java/com/thegongoliers/output/drivetrain/DriveSpeed.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.thegongoliers.math.GMath;
44

5+
import java.util.Objects;
6+
57
/**
68
* A class representing the speed of a differential drivetrain
79
*/
@@ -85,4 +87,16 @@ public DriveSpeed plus(DriveSpeed other){
8587
return new DriveSpeed(GMath.clamp(l, -1, 1), GMath.clamp(r, -1, 1));
8688
}
8789

90+
@Override
91+
public boolean equals(Object o) {
92+
if (this == o) return true;
93+
if (o == null || getClass() != o.getClass()) return false;
94+
DriveSpeed that = (DriveSpeed) o;
95+
return Double.compare(that.left, left) == 0 && Double.compare(that.right, right) == 0;
96+
}
97+
98+
@Override
99+
public int hashCode() {
100+
return Objects.hash(left, right);
101+
}
88102
}

src/main/java/com/thegongoliers/output/drivetrain/ModularDrivetrain.java

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,20 @@
99
import com.thegongoliers.input.time.RobotClock;
1010
import com.thegongoliers.output.interfaces.Drivetrain;
1111

12+
import com.thegongoliers.utils.Resettable;
1213
import edu.wpi.first.wpilibj.drive.DifferentialDrive;
1314

1415
/**
1516
* A wrapper class for a drivetrain which adds support for drive modules. Does not apply drive modules during tank driving.
1617
*/
17-
public class ModularDrivetrain implements Drivetrain {
18+
public class ModularDrivetrain implements Drivetrain, Resettable {
1819

1920
private Drivetrain drivetrain;
2021
private List<DriveModule> modules;
2122
private DriveSpeed currentSpeed;
2223
private Clock clock;
2324
private double lastTime;
25+
private double resetThreshold = Double.POSITIVE_INFINITY;
2426

2527
/**
2628
* Default constructor
@@ -74,6 +76,10 @@ public void tank(double leftSpeed, double rightSpeed) {
7476
double time = clock.getTime();
7577
double dt = time - lastTime;
7678

79+
if (dt >= resetThreshold){
80+
reset();
81+
}
82+
7783
var overrides = modules.stream().filter(DriveModule::overridesUser).collect(Collectors.toList());
7884
DriveModule override = null;
7985
if (!overrides.isEmpty()) {
@@ -94,8 +100,8 @@ public void tank(double leftSpeed, double rightSpeed) {
94100
if (override == null) {
95101
desiredSpeed = module.run(currentSpeed, desiredSpeed, dt);
96102
} else {
97-
// Run the module to ensure it stay updated, but don't update the desired speed since it should be overridden
98-
module.run(currentSpeed, desiredSpeed, dt);
103+
// Reset the modules not being run
104+
resetModule(module);
99105
}
100106
}
101107

@@ -133,6 +139,16 @@ public void removeModule(DriveModule module) {
133139
modules.remove(module);
134140
}
135141

142+
/**
143+
* Reset a module
144+
* @param module the module to reset
145+
*/
146+
public void resetModule(DriveModule module){
147+
if (module instanceof Resettable){
148+
((Resettable)module).reset();
149+
}
150+
}
151+
136152
/**
137153
* Get all the installed modules
138154
*
@@ -159,4 +175,16 @@ public <T> T getInstalledModule(Class<T> cls) {
159175
}
160176

161177

178+
@Override
179+
public void reset() {
180+
modules.forEach(this::resetModule);
181+
}
182+
183+
/**
184+
* If a drive method was not called within the reset threshold, all modules will be reset on the next invocation.
185+
* @param resetThreshold The reset threshold in seconds.
186+
*/
187+
public void setInactiveResetSeconds(double resetThreshold){
188+
this.resetThreshold = resetThreshold;
189+
}
162190
}

src/main/java/com/thegongoliers/output/drivetrain/PathFollowerModule.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@
1212
import com.thegongoliers.paths.PathStep;
1313
import com.thegongoliers.paths.PathStepType;
1414

15+
import com.thegongoliers.utils.Resettable;
1516
import edu.wpi.first.wpilibj.Encoder;
1617
import edu.wpi.first.wpilibj.interfaces.Gyro;
1718

1819
/**
1920
* A drivetrain module which will follow a path when activated
2021
*/
2122
@UsedInCompetition(team = "5112", year = "2020")
22-
public class PathFollowerModule implements DriveModule {
23+
public class PathFollowerModule implements DriveModule, Resettable {
2324

2425
private static final double DEFAULT_FORWARD_TOLERANCE = 0.1;
2526
private static final double DEFAULT_TURN_TOLERANCE = 0.1;
@@ -35,6 +36,9 @@ public class PathFollowerModule implements DriveModule {
3536
private SimplePath currentPath;
3637
private int currentStepIdx;
3738

39+
private double distanceZero;
40+
private double angleZero;
41+
3842

3943
public PathFollowerModule(Gyro gyro, List<EncoderSensor> encoders, double forwardStrength, double turnStrength){
4044
this(gyro, encoders, new PID(forwardStrength, 0, 0), new PID(turnStrength, 0, 0));
@@ -185,7 +189,7 @@ private DriveSpeed getToNextStep() {
185189
}
186190

187191
private DriveSpeed rotateTowards(double angle){
188-
return DriveSpeed.fromArcade(0, mTurnPID.calculate(mGyro.getAngle(), angle));
192+
return DriveSpeed.fromArcade(0, mTurnPID.calculate(getAngle(), angle));
189193
}
190194

191195
private DriveSpeed driveTowards(double distance){
@@ -202,11 +206,20 @@ private boolean isDoneDriving(){
202206
}
203207

204208
private double getDistance(){
205-
return mEncoder.getDistance();
209+
return mEncoder.getDistance() - distanceZero;
210+
}
211+
212+
private double getAngle(){
213+
return mGyro.getAngle() - angleZero;
206214
}
207215

208216
private void zeroSensors(){
209-
mEncoder.reset();
210-
mGyro.reset();
217+
distanceZero = mEncoder.getDistance();
218+
angleZero = mGyro.getAngle();
219+
}
220+
221+
@Override
222+
public void reset() {
223+
stopFollowingPath();
211224
}
212225
}

src/main/java/com/thegongoliers/output/drivetrain/StabilityModule.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
import com.thegongoliers.input.time.Clock;
44
import com.thegongoliers.input.time.RobotClock;
55

6+
import com.thegongoliers.utils.Resettable;
67
import edu.wpi.first.wpilibj.interfaces.Gyro;
78

89
/**
910
* A drivetrain module which will stabilize the drivetrain (rotation-wise) while attempting to drive straight
1011
*/
11-
public class StabilityModule implements DriveModule {
12+
public class StabilityModule implements DriveModule, Resettable {
1213

1314
private static final double DEFAULT_TURN_THRESHOLD = 0.01;
1415

@@ -28,7 +29,6 @@ public class StabilityModule implements DriveModule {
2829
* @param settlingTime the amount of time to allow the drivetrain to settle after turn inputs stop before applying turn corrections
2930
*/
3031
public StabilityModule(Gyro gyro, double strength, double settlingTime){
31-
super();
3232
setGyro(gyro);
3333
setStrength(strength);
3434
setSettlingTime(settlingTime);
@@ -81,6 +81,7 @@ public void setStrength(double strength){
8181
/**
8282
* Resets the desired heading of the robot to match the gyro
8383
**/
84+
@Override
8485
public void reset() {
8586
updateDesiredHeading();
8687
}

src/main/java/com/thegongoliers/output/drivetrain/TargetAlignmentModule.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
import com.kylecorry.pid.PID;
44
import com.thegongoliers.input.vision.TargetingCamera;
55
import com.thegongoliers.math.GMath;
6+
import com.thegongoliers.utils.Resettable;
67

78
/**
89
* A drivetrain module which align to vision targets
910
*/
10-
public class TargetAlignmentModule implements DriveModule {
11+
public class TargetAlignmentModule implements DriveModule, Resettable {
1112

1213
private static final double DEFAULT_RANGE_THRESHOLD = 0.1;
1314
private static final double DEFAULT_AIM_THRESHOLD = 0.1;
@@ -162,4 +163,9 @@ private boolean shouldOnlyAlignAim() {
162163
GMath.approximately(mRangePID.getI(), 0) &&
163164
GMath.approximately(mRangePID.getD(), 0);
164165
}
166+
167+
@Override
168+
public void reset() {
169+
stopAligning();
170+
}
165171
}

0 commit comments

Comments
 (0)