-
Notifications
You must be signed in to change notification settings - Fork 35
/
MotorChecker.java
126 lines (95 loc) · 4.08 KB
/
MotorChecker.java
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
package com.team254.lib.drivers;
import com.team254.frc2019.subsystems.Subsystem;
import com.team254.lib.util.Util;
import edu.wpi.first.wpilibj.Timer;
import java.util.ArrayList;
import java.util.function.DoubleSupplier;
public abstract class MotorChecker<T> {
public static class CheckerConfig {
public double mCurrentFloor = 5;
public double mRPMFloor = 2000;
public double mCurrentEpsilon = 5.0;
public double mRPMEpsilon = 500;
public DoubleSupplier mRPMSupplier = null;
public double mRunTimeSec = 4.0;
public double mWaitTimeSec = 2.0;
public double mRunOutputPercentage = 0.5;
}
public static class MotorConfig<T> {
public String mName;
public T mMotor;
public MotorConfig(String name, T motor) {
mName = name;
mMotor = motor;
}
}
protected ArrayList<MotorConfig<T>> mMotorsToCheck;
protected abstract void storeConfiguration();
protected abstract void restoreConfiguration();
protected abstract void setMotorOutput(T motor, double output);
protected abstract double getMotorCurrent(T motor);
protected boolean checkMotorsImpl(Subsystem subsystem,
ArrayList<MotorConfig<T>> motorsToCheck,
CheckerConfig checkerConfig) {
boolean failure = false;
System.out.println("////////////////////////////////////////////////");
System.out.println("Checking subsystem " + subsystem.getClass()
+ " for " + motorsToCheck.size() + " motors.");
ArrayList<Double> currents = new ArrayList<>();
ArrayList<Double> rpms = new ArrayList<>();
mMotorsToCheck = motorsToCheck;
storeConfiguration();
for (MotorConfig<T> config : motorsToCheck) {
setMotorOutput(config.mMotor, 0.0);
}
for (MotorConfig<T> config : motorsToCheck) {
System.out.println("Checking: " + config.mName);
setMotorOutput(config.mMotor, checkerConfig.mRunOutputPercentage);
Timer.delay(checkerConfig.mRunTimeSec);
// poll the interesting information
double current = getMotorCurrent(config.mMotor);
currents.add(current);
System.out.print("Current: " + current);
double rpm = Double.NaN;
if (checkerConfig.mRPMSupplier != null) {
rpm = checkerConfig.mRPMSupplier.getAsDouble();
rpms.add(rpm);
System.out.print(" RPM: " + rpm);
}
System.out.print('\n');
setMotorOutput(config.mMotor, 0.0);
// perform checks
if (current < checkerConfig.mCurrentFloor) {
System.out.println(config.mName + " has failed current floor check vs " +
checkerConfig.mCurrentFloor + "!!");
failure = true;
}
if (checkerConfig.mRPMSupplier != null) {
if (rpm < checkerConfig.mRPMFloor) {
System.out.println(config.mName + " has failed rpm floor check vs " +
checkerConfig.mRPMFloor + "!!");
failure = true;
}
}
Timer.delay(checkerConfig.mWaitTimeSec);
}
// run aggregate checks
if (currents.size() > 0) {
double average = currents.stream().mapToDouble(val -> val).average().getAsDouble();
if (!Util.allCloseTo(currents, average, checkerConfig.mCurrentEpsilon)) {
System.out.println("Currents varied!!!!!!!!!!!");
failure = true;
}
}
if (rpms.size() > 0) {
double average = rpms.stream().mapToDouble(val -> val).average().getAsDouble();
if (!Util.allCloseTo(rpms, average, checkerConfig.mRPMEpsilon)) {
System.out.println("RPMs varied!!!!!!!!");
failure = true;
}
}
// restore talon configurations
restoreConfiguration();
return !failure;
}
}