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

@@ -1,3 +1,9 @@
/*----------------------------------------------------------------------------*/
/* 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 edu.wpi.first.wpilibj.interfaces.Potentiometer;
import edu.wpi.first.wpilibj.livewindow.LiveWindowSendable;
@@ -12,18 +18,20 @@ import edu.wpi.first.wpilibj.tables.ITable;
* @author Alex Henning
*/
public class AnalogPotentiometer implements Potentiometer, LiveWindowSendable {
private int m_channel;
private double m_scale, m_offset;
private AnalogInput m_analog_input;
private boolean m_init_analog_input;
/**
* Common initialization code called by all constructors.
* @param input The {@link AnalogInput} this potentiometer is plugged into.
* @param scale The scaling to multiply the voltage by to get a meaningful unit.
* @param offset The offset to add to the scaled value for controlling the zero value
*/
private void initPot(final int channel, double scale, double offset) {
this.m_channel = channel;
private void initPot(final AnalogInput input, double scale, double offset) {
this.m_scale = scale;
this.m_offset = offset;
m_analog_input = new AnalogInput(channel);
m_analog_input = input;
}
/**
@@ -41,7 +49,28 @@ public class AnalogPotentiometer implements Potentiometer, LiveWindowSendable {
* @param offset The offset to add to the scaled value for controlling the zero value
*/
public AnalogPotentiometer(final int channel, double scale, double offset) {
initPot(channel, scale, offset);
AnalogInput input = new AnalogInput(channel);
m_init_analog_input = true;
initPot(input, scale, offset);
}
/**
* AnalogPotentiometer constructor.
*
* Use the scaling and offset values so that the output produces
* meaningful values. I.E: you have a 270 degree potentiometer and
* you want the output to be degrees with the halfway point as 0
* degrees. The scale value is 270.0(degrees)/5.0(volts) and the
* offset is -135.0 since the halfway point after scaling is 135
* degrees.
*
* @param input The {@link AnalogInput} this potentiometer is plugged into.
* @param scale The scaling to multiply the voltage by to get a meaningful unit.
* @param offset The offset to add to the scaled value for controlling the zero value
*/
public AnalogPotentiometer(final AnalogInput input, double scale, double offset) {
m_init_analog_input = false;
initPot(input, scale, offset);
}
/**
@@ -58,7 +87,24 @@ public class AnalogPotentiometer implements Potentiometer, LiveWindowSendable {
* @param scale The scaling to multiply the voltage by to get a meaningful unit.
*/
public AnalogPotentiometer(final int channel, double scale) {
initPot(channel, scale, 0);
this(channel, scale, 0);
}
/**
* AnalogPotentiometer constructor.
*
* Use the scaling and offset values so that the output produces
* meaningful values. I.E: you have a 270 degree potentiometer and
* you want the output to be degrees with the halfway point as 0
* degrees. The scale value is 270.0(degrees)/5.0(volts) and the
* offset is -135.0 since the halfway point after scaling is 135
* degrees.
*
* @param input The {@link AnalogInput} this potentiometer is plugged into.
* @param scale The scaling to multiply the voltage by to get a meaningful unit.
*/
public AnalogPotentiometer(final AnalogInput input, double scale) {
this(input, scale, 0);
}
/**
@@ -67,7 +113,16 @@ public class AnalogPotentiometer implements Potentiometer, LiveWindowSendable {
* @param channel The analog channel this potentiometer is plugged into.
*/
public AnalogPotentiometer(final int channel) {
initPot(channel, 1, 0);
this(channel, 1, 0);
}
/**
* AnalogPotentiometer constructor.
*
* @param input The {@link AnalogInput} this potentiometer is plugged into.
*/
public AnalogPotentiometer(final AnalogInput input) {
this(input, 1, 0);
}
/**
@@ -89,7 +144,7 @@ public class AnalogPotentiometer implements Potentiometer, LiveWindowSendable {
}
/*
/**
* Live Window code, only does anything if live window is activated.
*/
public String getSmartDashboardType(){
@@ -120,6 +175,14 @@ public class AnalogPotentiometer implements Potentiometer, LiveWindowSendable {
public ITable getTable(){
return m_table;
}
public void free(){
if(m_init_analog_input){
m_analog_input.free();
m_analog_input = null;
m_init_analog_input = false;
}
}
/**
* Analog Channels don't have to do anything special when entering the LiveWindow.

View File

@@ -1,7 +1,9 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*----------------------------------------------------------------------------*/
/* 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.interfaces;
import edu.wpi.first.wpilibj.PIDSource;

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);
}
}