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

Commit 75996c9

Browse files
Added scaling utilizing Vector2d which converts form cartesian coordinates to polar.
Signed-off-by: thenetworkgrinch <[email protected]>
1 parent b100007 commit 75996c9

File tree

7 files changed

+141
-32
lines changed

7 files changed

+141
-32
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package frc.robot;
66

77
import com.pathplanner.lib.util.PIDConstants;
8-
98
import edu.wpi.first.math.geometry.Translation3d;
109
import edu.wpi.first.math.util.Units;
1110
import swervelib.math.Matter;
@@ -24,13 +23,14 @@ public final class Constants
2423
public static final double ROBOT_MASS = (148 - 20.3) * 0.453592; // 32lbs * kg per pound
2524
public static final Matter CHASSIS = new Matter(new Translation3d(0, 0, Units.inchesToMeters(8)), ROBOT_MASS);
2625
public static final double LOOP_TIME = 0.13; //s, 20ms + 110ms sprk max velocity lag
27-
public static final double MAX_SPEED = Units.feetToMeters(14.5); // Maximum speed of the robot in meters per second, used to limit acceleration.
26+
public static final double MAX_SPEED = Units.feetToMeters(14.5);
27+
// Maximum speed of the robot in meters per second, used to limit acceleration.
2828

2929
public static final class AutonConstants
3030
{
3131

3232
public static final PIDConstants TRANSLATION_PID = new PIDConstants(0.7, 0, 0);
33-
public static final PIDConstants ANGLE_PID = new PIDConstants(0.4, 0, 0.01);
33+
public static final PIDConstants ANGLE_PID = new PIDConstants(0.4, 0, 0.01);
3434
}
3535

3636
public static final class DrivebaseConstants

src/main/java/frc/robot/RobotContainer.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,12 @@
2727
public class RobotContainer
2828
{
2929

30+
// Replace with CommandPS4Controller or CommandJoystick if needed
31+
final CommandXboxController driverXbox = new CommandXboxController(0);
3032
// The robot's subsystems and commands are defined here...
3133
private final SwerveSubsystem drivebase = new SwerveSubsystem(new File(Filesystem.getDeployDirectory(),
3234
"swerve/neo"));
3335

34-
// Replace with CommandPS4Controller or CommandJoystick if needed
35-
final CommandXboxController driverXbox = new CommandXboxController(0);
36-
3736
/**
3837
* The container for the robot. Contains subsystems, OI devices, and commands.
3938
*/
@@ -42,8 +41,6 @@ public RobotContainer()
4241
// Configure the trigger bindings
4342
configureBindings();
4443

45-
46-
4744
// Applies deadbands and inverts controls because joysticks
4845
// are back-right positive while robot
4946
// controls are front-left positive
@@ -53,11 +50,11 @@ public RobotContainer()
5350
// WARNING: default buttons are on the same buttons as the ones defined in configureBindings
5451
AbsoluteDriveAdv closedAbsoluteDriveAdv = new AbsoluteDriveAdv(drivebase,
5552
() -> -MathUtil.applyDeadband(driverXbox.getLeftY(),
56-
OperatorConstants.LEFT_Y_DEADBAND),
53+
OperatorConstants.LEFT_Y_DEADBAND),
5754
() -> -MathUtil.applyDeadband(driverXbox.getLeftX(),
58-
OperatorConstants.LEFT_X_DEADBAND),
55+
OperatorConstants.LEFT_X_DEADBAND),
5956
() -> -MathUtil.applyDeadband(driverXbox.getRightX(),
60-
OperatorConstants.RIGHT_X_DEADBAND),
57+
OperatorConstants.RIGHT_X_DEADBAND),
6158
driverXbox.getHID()::getYButtonPressed,
6259
driverXbox.getHID()::getAButtonPressed,
6360
driverXbox.getHID()::getXButtonPressed,

src/main/java/frc/robot/subsystems/swervedrive/SwerveSubsystem.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import swervelib.SwerveDrive;
3737
import swervelib.SwerveDriveTest;
3838
import swervelib.math.SwerveMath;
39+
import swervelib.math.Vector2d;
3940
import swervelib.parser.SwerveControllerConfiguration;
4041
import swervelib.parser.SwerveDriveConfiguration;
4142
import swervelib.parser.SwerveParser;
@@ -49,6 +50,10 @@ public class SwerveSubsystem extends SubsystemBase
4950
* Swerve drive object.
5051
*/
5152
private final SwerveDrive swerveDrive;
53+
/**
54+
* AprilTag field layout.
55+
*/
56+
private final AprilTagFieldLayout aprilTagFieldLayout = AprilTagFields.k2024Crescendo.loadAprilTagLayoutField();
5257

