Updates/Tests the AnalogPotentiometer

New constructor for the AnalogPotentiometer to allow users to pass it an AnalogInput into the constructor
New FakePotentiometerSource to emulate a AnalogPotentiometer

Change-Id: I0f85b3d38e99d7ccbc7676bdf17fc3ec7e041e84
This commit is contained in:
Jonathan Leitschuh
2014-06-18 11:02:23 -04:00
parent 60cfabca81
commit 1fb6d9fd0c
5 changed files with 192 additions and 12 deletions

View File

@@ -0,0 +1,77 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2008-2014. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
package edu.wpi.first.wpilibj;
import static org.junit.Assert.*;
import java.util.logging.Logger;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import edu.wpi.first.wpilibj.fixtures.AnalogCrossConnectFixture;
import edu.wpi.first.wpilibj.mockhardware.FakePotentiometerSource;
import edu.wpi.first.wpilibj.test.AbstractComsSetup;
import edu.wpi.first.wpilibj.test.TestBench;
/**
* @author jonathanleitschuh
*
*/
public class AnalogPotentiometerTest extends AbstractComsSetup {
private static final Logger logger = Logger.getLogger(AnalogPotentiometerTest.class.getName());
private AnalogCrossConnectFixture analogIO;
private FakePotentiometerSource potSource;
private AnalogPotentiometer pot;
private static final double DOUBLE_COMPARISON_DELTA = 1.0;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
analogIO = TestBench.getAnalogCrossConnectFixture();
potSource = new FakePotentiometerSource(analogIO.getOutput(), 360);
pot = new AnalogPotentiometer(analogIO.getInput(), 360.0/5.0, 0);
}
/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
potSource.reset();
pot.free();
analogIO.teardown();
}
@Override
protected Logger getClassLogger() {
return logger;
}
@Test
public void testInitialSettings(){
assertEquals(0, pot.get(), DOUBLE_COMPARISON_DELTA);
}
@Test
public void testRangeValues(){
for(double i = 0.0; i < 360.0; i = i+1.0){
potSource.setAngle(i);
assertEquals(i, pot.get(), DOUBLE_COMPARISON_DELTA);
Timer.delay(.02);
}
}
}

View File

@@ -17,6 +17,7 @@ import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({
AnalogCrossConnectTest.class,
AnalogPotentiometerTest.class,
CounterTest.class,
DIOCrossConnectTest.class,
EncoderTest.class,

View File

@@ -0,0 +1,37 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2008-2014. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
package edu.wpi.first.wpilibj.mockhardware;
import edu.wpi.first.wpilibj.AnalogOutput;
/**
* @author jonathanleitschuh
*
*/
public class FakePotentiometerSource {
private AnalogOutput output;
private double potMaxAngle;
private final double defaultPotMaxAngle;
public FakePotentiometerSource(AnalogOutput output, double defaultPotMaxAngle){
this.defaultPotMaxAngle = defaultPotMaxAngle;
potMaxAngle = defaultPotMaxAngle;
this.output = output;
}
public void setRange(double range){
potMaxAngle = range;
}
public void reset(){
potMaxAngle = defaultPotMaxAngle;
output.setVoltage(0.0);
}
public void setAngle(double angle){
output.setVoltage((5.0/potMaxAngle)*angle);
}
}