Skip to content
Open
Show file tree
Hide file tree
Changes from 111 commits
Commits
Show all changes
116 commits
Select commit Hold shift + click to select a range
4219d25
fixed typo
goooooooooooooooooose Oct 7, 2025
5fab74d
added SwerveModule.java
goooooooooooooooooose Oct 7, 2025
d725672
added SwerveModuleHolder.java
goooooooooooooooooose Oct 7, 2025
bc914e9
added SwerveDrivetrain.java
goooooooooooooooooose Oct 12, 2025
9065982
renamed stop func
goooooooooooooooooose Oct 12, 2025
6702cfb
deleted SwerveModuleHolder.java
goooooooooooooooooose Oct 12, 2025
bf05cac
added DriveSwerve.java
goooooooooooooooooose Oct 12, 2025
3756d36
deleted default center of mass
goooooooooooooooooose Oct 15, 2025
eb2de33
deleted "this" from currentRobotAngle
goooooooooooooooooose Oct 15, 2025
ea24ec5
ended WW3
goooooooooooooooooose Oct 15, 2025
17c046c
namespaceName
goooooooooooooooooose Oct 15, 2025
f001d16
fixing stupid and untrue name
goooooooooooooooooose Oct 15, 2025
b6a1393
ended WW3 2.0
goooooooooooooooooose Oct 15, 2025
eaf9da3
renamed "timer"
goooooooooooooooooose Oct 15, 2025
4047983
ctrl l + ctrl o
goooooooooooooooooose Oct 15, 2025
0770816
defaulted the namespace name
goooooooooooooooooose Oct 17, 2025
bf01952
deleted usePIDAngle
goooooooooooooooooose Oct 17, 2025
6ebdbb8
made the absoluteEncoder be neutral
goooooooooooooooooose Oct 17, 2025
20ca952
cntrl l + cntrl o
goooooooooooooooooose Oct 17, 2025
c46cf7b
protected my people
goooooooooooooooooose Oct 18, 2025
8c571e3
deleted unnecessary calling of setCurrentRobotAngle
goooooooooooooooooose Oct 18, 2025
c17ee7c
celeriac func purpose through its name
goooooooooooooooooose Oct 18, 2025
aa924ac
celeriac func purpose through its name 2.0
goooooooooooooooooose Oct 18, 2025
d3c4756
used a generic name
goooooooooooooooooose Oct 18, 2025
afb20e4
consistency!
goooooooooooooooooose Oct 18, 2025
8271e3e
ditto
goooooooooooooooooose Oct 18, 2025
6399dec
killed this PEASANT
goooooooooooooooooose Oct 18, 2025
27db4fa
protected my people
goooooooooooooooooose Oct 18, 2025
32ebf67
added maxVelocity
goooooooooooooooooose Oct 19, 2025
b7ffe31
corrected the error (didn't notice it)
goooooooooooooooooose Oct 19, 2025
2cadf78
added line breaks
goooooooooooooooooose Oct 19, 2025
c8dfa94
Spikeslib consistency (i don't like it ): )
goooooooooooooooooose Oct 19, 2025
0f62d78
ditto
goooooooooooooooooose Oct 19, 2025
0560d12
removed unnecessary desaturate
goooooooooooooooooose Oct 19, 2025
0ceaf4d
protected
goooooooooooooooooose Oct 19, 2025
8fa285b
ditto
goooooooooooooooooose Oct 19, 2025
aec42fa
corrected mistake
goooooooooooooooooose Oct 19, 2025
7fb759d
first we drive, then we turn
goooooooooooooooooose Oct 19, 2025
c19a62f
cntrl l
goooooooooooooooooose Oct 20, 2025
82dc7c7
trying to fix stuff
goooooooooooooooooose Oct 27, 2025
c0b5607
trying to fix stuff
goooooooooooooooooose Oct 27, 2025
69b1c87
trying to fix stuff
goooooooooooooooooose Oct 27, 2025
5c15aea
trying to fix stuff
goooooooooooooooooose Oct 27, 2025
d58a9f2
trying to fix stuff
goooooooooooooooooose Oct 27, 2025
ac6ca14
tried to change to supplier<Double>, hope this works
goooooooooooooooooose Oct 27, 2025
828033b
use pid velocity as a parameter
goooooooooooooooooose Oct 27, 2025
1ed442d
fixed mistakes
goooooooooooooooooose Oct 27, 2025
f6269db
fixed mistakes 2.0
goooooooooooooooooose Oct 27, 2025
b97e61a
abstract method
goooooooooooooooooose Oct 27, 2025
ca629b8
unnecessary func
goooooooooooooooooose Oct 27, 2025
28b0a5d
protected
goooooooooooooooooose Oct 27, 2025
3b6d3cb
changed name to Yoel's preference
goooooooooooooooooose Oct 28, 2025
9440253
removed unnecessary variables
goooooooooooooooooose Oct 28, 2025
bf95116
i desire speeddddddddddd
goooooooooooooooooose Oct 28, 2025
0c4025f
docu
goooooooooooooooooose Oct 28, 2025
417506e
getAngle instead of update
goooooooooooooooooose Oct 28, 2025
6bc545d
ditto but for the absolute module angle
goooooooooooooooooose Oct 28, 2025
aedcbef
docu
goooooooooooooooooose Oct 28, 2025
2900ddb
javadoc
goooooooooooooooooose Oct 29, 2025
72cdf92
javadoc 2.0
goooooooooooooooooose Oct 29, 2025
11b0a64
cntrl alt l
goooooooooooooooooose Oct 29, 2025
a1b72ce
protect
goooooooooooooooooose Oct 30, 2025
acb7a83
possible velocity instead of desired
goooooooooooooooooose Nov 8, 2025
3342f62
added param
goooooooooooooooooose Nov 8, 2025
5224847
added get relative encoder
goooooooooooooooooose Nov 8, 2025
3b2f383
added module velocity getter
goooooooooooooooooose Nov 9, 2025
fe2423b
consistency in names
goooooooooooooooooose Nov 9, 2025
456469c
javadoced
goooooooooooooooooose Nov 9, 2025
2117fb7
better names
goooooooooooooooooose Nov 9, 2025
ab7694c
line breaks
goooooooooooooooooose Nov 9, 2025
4414cb1
line breaks + better names
goooooooooooooooooose Nov 9, 2025
e412324
continued javadoc
goooooooooooooooooose Nov 9, 2025
d11b474
javadoc for funsis
goooooooooooooooooose Nov 10, 2025
99f4df7
javadoc cuz yes
goooooooooooooooooose Nov 10, 2025
e14a258
javadoc cuz yes 2.0
goooooooooooooooooose Nov 10, 2025
76b4eec
added getModulePosition
goooooooooooooooooose Nov 10, 2025
8386fa6
added a getKinematics func
goooooooooooooooooose Nov 10, 2025
23828f0
removed unnecessary variable
goooooooooooooooooose Nov 10, 2025
99d392f
split the calculation and the get process into 2 different functions
goooooooooooooooooose Nov 10, 2025
0be1eab
removed uncessary space
goooooooooooooooooose Nov 12, 2025
0a9e79b
split robot relative speeds and field relative speeds
goooooooooooooooooose Nov 12, 2025
6f47afb
fixed docu
goooooooooooooooooose Nov 12, 2025
163b6b6
fixed docu
goooooooooooooooooose Nov 12, 2025
904fe76
fixed docu
goooooooooooooooooose Nov 12, 2025
53d1bbe
fixed docu
goooooooooooooooooose Nov 12, 2025
dd1a22b
fixed docu
goooooooooooooooooose Nov 12, 2025
140fb7e
fixed docu
goooooooooooooooooose Nov 12, 2025
046477e
added getters for the modules
goooooooooooooooooose Nov 13, 2025
c35cb56
added a turn to degree instant command that we'll probably use for di…
goooooooooooooooooose Nov 13, 2025
07dad17
fixed space
goooooooooooooooooose Nov 13, 2025
d990e3d
docu docu
goooooooooooooooooose Nov 13, 2025
ff75776
giving myself credit
goooooooooooooooooose Nov 15, 2025
7f295c0
giving myself credit 2.0
goooooooooooooooooose Nov 16, 2025
d5e15ff
forgot docu here
goooooooooooooooooose Nov 16, 2025
65dd37f
why 2 names that are the same?!
goooooooooooooooooose Nov 16, 2025
2993fae
spacy
goooooooooooooooooose Nov 20, 2025
05c1136
added halves
goooooooooooooooooose Nov 20, 2025
2f87fd9
bette description
goooooooooooooooooose Nov 20, 2025
36e2f19
bette description
goooooooooooooooooose Nov 20, 2025
58b73be
docu
goooooooooooooooooose Nov 20, 2025
928d5b0
docu
goooooooooooooooooose Nov 20, 2025
8bb566d
useVelocityPID
goooooooooooooooooose Nov 27, 2025
6aecfb6
get not set
goooooooooooooooooose Nov 27, 2025
78d1f6d
fixed TurnToDegree.java
goooooooooooooooooose Dec 1, 2025
182ec6e
changed name
goooooooooooooooooose Dec 1, 2025
3d47272
forgot stop func, ops
goooooooooooooooooose Dec 2, 2025
0b34981
weird docu fix
goooooooooooooooooose Dec 2, 2025
1ef3b9a
weird docu fix
goooooooooooooooooose Dec 4, 2025
f392fd5
added resets field relative
goooooooooooooooooose Dec 6, 2025
287b627
public and protect
goooooooooooooooooose Dec 6, 2025
27ae22e
fixed problem
goooooooooooooooooose Dec 6, 2025
fc29896
removed reset relative encoders
goooooooooooooooooose Dec 8, 2025
d22d7ff
changed to simpler name
goooooooooooooooooose Dec 8, 2025
349af62
fixed docu
goooooooooooooooooose Dec 8, 2025
5a3b216
better names
goooooooooooooooooose Dec 8, 2025
f8a774a
capital letters
goooooooooooooooooose Dec 8, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ found <a href = "https://github.com/Spikes-2212-Programming-Guild/SpikesLib2-Exa
- util - additional utilities

<h2 style="color: #00156B;">Installation</h2>
After creating a Robot Project, import SpikesLib as an online vendor libray, using this link: <br> <br>
After creating a Robot Project, import SpikesLib as an online vendor library, using this link: <br> <br>
https://spikes2212.com/SpikesLib.json

<h2 style="color: #00156B;">2025 Season</h2>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
package com.spikes2212.command.drivetrains.swerve;

import com.spikes2212.command.DashboardedSubsystem;
import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.math.geometry.Translation2d;
import edu.wpi.first.math.kinematics.ChassisSpeeds;
import edu.wpi.first.math.kinematics.SwerveDriveKinematics;
import edu.wpi.first.math.kinematics.SwerveModuleState;

/**
* This class represents a swerve drivetrain that uses vector calculations to combine the robot’s desired translation
* and rotation, determining each wheel’s speed and direction for smooth, omnidirectional movement.*
*
* @author Gil Ein-Gar
* @see DashboardedSubsystem
*/
public abstract class SwerveDrivetrain extends DashboardedSubsystem {

private static final String DEFAULT_NAMESPACE_NAME = "drivetrain";

protected final SwerveModule frontLeftModule;
protected final SwerveModule frontRightModule;
protected final SwerveModule backLeftModule;
protected final SwerveModule backRightModule;

protected final double halfDrivetrainTrackWidth;
protected final double halfDrivetrainTrackLength;
protected final double maxPossibleVelocity;
protected final SwerveDriveKinematics kinematics;

/**
* Constructs a new instance of {@link SwerveDrivetrain}.
*
* @param namespaceName the namespace name for the drivetrain
* @param frontLeftModule the front left module using {@link SwerveModule}
* @param frontRightModule the front right module using {@link SwerveModule}
* @param backLeftModule the back left module using {@link SwerveModule}
* @param backRightModule the back right module using{@link SwerveModule}
* @param halfDrivetrainTrackWidth half of the width of the drivetrain
* @param halfDrivetrainTrackLength half of the length of the drivetrain
* @param maxPossibleVelocity the maximum possible velocity of the drive motors
*/
public SwerveDrivetrain(String namespaceName, SwerveModule frontLeftModule, SwerveModule frontRightModule,
SwerveModule backLeftModule, SwerveModule backRightModule, double halfDrivetrainTrackWidth,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

giving half of a value as a parameter is clunky imo. it's not horrible, but i think you should change it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i have no need for the full parameters, besides this allows me to write the position formula in a much nicer way

double halfDrivetrainTrackLength, double maxPossibleVelocity) {
super(namespaceName);
this.frontLeftModule = frontLeftModule;
this.frontRightModule = frontRightModule;
this.backLeftModule = backLeftModule;
this.backRightModule = backRightModule;
this.halfDrivetrainTrackWidth = halfDrivetrainTrackWidth;
this.halfDrivetrainTrackLength = halfDrivetrainTrackLength;
this.maxPossibleVelocity = maxPossibleVelocity;
kinematics = calculateKinematics();

resetRelativeEncoders();
}

/**
* Constructs a new instance of {@link SwerveDrivetrain} with the default name of "drivetrain".
*
* @param frontLeftModule the front left module using {@link SwerveModule}
* @param frontRightModule the front right module using {@link SwerveModule}
* @param backLeftModule the back left module using {@link SwerveModule}
* @param backRightModule the back right module using {@link SwerveModule}
* @param halfDrivetrainTrackWidth half the width of the drivetrain
* @param halfDrivetrainTrackLength half the length of the drivetrain
* @param maxPossibleVelocity the maximum possible velocity of the drive motors
*/
public SwerveDrivetrain(SwerveModule frontLeftModule, SwerveModule frontRightModule, SwerveModule backLeftModule,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

SwerveModule backRightModule, double halfDrivetrainTrackWidth, double halfDrivetrainTrackLength,
double maxPossibleVelocity) {
this(getClassName(DEFAULT_NAMESPACE_NAME), frontLeftModule, frontRightModule, backLeftModule, backRightModule,
halfDrivetrainTrackWidth, halfDrivetrainTrackLength, maxPossibleVelocity);
}

/**
* Function that moves the robot in a swerve motion.
*
* @param xSpeed the desired speed on the x-axis.
* @param ySpeed the desired speed on the y-axis.
* @param rotationSpeed the desired speed for the drivetrain rotation.
* @param isFieldRelative whether the drive should be relative to the field or to the robot.
* @param timeStep the amount of time between calculations, representing how long the speed value is applied
* before updating again.
* @param useVelocityPID whether the robot velocity will be applied using P.I.D or not.
*/
public void drive(double xSpeed, double ySpeed, double rotationSpeed, boolean isFieldRelative,
double timeStep, boolean useVelocityPID) {
SwerveModuleState[] states;
if (isFieldRelative) {
states = getFieldRelativeSpeeds(xSpeed, ySpeed, rotationSpeed, timeStep);
} else {
states = getRobotRelativeSpeeds(xSpeed, ySpeed, rotationSpeed, timeStep);
}
SwerveDriveKinematics.desaturateWheelSpeeds(states, maxPossibleVelocity);
setTargetModuleStates(states, useVelocityPID);
}

/**
* Sets the desired module states.
*
* @param targetModuleStates an array containing the desired speed and angle for each swerve module
* @param usePIDVelocity whether the modules will drive with P.I.D for the velocity
*/
protected void setTargetModuleStates(SwerveModuleState[] targetModuleStates, boolean usePIDVelocity) {
frontLeftModule.setTargetState(targetModuleStates[0], maxPossibleVelocity, usePIDVelocity);
frontRightModule.setTargetState(targetModuleStates[1], maxPossibleVelocity, usePIDVelocity);
backLeftModule.setTargetState(targetModuleStates[2], maxPossibleVelocity, usePIDVelocity);
backRightModule.setTargetState(targetModuleStates[3], maxPossibleVelocity, usePIDVelocity);
}

/**
* Gets the current robot angle using an external sensor.
*
* @return the current angle of the robot
*/
public abstract Rotation2d getCurrentRobotAngle();

/**
* Resets the field relativity to be the same as the robot relativity.
*/
public abstract void resetFieldRelative();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to help both generality and simplicity for the user, I suggest creating an abstract method called resetGyro(), making this method non-abstract and having it call resetGyro()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but the whole point of this library is that fit's anything. if i knew i had a gyro this whole library would have been so much easier to write, but since i made this library general and non specific my functions are non specific as well and allow the user to use whichever sensor and method they would like


/**
* Set the field relative speeds
*
* @param xSpeed the desired speed on the x-axis.
* @param ySpeed the desired speed on the y-axis.
* @param rotationSpeed the desired speed for the drivetrain rotation.
* @param timeStep the derivation of time the speed should be applied.
* @return the necessary {@link SwerveModuleState[]} for field relative movement
*/
protected SwerveModuleState[] getFieldRelativeSpeeds(double xSpeed, double ySpeed, double rotationSpeed,
double timeStep) {
return kinematics.toSwerveModuleStates(ChassisSpeeds.discretize(ChassisSpeeds.fromFieldRelativeSpeeds(xSpeed,
ySpeed, rotationSpeed, getCurrentRobotAngle()), timeStep));
}

/**
* get the robot relative speeds
*
* @param xSpeed the desired speed on the x-axis.
* @param ySpeed the desired speed on the y-axis.
* @param rotationSpeed the desired speed for the drivetrain rotation.
* @param timeStep the derivation of time the speed should be applied.
* @return the necessary {@link SwerveModuleState[]} for robot relative movement
*/
protected SwerveModuleState[] getRobotRelativeSpeeds(double xSpeed, double ySpeed, double rotationSpeed,
double timeStep) {
return kinematics.toSwerveModuleStates(ChassisSpeeds.discretize(new ChassisSpeeds(xSpeed, ySpeed,
rotationSpeed), timeStep));
}

/**
* Calculates the 2d positions of each wheel in relation to the center of the robot.
*
* @return a {@link SwerveDriveKinematics} according to the wheels positions in relation to the center
*/
protected SwerveDriveKinematics calculateKinematics() {
Translation2d frontLeftWheelDistanceFromCenter =
new Translation2d(halfDrivetrainTrackWidth, halfDrivetrainTrackLength);
Translation2d frontRightWheelDistanceFromCenter =
new Translation2d(halfDrivetrainTrackWidth, -halfDrivetrainTrackLength);
Translation2d backLeftWheelDistanceFromCenter =
new Translation2d(-halfDrivetrainTrackWidth, halfDrivetrainTrackLength);
Translation2d backRightWheelDistanceFromCenter =
new Translation2d(-halfDrivetrainTrackWidth, -halfDrivetrainTrackLength);

return new SwerveDriveKinematics(frontLeftWheelDistanceFromCenter,
frontRightWheelDistanceFromCenter, backLeftWheelDistanceFromCenter,
backRightWheelDistanceFromCenter);
}

/**
* @return the robots current {@link ChassisSpeeds}
*/
protected ChassisSpeeds getSpeeds() {
return kinematics.toChassisSpeeds(frontLeftModule.getModuleState(), frontRightModule.getModuleState(),
backLeftModule.getModuleState(), backRightModule.getModuleState());
}

/**
* @return returns the front left {@link SwerveModule}
*/
public SwerveModule getFrontLeftModule() {
return frontLeftModule;
}

/**
* @return returns the front right {@link SwerveModule}
*/
public SwerveModule getFrontRightModule() {
return frontRightModule;
}

/**
* @return returns the back left {@link SwerveModule}
*/
public SwerveModule getBackLeftModule() {
return backLeftModule;
}

/**
* @return returns the back right {@link SwerveModule}
*/
public SwerveModule getBackRightModule() {
return backRightModule;
}

/**
* @return the robot's swerve kinematics
*/
protected SwerveDriveKinematics getKinematics() {
return kinematics;
}

/**
* Resets the relative encoders of every module according to their absolute encoder.
*/
public void resetRelativeEncoders() {
frontLeftModule.resetRelativeEncoder();
frontRightModule.resetRelativeEncoder();
backLeftModule.resetRelativeEncoder();
backRightModule.resetRelativeEncoder();
}

/**
* Stops all modules.
*/
public void stop() {
frontLeftModule.stop();
frontRightModule.stop();
backLeftModule.stop();
backRightModule.stop();
}
}
Loading