5358
/**
5459
* Initialize {@link SwerveDrive} with the directory provided.
@@ -131,11 +136,6 @@ public void setupPathPlanner()
131136
);
132137
}
133138

134-
/**
135-
* AprilTag field layout.
136-
*/
137-
private final AprilTagFieldLayout aprilTagFieldLayout = AprilTagFields.k2024Crescendo.loadAprilTagLayoutField();
138-
139139
/**
140140
* Get the distance to the speaker.
141141
*
@@ -252,10 +252,11 @@ public Command driveCommand(DoubleSupplier translationX, DoubleSupplier translat
252252
{
253253
// swerveDrive.setHeadingCorrection(true); // Normally you would want heading correction for this kind of control.
254254
return run(() -> {
255-
double xInput = Math.pow(translationX.getAsDouble(), 3); // Smooth controll out
256-
double yInput = Math.pow(translationY.getAsDouble(), 3); // Smooth controll out
255+
Translation2d scaledInputs = new Vector2d(translationX.getAsDouble(), translationY.getAsDouble()).pow(3)
256+
.getTranslation();
257+
257258
// Make the robot move
258-
driveFieldOriented(swerveDrive.swerveController.getTargetSpeeds(xInput, yInput,
259+
driveFieldOriented(swerveDrive.swerveController.getTargetSpeeds(scaledInputs.getX(), scaledInputs.getY(),
259260
headingX.getAsDouble(),
260261
headingY.getAsDouble(),
261262
swerveDrive.getOdometryHeading().getRadians(),
@@ -324,8 +325,9 @@ public Command driveCommand(DoubleSupplier translationX, DoubleSupplier translat
324325
{
325326
return run(() -> {
326327
// Make the robot move
327-
swerveDrive.drive(new Translation2d(Math.pow(translationX.getAsDouble(), 3) * swerveDrive.getMaximumVelocity(),
328-
Math.pow(translationY.getAsDouble(), 3) * swerveDrive.getMaximumVelocity()),
328+
swerveDrive.drive(new Vector2d(new Translation2d(translationX.getAsDouble() * swerveDrive.getMaximumVelocity(),
329+
translationY.getAsDouble() *
330+
swerveDrive.getMaximumVelocity())).pow(3).getTranslation(),
329331
Math.pow(angularRotationX.getAsDouble(), 3) * swerveDrive.getMaximumAngularVelocity(),
330332
true,
331333
false);
@@ -506,10 +508,9 @@ public Rotation2d getHeading()
506508
*/
507509
public ChassisSpeeds getTargetSpeeds(double xInput, double yInput, double headingX, double headingY)
508510
{
509-
xInput = Math.pow(xInput, 3);
510-
yInput = Math.pow(yInput, 3);
511-
return swerveDrive.swerveController.getTargetSpeeds(xInput,
512-
yInput,
511+
Translation2d scaledInputs = new Vector2d(xInput, yInput).pow(3).getTranslation();
512+
return swerveDrive.swerveController.getTargetSpeeds(scaledInputs.getX(),
513+
scaledInputs.getY(),
513514
headingX,
514515
headingY,
515516
getHeading().getRadians(),
@@ -527,10 +528,10 @@ public ChassisSpeeds getTargetSpeeds(double xInput, double yInput, double headin
527528
*/
528529
public ChassisSpeeds getTargetSpeeds(double xInput, double yInput, Rotation2d angle)
529530
{
530-
xInput = Math.pow(xInput, 3);
531-
yInput = Math.pow(yInput, 3);
532-
return swerveDrive.swerveController.getTargetSpeeds(xInput,
533-
yInput,
531+
Translation2d scaledInputs = new Vector2d(xInput, yInput).pow(3).getTranslation();
532+
533+
return swerveDrive.swerveController.getTargetSpeeds(scaledInputs.getX(),
534+
scaledInputs.getY(),
534535
angle.getRadians(),
535536
getHeading().getRadians(),
536537
Constants.MAX_SPEED);

src/main/java/swervelib/SwerveDriveTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,8 @@ public static void logAngularMotorActivity(SwerveModule module, SysIdRoutineLog
360360
double velocity = module.getAbsoluteEncoder().getVelocity();
361361
SmartDashboard.putNumber("swerve/modules/" + module.configuration.name + "/SysId Angle Power", power);
362362
SmartDashboard.putNumber("swerve/modules/" + module.configuration.name + "/SysId Angle Position", angle);
363-
SmartDashboard.putNumber("swerve/modules/" + module.configuration.name + "/SysId Absolute Encoder Velocity", velocity);
363+
SmartDashboard.putNumber("swerve/modules/" + module.configuration.name + "/SysId Absolute Encoder Velocity",
364+
velocity);
364365
log.motor("angle-" + module.configuration.name)
365366
.voltage(m_appliedVoltage.mut_replace(power, Volts))
366367
.angularPosition(m_anglePosition.mut_replace(angle, Degrees))

src/main/java/swervelib/SwerveModule.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,10 @@ public void setDesiredState(SwerveModuleState desiredState, boolean isOpenLoop,
379379

380380
if (SwerveDriveTelemetry.verbosity == TelemetryVerbosity.HIGH)
381381
{
382-
SmartDashboard.putNumber("swerve/modules/" + configuration.name + "/Speed Setpoint", desiredState.speedMetersPerSecond);
383-
SmartDashboard.putNumber("swerve/modules/" + configuration.name + "/Angle Setpoint", desiredState.angle.getDegrees());
382+
SmartDashboard.putNumber("swerve/modules/" + configuration.name + "/Speed Setpoint",
383+
desiredState.speedMetersPerSecond);
384+
SmartDashboard.putNumber("swerve/modules/" + configuration.name + "/Angle Setpoint",
385+
desiredState.angle.getDegrees());
384386
}
385387
}
386388

src/main/java/swervelib/math/SwerveMath.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,4 +410,16 @@ public static void antiJitter(SwerveModuleState moduleState, SwerveModuleState l
410410
moduleState.angle = lastModuleState.angle;
411411
}
412412
}
413+
414+
/**
415+
* Get the scaled {@link Translation2d} with the given scalar to change the magnitude of the {@link Vector2d}.
416+
*
417+
* @param cartesian {@link Translation2d} cartesian coordinates.
418+
* @param scalar Scalar to change the polar radius of the {@link Vector2d} by.
419+
* @return {@link Translation2d} scaled by the given input.
420+
*/
421+
public static Translation2d scaleTranslation2d(Translation2d cartesian, double scalar)
422+
{
423+
return new Vector2d(cartesian).scale(scalar).getTranslation();
424+
}
413425
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package swervelib.math;
2+
3+
import edu.wpi.first.math.geometry.Rotation2d;
4+
import edu.wpi.first.math.geometry.Translation2d;
5+
6+
/**
7+
* Vector containing Magnitude and Position
8+
*/
9+
public class Vector2d
10+
{
11+
12+
/**
13+
* Position given as an angle {@link Rotation2d}.
14+
*/
15+
public final Rotation2d position;
16+
/**
17+
* Arbitrary magnitude of the vector.
18+
*/
19+
public final double magnitude;
20+
21+
/**
22+
* Construct a Vector with a position and magnitude of 0.
23+
*/
24+
public Vector2d()
25+
{
26+
position = new Rotation2d();
27+
magnitude = 0;
28+
}
29+
30+
/**
31+
* Create a vector based off of the given {@link Translation2d}. The magnitude is the
32+
* {@link Math#hypot(double, double)} of the X and Y.
33+
*
34+
* @param cartesian {@link Translation2d} with the X and Y set.
35+
*/
36+
public Vector2d(Translation2d cartesian)
37+
{
38+
position = cartesian.getAngle();
39+
magnitude = cartesian.getNorm();
40+
}
41+
42+
/**
43+
* Construct a {@link Vector2d} given the position and magnitude.
44+
*
45+
* @param position Position as a {@link Rotation2d}.
46+
* @param magnitude Magnitude as a double.
47+
*/
48+
public Vector2d(Rotation2d position, double magnitude)
49+
{
50+
this.position = position;
51+
this.magnitude = magnitude;
52+
}
53+
54+
/**
55+
* Convert cartesian coordinates to Vector.
56+
*
57+
* @param x X position
58+
* @param y Y position
59+
*/
60+
public Vector2d(double x, double y)
61+
{
62+
this(new Translation2d(x, y));
63+
}
64+
65+
/**
66+
* Convert the Vector back into cartesian coordinates.
67+
*
68+
* @return {@link Translation2d} of the vector.
69+
*/
70+
public Translation2d getTranslation()
71+
{
72+
return new Translation2d(magnitude, position);
73+
}
74+
75+
/**
76+
* Scale the magnitude by the multiplier given
77+
*
78+
* @param scalar Multiplier of the magnitude.
79+
* @return {@link Vector2d} for chained functions.
80+
*/
81+
public Vector2d scale(double scalar)
82+
{
83+
return new Vector2d(position, magnitude * scalar);
84+
}
85+
86+
/**
87+
* Exponentially modify the magnitude.
88+
*
89+
* @param pow Power for the magnitude
90+
* @return {@link Vector2d} with the magnitude^pow
91+
*/
92+
public Vector2d pow(double pow)
93+
{
94+
return new Vector2d(position, Math.pow(this.magnitude, pow));
95+
}
96+
}

0 commit comments

Comments
 (0)