-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMotorControllerFactory.java
More file actions
167 lines (151 loc) · 6.01 KB
/
MotorControllerFactory.java
File metadata and controls
167 lines (151 loc) · 6.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2018-2019 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
package org.carlmontrobotics.lib199;
import org.carlmontrobotics.lib199.sim.MockSparkFlex;
import org.carlmontrobotics.lib199.sim.MockSparkMax;
// import org.carlmontrobotics.lib199.sim.MockSparkFlex;
// import org.carlmontrobotics.lib199.sim.MockSparkMax;
import org.carlmontrobotics.lib199.sim.MockTalonSRX;
import com.ctre.phoenix.motorcontrol.NeutralMode;
import com.ctre.phoenix.motorcontrol.can.WPI_TalonSRX;
import com.revrobotics.spark.SparkBase;
import com.revrobotics.spark.SparkFlex;
import com.revrobotics.spark.SparkLowLevel;
import com.revrobotics.spark.SparkMax;
import com.revrobotics.spark.config.SparkBaseConfig;
import com.revrobotics.spark.config.SparkBaseConfig.IdleMode;
import com.revrobotics.spark.config.SparkFlexConfig;
import com.revrobotics.spark.config.SparkMaxConfig;
import com.revrobotics.spark.ClosedLoopSlot;
import com.revrobotics.spark.SparkBase;
import com.revrobotics.spark.SparkFlex;
import com.revrobotics.spark.SparkLowLevel;
import com.revrobotics.spark.SparkClosedLoopController;
// import org.carlmontrobotics.lib199.sim.MockSparkFlex;
// import org.carlmontrobotics.lib199.sim.MockSparkMax;
import org.carlmontrobotics.lib199.sim.MockTalonSRX;
import org.carlmontrobotics.lib199.sim.MockedCANCoder;
import edu.wpi.first.cameraserver.CameraServer;
import edu.wpi.first.cscore.UsbCamera;
import edu.wpi.first.cscore.VideoSource.ConnectionStrategy;
import edu.wpi.first.wpilibj.RobotBase;
/**
* Add your docs here.
*/
public class MotorControllerFactory {
public static WPI_TalonSRX createTalon(int id) {
WPI_TalonSRX talon;
if (RobotBase.isReal()) {
talon = new WPI_TalonSRX(id);
} else {
talon = MockTalonSRX.createMockTalonSRX(id);
}
// Put all configurations for the talon motor controllers in here.
// All values are from last year's code.
MotorErrors.reportError(talon.configNominalOutputForward(0, 10));
MotorErrors.reportError(talon.configNominalOutputReverse(0, 10));
MotorErrors.reportError(talon.configPeakOutputForward(1, 10));
MotorErrors.reportError(talon.configPeakOutputReverse(-1, 10));
MotorErrors.reportError(talon.configPeakCurrentLimit(0, 0));
MotorErrors.reportError(talon.configPeakCurrentDuration(0, 0));
// 40 Amps is the amp limit of a CIM. lThe PDP has 40 amp circuit breakers,
MotorErrors.reportError(talon.configContinuousCurrentLimit(30, 0));
talon.enableCurrentLimit(true);
MotorErrors.reportError(talon.configNeutralDeadband(0.001, 10));
talon.setNeutralMode(NeutralMode.Brake);
return talon;
}
/**
* Create a default sparkMax controller (NEO or 550).
*
* @param id the port of the motor controller
* @param motorConfig either MotorConfig.NEO or MotorConfig.NEO_550
*/
public static SparkMax createSparkMax(int id, MotorConfig motorConfig) {
return createSparkMax(id, motorConfig, sparkConfig(motorConfig));
}
/**
* Create a sparkMax controller (NEO or 550) with custom settings.
*
* @param id the port of the motor controller
* @param config the custom config to set
*/
public static SparkMax createSparkMax(int id, MotorConfig motorConfig, SparkBaseConfig config) {
SparkMax spark;
if (RobotBase.isReal()) {
spark = new SparkMax(id, SparkLowLevel.MotorType.kBrushless);
} else {
spark = MockSparkMax.createMockSparkMax(id, SparkLowLevel.MotorType.kBrushless, MockSparkMax.NEOType.NEO);
}
spark.configure(
config,
SparkBase.ResetMode.kResetSafeParameters,
SparkBase.PersistMode.kNoPersistParameters
);
MotorErrors.reportSparkTemp(spark, motorConfig.temperatureLimitCelsius);
MotorErrors.checkSparkErrors(spark);
return spark;
}
/**
* Create a default SparkFlex-Vortex controller.
*
* @param id the port of the motor controller
*/
public static SparkFlex createSparkFlex(int id) {
return createSparkFlex(id, MotorConfig.NEO_VORTEX, sparkConfig(MotorConfig.NEO_VORTEX));
}
/**
* Create a sparkFlex controller (VORTEX) with custom settings.
*
* @param id the port of the motor controller
* @param config the custom config to set
*/
public static SparkFlex createSparkFlex(int id, MotorConfig motorConfig, SparkBaseConfig config) {
SparkFlex spark = null;
if (RobotBase.isReal()) {
spark = new SparkFlex(id, SparkLowLevel.MotorType.kBrushless);
} else {
spark = MockSparkFlex.createMockSparkFlex(id, SparkLowLevel.MotorType.kBrushless);
}
MotorErrors.reportSparkTemp(spark, motorConfig.temperatureLimitCelsius);
MotorErrors.checkSparkErrors(spark);
return spark;
}
public static SparkBaseConfig createConfig(MotorControllerType type) {
SparkBaseConfig config = null;
switch(type){
case SPARK_MAX:
config = new SparkMaxConfig();
break;
case SPARK_FLEX:
config = new SparkFlexConfig();
break;
}
return config;
}
public static MotorControllerType getControllerType(SparkBase motor){
if(motor instanceof SparkMax){
return MotorControllerType.SPARK_MAX;
}else if(motor instanceof SparkFlex){
return MotorControllerType.SPARK_FLEX;
}
return null;
}
public static SparkBaseConfig sparkConfig(MotorConfig motorConfig){
SparkBaseConfig config = motorConfig.controllerType.createConfig();
//configs that apply to all motors
config.idleMode(IdleMode.kBrake);
config.voltageCompensation(12);
config.smartCurrentLimit(motorConfig.currentLimitAmps);
config.closedLoop
.minOutput(-1)
.maxOutput(1)
.pid(0,0,0)
.velocityFF(0);
return config;
}
}