Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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 build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ java {
}

// set a valid semver version
version = '4.1.2'
version = '5.0.0'

repositories {
mavenCentral()
Expand Down
28 changes: 19 additions & 9 deletions src/main/java/ca/team1310/swerve/core/SwerveModuleImpl.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ca.team1310.swerve.core;

import ca.team1310.swerve.core.config.EncoderType;
import ca.team1310.swerve.core.config.ModuleConfig;
import ca.team1310.swerve.core.hardware.cancoder.CanCoder;
import ca.team1310.swerve.core.hardware.rev.neospark.NSFAngleMotor;
Expand All @@ -14,7 +15,6 @@
class SwerveModuleImpl implements SwerveModule {

private static final double ANGLE_ENCODER_SYNC_PERIOD_MS = 500;
private final Notifier encoderSynchronizer = new Notifier(this::syncAngleEncoder);

private final String name;
private final Coordinates location;
Expand All @@ -40,10 +40,15 @@ class SwerveModuleImpl implements SwerveModule {
measuredState.setLocation(cfg.location());
this.driveMotor = getDriveMotor(cfg, maxAttainableModuleSpeedMps);
this.angleMotor = getAngleMotor(cfg);
this.angleEncoder = getAbsoluteAngleEncoder(cfg);

this.encoderSynchronizer.setName("RunnymedeSwerve Angle Encoder Sync " + name);
this.encoderSynchronizer.startPeriodic(ANGLE_ENCODER_SYNC_PERIOD_MS / 1000);
if (cfg.absoluteAngleEncoderConfig().type() == EncoderType.CANCODER)
this.angleEncoder = getAbsoluteAngleEncoder(cfg);
else this.angleEncoder = null;

if (angleEncoder != null) {
Notifier encoderSynchronizer = new Notifier(this::syncAngleEncoder);
encoderSynchronizer.setName("RunnymedeSwerve Angle Encoder Sync " + name);
encoderSynchronizer.startPeriodic(ANGLE_ENCODER_SYNC_PERIOD_MS / 1000);
}

driveMotorFaultPresent =
new Alert("Swerve Drive Motor [" + name + "] Fault Present", Alert.AlertType.kError);
Expand Down Expand Up @@ -77,8 +82,12 @@ private DriveMotor getDriveMotor(ModuleConfig cfg, double maxAttainableModuleSpe

private AngleMotor getAngleMotor(ModuleConfig cfg) {
return switch (cfg.angleMotorConfig().type()) {
case NEO_SPARK_FLEX -> new NSFAngleMotor(cfg.angleMotorCanId(), cfg.angleMotorConfig());
case NEO_SPARK_MAX -> new NSMAngleMotor(cfg.angleMotorCanId(), cfg.angleMotorConfig());
case NEO_SPARK_FLEX ->
new NSFAngleMotor(
cfg.angleMotorCanId(), cfg.angleMotorConfig(), cfg.absoluteAngleEncoderConfig());
case NEO_SPARK_MAX ->
new NSMAngleMotor(
cfg.angleMotorCanId(), cfg.angleMotorConfig(), cfg.absoluteAngleEncoderConfig());
};
}

Expand Down Expand Up @@ -108,7 +117,7 @@ public synchronized void readState() {
public synchronized void readVerboseState() {
measuredState.setVelocity(driveMotor.getVelocity());
measuredState.setDriveOutputPower(driveMotor.getMeasuredVoltage());
measuredState.setAbsoluteEncoderAngle(angleEncoder.getPosition());
measuredState.setAbsoluteEncoderAngle(angleMotor.getPosition());
}

public synchronized ModuleState getState() {
Expand Down Expand Up @@ -138,7 +147,8 @@ public synchronized void setDesiredState(ModuleDirective desiredState) {
public boolean checkFaults() {
boolean driveFaults = driveMotor.hasFaults();
boolean angleFaults = angleMotor.hasFaults();
boolean angleEncoderFaults = angleEncoder.hasFaults();
boolean angleEncoderFaults = false;
if (angleEncoder != null) angleEncoderFaults = angleEncoder.hasFaults();

driveMotorFaultPresent.set(driveFaults);
angleMotorFaultPresent.set(angleFaults);
Expand Down
13 changes: 12 additions & 1 deletion src/main/java/ca/team1310/swerve/core/config/EncoderConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,19 @@
/**
* Configuration for the encoder.
*
* @param type the type of encoder, either Cancoder or Integrated encoder
* @param inverted true if the encoder is upside down with respect to its expected orientation
* @param retrySeconds how long to wait before retrying communication with the encoder
* @param retryCount how many times to retry communication with the encoder
*/
public record EncoderConfig(boolean inverted, double retrySeconds, int retryCount) {}
public record EncoderConfig(
EncoderType type, boolean inverted, double retrySeconds, int retryCount) {

public static EncoderConfig cancoder(boolean inverted, double retrySeconds, int retryCount) {
return new EncoderConfig(EncoderType.CANCODER, inverted, retrySeconds, retryCount);
}

public static EncoderConfig integrated(boolean inverted) {
return new EncoderConfig(EncoderType.INTEGRATED, inverted, 0, 0);
}
}
9 changes: 9 additions & 0 deletions src/main/java/ca/team1310/swerve/core/config/EncoderType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ca.team1310.swerve.core.config;

/** The type of motor used in the swerve drive. */
public enum EncoderType {
/** A CTRE CANcoder. */
CANCODER,
/** An encoder plugged directly into the angle motor controller */
INTEGRATED
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import static ca.team1310.swerve.utils.SwerveUtils.normalizeDegrees;

import ca.team1310.swerve.core.AngleMotor;
import ca.team1310.swerve.core.config.EncoderConfig;
import ca.team1310.swerve.core.config.EncoderType;
import ca.team1310.swerve.core.config.MotorConfig;
import com.revrobotics.spark.FeedbackSensor;
import com.revrobotics.spark.SparkBase;
Expand All @@ -27,8 +29,9 @@ public abstract class NSAngleMotor<T extends SparkBase> extends NSBase<T> implem
*
* @param spark The spark motor controller
* @param cfg The configuration of the motor
* @param encoderConfig the configuration of the absolute encoder
*/
public NSAngleMotor(T spark, MotorConfig cfg) {
public NSAngleMotor(T spark, MotorConfig cfg, EncoderConfig encoderConfig) {
super(spark);
SparkMaxConfig config = new SparkMaxConfig();
config.inverted(cfg.inverted());
Expand Down Expand Up @@ -83,13 +86,16 @@ public NSAngleMotor(T spark, MotorConfig cfg) {
// configure PID controller
config
.closedLoop
.feedbackSensor(FeedbackSensor.kPrimaryEncoder)
.pidf(cfg.p(), cfg.i(), cfg.d(), cfg.ff())
.iZone(cfg.izone())
.outputRange(-180, 180)
.positionWrappingEnabled(true)
.positionWrappingInputRange(-180, 180);

if (encoderConfig.type() == EncoderType.INTEGRATED)
config.closedLoop.feedbackSensor(FeedbackSensor.kAbsoluteEncoder);
else config.closedLoop.feedbackSensor(FeedbackSensor.kPrimaryEncoder);

// send them to the motor
doWithRetry(
() ->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ca.team1310.swerve.core.hardware.rev.neospark;

import ca.team1310.swerve.core.config.EncoderConfig;
import ca.team1310.swerve.core.config.MotorConfig;
import com.revrobotics.spark.SparkFlex;
import com.revrobotics.spark.SparkLowLevel;
Expand All @@ -15,8 +16,9 @@ public class NSFAngleMotor extends NSAngleMotor<SparkFlex> {
*
* @param canId The CAN ID of the motor
* @param cfg The configuration of the motor
* @param encoderConfig the configuration of the absolute encoder
*/
public NSFAngleMotor(int canId, MotorConfig cfg) {
super(new SparkFlex(canId, SparkLowLevel.MotorType.kBrushless), cfg);
public NSFAngleMotor(int canId, MotorConfig cfg, EncoderConfig encoderConfig) {
super(new SparkFlex(canId, SparkLowLevel.MotorType.kBrushless), cfg, encoderConfig);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ca.team1310.swerve.core.hardware.rev.neospark;

import ca.team1310.swerve.core.config.EncoderConfig;
import ca.team1310.swerve.core.config.MotorConfig;
import com.revrobotics.spark.SparkLowLevel;
import com.revrobotics.spark.SparkMax;
Expand All @@ -15,8 +16,9 @@ public class NSMAngleMotor extends NSAngleMotor<SparkMax> {
*
* @param canId The CAN ID of the motor
* @param cfg The configuration of the motor
* @param encoderConfig the configuration of the absolute encoder
*/
public NSMAngleMotor(int canId, MotorConfig cfg) {
super(new SparkMax(canId, SparkLowLevel.MotorType.kBrushless), cfg);
public NSMAngleMotor(int canId, MotorConfig cfg, EncoderConfig encoderConfig) {
super(new SparkMax(canId, SparkLowLevel.MotorType.kBrushless), cfg, encoderConfig);
}
}