Skip to content
This repository was archived by the owner on Dec 30, 2024. It is now read-only.

Commit c04de04

Browse files
committed
makes limiting the max power better
1 parent a2d896b commit c04de04

File tree

2 files changed

+42
-34
lines changed

2 files changed

+42
-34
lines changed

TeamCode/src/main/java/org/firstinspires/ftc/teamcode/pedroPathing/follower/DriveVectorScaler.java

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
public class DriveVectorScaler {
1616
// This is ordered left front, left back, right front, right back. These are also normalized.
1717
private Vector[] mecanumVectors;
18+
private double maxPowerScaling = 1;
1819

1920
/**
2021
* This creates a new DriveVectorScaler, which takes in various movement vectors and outputs
@@ -52,9 +53,9 @@ public DriveVectorScaler(Vector frontLeftVector) {
5253
*/
5354
public double[] getDrivePowers(Vector correctivePower, Vector headingPower, Vector pathingPower, double robotHeading) {
5455
// clamps down the magnitudes of the input vectors
55-
if (correctivePower.getMagnitude() > 1) correctivePower.setMagnitude(1);
56-
if (headingPower.getMagnitude() > 1) headingPower.setMagnitude(1);
57-
if (pathingPower.getMagnitude() > 1) pathingPower.setMagnitude(1);
56+
if (correctivePower.getMagnitude() > maxPowerScaling) correctivePower.setMagnitude(maxPowerScaling);
57+
if (headingPower.getMagnitude() > maxPowerScaling) headingPower.setMagnitude(maxPowerScaling);
58+
if (pathingPower.getMagnitude() > maxPowerScaling) pathingPower.setMagnitude(maxPowerScaling);
5859

5960
// the powers for the wheel vectors
6061
double [] wheelPowers = new double[4];
@@ -65,16 +66,16 @@ public double[] getDrivePowers(Vector correctivePower, Vector headingPower, Vect
6566
// this contains the pathing vectors, one for each side (heading control requires 2)
6667
Vector[] truePathingVectors = new Vector[2];
6768

68-
if (correctivePower.getMagnitude() == 1) {
69-
// checks for corrective power equal to 1 in magnitude. if equal to one, then set pathing power to that
69+
if (correctivePower.getMagnitude() == maxPowerScaling) {
70+
// checks for corrective power equal to max power scaling in magnitude. if equal, then set pathing power to that
7071
truePathingVectors[0] = MathFunctions.copyVector(correctivePower);
7172
truePathingVectors[1] = MathFunctions.copyVector(correctivePower);
7273
} else {
7374
// corrective power did not take up all the power, so add on heading power
7475
Vector leftSideVector = MathFunctions.subtractVectors(correctivePower, headingPower);
7576
Vector rightSideVector = MathFunctions.addVectors(correctivePower, headingPower);
7677

77-
if (leftSideVector.getMagnitude() > 1 || rightSideVector.getMagnitude() > 1) {
78+
if (leftSideVector.getMagnitude() > maxPowerScaling || rightSideVector.getMagnitude() > maxPowerScaling) {
7879
//if the combined corrective and heading power is greater than 1, then scale down heading power
7980
double headingScalingFactor = Math.min(findNormalizingScaling(correctivePower, headingPower), findNormalizingScaling(correctivePower, MathFunctions.scalarMultiplyVector(headingPower, -1)));
8081
truePathingVectors[0] = MathFunctions.subtractVectors(correctivePower, MathFunctions.scalarMultiplyVector(headingPower, headingScalingFactor));
@@ -84,7 +85,7 @@ public double[] getDrivePowers(Vector correctivePower, Vector headingPower, Vect
8485
Vector leftSideVectorWithPathing = MathFunctions.addVectors(leftSideVector, pathingPower);
8586
Vector rightSideVectorWithPathing = MathFunctions.addVectors(rightSideVector, pathingPower);
8687

87-
if (leftSideVectorWithPathing.getMagnitude() > 1 || rightSideVectorWithPathing.getMagnitude() > 1) {
88+
if (leftSideVectorWithPathing.getMagnitude() > maxPowerScaling || rightSideVectorWithPathing.getMagnitude() > maxPowerScaling) {
8889
// too much power now, so we scale down the pathing vector
8990
double pathingScalingFactor = Math.min(findNormalizingScaling(leftSideVector, pathingPower), findNormalizingScaling(rightSideVector, pathingPower));
9091
truePathingVectors[0] = MathFunctions.addVectors(leftSideVector, MathFunctions.scalarMultiplyVector(pathingPower, pathingScalingFactor));
@@ -113,7 +114,7 @@ public double[] getDrivePowers(Vector correctivePower, Vector headingPower, Vect
113114
wheelPowers[3] = (mecanumVectorsCopy[2].getXComponent()*truePathingVectors[1].getYComponent() - truePathingVectors[1].getXComponent()*mecanumVectorsCopy[2].getYComponent()) / (mecanumVectorsCopy[2].getXComponent()*mecanumVectorsCopy[3].getYComponent() - mecanumVectorsCopy[3].getXComponent()*mecanumVectorsCopy[2].getYComponent());
114115

115116
double wheelPowerMax = Math.max(Math.max(Math.abs(wheelPowers[0]), Math.abs(wheelPowers[1])), Math.max(Math.abs(wheelPowers[2]), Math.abs(wheelPowers[3])));
116-
if (wheelPowerMax > 1) {
117+
if (wheelPowerMax > maxPowerScaling) {
117118
wheelPowers[0] /= wheelPowerMax;
118119
wheelPowers[1] /= wheelPowerMax;
119120
wheelPowers[2] /= wheelPowerMax;
@@ -126,12 +127,12 @@ public double[] getDrivePowers(Vector correctivePower, Vector headingPower, Vect
126127
/**
127128
* This takes in two Vectors, one static and one variable, and returns the scaling factor that,
128129
* when multiplied to the variable Vector, results in magnitude of the sum of the static Vector
129-
* and the scaled variable Vector being 1.
130+
* and the scaled variable Vector being the max power scaling.
130131
*
131132
* IMPORTANT NOTE: I did not intend for this to be used for anything other than the method above
132-
* this one in this class, so there will be errors if you input Vectors of length greater than 1,
133+
* this one in this class, so there will be errors if you input Vectors of length greater than maxPowerScaling,
133134
* and it will scale up the variable Vector if the magnitude of the sum of the two input Vectors
134-
* isn't greater than 1. So, just don't use this elsewhere. There's gotta be a better way to do
135+
* isn't greater than maxPowerScaling. So, just don't use this elsewhere. There's gotta be a better way to do
135136
* whatever you're trying to do.
136137
*
137138
* I know that this is used outside of this class, however, I created this method so I get to
@@ -140,13 +141,32 @@ public double[] getDrivePowers(Vector correctivePower, Vector headingPower, Vect
140141
*
141142
* @param staticVector the Vector that is held constant.
142143
* @param variableVector the Vector getting scaled to make the sum of the input Vectors have a
143-
* magnitude of 1.
144+
* magnitude of maxPowerScaling.
144145
* @return returns the scaling factor for the variable Vector.
145146
*/
146147
public double findNormalizingScaling(Vector staticVector, Vector variableVector) {
147148
double a = Math.pow(variableVector.getXComponent(), 2) + Math.pow(variableVector.getYComponent(), 2);
148149
double b = staticVector.getXComponent() * variableVector.getXComponent() + staticVector.getYComponent() * variableVector.getYComponent();
149-
double c = Math.pow(staticVector.getXComponent(), 2) + Math.pow(staticVector.getYComponent(), 2) - 1.0;
150+
double c = Math.pow(staticVector.getXComponent(), 2) + Math.pow(staticVector.getYComponent(), 2) - Math.pow(maxPowerScaling, 2);
150151
return (-b + Math.sqrt(Math.pow(b, 2) - a*c))/(a);
152+
153+
}
154+
155+
/**
156+
* Sets the maximum power that can be used by the drive vector scaler. Clamped between 0 and 1.
157+
*
158+
* @param maxPowerScaling setting the max power scaling
159+
*/
160+
public void setMaxPowerScaling(double maxPowerScaling) {
161+
this.maxPowerScaling = MathFunctions.clamp(maxPowerScaling, 0, 1);
162+
}
163+
164+
/**
165+
* Gets the maximum power that can be used by the drive vector scaler. Ranges between 0 and 1.
166+
*
167+
* @return returns the max power scaling
168+
*/
169+
public double getMaxPowerScaling() {
170+
return maxPowerScaling;
151171
}
152172
}

TeamCode/src/main/java/org/firstinspires/ftc/teamcode/pedroPathing/follower/Follower.java

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ public class Follower {
9393
private boolean holdPositionAtEnd;
9494
private boolean teleopDrive;
9595

96-
private double maxPower = 1;
9796
private double previousSecondaryTranslationalIntegral;
9897
private double previousTranslationalIntegral;
9998
private double holdPointTranslationalScaling = FollowerConstants.holdPointTranslationalScaling;
@@ -194,18 +193,7 @@ public void initialize() {
194193
* @param set This caps the motor power from [0, 1].
195194
*/
196195
public void setMaxPower(double set) {
197-
maxPower = MathFunctions.clamp(set, 0, 1);
198-
}
199-
200-
/**
201-
* This handles the limiting of the drive powers array to the max power.
202-
*/
203-
public void limitDrivePowers() {
204-
for (int i = 0; i < drivePowers.length; i++) {
205-
if (Math.abs(drivePowers[i]) > maxPower) {
206-
drivePowers[i] = maxPower * MathFunctions.getSign(drivePowers[i]);
207-
}
208-
}
196+
driveVectorScaler.setMaxPowerScaling(set);
209197
}
210198

211199
/**
@@ -693,19 +681,19 @@ public boolean isBusy() {
693681
public Vector getDriveVector() {
694682
if (!useDrive) return new Vector();
695683
if (followingPathChain && chainIndex < currentPathChain.size() - 1) {
696-
return new Vector(1, currentPath.getClosestPointTangentVector().getTheta());
684+
return new Vector(driveVectorScaler.getMaxPowerScaling(), currentPath.getClosestPointTangentVector().getTheta());
697685
}
698686

699687
driveError = getDriveVelocityError();
700688

701689
if (Math.abs(driveError) < drivePIDFSwitch && useSecondaryDrivePID) {
702690
secondaryDrivePIDF.updateError(driveError);
703-
driveVector = new Vector(MathFunctions.clamp(secondaryDrivePIDF.runPIDF() + secondaryDrivePIDFFeedForward * MathFunctions.getSign(driveError), -1, 1), currentPath.getClosestPointTangentVector().getTheta());
691+
driveVector = new Vector(MathFunctions.clamp(secondaryDrivePIDF.runPIDF() + secondaryDrivePIDFFeedForward * MathFunctions.getSign(driveError), -driveVectorScaler.getMaxPowerScaling(), driveVectorScaler.getMaxPowerScaling()), currentPath.getClosestPointTangentVector().getTheta());
704692
return MathFunctions.copyVector(driveVector);
705693
}
706694

707695
drivePIDF.updateError(driveError);
708-
driveVector = new Vector(MathFunctions.clamp(drivePIDF.runPIDF() + drivePIDFFeedForward * MathFunctions.getSign(driveError), -1, 1), currentPath.getClosestPointTangentVector().getTheta());
696+
driveVector = new Vector(MathFunctions.clamp(drivePIDF.runPIDF() + drivePIDFFeedForward * MathFunctions.getSign(driveError), -driveVectorScaler.getMaxPowerScaling(), driveVectorScaler.getMaxPowerScaling()), currentPath.getClosestPointTangentVector().getTheta());
709697
return MathFunctions.copyVector(driveVector);
710698
}
711699

@@ -774,11 +762,11 @@ public Vector getHeadingVector() {
774762
headingError = MathFunctions.getTurnDirection(poseUpdater.getPose().getHeading(), currentPath.getClosestPointHeadingGoal()) * MathFunctions.getSmallestAngleDifference(poseUpdater.getPose().getHeading(), currentPath.getClosestPointHeadingGoal());
775763
if (Math.abs(headingError) < headingPIDFSwitch && useSecondaryHeadingPID) {
776764
secondaryHeadingPIDF.updateError(headingError);
777-
headingVector = new Vector(MathFunctions.clamp(secondaryHeadingPIDF.runPIDF() + secondaryHeadingPIDFFeedForward * MathFunctions.getTurnDirection(poseUpdater.getPose().getHeading(), currentPath.getClosestPointHeadingGoal()), -1, 1), poseUpdater.getPose().getHeading());
765+
headingVector = new Vector(MathFunctions.clamp(secondaryHeadingPIDF.runPIDF() + secondaryHeadingPIDFFeedForward * MathFunctions.getTurnDirection(poseUpdater.getPose().getHeading(), currentPath.getClosestPointHeadingGoal()), -driveVectorScaler.getMaxPowerScaling(), driveVectorScaler.getMaxPowerScaling()), poseUpdater.getPose().getHeading());
778766
return MathFunctions.copyVector(headingVector);
779767
}
780768
headingPIDF.updateError(headingError);
781-
headingVector = new Vector(MathFunctions.clamp(headingPIDF.runPIDF() + headingPIDFFeedForward * MathFunctions.getTurnDirection(poseUpdater.getPose().getHeading(), currentPath.getClosestPointHeadingGoal()), -1, 1), poseUpdater.getPose().getHeading());
769+
headingVector = new Vector(MathFunctions.clamp(headingPIDF.runPIDF() + headingPIDFFeedForward * MathFunctions.getTurnDirection(poseUpdater.getPose().getHeading(), currentPath.getClosestPointHeadingGoal()), -driveVectorScaler.getMaxPowerScaling(), driveVectorScaler.getMaxPowerScaling()), poseUpdater.getPose().getHeading());
782770
return MathFunctions.copyVector(headingVector);
783771
}
784772

@@ -795,7 +783,7 @@ public Vector getCorrectiveVector() {
795783
Vector translational = getTranslationalCorrection();
796784
Vector corrective = MathFunctions.addVectors(centripetal, translational);
797785

798-
if (corrective.getMagnitude() > 1) {
786+
if (corrective.getMagnitude() > driveVectorScaler.getMaxPowerScaling()) {
799787
return MathFunctions.addVectors(centripetal, MathFunctions.scalarMultiplyVector(translational, driveVectorScaler.findNormalizingScaling(centripetal, translational)));
800788
}
801789

@@ -844,7 +832,7 @@ public Vector getTranslationalCorrection() {
844832
translationalVector = MathFunctions.addVectors(translationalVector, translationalIntegralVector);
845833
}
846834

847-
translationalVector.setMagnitude(MathFunctions.clamp(translationalVector.getMagnitude(), 0, 1));
835+
translationalVector.setMagnitude(MathFunctions.clamp(translationalVector.getMagnitude(), 0, driveVectorScaler.getMaxPowerScaling()));
848836

849837
this.translationalVector = MathFunctions.copyVector(translationalVector);
850838

@@ -883,7 +871,7 @@ public Vector getCentripetalForceCorrection() {
883871
curvature = (yDoublePrime) / (Math.pow(Math.sqrt(1 + Math.pow(yPrime, 2)), 3));
884872
}
885873
if (Double.isNaN(curvature)) return new Vector();
886-
centripetalVector = new Vector(MathFunctions.clamp(FollowerConstants.centripetalScaling * FollowerConstants.mass * Math.pow(MathFunctions.dotProduct(poseUpdater.getVelocity(), MathFunctions.normalizeVector(currentPath.getClosestPointTangentVector())), 2) * curvature, -1, 1), currentPath.getClosestPointTangentVector().getTheta() + Math.PI / 2 * MathFunctions.getSign(currentPath.getClosestPointNormalVector().getTheta()));
874+
centripetalVector = new Vector(MathFunctions.clamp(FollowerConstants.centripetalScaling * FollowerConstants.mass * Math.pow(MathFunctions.dotProduct(poseUpdater.getVelocity(), MathFunctions.normalizeVector(currentPath.getClosestPointTangentVector())), 2) * curvature, -driveVectorScaler.getMaxPowerScaling(), driveVectorScaler.getMaxPowerScaling()), currentPath.getClosestPointTangentVector().getTheta() + Math.PI / 2 * MathFunctions.getSign(currentPath.getClosestPointNormalVector().getTheta()));
887875
return centripetalVector;
888876
}
889877

0 commit comments

Comments
 (0)