Skip to content

Commit 32da183

Browse files
simplify sparkmax factory
1 parent bc5e330 commit 32da183

File tree

1 file changed

+52
-42
lines changed

1 file changed

+52
-42
lines changed

src/main/java/org/carlmontrobotics/lib199/MotorControllerFactory.java

Lines changed: 52 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -83,53 +83,34 @@ public static WPI_TalonSRX createTalon(int id) {
8383
return talon;
8484
}
8585

86-
//checks for spark max errors
87-
88-
@Deprecated
89-
public static SparkMax createSparkMax(int id, MotorErrors.TemperatureLimit temperatureLimit) {
90-
return createSparkMax(id, temperatureLimit.limit);
91-
}
92-
93-
@Deprecated
94-
public static SparkMax createSparkMax(int id, int temperatureLimit) {
86+
/**
87+
* Create a sparkMax controller (NEO or 550) with defautl settings.
88+
*
89+
* @param id the port of the motor controller
90+
* @param motorConfig either MotorConfig.NEO or MotorConfig.NEO_550
91+
*/
92+
public static SparkMax createSparkMax(int id, MotorConfig motorConfig) {
9593
SparkMax spark=null;
9694
if (RobotBase.isReal()) {
9795
spark = new SparkMax(id, SparkLowLevel.MotorType.kBrushless);
9896
} else {
9997
System.err.println("heyy... lib199 doesn't have sim support sorri");
10098
// spark = MockSparkMax.createMockSparkMax(id, SparkLowLevel.MotorType.kBrushless);
10199
}
102-
SparkMaxConfig config = new SparkMaxConfig();
100+
103101
// config.setPeriodicFramePeriod(SparkLowLevel.PeriodicFrame.kStatus0, 1);
104-
//FIXME: What is kStatus0
105-
// config.signals.
106102
if (spark!=null)
107-
MotorErrors.reportSparkMaxTemp(spark, temperatureLimit);
108-
109-
// MotorErrors.reportError(config.follow(ExternalFollower.kFollowerDisabled, 0));
110-
// config.follow(null, false); dont follow nothing because thats the norm
111-
// MotorErrors.reportError(config.setIdleMode(IdleMode.kBrake));
112-
config.idleMode(IdleMode.kBrake);
113-
// MotorErrors.reportError(config.enableVoltageCompensation(12));
114-
config.voltageCompensation(12);
115-
// MotorErrors.reportError(config.smartCurrentLimit(50));
116-
config.smartCurrentLimit(50);
103+
MotorErrors.reportSparkMaxTemp(spark, motorConfig.temperatureLimitCelsius);
117104

118105
MotorErrors.checkSparkMaxErrors(spark);
119-
SparkClosedLoopController controller = spark.getClosedLoopController();
120-
// MotorErrors.reportError(controller.setOutputRange(-1, 1));
121-
config.closedLoop.minOutput(-1);
122-
config.closedLoop.maxOutput(1);
123-
// MotorErrors.reportError(controller.setP(0));
124-
// MotorErrors.reportError(controller.setI(0));
125-
// MotorErrors.reportError(controller.setD(0));
126-
config.closedLoop.p(0, ClosedLoopSlot.kSlot0);
127-
config.closedLoop.i(0, ClosedLoopSlot.kSlot0);
128-
config.closedLoop.d(0, ClosedLoopSlot.kSlot0);
129-
// MotorErrors.reportError(controller.setFF(0));
130-
config.closedLoop.velocityFF(0);
131-
132-
spark.configure(config, SparkBase.ResetMode.kResetSafeParameters, SparkBase.PersistMode.kPersistParameters);
106+
107+
if (motorConfig==MotorConfig.NEO || motorConfig==MotorConfig.NEO_550)
108+
spark.configure(baseSparkMaxConfig(), SparkBase.ResetMode.kResetSafeParameters, SparkBase.PersistMode.kPersistParameters);
109+
else if (motorConfig==MotorConfig.NEO_VORTEX)
110+
spark.configure(baseSparkFlexConfig(), SparkBase.ResetMode.kResetSafeParameters, SparkBase.PersistMode.kPersistParameters);
111+
else
112+
spark.configure(baseSparkConfig(), SparkBase.ResetMode.kResetSafeParameters, SparkBase.PersistMode.kPersistParameters);
113+
133114

134115
return spark;
135116
}
@@ -170,13 +151,13 @@ public static SparkFlex createSparkFlex(int id, SparkBaseConfig config) {
170151
return spark;
171152
}
172153

173-
private static SparkBaseConfig baseSparkConfig(MotorConfig motorConfig) {
154+
private static SparkBaseConfig baseSparkConfig() {
174155
SparkMaxConfig config = new SparkMaxConfig();
175156

176157
config.idleMode(IdleMode.kBrake);
177158

178159
config.voltageCompensation(12);//FIXME does this need to be different for different motors?
179-
config.smartCurrentLimit(motorConfig.currentLimitAmps);
160+
config.smartCurrentLimit(50);
180161

181162
config.closedLoop
182163
.minOutput(-1)
@@ -186,13 +167,42 @@ private static SparkBaseConfig baseSparkConfig(MotorConfig motorConfig) {
186167

187168
return config;
188169
}
189-
private static SparkMaxConfig baseSparkMaxConfig(MotorConfig motorConfig){
170+
/**
171+
* Overrides an old config - but does not change other settings.
172+
*/
173+
private static SparkBaseConfig baseSparkConfig(SparkMaxConfig config) {
174+
config.idleMode(IdleMode.kBrake);
175+
176+
config.voltageCompensation(12);//FIXME does this need to be different for different motors?
177+
config.smartCurrentLimit(50);
178+
179+
config.closedLoop
180+
.minOutput(-1)
181+
.maxOutput(1)
182+
.pid(0,0,0)
183+
.velocityFF(0);
184+
185+
return config;
186+
}
187+
/**
188+
* Overrides an old config - but does not change other settings.
189+
*/
190+
private static SparkMaxConfig baseSparkMaxConfig(SparkMaxConfig config){
190191
//typical operating voltage: 12V.
191-
return (SparkMaxConfig) baseSparkConfig(motorConfig);//FIXME apply needed config changes for each controller
192+
return (SparkMaxConfig) baseSparkConfig(config);//FIXME apply needed config changes for each controller
192193
}
193-
private static SparkFlexConfig baseSparkFlexConfig(MotorConfig motorConfig){
194+
private static SparkMaxConfig baseSparkMaxConfig(){
195+
return (SparkMaxConfig) baseSparkConfig();
196+
}
197+
/**
198+
* Overrides an old config - but does not change other settings.
199+
*/
200+
private static SparkFlexConfig baseSparkFlexConfig(SparkMaxConfig config){
194201
//typical operating voltage: 12V. ( same as sparkMax )
195-
return (SparkFlexConfig) baseSparkConfig(motorConfig);//criminal casting usage
202+
return (SparkFlexConfig) baseSparkConfig(config);//criminal casting usage
203+
}
204+
private static SparkFlexConfig baseSparkFlexConfig(){//why? no Se.
205+
return (SparkFlexConfig) baseSparkConfig();
196206
}
197207

198208
/**

0 commit comments

Comments
 (0)