forwardTranslation *= allianceReversal;
sideTranslation *= allianceReversal;
- ChassisSpeeds driverInput = new ChassisSpeeds(forwardTranslation, sideTranslation, rotation);
- ChassisSpeeds corrected =
+ ChassisVelocities driverInput = new ChassisVelocities(forwardTranslation, sideTranslation, rotation);
+ ChassisVelocities corrected =
DriverAssist.calculate(swerve, driverInput, swerve.getDesiredPose(), true);
}
/**
* Drives the robot
*
- * @param speeds The ChassisSpeeds to drive at
+ * @param speeds The ChassisVelocities to drive at
*/
- protected void drive(ChassisSpeeds speeds) {
+ protected void drive(ChassisVelocities speeds) {
// If the driver is pressing the align button or a command set the drivetrain to
// align, then align to speaker
if (driver.getIsAlign() || swerve.getIsAlign()) {
swerve.driveHeading(
- speeds.vxMetersPerSecond, speeds.vyMetersPerSecond, swerve.getAlignAngle(), true);
+ speeds.vx, speeds.vy, swerve.getAlignAngle(), true);
} else {
swerve.drive(
- speeds.vxMetersPerSecond,
- speeds.vyMetersPerSecond,
- speeds.omegaRadiansPerSecond,
+ speeds.vx,
+ speeds.vy,
+ speeds.omega,
true,
false);
}
ChassisVelocities fieldVelocity =
ChassisVelocities.fromRobotRelativeSpeeds(drive.getChassisVelocities(), currentPose.getRotation());
Translation2d linearFieldVelocity =
- new Translation2d(fieldVelocity.vxMetersPerSecond, fieldVelocity.vyMetersPerSecond);
+ new Translation2d(fieldVelocity.vx, fieldVelocity.vy);
thetaController.reset(
- currentPose.getRotation().getRadians(), fieldVelocity.omegaRadiansPerSecond);
+ currentPose.getRotation().getRadians(), fieldVelocity.omega);
lastSetpointTranslation = currentPose.getTranslation();
if (targetPose != null) {
package frc.robot.commands.drive_comm;
import org.wpilib.math.geometry.Rotation2d;
-import org.wpilib.math.kinematics.SwerveModuleState;
+import org.wpilib.math.kinematics.SwerveModuleVelocity;
+import org.wpilib.math.kinematics.SwerveModuleVelocity;
import org.wpilib.math.util.Units;
import org.wpilib.command2.InstantCommand;
import org.wpilib.command2.RunCommand;
new RunCommand(
() ->
drive.setModuleStates(
- new SwerveModuleState[] {
- new SwerveModuleState(0, new Rotation2d(Units.degreesToRadians(45))),
- new SwerveModuleState(0, new Rotation2d(Units.degreesToRadians(-45))),
- new SwerveModuleState(0, new Rotation2d(Units.degreesToRadians(-45))),
- new SwerveModuleState(0, new Rotation2d(Units.degreesToRadians(45)))
+ new SwerveModuleVelocity[] {
+ new SwerveModuleVelocity(0, new Rotation2d(Units.degreesToRadians(45))),
+ new SwerveModuleVelocity(0, new Rotation2d(Units.degreesToRadians(-45))),
+ new SwerveModuleVelocity(0, new Rotation2d(Units.degreesToRadians(-45))),
+ new SwerveModuleVelocity(0, new Rotation2d(Units.degreesToRadians(45)))
},
false),
drive));
package frc.robot.commands.drive_comm;
import org.wpilib.math.geometry.Rotation2d;
-import org.wpilib.math.kinematics.SwerveModuleState;
+import org.wpilib.math.kinematics.SwerveModuleVelocity;
import org.wpilib.command2.InstantCommand;
import frc.robot.subsystems.drivetrain.Drivetrain;
() -> {
drive.setStateDeadband(false);
drive.setModuleStates(
- new SwerveModuleState[] {
- new SwerveModuleState(0, rotation),
- new SwerveModuleState(0, rotation),
- new SwerveModuleState(0, rotation),
- new SwerveModuleState(0, rotation)
+ new SwerveModuleVelocity[] {
+ new SwerveModuleVelocity(0, rotation),
+ new SwerveModuleVelocity(0, rotation),
+ new SwerveModuleVelocity(0, rotation),
+ new SwerveModuleVelocity(0, rotation)
},
true);
drive.setStateDeadband(true);
import org.wpilib.math.geometry.Pose2d;
import org.wpilib.math.kinematics.ChassisVelocities;
-import org.wpilib.math.kinematics.SwerveModuleState;
+import org.wpilib.math.kinematics.SwerveModuleVelocity;
import org.wpilib.math.trajectory.Trajectory;
import org.wpilib.math.trajectory.Trajectory.State;
import org.wpilib.command2.InstantCommand;
State sample = trajectory.sample(time);
Pose2d nextPose = sample.poseMeters;
- double xVelocity = sample.velocityMetersPerSecond * nextPose.getRotation().getCos();
- double yVelocity = sample.velocityMetersPerSecond * nextPose.getRotation().getSin();
+ double xVelocity = sample.velocity * nextPose.getRotation().getCos();
+ double yVelocity = sample.velocity * nextPose.getRotation().getSin();
double angularVelo =
(nextPose.getRotation().getRadians() - initialPose.getRotation().getRadians()) / time;
chassisSpeeds =
ChassisVelocities.fromFieldRelativeSpeeds(chassisSpeeds, initialPose.getRotation());
- SwerveModuleState[] swerveModuleStates =
- DriveConstants.KINEMATICS.toSwerveModuleStates(chassisSpeeds);
- for (SwerveModuleState swerveModuleState : swerveModuleStates) {
- swerveModuleState.speedMetersPerSecond = 0;
+ SwerveModuleVelocity[] SwerveModuleVelocitys =
+ DriveConstants.KINEMATICS.toSwerveModuleVelocitys(chassisSpeeds);
+ for (SwerveModuleVelocity SwerveModuleVelocity : SwerveModuleVelocitys) {
+ SwerveModuleVelocity.speedMetersPerSecond = 0;
}
- drive.setModuleStates(swerveModuleStates, true);
+ drive.setModuleStates(SwerveModuleVelocitys, true);
drive.setStateDeadband(true);
},
drive);
// System.out.println("objangle " + object.getAngle());
swerve.driveHeading(
- speeds.vxMetersPerSecond,
- speeds.vyMetersPerSecond,
+ speeds.vx,
+ speeds.vy,
MathUtil.angleModulus(object.getAngle()),
true);
}
public void initialize() {
pose = poseSupplier.get();
ChassisVelocities v = drive.getChassisVelocities();
- vx = v.vxMetersPerSecond;
- vy = v.vyMetersPerSecond;
+ vx = v.vx;
+ vy = v.vy;
error = null;
}
import org.wpilib.math.kinematics.ChassisVelocities;
import org.wpilib.math.kinematics.SwerveDriveKinematics;
import org.wpilib.math.kinematics.SwerveModulePosition;
-import org.wpilib.math.kinematics.SwerveModuleState;
+import org.wpilib.math.kinematics.SwerveModuleVelocity;
import org.wpilib.math.util.Units;
import org.wpilib.units.measure.Voltage;
import org.wpilib.framework.RobotBase;
private SwerveSetpoint currentSetpoint =
new SwerveSetpoint(
new ChassisVelocities(),
- new SwerveModuleState[] {
- new SwerveModuleState(),
- new SwerveModuleState(),
- new SwerveModuleState(),
- new SwerveModuleState()
+ new SwerveModuleVelocity[] {
+ new SwerveModuleVelocity(),
+ new SwerveModuleVelocity(),
+ new SwerveModuleVelocity(),
+ new SwerveModuleVelocity()
});
// Odometry
private final SwerveDrivePoseEstimator poseEstimator;
// if (Robot.isSimulation()) {
// pigeon.getSimState().addYaw(
- // +Units.radiansToDegrees(currentSetpoint.chassisSpeeds().omegaRadiansPerSecond
+ // +Units.radiansToDegrees(currentSetpoint.chassisSpeeds().omega
// * Constants.LOOP_TIME));
// }
}
/**
* Sets the desired states for all swerve modules.
*
- * @param swerveModuleStates an array of module states to set swerve modules to. Order of the
+ * @param SwerveModuleVelocitys an array of module states to set swerve modules to. Order of the
* array matters here!
*/
- public void setModuleStates(SwerveModuleState[] swerveModuleStates, boolean isOpenLoop) {
+ public void setModuleStates(SwerveModuleVelocity[] SwerveModuleVelocitys, boolean isOpenLoop) {
// makes sure speeds of modules don't exceed maximum allowed
- SwerveDriveKinematics.desaturateWheelSpeeds(swerveModuleStates, DriveConstants.MAX_SPEED);
+ SwerveDriveKinematics.desaturateWheelSpeeds(SwerveModuleVelocitys, DriveConstants.MAX_SPEED);
for (int i = 0; i < 4; i++) {
- modules[i].setDesiredState(swerveModuleStates[i], isOpenLoop);
+ modules[i].setDesiredState(SwerveModuleVelocitys[i], isOpenLoop);
}
}
Constants.LOOP_TIME);
}
- SwerveModuleState[] swerveModuleStates = currentSetpoint.moduleStates();
- setModuleStates(swerveModuleStates, isOpenLoop);
+ SwerveModuleVelocity[] SwerveModuleVelocitys = currentSetpoint.moduleStates();
+ setModuleStates(SwerveModuleVelocitys, isOpenLoop);
}
public void setDriveVoltages(Voltage voltage) {
}
/**
- * Calculates chassis speed of drivetrain using the current SwerveModuleStates
+ * Calculates chassis speed of drivetrain using the current SwerveModuleVelocitys
*
* @return ChassisVelocities object This is often used as an input for other methods
*/
/**
* Gets the state of each module
*
- * @return An array of 4 SwerveModuleStates
+ * @return An array of 4 SwerveModuleVelocitys
*/
- public SwerveModuleState[] getModuleStates() {
- return Arrays.stream(modules).map(Module::getState).toArray(SwerveModuleState[]::new);
+ public SwerveModuleVelocity[] getModuleStates() {
+ return Arrays.stream(modules).map(Module::getState).toArray(SwerveModuleVelocity[]::new);
}
public SwerveSetpoint getCurrSetpoint() {
}
public void alignWheels() {
- SwerveModuleState state = new SwerveModuleState(0, new Rotation2d(0));
- setModuleStates(new SwerveModuleState[] {state, state, state, state}, false);
+ SwerveModuleVelocity state = new SwerveModuleVelocity(0, new Rotation2d(0));
+ setModuleStates(new SwerveModuleVelocity[] {state, state, state, state}, false);
}
}
import org.wpilib.math.filter.Debouncer;
import org.wpilib.math.geometry.Rotation2d;
import org.wpilib.math.kinematics.SwerveModulePosition;
-import org.wpilib.math.kinematics.SwerveModuleState;
+import org.wpilib.math.kinematics.SwerveModuleVelocity;
import org.wpilib.math.util.Units;
import org.wpilib.units.measure.Angle;
import frc.robot.constants.Constants;
private final TalonFX angleMotor;
private final TalonFX driveMotor;
private final CANcoder CANcoder;
- private SwerveModuleState desiredState;
+ private SwerveModuleVelocity desiredState;
protected boolean stateDeadband = true;
turnAppliedVolts,
turnCurrent);
- setDesiredState(new SwerveModuleState(0, getAngle()), false);
+ setDesiredState(new SwerveModuleVelocity(0, getAngle()), false);
}
public void close() {
}
}
- public void setDesiredState(SwerveModuleState wantedState, boolean isOpenLoop) {
+ public void setDesiredState(SwerveModuleVelocity wantedState, boolean isOpenLoop) {
// Separate if here and in setAngle() to avoid warning
if (!DriveConstants.DISABLE_DEADBAND_AND_OPTIMIZATION) {
/*
driveMotor.optimizeBusUtilization();
}
- public SwerveModuleState getState() {
- return new SwerveModuleState(
+ public SwerveModuleVelocity getState() {
+ return new SwerveModuleVelocity(
inputs.driveVelocityRadPerSec * DriveConstants.WHEEL_RADIUS, getAngle());
}
inputs.drivePositionRad * DriveConstants.WHEEL_RADIUS, getAngle());
}
- public SwerveModuleState getDesiredState() {
+ public SwerveModuleVelocity getDesiredState() {
return desiredState;
}
import org.wpilib.math.geometry.Rotation2d;
import org.wpilib.math.kinematics.SwerveModulePosition;
-import org.wpilib.math.kinematics.SwerveModuleState;
+import org.wpilib.math.kinematics.SwerveModuleVelocity;
import org.wpilib.system.Timer;
import frc.robot.constants.Constants;
import frc.robot.constants.swerve.DriveConstants;
private double currentDrivePositionMeters = 0;
private double currentSpeed = 0;
- private SwerveModuleState desiredState;
+ private SwerveModuleVelocity desiredState;
protected boolean stateDeadband = true;
* @param desiredState Desired state with speed and angle.
* @param isOpenLoop whether to use closed/open loop control for drive velocity
*/
- public void setDesiredState(SwerveModuleState desiredState, boolean isOpenLoop) {
+ public void setDesiredState(SwerveModuleVelocity desiredState, boolean isOpenLoop) {
if (!DriveConstants.DISABLE_DEADBAND_AND_OPTIMIZATION) {
// If the module isn't moving, don't rotate it
if (Math.abs(desiredState.speedMetersPerSecond) < 0.001) {
// does nothing when robot does not have a swerve drivetrain
}
- public SwerveModuleState getDesiredState() {
+ public SwerveModuleVelocity getDesiredState() {
return desiredState;
}
currentSpeed = 0;
}
- public SwerveModuleState getState() {
- return new SwerveModuleState(currentSpeed, getAngle());
+ public SwerveModuleVelocity getState() {
+ return new SwerveModuleVelocity(currentSpeed, getAngle());
}
public SwerveModulePosition getPosition() {
*/
public static Twist2d toTwist2d(ChassisVelocities speeds) {
return new Twist2d(
- speeds.vxMetersPerSecond, speeds.vyMetersPerSecond, speeds.omegaRadiansPerSecond);
+ speeds.vx, speeds.vy, speeds.omega);
}
/**
import org.wpilib.math.geometry.Rotation2d;
import org.wpilib.math.geometry.Translation2d;
import org.wpilib.math.geometry.Twist2d;
-import org.wpilib.math.kinematics.SwerveModuleState;
+import org.wpilib.math.kinematics.SwerveModuleVelocity;
import frc.robot.subsystems.drivetrain.Drivetrain;
/** Stores and updates the position of each module */
/** Updates the module positions */
public void update() {
- SwerveModuleState[] states = drive.getModuleStates();
+ SwerveModuleVelocity[] states = drive.getModuleStates();
double currentRotation = drive.getYaw().getRadians();
double chassisRotation = currentRotation - prevRotation;
/** Resets the modules to the correct positions relative to the robot */
public void reset() {
Pose2d chassisPose2d = drive.getPose();
- SwerveModuleState[] states = drive.getModuleStates();
+ SwerveModuleVelocity[] states = drive.getModuleStates();
for (int i = 0; i < 4; i++) {
angles[i] = states[i].angle.getRadians();
this.modulePositions[i] =
package frc.robot.util.SwerveStuff;
import org.wpilib.math.kinematics.ChassisVelocities;
-import org.wpilib.math.kinematics.SwerveModuleState;
+import org.wpilib.math.kinematics.SwerveModuleVelocity;
-public record SwerveSetpoint(ChassisVelocities chassisSpeeds, SwerveModuleState[] moduleStates) {}
+public record SwerveSetpoint(ChassisVelocities chassisSpeeds, SwerveModuleVelocity[] moduleStates) {}
import org.wpilib.math.geometry.Twist2d;
import org.wpilib.math.kinematics.ChassisVelocities;
import org.wpilib.math.kinematics.SwerveDriveKinematics;
-import org.wpilib.math.kinematics.SwerveModuleState;
+import org.wpilib.math.kinematics.SwerveModuleVelocity;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
double dt) {
final Translation2d[] modules = moduleLocations;
- SwerveModuleState[] desiredModuleState = kinematics.toSwerveModuleStates(desiredState);
+ SwerveModuleVelocity[] desiredModuleState = kinematics.toSwerveModuleVelocitys(desiredState);
// Make sure desiredState respects velocity limits.
if (limits.maxDriveVelocity() > 0.0) {
SwerveDriveKinematics.desaturateWheelSpeeds(desiredModuleState, limits.maxDriveVelocity());
// the goal state; then
// find the amount we can move from start towards goal in this cycle such that no kinematic
// limit is exceeded.
- double dx = desiredState.vxMetersPerSecond - prevSetpoint.chassisSpeeds().vxMetersPerSecond;
- double dy = desiredState.vyMetersPerSecond - prevSetpoint.chassisSpeeds().vyMetersPerSecond;
+ double dx = desiredState.vx - prevSetpoint.chassisSpeeds().vx;
+ double dy = desiredState.vy - prevSetpoint.chassisSpeeds().vy;
double dtheta =
- desiredState.omegaRadiansPerSecond - prevSetpoint.chassisSpeeds().omegaRadiansPerSecond;
+ desiredState.omega - prevSetpoint.chassisSpeeds().omega;
// 's' interpolates between start and goal. At 0, we are at prevState and at 1, we are at
// desiredState.
// x and y are limited separately because, when tipping in a diagonal direction, the distance
// is longer
double xAccel =
- Math.abs(desiredState.vxMetersPerSecond - prevSetpoint.chassisSpeeds().vxMetersPerSecond)
+ Math.abs(desiredState.vx - prevSetpoint.chassisSpeeds().vx)
/ dt;
double yAccel =
- Math.abs(desiredState.vyMetersPerSecond - prevSetpoint.chassisSpeeds().vyMetersPerSecond)
+ Math.abs(desiredState.vy - prevSetpoint.chassisSpeeds().vy)
/ dt;
if (!epsilonEquals(xAccel, 0)) {
double s = maxAccel / xAccel;
ChassisVelocities retSpeeds =
new ChassisVelocities(
- prevSetpoint.chassisSpeeds().vxMetersPerSecond + min_s * dx,
- prevSetpoint.chassisSpeeds().vyMetersPerSecond + min_s * dy,
- prevSetpoint.chassisSpeeds().omegaRadiansPerSecond + min_s * dtheta);
- var retStates = kinematics.toSwerveModuleStates(retSpeeds);
+ prevSetpoint.chassisSpeeds().vx + min_s * dx,
+ prevSetpoint.chassisSpeeds().vy + min_s * dy,
+ prevSetpoint.chassisSpeeds().omega + min_s * dtheta);
+ var retStates = kinematics.toSwerveModuleVelocitys(retSpeeds);
for (int i = 0; i < modules.length; ++i) {
final var maybeOverride = overrideSteering.get(i);
if (maybeOverride.isPresent()) {
public double getVelocityRelativeAngle() {
ChassisVelocities speeds = drive.getChassisVelocities();
double angle =
- getRelativeAngle() - Math.atan2(speeds.vyMetersPerSecond, speeds.vxMetersPerSecond);
+ getRelativeAngle() - Math.atan2(speeds.vy, speeds.vx);
return MathUtil.angleModulus(angle);
}
driveSpeeds,
yaw); // Changing this does not cause problems because getChassisVelocities() creates a new
// object
- State xState = new State(currentPose.getX(), driveSpeeds.vxMetersPerSecond);
- State yState = new State(currentPose.getY(), driveSpeeds.vyMetersPerSecond);
+ State xState = new State(currentPose.getX(), driveSpeeds.vx);
+ State yState = new State(currentPose.getY(), driveSpeeds.vy);
State angleState =
- new State(currentPose.getRotation().getRadians(), driveSpeeds.omegaRadiansPerSecond);
+ new State(currentPose.getRotation().getRadians(), driveSpeeds.omega);
// Store goal states
State xGoal = new State(desiredPose.getX(), 0);
// Driver input speed
double driverInputSpeed =
- Math.hypot(driverInput.vxMetersPerSecond, driverInput.vyMetersPerSecond);
+ Math.hypot(driverInput.vx, driverInput.vy);
// The amount to correct by
ChassisVelocities correction =
return driverSpeeds.plus(correction);
// return
- // nextChassisSpeed.times(CORRECTION_FACTOR).plus(driverInput.times(1-CORRECTION_FACTOR));
+ // nextChassisVelocities.times(CORRECTION_FACTOR).plus(driverInput.times(1-CORRECTION_FACTOR));
}
// Constants used for second method
keepAngle
? desiredPose.getRotation().getRadians()
: MathUtil.angleModulus(velocityAngle + Math.PI / 2);
- double inputSpeed = Math.hypot(driverInput.vxMetersPerSecond, driverInput.vyMetersPerSecond);
- double driverAngle = Math.atan2(driverInput.vyMetersPerSecond, driverInput.vxMetersPerSecond);
+ double inputSpeed = Math.hypot(driverInput.vx, driverInput.vy);
+ double driverAngle = Math.atan2(driverInput.vy, driverInput.vx);
double velocityAngleError = MathUtil.angleModulus(velocityAngle - driverAngle);
if (Math.abs(velocityAngleError) > MAX_VELOCITY_ANGLE_ERROR) {
return driverInput;
ROTATION_CORRECTION_FACTOR
* Math.signum(rotationError)
* Math.sqrt(2 * DriveConstants.MAX_ANGULAR_ACCEL * Math.abs(rotationError))
- - ROTATION_CORRECTION_FACTOR * driverInput.omegaRadiansPerSecond;
+ - ROTATION_CORRECTION_FACTOR * driverInput.omega;
return driverInput.plus(
new ChassisVelocities(
correctionSpeed * Math.cos(perpendicularAngle),
package lib;
import org.wpilib.math.geometry.Rotation2d;
-import org.wpilib.math.kinematics.SwerveModuleState;
+import org.wpilib.math.kinematics.SwerveModuleVelocity;
public class CTREModuleState {
* @param desiredState The desired state.
* @param currentAngle The current module angle.
*/
- public static SwerveModuleState optimize(
- SwerveModuleState desiredState, Rotation2d currentAngle) {
+ public static SwerveModuleVelocity optimize(
+ SwerveModuleVelocity desiredState, Rotation2d currentAngle) {
double targetAngle =
placeInAppropriate0To360Scope(currentAngle.getDegrees(), desiredState.angle.getDegrees());
double targetSpeed = desiredState.speedMetersPerSecond;
targetAngle += 180;
}
}
- return new SwerveModuleState(targetSpeed, Rotation2d.fromDegrees(targetAngle));
+ return new SwerveModuleVelocity(targetSpeed, Rotation2d.fromDegrees(targetAngle));
}
/**