Merge "Changed AnalogPotentiometer to use angle specification and rail voltage."

This commit is contained in:
Fred Silberberg (WPI)
2014-12-05 16:58:04 -08:00
committed by Gerrit Code Review
5 changed files with 75 additions and 62 deletions

View File

@@ -5,38 +5,41 @@
/**
* Class for reading analog potentiometers. Analog potentiometers read
* in an analog voltage that corresponds to a position. Usually the
* position is either degrees or meters. However, if no conversion is
* given it remains volts.
* in an analog voltage that corresponds to a position. The position is
* in whichever units you choose, by way of the scaling and offset
* constants passed to the constructor.
*
* @author Alex Henning
* @author Colby Skeggs (rail voltage)
*/
class AnalogPotentiometer : public Potentiometer, public LiveWindowSendable {
public:
/**
* AnalogPotentiometer constructor.
*
* Use the scaling and offset values so that the output produces
* Use the fullRange 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.
* degrees. The fullRange value is 270.0(degrees) and the offset is
* -135.0 since the halfway point after scaling is 135 degrees.
*
* This will calculate the result from the fullRange times the
* fraction of the supply voltage, plus the offset.
*
* @param channel The analog channel this potentiometer is plugged into.
* @param scale The scaling to multiply the voltage by to get a meaningful unit.
* @param fullRange 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
*/
explicit AnalogPotentiometer(int channel, double scale = 1.0, double offset = 0.0);
explicit AnalogPotentiometer(int channel, double fullRange = 1.0, double offset = 0.0);
explicit AnalogPotentiometer(AnalogInput *input, double scale = 1.0, double offset = 0.0);
explicit AnalogPotentiometer(AnalogInput *input, double fullRange = 1.0, double offset = 0.0);
explicit AnalogPotentiometer(AnalogInput &input, double scale = 1.0, double offset = 0.0);
explicit AnalogPotentiometer(AnalogInput &input, double fullRange = 1.0, double offset = 0.0);
virtual ~AnalogPotentiometer();
/**
* Get the current reading of the potentiomere.
* Get the current reading of the potentiomer.
*
* @return The current position of the potentiometer.
*/
@@ -70,7 +73,7 @@ public:
virtual void StopLiveWindowMode() {}
private:
double m_scale, m_offset;
double m_fullRange, m_offset;
AnalogInput* m_analog_input;
ITable* m_table;
bool m_init_analog_input;
@@ -78,5 +81,5 @@ private:
/**
* Common initialization code called by all constructors.
*/
void initPot(AnalogInput *input, double scale, double offset);
void initPot(AnalogInput *input, double fullRange, double offset);
};

View File

