Skip to content

Commit 40b1d4c

Browse files
authored
Merge pull request #124 from DeepBlueRobotics/createSpark
2 parents 26c3893 + 3fec01c commit 40b1d4c

2 files changed

Lines changed: 49 additions & 0 deletions

File tree

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,37 @@ public static WPI_TalonSRX createTalon(int id) {
5656

5757
return talon;
5858
}
59+
/**
60+
* This class returns a SparkBase, initialized to either a SparkMax or SparkFlex depending on the {@link MotorConfig#controllerType}.
61+
* 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.
62+
* To use the configAccessor, use {@link MotorControllerFactory#getConfigAccessor(SparkBase)}
63+
*
64+
* @param id the port of the motor controller
65+
* @param motorConfig the motor configuration to use
66+
*/
67+
public static SparkBase createSpark(int id, MotorConfig motorConfig) {
68+
return createSpark(id, motorConfig, sparkConfig(motorConfig));
69+
}
70+
/**
71+
* This class returns a SparkBase, initialized to either a SparkMax or SparkFlex depending on the {@link MotorConfig#controllerType}.
72+
* 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.
73+
* To use the configAccessor, use {@link MotorControllerFactory#getConfigAccessor(SparkBase)}
74+
*
75+
* @param id the port of the motor controller
76+
* @param motorConfig the motor configuration to use
77+
* @param config the custom SparkBase configuration to apply instead of the default for the motorConfig
78+
*/
79+
public static SparkBase createSpark(int id, MotorConfig motorConfig, SparkBaseConfig config) {
80+
switch(motorConfig.controllerType){
81+
case SPARK_MAX:
82+
return createSparkMax(id, motorConfig, config);
83+
case SPARK_FLEX:
84+
return createSparkFlex(id, motorConfig, config);
85+
default:
86+
return null;
87+
}
88+
}
89+
5990
/**
6091
* Create a default sparkMax controller (NEO or 550).
6192
*
@@ -88,6 +119,7 @@ public static SparkMax createSparkMax(int id, MotorConfig motorConfig, SparkBase
88119

89120
return spark;
90121
}
122+
91123
/**
92124
* Create a default SparkFlex-Vortex controller.
93125
*

src/test/java/org/carlmontrobotics/lib199/MotorControllerFactoryTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package org.carlmontrobotics.lib199;
22

33
import static org.junit.Assert.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
45

56
import org.carlmontrobotics.lib199.testUtils.ErrStreamTest;
67
import org.carlmontrobotics.lib199.testUtils.TestRules;
78
import org.junit.ClassRule;
89
import org.junit.Rule;
910
import org.junit.Test;
1011

12+
import com.revrobotics.spark.SparkFlex;
13+
import com.revrobotics.spark.SparkMax;
14+
1115
public class MotorControllerFactoryTest extends ErrStreamTest {
1216

1317
@ClassRule
@@ -20,7 +24,20 @@ public class MotorControllerFactoryTest extends ErrStreamTest {
2024
public void testCreateNoErrors() throws Exception {
2125
// Call close to free PWM ports
2226
((AutoCloseable)MotorControllerFactory.createTalon(0)).close();
27+
2328
MotorControllerFactory.createSparkMax(2, MotorConfig.NEO);
29+
MotorControllerFactory.createSparkMax(3, MotorConfig.NEO, MotorControllerFactory.sparkConfig(MotorConfig.NEO));
30+
31+
MotorControllerFactory.createSparkFlex(10);
32+
MotorControllerFactory.createSparkFlex(10, MotorConfig.NEO_VORTEX, MotorControllerFactory.sparkConfig(MotorConfig.NEO_VORTEX));
33+
34+
assertInstanceOf(SparkMax.class, MotorControllerFactory.createSpark(20, MotorConfig.NEO));
35+
assertInstanceOf(SparkMax.class, MotorControllerFactory.createSpark(21, MotorConfig.NEO_2));
36+
assertInstanceOf(SparkFlex.class, MotorControllerFactory.createSpark(22, MotorConfig.NEO_VORTEX));
37+
assertInstanceOf(SparkMax.class, MotorControllerFactory.createSpark(23, MotorConfig.NEO, MotorControllerFactory.sparkConfig(MotorConfig.NEO)));
38+
assertInstanceOf(SparkMax.class, MotorControllerFactory.createSpark(24, MotorConfig.NEO_2, MotorControllerFactory.sparkConfig(MotorConfig.NEO_2)));
39+
assertInstanceOf(SparkFlex.class, MotorControllerFactory.createSpark(25, MotorConfig.NEO_VORTEX, MotorControllerFactory.sparkConfig(MotorConfig.NEO_VORTEX)));
40+
2441
assertEquals(0, errStream.toByteArray().length);
2542
}
2643

0 commit comments

Comments
 (0)