mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
Fixed the motor tests by reducing speed to within the limits of the encoders we use. Also fixed java pid tolerances since getAvgError() was broken. It is now fixed and works properly. Added tests for both java and cpp that test if pid tolerances are working using fake input output pairs.
Change-Id: I5bf23dbbdab996c582e1035fc2b2f36dd5f52417
This commit is contained in:
@@ -60,7 +60,7 @@ public class MotorEncoderTest extends AbstractComsSetup {
|
||||
@Before
|
||||
public void setUp() {
|
||||
double initialSpeed = me.getMotor().get();
|
||||
assertTrue(me.getType() + " Did not start with an initial speeed of 0 instead got: "
|
||||
assertTrue(me.getType() + " Did not start with an initial speed of 0 instead got: "
|
||||
+ initialSpeed, Math.abs(initialSpeed) < 0.001);
|
||||
me.setup();
|
||||
|
||||
@@ -101,7 +101,7 @@ public class MotorEncoderTest extends AbstractComsSetup {
|
||||
public void testIncrement() {
|
||||
int startValue = me.getEncoder().get();
|
||||
|
||||
me.getMotor().set(.75);
|
||||
me.getMotor().set(.2);
|
||||
Timer.delay(MOTOR_RUNTIME);
|
||||
int currentValue = me.getEncoder().get();
|
||||
assertTrue(me.getType() + " Encoder not incremented: start: " + startValue + "; current: "
|
||||
@@ -117,7 +117,7 @@ public class MotorEncoderTest extends AbstractComsSetup {
|
||||
public void testDecrement() {
|
||||
int startValue = me.getEncoder().get();
|
||||
|
||||
me.getMotor().set(-.75);
|
||||
me.getMotor().set(-.2);
|
||||
Timer.delay(MOTOR_RUNTIME);
|
||||
int currentValue = me.getEncoder().get();
|
||||
assertTrue(me.getType() + " Encoder not decremented: start: " + startValue + "; current: "
|
||||
@@ -170,18 +170,18 @@ public class MotorEncoderTest extends AbstractComsSetup {
|
||||
@Test
|
||||
public void testPositionPIDController() {
|
||||
me.getEncoder().setPIDSourceType(PIDSourceType.kDisplacement);
|
||||
PIDController pid = new PIDController(0.003, 0.001, 0, me.getEncoder(), me.getMotor());
|
||||
pid.setAbsoluteTolerance(50);
|
||||
PIDController pid = new PIDController(0.001, 0.0005, 0, me.getEncoder(), me.getMotor());
|
||||
pid.setAbsoluteTolerance(50.0);
|
||||
pid.setOutputRange(-0.2, 0.2);
|
||||
pid.setSetpoint(2500);
|
||||
pid.setSetpoint(1000);
|
||||
|
||||
pid.enable();
|
||||
Timer.delay(10.0);
|
||||
pid.disable();
|
||||
|
||||
assertTrue(
|
||||
"PID loop did not reach setpoint within 10 seconds. The error was: " + pid.getError(),
|
||||
pid.onTarget());
|
||||
"PID loop did not reach setpoint within 10 seconds. The average error was: " + pid.getAvgError() +
|
||||
"The current error was" + pid.getError(), pid.onTarget());
|
||||
|
||||
pid.free();
|
||||
}
|
||||
@@ -194,7 +194,7 @@ public class MotorEncoderTest extends AbstractComsSetup {
|
||||
pid.setAbsoluteTolerance(200);
|
||||
pid.setToleranceBuffer(50);
|
||||
pid.setOutputRange(-0.3, 0.3);
|
||||
pid.setSetpoint(2000);
|
||||
pid.setSetpoint(600);
|
||||
|
||||
pid.enable();
|
||||
Timer.delay(10.0);
|
||||
|
||||
@@ -30,7 +30,7 @@ import java.util.logging.Logger;
|
||||
@RunWith(Parameterized.class)
|
||||
public class MotorInvertingTest extends AbstractComsSetup {
|
||||
static MotorEncoderFixture<?> fixture = null;
|
||||
private static final double motorspeed = 0.35;
|
||||
private static final double motorspeed = 0.2;
|
||||
private static final double delaytime = 0.3;
|
||||
|
||||
|
||||
|
||||
@@ -46,7 +46,7 @@ public class PIDTest extends AbstractComsSetup {
|
||||
private NetworkTable table;
|
||||
|
||||
private static final double absoluteTolerance = 50;
|
||||
private static final double outputRange = 0.3;
|
||||
private static final double outputRange = 0.25;
|
||||
|
||||
private PIDController controller = null;
|
||||
private static MotorEncoderFixture me = null;
|
||||
@@ -181,7 +181,7 @@ public class PIDTest extends AbstractComsSetup {
|
||||
public void testRotateToTarget() {
|
||||
setupAbsoluteTolerance();
|
||||
setupOutputRange();
|
||||
double setpoint = 2500.0;
|
||||
double setpoint = 1000.0;
|
||||
assertEquals(pidData() + "did not start at 0", 0, controller.get(), 0);
|
||||
controller.setSetpoint(setpoint);
|
||||
assertEquals(pidData() + "did not have an error of " + setpoint, setpoint,
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
package edu.wpi.first.wpilibj;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import edu.wpi.first.wpilibj.test.AbstractComsSetup;
|
||||
|
||||
public class PIDToleranceTest extends AbstractComsSetup {
|
||||
private static final Logger logger = Logger.getLogger(PIDToleranceTest.class.getName());
|
||||
private PIDController pid;
|
||||
private final double setPoint = 50.0;
|
||||
private final double tolerance = 10.0;
|
||||
private final double range = 200;
|
||||
private class fakeInput implements PIDSource{
|
||||
public double val;
|
||||
public fakeInput(){
|
||||
val = 0;
|
||||
}
|
||||
@Override
|
||||
public PIDSourceType getPIDSourceType() {
|
||||
return PIDSourceType.kDisplacement;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double pidGet() {
|
||||
return val;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPIDSourceType(PIDSourceType arg0) {}
|
||||
};
|
||||
private fakeInput inp;
|
||||
private PIDOutput out = new PIDOutput(){
|
||||
@Override
|
||||
public void pidWrite(double out) {
|
||||
}
|
||||
|
||||
};
|
||||
@Override
|
||||
protected Logger getClassLogger() {
|
||||
return logger;
|
||||
}
|
||||
@Before
|
||||
public void setUp() throws Exception{
|
||||
inp = new fakeInput();
|
||||
pid = new PIDController(0.05,0.0,0.0,inp,out);
|
||||
pid.setInputRange(-range/2, range/2);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception{
|
||||
pid.free();
|
||||
pid = null;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAbsoluteTolerance(){
|
||||
pid.setAbsoluteTolerance(tolerance);
|
||||
pid.setSetpoint(setPoint);
|
||||
pid.enable();
|
||||
Timer.delay(1);
|
||||
assertFalse("Error was in tolerance when it should not have been. Error was "+pid.getAvgError(),pid.onTarget());
|
||||
inp.val = setPoint+tolerance/2;
|
||||
Timer.delay(1.0);
|
||||
assertTrue("Error was not in tolerance when it should have been. Error was "+pid.getAvgError(),pid.onTarget());
|
||||
inp.val = setPoint + 10*tolerance;
|
||||
Timer.delay(1.0);
|
||||
assertFalse("Error was in tolerance when it should not have been. Error was "+pid.getAvgError(),pid.onTarget());
|
||||
}
|
||||
@Test
|
||||
public void testPercentTolerance(){
|
||||
pid.setPercentTolerance(tolerance);
|
||||
pid.setSetpoint(setPoint);
|
||||
pid.enable();
|
||||
assertFalse("Error was in tolerance when it should not have been. Error was "+pid.getAvgError(),pid.onTarget());
|
||||
inp.val = setPoint+(tolerance)/200*range;//half of percent tolerance away from setPoint
|
||||
Timer.delay(1.0);
|
||||
assertTrue("Error was not in tolerance when it should have been. Error was "+pid.getAvgError(),pid.onTarget());
|
||||
inp.val = setPoint + (tolerance)/50*range;//double percent tolerance away from setPoint
|
||||
Timer.delay(1.0);
|
||||
assertFalse("Error was in tolerance when it should not have been. Error was "+pid.getAvgError(),pid.onTarget());
|
||||
}
|
||||
}
|
||||
@@ -25,7 +25,7 @@ import edu.wpi.first.wpilibj.test.AbstractTestSuite;
|
||||
DIOCrossConnectTest.class, EncoderTest.class, FilterNoiseTest.class,
|
||||
FilterOutputTest.class, GyroTest.class, MotorEncoderTest.class,
|
||||
MotorInvertingTest.class, PCMTest.class, PDPTest.class, PIDTest.class,
|
||||
PreferencesTest.class, RelayCrossConnectTest.class, SampleTest.class,
|
||||
TimerTest.class})
|
||||
PIDToleranceTest.class, PreferencesTest.class, RelayCrossConnectTest.class,
|
||||
SampleTest.class, TimerTest.class})
|
||||
public class WpiLibJTestSuite extends AbstractTestSuite {
|
||||
}
|
||||
|
||||
@@ -18,9 +18,9 @@ import java.util.logging.Logger;
|
||||
*/
|
||||
public class CANJaguarInversionTest extends AbstractCANTest {
|
||||
private static final Logger logger = Logger.getLogger(CANJaguarInversionTest.class.getName());
|
||||
private static final double motorVoltage = 5.0;
|
||||
private static final double motorPercent = 0.5;
|
||||
private static final double motorSpeed = 100;
|
||||
private static final double motorVoltage = 2.0;
|
||||
private static final double motorPercent = 0.1;
|
||||
private static final double motorSpeed = 10;
|
||||
private static final double delayTime = 0.75;
|
||||
private static final double speedModeDelayTime = 2.0;
|
||||
|
||||
|
||||
@@ -37,7 +37,7 @@ public class CANPercentQuadEncoderModeTest extends AbstractCANTest {
|
||||
private static final Logger logger = Logger.getLogger(CANPercentQuadEncoderModeTest.class
|
||||
.getName());
|
||||
private static final double kStoppedValue = 0;
|
||||
private static final double kRunningValue = 1;
|
||||
private static final double kRunningValue = 0.3;
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
|
||||
@@ -31,7 +31,7 @@ public class CANSpeedQuadEncoderModeTest extends AbstractCANTest {
|
||||
/** The stopped value in rev/min */
|
||||
private static final double kStoppedValue = 0;
|
||||
/** The running value in rev/min */
|
||||
private static final double kRunningValue = 200;
|
||||
private static final double kRunningValue = 50;
|
||||
|
||||
@Override
|
||||
protected Logger getClassLogger() {
|
||||
|
||||
Reference in New Issue
Block a user