Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
thenetworkgrinch committed Jun 12, 2024
1 parent 8c3470f commit 2d992a4
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 39 deletions.
55 changes: 41 additions & 14 deletions swervelib/SwerveDrive.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ public class SwerveDrive
* Whether to correct heading when driving translationally. Set to true to enable.
*/
public boolean headingCorrection = false;
/**
* Amount of seconds the duration of the timestep the speeds should be applied for.
*/
private double discretizationdtSeconds = 0.02;
/**
* Deadband for speeds in heading correction.
*/
Expand Down Expand Up @@ -185,12 +189,12 @@ public SwerveDrive(
setMaximumSpeed(maxSpeedMPS);

// Initialize Telemetry
if (SwerveDriveTelemetry.verbosity.ordinal() >= TelemetryVerbosity.LOW.ordinal())
if (SwerveDriveTelemetry.verbosity.ordinal() >= TelemetryVerbosity.POSE.ordinal())
{
SmartDashboard.putData("Field", field);
}

if (SwerveDriveTelemetry.verbosity.ordinal() >= TelemetryVerbosity.HIGH.ordinal())
if (SwerveDriveTelemetry.verbosity.ordinal() >= TelemetryVerbosity.INFO.ordinal())
{
SwerveDriveTelemetry.maxSpeed = maxSpeedMPS;
SwerveDriveTelemetry.maxAngularVelocity = swerveController.config.maxAngularVelocity;
Expand Down Expand Up @@ -342,6 +346,19 @@ public void setHeadingCorrection(boolean state, double deadband)
HEADING_CORRECTION_DEADBAND = deadband;
}

/**
* Tertiary method of controlling the drive base given velocity in both field oriented and robot oriented at the same time.
* The inputs are added together so this is not intneded to be used to give the driver both methods of control.
*
* @param fieldOrientedVelocity The field oriented velocties to use
* @param robotOrientedVelocity The robot oriented velocties to use
*/
public void driveFieldOrientedandRobotOriented(ChassisSpeeds fieldOrientedVelocity, ChassisSpeeds robotOrientedVelocity)
{
ChassisSpeeds TotalVelocties = ChassisSpeeds.fromFieldRelativeSpeeds(fieldOrientedVelocity, getOdometryHeading()).plus(robotOrientedVelocity);
drive(TotalVelocties);
}

/**
* Secondary method of controlling the drive base given velocity and adjusting it for field oriented use.
*
Expand Down Expand Up @@ -464,7 +481,7 @@ public void drive(ChassisSpeeds velocity, boolean isOpenLoop, Translation2d cent
// https://www.chiefdelphi.com/t/whitepaper-swerve-drive-skew-and-second-order-kinematics/416964/5
if (chassisVelocityCorrection)
{
velocity = ChassisSpeeds.discretize(velocity, 0.02);
velocity = ChassisSpeeds.discretize(velocity, discretizationdtSeconds);
}

// Heading Angular Velocity Deadband, might make a configuration option later.
Expand All @@ -485,11 +502,11 @@ public void drive(ChassisSpeeds velocity, boolean isOpenLoop, Translation2d cent
}

// Display commanded speed for testing
if (SwerveDriveTelemetry.verbosity == TelemetryVerbosity.HIGH)
if (SwerveDriveTelemetry.verbosity == TelemetryVerbosity.INFO)
{
SmartDashboard.putString("RobotVelocity", velocity.toString());
}
if (SwerveDriveTelemetry.verbosity.ordinal() >= TelemetryVerbosity.HIGH.ordinal())
if (SwerveDriveTelemetry.verbosity.ordinal() >= TelemetryVerbosity.LOW.ordinal())
{
SwerveDriveTelemetry.desiredChassisSpeeds[1] = velocity.vyMetersPerSecond;
SwerveDriveTelemetry.desiredChassisSpeeds[0] = velocity.vxMetersPerSecond;
Expand All @@ -502,7 +519,6 @@ public void drive(ChassisSpeeds velocity, boolean isOpenLoop, Translation2d cent
setRawModuleStates(swerveModuleStates, isOpenLoop);
}


/**
* Set the maximum speeds for desaturation.
*
Expand Down Expand Up @@ -660,7 +676,7 @@ public void resetOdometry(Pose2d pose)
*/
public void postTrajectory(Trajectory trajectory)
{
if (SwerveDriveTelemetry.verbosity.ordinal() >= TelemetryVerbosity.LOW.ordinal())
if (SwerveDriveTelemetry.verbosity.ordinal() >= TelemetryVerbosity.POSE.ordinal())
{
field.getObject("Trajectory").setTrajectory(trajectory);
}
Expand Down Expand Up @@ -871,7 +887,7 @@ public void lockPose()
{
SwerveModuleState desiredState =
new SwerveModuleState(0, swerveModule.configuration.moduleLocation.getAngle());
if (SwerveDriveTelemetry.verbosity.ordinal() >= TelemetryVerbosity.HIGH.ordinal())
if (SwerveDriveTelemetry.verbosity.ordinal() >= TelemetryVerbosity.INFO.ordinal())
{
SwerveDriveTelemetry.desiredStates[swerveModule.moduleNumber * 2] =
desiredState.angle.getDegrees();
Expand Down Expand Up @@ -932,7 +948,7 @@ public void updateOdometry()
swerveDrivePoseEstimator.update(getYaw(), getModulePositions());

// Update angle accumulator if the robot is simulated
if (SwerveDriveTelemetry.verbosity.ordinal() >= TelemetryVerbosity.HIGH.ordinal())
if (SwerveDriveTelemetry.verbosity.ordinal() >= TelemetryVerbosity.INFO.ordinal())
{
Pose2d[] modulePoses = getSwerveModulePoses(swerveDrivePoseEstimator.getEstimatedPosition());
if (SwerveDriveTelemetry.isSimulation)
Expand All @@ -951,7 +967,7 @@ public void updateOdometry()
SwerveDriveTelemetry.robotRotation = getOdometryHeading().getDegrees();
}

if (SwerveDriveTelemetry.verbosity.ordinal() >= TelemetryVerbosity.LOW.ordinal())
if (SwerveDriveTelemetry.verbosity.ordinal() >= TelemetryVerbosity.POSE.ordinal())
{
field.setRobotPose(swerveDrivePoseEstimator.getEstimatedPosition());
}
Expand All @@ -967,7 +983,7 @@ public void updateOdometry()
SmartDashboard.putNumber("Raw IMU Yaw", getYaw().getDegrees());
SmartDashboard.putNumber("Adjusted IMU Yaw", getOdometryHeading().getDegrees());
}
if (SwerveDriveTelemetry.verbosity.ordinal() >= TelemetryVerbosity.HIGH.ordinal())
if (SwerveDriveTelemetry.verbosity.ordinal() >= TelemetryVerbosity.INFO.ordinal())
{
SwerveDriveTelemetry.measuredStates[module.moduleNumber * 2] = moduleState.angle.getDegrees();
SwerveDriveTelemetry.measuredStates[(module.moduleNumber * 2) + 1] = moduleState.speedMetersPerSecond;
Expand All @@ -983,7 +999,7 @@ public void updateOdometry()
moduleSynchronizationCounter = 0;
}

if (SwerveDriveTelemetry.verbosity.ordinal() >= TelemetryVerbosity.HIGH.ordinal())
if (SwerveDriveTelemetry.verbosity.ordinal() >= TelemetryVerbosity.INFO.ordinal())
{
SwerveDriveTelemetry.updateData();
}
Expand Down Expand Up @@ -1122,11 +1138,11 @@ public void resetDriveEncoders()
* Pushes the Absolute Encoder offsets to the Encoder or Motor Controller, depending on type. Also removes the
* internal offsets to prevent double offsetting.
*/
public void pushOffsetsToControllers()
public void pushOffsetsToEncoders()
{
for (SwerveModule module : swerveModules)
{
module.pushOffsetsToControllers();
module.pushOffsetsToEncoders();
}
}

Expand Down Expand Up @@ -1156,4 +1172,15 @@ public void setCosineCompensator(boolean enabled)
}
}

/**
* Sets the Chassis discretization seconds as well as enableing/disabling the Chassis velocity correction
*
* @param enable
* @param dtSeconds
*/
public void setChassisDiscretization(boolean enable, double dtSeconds){
chassisVelocityCorrection = enable;
discretizationdtSeconds = dtSeconds;
}

}
13 changes: 9 additions & 4 deletions swervelib/SwerveModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ public class SwerveModule
* NT3 Raw drive motor.
*/
private final String rawDriveName;
/**
* NT3 Raw drive motor.
*/
private final String rawDriveVelName;
/**
* Module number for kinematics, usually 0 to 3. front left -> front right -> back left -> back right.
*/
Expand Down Expand Up @@ -207,6 +211,7 @@ public SwerveModule(int moduleNumber, SwerveModuleConfiguration moduleConfigurat
absoluteEncoderIssueName = "Module[" + configuration.name + "] Absolute Encoder Read Issue";
rawAngleName = "Module[" + configuration.name + "] Raw Angle Encoder";
rawDriveName = "Module[" + configuration.name + "] Raw Drive Encoder";
rawDriveVelName = "Module[" + configuration.name + "] Raw Drive Velocity";
}

