Skip to content
This repository was archived by the owner on Sep 14, 2019. It is now read-only.

Commit 5bfb1c5

Browse files
committed
Added auto testing code and other minor fixes
1 parent bff95a8 commit 5bfb1c5

1 file changed

Lines changed: 179 additions & 0 deletions

File tree

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
package org.usfirst.frc.team199.Robot2018;
2+
3+
import org.junit.jupiter.api.BeforeEach;
4+
import org.junit.jupiter.api.Test;
5+
import org.mockito.invocation.InvocationOnMock;
6+
import org.mockito.stubbing.Answer;
7+
8+
import static org.mockito.Mockito.mock;
9+
import static org.mockito.Mockito.when;
10+
11+
import edu.wpi.first.wpilibj.HLUsageReporting;
12+
import edu.wpi.first.wpilibj.PIDController;
13+
import edu.wpi.first.wpilibj.PIDOutput;
14+
import edu.wpi.first.wpilibj.PIDSource;
15+
import edu.wpi.first.wpilibj.PIDSourceType;
16+
import edu.wpi.first.wpilibj.Timer;
17+
import edu.wpi.first.wpilibj.internal.HardwareTimer;
18+
19+
class AutoLiftTest {
20+
21+
final double liftkP = 0.01;
22+
final double liftkI = 0.0001;
23+
final double liftkD = 0.002;
24+
private final double targetOne = 24;
25+
private final double targetTwo = 60;
26+
private final double targetThree = 45;
27+
private int iterations;
28+
private final double tolerance = 1;
29+
30+
@BeforeEach
31+
void setUp() {
32+
// Since VelocityPIDController extends PIDController and PIDController calls
33+
// static methods in wpilib that only work on robot,
34+
// we setup these mocks to allow the tests to run off robot.
35+
iterations = 0;
36+
HardwareTimer tim = mock(HardwareTimer.class);
37+
Timer.Interface timerInstance = mock(Timer.Interface.class);
38+
when(tim.newTimer()).thenReturn(timerInstance);
39+
when(timerInstance.get()).thenAnswer(
40+
new Answer() {
41+
public Object answer(InvocationOnMock invocation) {
42+
return getTime();
43+
}
44+
});
45+
46+
Timer.SetImplementation(tim);
47+
HLUsageReporting.Interface usageReporter = mock(HLUsageReporting.Interface.class);
48+
HLUsageReporting.SetImplementation(usageReporter);
49+
}
50+
51+
private double getTime() {
52+
iterations++;
53+
return iterations * 0.02;
54+
}
55+
56+
class PIDSim{
57+
private double totalDist = 0;
58+
private final double distPerPulse = 0.01;
59+
// 1/pulses per rev, 1/99?
60+
private final double circum = Math.PI * 6;
61+
public PIDSim() {
62+
totalDist = 0;
63+
}
64+
65+
public double pidGet() {
66+
return totalDist;
67+
}
68+
69+
public void pidWrite(double output) {
70+
//256 rpm
71+
totalDist += (output * 256) * distPerPulse * circum;
72+
}
73+
}
74+
75+
class PIDSimController extends PIDController{
76+
77+
public PIDSimController(double p, double i, double d, PIDSource src, PIDOutput out) {
78+
super(p,i,d,src,out);
79+
}
80+
81+
public void calc() {
82+
this.calculate();
83+
}
84+
85+
}
86+
87+
class PIDSimSource implements PIDSource{
88+
private PIDSim sim;
89+
private PIDSourceType type;
90+
public PIDSimSource(PIDSim sim) {
91+
this.sim = sim;
92+
}
93+
94+
@Override
95+
public PIDSourceType getPIDSourceType() {
96+
return type;
97+
}
98+
99+
@Override
100+
public double pidGet() {
101+
return sim.pidGet();
102+
}
103+
104+
@Override
105+
public void setPIDSourceType(PIDSourceType pidSource) {
106+
// TODO Auto-generated method stub
107+
type = pidSource;
108+
}
109+
}
110+
111+
class PIDSimOutput implements PIDOutput{
112+
private PIDSim sim;
113+
public PIDSimOutput(PIDSim sim) {
114+
this.sim = sim;
115+
}
116+
@Override
117+
public void pidWrite(double output) {
118+
sim.pidWrite(output);
119+
}
120+
}
121+
122+
@Test
123+
void test24() {
124+
PIDSim sim = new PIDSim();
125+
PIDSimSource src = new PIDSimSource(sim);
126+
src.setPIDSourceType(PIDSourceType.kDisplacement);
127+
PIDSimOutput out = new PIDSimOutput(sim);
128+
PIDSimController liftController = new PIDSimController(liftkP, liftkI, liftkD, src, out);
129+
liftController.setSetpoint(targetOne);
130+
liftController.setOutputRange(-1.0, 1.0);
131+
liftController.enable();
132+
//1 second?
133+
for(int i = 0; i < 50; i++) {
134+
liftController.calc();
135+
}
136+
System.out.println("" + sim.pidGet());
137+
assert(sim.pidGet() > 0);
138+
assert(Math.abs(targetOne - sim.pidGet()) < tolerance);
139+
}
140+
141+
@Test
142+
void test60() {
143+
PIDSim sim = new PIDSim();
144+
PIDSimSource src = new PIDSimSource(sim);
145+
src.setPIDSourceType(PIDSourceType.kDisplacement);
146+
PIDSimOutput out = new PIDSimOutput(sim);
147+
PIDSimController liftController = new PIDSimController(liftkP, liftkI, liftkD, src, out);
148+
liftController.setSetpoint(targetTwo);
149+
liftController.setOutputRange(-1.0, 1.0);
150+
liftController.enable();
151+
//1 second?
152+
for(int i = 0; i < 50; i++) {
153+
liftController.calc();
154+
}
155+
System.out.println("" + sim.pidGet());
156+
assert(sim.pidGet() > 0);
157+
assert(Math.abs(targetTwo - sim.pidGet()) < tolerance);
158+
}
159+
160+
@Test
161+
void test45() {
162+
PIDSim sim = new PIDSim();
163+
PIDSimSource src = new PIDSimSource(sim);
164+
src.setPIDSourceType(PIDSourceType.kDisplacement);
165+
PIDSimOutput out = new PIDSimOutput(sim);
166+
PIDSimController liftController = new PIDSimController(liftkP, liftkI, liftkD, src, out);
167+
liftController.setSetpoint(targetThree);
168+
liftController.setOutputRange(-1.0, 1.0);
169+
liftController.enable();
170+
//1 second?
171+
for(int i = 0; i < 50; i++) {
172+
liftController.calc();
173+
}
174+
System.out.println("" + sim.pidGet());
175+
assert(sim.pidGet() > 0);
176+
assert(Math.abs(targetThree - sim.pidGet()) < tolerance);
177+
}
178+
179+
}

0 commit comments

Comments
 (0)