Skip to content

Commit 6bef0d1

Browse files
committed
spark max utilities
1 parent 53e824d commit 6bef0d1

3 files changed

Lines changed: 134 additions & 6 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package frc.robot.lib;
2+
3+
import com.revrobotics.CANSparkMax;
4+
import com.revrobotics.RelativeEncoder;
5+
import com.revrobotics.SparkMaxPIDController;
6+
7+
public class CachedSparkMax extends CANSparkMax {
8+
9+
private RelativeEncoder encoder;
10+
private SparkMaxPIDController pidController;
11+
12+
public CachedSparkMax(int deviceId, MotorType type) {
13+
super(deviceId, type);
14+
this.encoder = super.getEncoder();
15+
this.pidController = super.getPIDController();
16+
}
17+
18+
@Override
19+
public RelativeEncoder getEncoder() {
20+
return encoder;
21+
}
22+
23+
@Override
24+
public SparkMaxPIDController getPIDController() {
25+
return pidController;
26+
}
27+
28+
}

src/main/java/frc/robot/lib/MotorControllerFactory.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,13 @@ public static WPI_TalonSRX createTalon(int id) {
7373

7474
public static CANSparkMax createSparkMax(int id) {
7575
CANSparkMax spark;
76-
if (RobotBase.isReal()) {
77-
spark = new CANSparkMax(id, CANSparkMaxLowLevel.MotorType.kBrushless);
76+
if (RobotBase.isReal()) {
77+
spark = new CachedSparkMax(id, CANSparkMaxLowLevel.MotorType.kBrushless);
7878
if (spark.getFirmwareVersion() == 0) {
79-
spark.close();
80-
System.err.println("SparkMax on port: " + id + " is not connected!");
81-
return MotorErrors.createDummySparkMax();
82-
}
79+
spark.close();
80+
System.err.println("SparkMax on port: " + id + " is not connected!");
81+
return MotorErrors.createDummySparkMax();
82+
}
8383
} else {
8484
spark = MockSparkMax.createMockSparkMax(id, CANSparkMaxLowLevel.MotorType.kBrushless);
8585
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package frc.robot.lib;
2+
3+
import com.revrobotics.CANSparkMax;
4+
import com.revrobotics.RelativeEncoder;
5+
import com.revrobotics.SparkMaxPIDController;
6+
import com.revrobotics.CANSparkMax.ControlType;
7+
8+
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
9+
10+
public class SparkVelocityPIDController {
11+
12+
private final CANSparkMax spark;
13+
private final SparkMaxPIDController pidController;
14+
private final RelativeEncoder encoder;
15+
private final String name;
16+
private double targetSpeed, tolerance;
17+
private double currentP, currentI, currentD, kS, kV;
18+
19+
public SparkVelocityPIDController(String name, CANSparkMax spark, double defaultP, double defaultI, double defaultD, double kS, double kV, double targetSpeed, double tolerance) {
20+
this.spark = spark;
21+
this.pidController = spark.getPIDController();
22+
this.encoder = spark.getEncoder();
23+
this.name = name;
24+
this.targetSpeed = targetSpeed;
25+
this.tolerance = tolerance;
26+
pidController.setP(this.currentP = defaultP);
27+
pidController.setI(this.currentI = defaultI);
28+
pidController.setD(this.currentD = defaultD);
29+
this.kS = kS;
30+
this.kV = kV;
31+
32+
SmartDashboard.putNumber(name + ": P", currentP);
33+
SmartDashboard.putNumber(name + ": I", currentI);
34+
SmartDashboard.putNumber(name + ": D", currentD);
35+
SmartDashboard.putNumber(name + ": Target Speed", targetSpeed);
36+
SmartDashboard.putNumber(name + ": Tolerance", tolerance);
37+
38+
pidController.setReference(targetSpeed, ControlType.kVelocity, 0, calculateFF(targetSpeed));
39+
}
40+
41+
public void periodic() {
42+
double p = SmartDashboard.getNumber(name + ": P", currentP);
43+
double i = SmartDashboard.getNumber(name + ": I", currentI);
44+
double d = SmartDashboard.getNumber(name + ": D", currentD);
45+
46+
if(p != currentP) {
47+
pidController.setP(p);
48+
currentP = p;
49+
}
50+
if(i != currentI) {
51+
pidController.setI(i);
52+
currentI = i;
53+
}
54+
if(d != currentD) {
55+
pidController.setD(d);
56+
currentD = d;
57+
}
58+
59+
setTargetSpeed(SmartDashboard.getNumber(name + ": Target Speed", targetSpeed));
60+
setTolerance(SmartDashboard.getNumber(name + ": Tolerance", tolerance));
61+
SmartDashboard.putNumber(name + ": Current Speed", encoder.getVelocity());
62+
}
63+
64+
public RelativeEncoder getEncoder() {
65+
return encoder;
66+
}
67+
68+
public boolean isAtTargetSpeed() {
69+
return encoder.getVelocity() > targetSpeed - tolerance;
70+
}
71+
72+
public String getName() {
73+
return name;
74+
}
75+
76+
public double getTargetSpeed() {
77+
return targetSpeed;
78+
}
79+
80+
public void setTargetSpeed(double targetSpeed) {
81+
if(targetSpeed == this.targetSpeed) return;
82+
SmartDashboard.putNumber(name + ": Target Speed", targetSpeed);
83+
this.targetSpeed = targetSpeed;
84+
pidController.setReference(targetSpeed, ControlType.kVelocity, 0, calculateFF(targetSpeed));
85+
}
86+
87+
public double getTolerance() {
88+
return tolerance;
89+
}
90+
91+
public void setTolerance(double tolerance) {
92+
SmartDashboard.putNumber(name + ": Tolerance", tolerance);
93+
this.tolerance = tolerance;
94+
}
95+
96+
public double calculateFF(double velocity) {
97+
return kS * Math.signum(velocity) + kV * velocity;
98+
}
99+
100+
}

0 commit comments

Comments
 (0)