/**
Expand Down Expand Up @@ -252,7 +257,7 @@ public void setAntiJitter(boolean antiJitter)
this.antiJitterEnabled = antiJitter;
if (antiJitter)
{
pushOffsetsToControllers();
pushOffsetsToEncoders();
} else
{
restoreInternalOffset();
Expand Down Expand Up @@ -366,7 +371,7 @@ public void setDesiredState(SwerveModuleState desiredState, boolean isOpenLoop,
simModule.updateStateAndPosition(desiredState);
}

if (SwerveDriveTelemetry.verbosity.ordinal() >= TelemetryVerbosity.HIGH.ordinal())
if (SwerveDriveTelemetry.verbosity.ordinal() >= TelemetryVerbosity.INFO.ordinal())
{
SwerveDriveTelemetry.desiredStates[moduleNumber * 2] = desiredState.angle.getDegrees();
SwerveDriveTelemetry.desiredStates[(moduleNumber * 2) + 1] = velocity;
Expand Down Expand Up @@ -580,7 +585,7 @@ public SwerveModuleConfiguration getConfiguration()
/**
* Push absolute encoder offset in the memory of the encoder or controller. Also removes the internal angle offset.
*/
public void pushOffsetsToControllers()
public void pushOffsetsToEncoders()
{
if (absoluteEncoder != null && angleOffset == configuration.angleOffset)
{
Expand Down Expand Up @@ -633,7 +638,7 @@ public void updateTelemetry()
}
SmartDashboard.putNumber(rawAngleName, angleMotor.getPosition());
SmartDashboard.putNumber(rawDriveName, driveMotor.getPosition());
SmartDashboard.putNumber(adjAbsoluteAngleName, getAbsolutePosition());
SmartDashboard.putNumber(rawDriveVelName, driveMotor.getVelocity()); SmartDashboard.putNumber(adjAbsoluteAngleName, getAbsolutePosition());
SmartDashboard.putNumber(absoluteEncoderIssueName, getAbsoluteEncoderReadIssue() ? 1 : 0);
}
}
10 changes: 0 additions & 10 deletions swervelib/encoders/PWMDutyCycleEncoderSwerve.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ public class PWMDutyCycleEncoderSwerve extends SwerveAbsoluteEncoder
* An {@link Alert} for if the encoder cannot report accurate velocities.
*/
private Alert inaccurateVelocities;
/**
* An {@link Alert} for if the encoder is disconnected.
*/
private Alert disconnected;

