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

Commit 72c2b79

Browse files
committed
velocityPIDController class(es) and attempted tests
1 parent b9cb9d8 commit 72c2b79

6 files changed

Lines changed: 177 additions & 0 deletions

File tree

Robot2018/.classpath

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,7 @@
1313
<classpathentry kind="lib" path="lib/mockito-core-2.13.3.jar"/>
1414
<classpathentry kind="lib" path="lib/byte-buddy-1.7.9.jar"/>
1515
<classpathentry kind="lib" path="lib/objenesis-2.6.jar"/>
16+
<classpathentry kind="lib" path="lib/CTRE_Phoenix-sources.jar"/>
17+
<classpathentry kind="lib" path="lib/CTRE_Phoenix.jar"/>
1618
<classpathentry kind="output" path="bin"/>
1719
</classpath>
59.8 KB
Binary file not shown.

Robot2018/lib/CTRE_Phoenix.jar

82.1 KB
Binary file not shown.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package org.usfirst.frc.team199.Robot2018.subsystems;
2+
3+
import com.ctre.phoenix.motorcontrol.can.WPI_TalonSRX;
4+
5+
import edu.wpi.first.wpilibj.SpeedController;
6+
7+
public class TalonVelocityController implements SpeedController {
8+
9+
private WPI_TalonSRX talon;
10+
11+
public TalonVelocityController(WPI_TalonSRX output) {
12+
talon = output;
13+
}
14+
15+
@Override
16+
public void pidWrite(double output) {
17+
// TODO Auto-generated method stub
18+
set(output);
19+
}
20+
21+
@Override
22+
public void set(double speed) {
23+
// TODO Auto-generated method stub
24+
25+
}
26+
27+
@Override
28+
public double get() {
29+
// TODO Auto-generated method stub
30+
return 0;
31+
}
32+
33+
@Override
34+
public void setInverted(boolean isInverted) {
35+
// TODO Auto-generated method stub
36+
37+
}
38+
39+
@Override
40+
public boolean getInverted() {
41+
// TODO Auto-generated method stub
42+
return false;
43+
}
44+
45+
@Override
46+
public void disable() {
47+
// TODO Auto-generated method stub
48+
49+
}
50+
51+
@Override
52+
public void stopMotor() {
53+
// TODO Auto-generated method stub
54+
55+
}
56+
57+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package org.usfirst.frc.team199.Robot2018.subsystems;
2+
3+
import edu.wpi.first.wpilibj.PIDController;
4+
import edu.wpi.first.wpilibj.PIDSource;
5+
import edu.wpi.first.wpilibj.SpeedController;
6+
7+
public class VelocityPIDController extends PIDController implements SpeedController {
8+
9+
private SpeedController out;
10+
11+
/**
12+
* @param kp the proportional PID constant
13+
* @param ki the integral PID constant
14+
* @param kd the derivative PID constant
15+
* @param source
16+
* the SpeedController you are reading from
17+
* @param output
18+
* the SpeedController you are telling what to do
19+
*/
20+
public VelocityPIDController(double kp, double ki, double kd, PIDSource source, SpeedController output) {
21+
super(kp, ki, kd, source, output);
22+
out = output;
23+
}
24+
25+
@Override
26+
public void pidWrite(double output) {
27+
setSetpoint(output);
28+
}
29+
30+
@Override
31+
public void set(double speed) {
32+
// TODO Auto-generated method stub
33+
setSetpoint(speed);
34+
}
35+
36+
@Override
37+
public double get() {
38+
// TODO Auto-generated method stub
39+
// should possibly be actual spdCtr value instead???
40+
return out.get();
41+
}
42+
43+
@Override
44+
public void setInverted(boolean isInverted) {
45+
out.setInverted(isInverted);
46+
}
47+
48+
@Override
49+
public boolean getInverted() {
50+
return out.getInverted();
51+
}
52+
53+
@Override
54+
public void stopMotor() {
55+
// TODO Auto-generated method stub
56+
out.stopMotor();
57+
}
58+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package org.usfirst.frc.team199.Robot2018;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.mockito.Mockito.mock;
5+
import static org.mockito.Mockito.verify;
6+
import static org.mockito.Mockito.when;
7+
8+
import org.junit.jupiter.api.Test;
9+
import org.usfirst.frc.team199.Robot2018.autonomous.VelocityPIDController;
10+
11+
import edu.wpi.first.wpilibj.PIDSource;
12+
import edu.wpi.first.wpilibj.SpeedController;
13+
import edu.wpi.first.wpilibj.Timer;
14+
import edu.wpi.first.wpilibj.internal.HardwareTimer;
15+
16+
class VelocityPIDControllerTest {
17+
18+
@Test
19+
void test1() {
20+
HardwareTimer tim = new HardwareTimer();
21+
Timer.SetImplementation(tim);
22+
PIDSource source = mock(PIDSource.class);
23+
SpeedController out = mock(SpeedController.class);
24+
double p = 1;
25+
double i = 0.5;
26+
double d = 0.0037;
27+
VelocityPIDController vPID = new VelocityPIDController(p, i, d, source, out);
28+
29+
when(out.get()).thenReturn(6.5);
30+
assertEquals(6.5, vPID.get());
31+
verify(out).get();
32+
}
33+
34+
@Test
35+
void test2() {
36+
PIDSource source = mock(PIDSource.class);
37+
SpeedController out = mock(SpeedController.class);
38+
double p = 1;
39+
double i = 0.5;
40+
double d = 0.0037;
41+
VelocityPIDController vPID = new VelocityPIDController(p, i, d, source, out);
42+
43+
vPID.pidWrite(20);
44+
assertEquals(vPID.getSetpoint(), 20);
45+
}
46+
47+
@Test
48+
void test3() {
49+
PIDSource source = mock(PIDSource.class);
50+
SpeedController out = mock(SpeedController.class);
51+
double p = 1;
52+
double i = 0.5;
53+
double d = 0.0037;
54+
VelocityPIDController vPID = new VelocityPIDController(p, i, d, source, out);
55+
56+
vPID.set(20);
57+
assertEquals(vPID.getSetpoint(), 20);
58+
}
59+
60+
}

0 commit comments

Comments
 (0)