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

Commit 779eeb8

Browse files
Added delay to config loop. Updated setInverted to have redundant checks.
Signed-off-by: thenetworkgrinch <[email protected]>
1 parent 2c86426 commit 779eeb8

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

src/main/java/swervelib/motors/SparkFlexSwerve.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.revrobotics.RelativeEncoder;
1313
import com.revrobotics.SparkAnalogSensor;
1414
import com.revrobotics.SparkPIDController;
15+
import edu.wpi.first.wpilibj.Timer;
1516
import java.util.function.Supplier;
1617
import swervelib.encoders.SwerveAbsoluteEncoder;
1718
import swervelib.parser.PIDFConfig;
@@ -108,6 +109,7 @@ private void configureSparkFlex(Supplier<REVLibError> config)
108109
{
109110
return;
110111
}
112+
Timer.delay(0.01);
111113
}
112114
failureConfiguring.set(true);
113115
}
@@ -329,7 +331,10 @@ public void setMotorBrake(boolean isBrakeMode)
329331
@Override
330332
public void setInverted(boolean inverted)
331333
{
332-
motor.setInverted(inverted);
334+
configureSparkFlex(() -> {
335+
motor.setInverted(inverted);
336+
return motor.getLastError();
337+
});
333338
}
334339

335340
/**

src/main/java/swervelib/motors/SparkMaxBrushedMotorSwerve.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.revrobotics.SparkMaxAlternateEncoder;
1212
import com.revrobotics.SparkPIDController;
1313
import com.revrobotics.SparkRelativeEncoder.Type;
14+
import edu.wpi.first.wpilibj.Timer;
1415
import java.util.function.Supplier;
1516
import swervelib.encoders.SwerveAbsoluteEncoder;
1617
import swervelib.parser.PIDFConfig;
@@ -145,6 +146,7 @@ private void configureSparkMax(Supplier<REVLibError> config)
145146
{
146147
return;
147148
}
149+
Timer.delay(0.01);
148150
}
149151
failureConfiguringAlert.set(true);
150152
}
@@ -351,7 +353,10 @@ public void setMotorBrake(boolean isBrakeMode)
351353
@Override
352354
public void setInverted(boolean inverted)
353355
{
354-
motor.setInverted(inverted);
356+
configureSparkMax(() -> {
357+
motor.setInverted(inverted);
358+
return motor.getLastError();
359+
});
355360
}
356361

357362
/**

src/main/java/swervelib/motors/SparkMaxSwerve.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.revrobotics.SparkAnalogSensor;
1313
import com.revrobotics.SparkPIDController;
1414
import edu.wpi.first.wpilibj.DriverStation;
15+
import edu.wpi.first.wpilibj.Timer;
1516
import java.util.function.Supplier;
1617
import swervelib.encoders.SwerveAbsoluteEncoder;
1718
import swervelib.parser.PIDFConfig;
@@ -26,31 +27,31 @@ public class SparkMaxSwerve extends SwerveMotor
2627
/**
2728
* {@link CANSparkMax} Instance.
2829
*/
29-
private final CANSparkMax motor;
30+
private final CANSparkMax motor;
3031
/**
3132
* Integrated encoder.
3233
*/
33-
public RelativeEncoder encoder;
34+
public RelativeEncoder encoder;
3435
/**
3536
* Absolute encoder attached to the SparkMax (if exists)
3637
*/
37-
public SwerveAbsoluteEncoder absoluteEncoder;
38+
public SwerveAbsoluteEncoder absoluteEncoder;
3839
/**
3940
* Closed-loop PID controller.
4041
*/
41-
public SparkPIDController pid;
42+
public SparkPIDController pid;
4243
/**
4344
* Factory default already occurred.
4445
*/
45-
private boolean factoryDefaultOccurred = false;
46+
private boolean factoryDefaultOccurred = false;
4647
/**
4748
* Supplier for the velocity of the motor controller.
4849
*/
49-
private Supplier<Double> velocity;
50+
private Supplier<Double> velocity;
5051
/**
5152
* Supplier for the position of the motor controller.
5253
*/
53-
private Supplier<Double> position;
54+
private Supplier<Double> position;
5455

5556
/**
5657
* Initialize the swerve motor.
@@ -100,6 +101,7 @@ private void configureSparkMax(Supplier<REVLibError> config)
100101
{
101102
return;
102103
}
104+
Timer.delay(0.01);
103105
}
104106
DriverStation.reportWarning("Failure configuring motor " + motor.getDeviceId(), true);
105107
}
@@ -357,7 +359,10 @@ public void setMotorBrake(boolean isBrakeMode)
357359
@Override
358360
public void setInverted(boolean inverted)
359361
{
360-
motor.setInverted(inverted);
362+
configureSparkMax(() -> {
363+
motor.setInverted(inverted);
364+
return motor.getLastError();
365+
});
361366
}
362367

363368
/**

0 commit comments

Comments
 (0)