Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,37 @@ public static WPI_TalonSRX createTalon(int id) {

return talon;
}
/**
* This class returns a SparkBase, initialized to either a SparkMax or SparkFlex depending on the {@link MotorConfig#controllerType}.
* Note that SparkBase lacks some of the functionality of SparkMax and SparkFlex, use {@link MotorControllerFactory#createSparkMax(int, MotorConfig)} or {@link MotorControllerFactory#createSparkFlex(int)} to get a SparkMax or SparkFlex object.
* To use the configAccessor, use {@link MotorControllerFactory#getConfigAccessor(SparkBase)}
*
* @param id the port of the motor controller
* @param motorConfig the motor configuration to use
*/
public static SparkBase createSpark(int id, MotorConfig motorConfig) {
return createSpark(id, motorConfig, sparkConfig(motorConfig));
}
/**
* This class returns a SparkBase, initialized to either a SparkMax or SparkFlex depending on the {@link MotorConfig#controllerType}.
* Note that SparkBase lacks some of the functionality of SparkMax and SparkFlex, use {@link MotorControllerFactory#createSparkMax(int, MotorConfig, SparkBaseConfig)} or {@link MotorControllerFactory#createSparkFlex(int, MotorConfig, SparkBaseConfig)} to get a SparkMax or SparkFlex object.
* To use the configAccessor, use {@link MotorControllerFactory#getConfigAccessor(SparkBase)}
*
* @param id the port of the motor controller
* @param motorConfig the motor configuration to use
* @param config the custom SparkBase configuration to apply instead of the default for the motorConfig
*/
public static SparkBase createSpark(int id, MotorConfig motorConfig, SparkBaseConfig config) {
switch(motorConfig.controllerType){
case SPARK_MAX:
return createSparkMax(id, motorConfig, config);
case SPARK_FLEX:
return createSparkFlex(id, motorConfig, config);
default:
return null;
Copy link

Copilot AI Feb 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default case returns null, which could lead to NullPointerExceptions if an unexpected controllerType is passed. While this pattern is consistent with other methods in this class (e.g., getConfigAccessor at line 157-158, createConfig at line 161-171, getControllerType at line 180), consider whether it would be safer to throw an IllegalArgumentException with a descriptive message explaining that the controllerType is unsupported. This would make debugging easier and fail faster if an invalid configuration is used.

Suggested change
return null;
throw new IllegalArgumentException(
"Unsupported controllerType in MotorControllerFactory.createSpark: "
+ motorConfig.controllerType);

Copilot uses AI. Check for mistakes.
}
}

/**
* Create a default sparkMax controller (NEO or 550).
*
Expand Down Expand Up @@ -88,6 +119,7 @@ public static SparkMax createSparkMax(int id, MotorConfig motorConfig, SparkBase

return spark;
}

/**
* Create a default SparkFlex-Vortex controller.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package org.carlmontrobotics.lib199;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;

import org.carlmontrobotics.lib199.testUtils.ErrStreamTest;
import org.carlmontrobotics.lib199.testUtils.TestRules;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;

import com.revrobotics.spark.SparkFlex;
import com.revrobotics.spark.SparkMax;

public class MotorControllerFactoryTest extends ErrStreamTest {

@ClassRule
Expand All @@ -20,7 +24,20 @@ public class MotorControllerFactoryTest extends ErrStreamTest {
public void testCreateNoErrors() throws Exception {
// Call close to free PWM ports
((AutoCloseable)MotorControllerFactory.createTalon(0)).close();

MotorControllerFactory.createSparkMax(2, MotorConfig.NEO);
MotorControllerFactory.createSparkMax(3, MotorConfig.NEO, MotorControllerFactory.sparkConfig(MotorConfig.NEO));

MotorControllerFactory.createSparkFlex(10);
MotorControllerFactory.createSparkFlex(10, MotorConfig.NEO_VORTEX, MotorControllerFactory.sparkConfig(MotorConfig.NEO_VORTEX));

assertInstanceOf(SparkMax.class, MotorControllerFactory.createSpark(20, MotorConfig.NEO));
assertInstanceOf(SparkMax.class, MotorControllerFactory.createSpark(21, MotorConfig.NEO_2));
assertInstanceOf(SparkFlex.class, MotorControllerFactory.createSpark(22, MotorConfig.NEO_VORTEX));
assertInstanceOf(SparkMax.class, MotorControllerFactory.createSpark(23, MotorConfig.NEO, MotorControllerFactory.sparkConfig(MotorConfig.NEO)));
assertInstanceOf(SparkMax.class, MotorControllerFactory.createSpark(24, MotorConfig.NEO_2, MotorControllerFactory.sparkConfig(MotorConfig.NEO_2)));
assertInstanceOf(SparkFlex.class, MotorControllerFactory.createSpark(25, MotorConfig.NEO_VORTEX, MotorControllerFactory.sparkConfig(MotorConfig.NEO_VORTEX)));

assertEquals(0, errStream.toByteArray().length);
}

Expand Down