@@ -1,28 +1,29 @@
#include "AnalogPotentiometer.h"
#include "ControllerPower.h"
/**
* Common initialization code called by all constructors.
*/
void AnalogPotentiometer::initPot(AnalogInput *input, double scale, double offset) {
m_scale = scale;
void AnalogPotentiometer::initPot(AnalogInput *input, double fullRange, double offset) {
m_fullRange = fullRange;
m_offset = offset;
m_analog_input = input;
}
AnalogPotentiometer::AnalogPotentiometer(int channel, double scale, double offset) {
AnalogPotentiometer::AnalogPotentiometer(int channel, double fullRange, double offset) {
m_init_analog_input = true;
initPot(new AnalogInput(channel), scale, offset);
initPot(new AnalogInput(channel), fullRange, offset);
}
AnalogPotentiometer::AnalogPotentiometer(AnalogInput *input, double scale, double offset) {
AnalogPotentiometer::AnalogPotentiometer(AnalogInput *input, double fullRange, double offset) {
m_init_analog_input = false;
initPot(input, scale, offset);
initPot(input, fullRange, offset);
}
AnalogPotentiometer::AnalogPotentiometer(AnalogInput &input, double scale, double offset) {
AnalogPotentiometer::AnalogPotentiometer(AnalogInput &input, double fullRange, double offset) {
m_init_analog_input = false;
initPot(&input, scale, offset);
initPot(&input, fullRange, offset);
}
AnalogPotentiometer::~AnalogPotentiometer() {
@@ -33,12 +34,12 @@ AnalogPotentiometer::~AnalogPotentiometer() {
}
/**
* Get the current reading of the potentiomere.
* Get the current reading of the potentiometer.
*
* @return The current position of the potentiometer.
*/
double AnalogPotentiometer::Get() {
return m_analog_input->GetVoltage() * m_scale + m_offset;
return (m_analog_input->GetVoltage() / ControllerPower::GetVoltage5V()) * m_fullRange + m_offset;
}
/**

View File

@@ -10,7 +10,7 @@
#include "TestBench.h"
#include "gtest/gtest.h"
static const double kScale = 54.0;
static const double kScale = 270.0;
static const double kVoltage = 3.33;
static const double kAngle = 180.0;
@@ -40,6 +40,6 @@ TEST_F(AnalogPotentiometerTest, TestInitialSettings) {
TEST_F(AnalogPotentiometerTest,TestRangeValues) {
m_fakePot->SetVoltage(kVoltage);
Wait(0.1);
EXPECT_NEAR(kAngle, m_pot->Get(), 1.0)
EXPECT_NEAR(kAngle, m_pot->Get(), 2.0)
<< "The potentiometer did not measure the correct angle.";
}

View File

@@ -5,31 +5,37 @@
/* the project. */
/*----------------------------------------------------------------------------*/
package edu.wpi.first.wpilibj;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import edu.wpi.first.wpilibj.hal.HALUtil;
import edu.wpi.first.wpilibj.hal.PowerJNI;
import edu.wpi.first.wpilibj.interfaces.Potentiometer;
import edu.wpi.first.wpilibj.livewindow.LiveWindowSendable;
import edu.wpi.first.wpilibj.tables.ITable;
/**
* Class for reading analog potentiometers. Analog potentiometers read
* in an analog voltage that corresponds to a position. Usually the
* position is either degrees or meters. However, if no conversion is
* given it remains volts.
* in an analog voltage that corresponds to a position. The position is
* in whichever units you choose, by way of the scaling and offset
* constants passed to the constructor.
*
* @author Alex Henning
* @author Colby Skeggs (rail voltage)
*/
public class AnalogPotentiometer implements Potentiometer, LiveWindowSendable {
private double m_scale, m_offset;
private double m_fullRange, 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 fullRange 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 AnalogInput input, double scale, double offset) {
this.m_scale = scale;
private void initPot(final AnalogInput input, double fullRange, double offset) {
this.m_fullRange = fullRange;
this.m_offset = offset;
m_analog_input = input;
}
@@ -37,51 +43,54 @@ public class AnalogPotentiometer implements Potentiometer, LiveWindowSendable {
/**
* AnalogPotentiometer constructor.
*
* Use the scaling and offset values so that the output produces
* Use the fullRange 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.
* degrees. The fullRange value is 270.0(degrees) and the offset is
* -135.0 since the halfway point after scaling is 135 degrees.
*
* This will calculate the result from the fullRange times the
* fraction of the supply voltage, plus the offset.
*
* @param channel The analog channel this potentiometer is plugged into.
* @param scale The scaling to multiply the voltage by to get a meaningful unit.
* @param fullRange The scaling to multiply the fraction by to get a meaningful unit.
* @param offset The offset to add to the scaled value for controlling the zero value
*/
public AnalogPotentiometer(final int channel, double scale, double offset) {
public AnalogPotentiometer(final int channel, double fullRange, double offset) {
AnalogInput input = new AnalogInput(channel);
m_init_analog_input = true;
initPot(input, scale, offset);
initPot(input, fullRange, offset);
}
/**
* AnalogPotentiometer constructor.
*
* Use the scaling and offset values so that the output produces
* Use the fullRange 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.
* degrees. The fullRange value is 270.0(degrees) and the offset is
* -135.0 since the halfway point after scaling is 135 degrees.
*
* This will calculate the result from the fullRange times the
* fraction of the supply voltage, plus the offset.
*
* @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 fullRange The scaling to multiply the fraction 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) {
public AnalogPotentiometer(final AnalogInput input, double fullRange, double offset) {
m_init_analog_input = false;
initPot(input, scale, offset);
initPot(input, fullRange, offset);
}
/**
* AnalogPotentiometer constructor.
*
* Use the scaling and offset values so that the output produces
* Use the fullRange 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.
* degrees. The fullRange value is 270.0(degrees) and the offset is
* -135.0 since the halfway point after scaling is 135 degrees.
*
* @param channel The analog channel this potentiometer is plugged into.
* @param scale The scaling to multiply the voltage by to get a meaningful unit.
@@ -93,12 +102,11 @@ public class AnalogPotentiometer implements Potentiometer, LiveWindowSendable {
/**
* AnalogPotentiometer constructor.
*
* Use the scaling and offset values so that the output produces
* Use the fullRange 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.
* degrees. The fullRange value is 270.0(degrees) 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.
@@ -115,7 +123,7 @@ public class AnalogPotentiometer implements Potentiometer, LiveWindowSendable {
public AnalogPotentiometer(final int channel) {
this(channel, 1, 0);
}
/**
* AnalogPotentiometer constructor.
*
@@ -126,12 +134,12 @@ public class AnalogPotentiometer implements Potentiometer, LiveWindowSendable {
}
/**
* Get the current reading of the potentiomere.
* Get the current reading of the potentiometer.
*
* @return The current position of the potentiometer.
*/
public double get() {
return m_analog_input.getVoltage() * m_scale + m_offset;
return (m_analog_input.getVoltage() / ControllerPower.getVoltage5V()) * m_fullRange + m_offset;
}
/**

View File

@@ -29,7 +29,7 @@ public class AnalogPotentiometerTest extends AbstractComsSetup {
private FakePotentiometerSource potSource;
private AnalogPotentiometer pot;
private static final double DOUBLE_COMPARISON_DELTA = 1.0;
private static final double DOUBLE_COMPARISON_DELTA = 2.0;
/**
* @throws java.lang.Exception
@@ -38,7 +38,7 @@ public class AnalogPotentiometerTest extends AbstractComsSetup {
public void setUp() throws Exception {
analogIO = TestBench.getAnalogCrossConnectFixture();
potSource = new FakePotentiometerSource(analogIO.getOutput(), 360);
pot = new AnalogPotentiometer(analogIO.getInput(), 360.0/5.0, 0);
pot = new AnalogPotentiometer(analogIO.getInput(), 360.0, 0);
}
@@ -66,6 +66,7 @@ public class AnalogPotentiometerTest extends AbstractComsSetup {
public void testRangeValues(){
for(double i = 0.0; i < 360.0; i = i+1.0){
potSource.setAngle(i);
potSource.setMaxVoltage(ControllerPower.getVoltage5V());
Timer.delay(.02);
assertEquals(i, pot.get(), DOUBLE_COMPARISON_DELTA);
}