/**
* Constructor for the PWM duty cycle encoder.
Expand All @@ -43,10 +39,6 @@ public PWMDutyCycleEncoderSwerve(int pin)
"Encoders",
"The PWM Duty Cycle encoder may not report accurate velocities!",
Alert.AlertType.WARNING_TRACE);
inaccurateVelocities = new Alert(
"Encoders",
"The swerve encoder on port " + pin + "is disconnected!",
Alert.AlertType.ERROR_TRACE);

}

Expand All @@ -69,7 +61,6 @@ public void configure(boolean inverted)
@Override
public double getAbsolutePosition()
{
disconnected.set(!encoder.isConnected());
return (isInverted ? -1.0 : 1.0) * encoder.getAbsolutePosition() * 360;
}

Expand All @@ -92,7 +83,6 @@ public Object getAbsoluteEncoder()
@Override
public double getVelocity()
{
disconnected.set(!encoder.isConnected());
inaccurateVelocities.set(true);
return encoder.get();
}
Expand Down
2 changes: 1 addition & 1 deletion swervelib/encoders/SwerveAbsoluteEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public abstract class SwerveAbsoluteEncoder
/**
* Sets the Absolute Encoder offset at the Encoder Level.
*
* @param offset the offset the Absolute Encoder uses as the zero point.
* @param offset the offset the Absolute Encoder uses as the zero point in degrees.
* @return if setting Absolute Encoder Offset was successful or not.
*/
public abstract boolean setAbsoluteEncoderOffset(double offset);
Expand Down
10 changes: 1 addition & 9 deletions swervelib/motors/SparkMaxSwerve.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,6 @@ public class SparkMaxSwerve extends SwerveMotor
*/
private Supplier<Double> position;

/**
* An {@link Alert} for if there is an error configuring the motor.
*/
private Alert failureConfiguringAlert;

/**
* Initialize the swerve motor.
*
Expand All @@ -79,9 +74,6 @@ public SparkMaxSwerve(CANSparkMax motor, boolean isDriveMotor)
position = encoder::getPosition;
// Spin off configurations in a different thread.
// configureSparkMax(() -> motor.setCANTimeout(0)); // Commented out because it prevents feedback.
failureConfiguringAlert = new Alert("Motors",
"Failure configuring motor " + motor.getDeviceId(),
Alert.AlertType.WARNING_TRACE);
}

/**
Expand Down Expand Up @@ -109,7 +101,7 @@ private void configureSparkMax(Supplier<REVLibError> config)
return;
}
}
failureConfiguringAlert.set(true);
DriverStation.reportWarning("Failure configuring motor " + motor.getDeviceId(), true);
}

/**
Expand Down
1 change: 0 additions & 1 deletion swervelib/motors/TalonSRXSwerve.java
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,6 @@ public void setMotorBrake(boolean isBrakeMode)
@Override
public void setInverted(boolean inverted)
{
Timer.delay(1);
motor.setInverted(inverted);
}

Expand Down
8 changes: 8 additions & 0 deletions swervelib/telemetry/SwerveDriveTelemetry.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@ public enum TelemetryVerbosity
* Low telemetry data, only post the robot position on the field.
*/
LOW,
/**
* Medium telemetry data, swerve directory
*/
INFO,
/**
* Info level + field info
*/
POSE,
/**
* Full swerve drive data is sent back in both human and machine readable forms.
*/
Expand Down

0 comments on commit 2d992a4

Please sign in to comment.