1515public 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}
0 commit comments