Changed AnalogChannel to AnalogInput, added AnalogOutput for MXP

Change-Id: I863d89c61ff2cf4a7a3aeeca1296978e728758b6
This commit is contained in:
thomasclark
2014-06-12 18:07:45 -04:00
parent a46c246587
commit 3538e2c05c
32 changed files with 658 additions and 268 deletions

View File

@@ -23,7 +23,7 @@ import edu.wpi.first.wpilibj.tables.ITable;
*/
public class Accelerometer extends SensorBase implements PIDSource, ISensor, LiveWindowSendable {
private AnalogChannel m_analogChannel;
private AnalogInput m_analogChannel;
private double m_voltsPerG = 1.0;
private double m_zeroGVoltage = 2.5;
private boolean m_allocatedChannel;
@@ -45,7 +45,7 @@ public class Accelerometer extends SensorBase implements PIDSource, ISensor, Liv
*/
public Accelerometer(final int channel) {
m_allocatedChannel = true;
m_analogChannel = new AnalogChannel(channel);
m_analogChannel = new AnalogInput(channel);
initAccelerometer();
}
@@ -59,7 +59,7 @@ public class Accelerometer extends SensorBase implements PIDSource, ISensor, Liv
*/
public Accelerometer(final int slot, final int channel) {
m_allocatedChannel = true;
m_analogChannel = new AnalogChannel(slot, channel);
m_analogChannel = new AnalogInput(slot, channel);
initAccelerometer();
}
@@ -70,7 +70,7 @@ public class Accelerometer extends SensorBase implements PIDSource, ISensor, Liv
* the Accelerometer class.
* @param channel an already initialized analog channel
*/
public Accelerometer(AnalogChannel channel) {
public Accelerometer(AnalogInput channel) {
m_allocatedChannel = false;
if (channel == null)
throw new NullPointerException("Analog Channel given was null");

View File

@@ -26,10 +26,10 @@ import edu.wpi.first.wpilibj.util.CheckedAllocationException;
/**
* Analog channel class.
*
*
* Each analog channel is read from hardware as a 12-bit number representing
* -10V to 10V.
*
*
* Connected to each analog channel is an averaging and oversampling engine.
* This engine accumulates the specified ( by setAverageBits() and
* setOversampleBits() ) number of samples before returning a new value. This is
@@ -39,12 +39,12 @@ import edu.wpi.first.wpilibj.util.CheckedAllocationException;
* are divided by the number of samples to retain the resolution, but get more
* stable values.
*/
public class AnalogChannel extends SensorBase implements PIDSource,
public class AnalogInput extends SensorBase implements PIDSource,
LiveWindowSendable {
private static final int kAccumulatorSlot = 1;
private static Resource channels = new Resource(kAnalogModules
* kAnalogChannels);
* kAnalogInputChannels);
private ByteBuffer m_port;
private int m_moduleNumber, m_channel;
private static final int[] kAccumulatorChannels = { 0, 1 };
@@ -52,39 +52,39 @@ public class AnalogChannel extends SensorBase implements PIDSource,
/**
* Construct an analog channel on the default module.
*
*
* @param channel
* The channel number to represent.
*/
public AnalogChannel(final int channel) {
public AnalogInput(final int channel) {
this(getDefaultAnalogModule(), channel);
}
/**
* Construct an analog channel on a specified module.
*
*
* @param moduleNumber
* The digital module to use (1 or 2).
* @param channel
* The channel number to represent.
*/
public AnalogChannel(final int moduleNumber, final int channel) {
public AnalogInput(final int moduleNumber, final int channel) {
m_channel = channel;
m_moduleNumber = moduleNumber;
if (AnalogJNI.checkAnalogModule((byte)moduleNumber) == 0) {
throw new AllocationException("Analog channel " + m_channel
throw new AllocationException("Analog input channel " + m_channel
+ " on module " + m_moduleNumber
+ " cannot be allocated. Module is not present.");
}
if (AnalogJNI.checkAnalogInputChannel(channel) == 0) {
throw new AllocationException("Analog channel " + m_channel
throw new AllocationException("Analog input channel " + m_channel
+ " on module " + m_moduleNumber
+ " cannot be allocated. Channel is not present.");
}
try {
channels.allocate((moduleNumber - 1) * kAnalogChannels + channel);
channels.allocate((moduleNumber - 1) * kAnalogInputChannels + channel);
} catch (CheckedAllocationException e) {
throw new AllocationException("Analog channel " + m_channel
throw new AllocationException("Analog input channel " + m_channel
+ " on module " + m_moduleNumber + " is already allocated");
}
@@ -97,7 +97,7 @@ public class AnalogChannel extends SensorBase implements PIDSource,
m_port = AnalogJNI.initializeAnalogInputPort(port_pointer, status.asIntBuffer());
HALUtil.checkStatus(status.asIntBuffer());
LiveWindow.addSensor("Analog", moduleNumber, channel, this);
LiveWindow.addSensor("AnalogInput", moduleNumber, channel, this);
UsageReporting.report(tResourceType.kResourceType_AnalogChannel,
channel, moduleNumber - 1);
}
@@ -106,7 +106,7 @@ public class AnalogChannel extends SensorBase implements PIDSource,
* Channel destructor.
*/
public void free() {
channels.free(((m_moduleNumber - 1) * kAnalogChannels + m_channel));
channels.free(((m_moduleNumber - 1) * kAnalogInputChannels + m_channel));
m_channel = 0;
m_moduleNumber = 0;
m_accumulatorOffset = 0;
@@ -114,7 +114,7 @@ public class AnalogChannel extends SensorBase implements PIDSource,
/**
* Get the analog module that this channel is on.
*
*
* @return The AnalogModule that this channel is on.
*/
public AnalogModule getModule() {
@@ -126,7 +126,7 @@ public class AnalogChannel extends SensorBase implements PIDSource,
* 12-bit value representing the -10V to 10V range of the A/D converter in
* the module. The units are in A/D converter codes. Use GetVoltage() to get
* the analog value in calibrated units.
*
*
* @return A sample straight from this channel on the module.
*/
public int getValue() {
@@ -146,7 +146,7 @@ public class AnalogChannel extends SensorBase implements PIDSource,
* sliding window. The sample will not change until 2**(OversamplBits +
* AverageBits) samples have been acquired from the module on this channel.
* Use getAverageVoltage() to get the analog value in calibrated units.
*
*
* @return A sample from the oversample and average engine for this channel.
*/
public int getAverageValue() {
@@ -162,7 +162,7 @@ public class AnalogChannel extends SensorBase implements PIDSource,
* Get a scaled sample straight from this channel on the module. The value
* is scaled to units of Volts using the calibrated scaling data from
* getLSBWeight() and getOffset().
*
*
* @return A scaled sample straight from this channel on the module.
*/
public double getVoltage() {
@@ -181,7 +181,7 @@ public class AnalogChannel extends SensorBase implements PIDSource,
* oversampling will cause this value to be higher resolution, but it will
* update more slowly. Using averaging will cause this value to be more
* stable, but it will update more slowly.
*
*
* @return A scaled sample from the output of the oversample and average
* engine for this channel.
*/
@@ -198,9 +198,9 @@ public class AnalogChannel extends SensorBase implements PIDSource,
* Get the factory scaling least significant bit weight constant. The least
* significant bit weight constant for the channel that was calibrated in
* manufacturing and stored in an eeprom in the module.
*
*
* Volts = ((LSB_Weight * 1e-9) * raw) - (Offset * 1e-9)
*
*
* @return Least significant bit weight.
*/
public long getLSBWeight() {
@@ -216,9 +216,9 @@ public class AnalogChannel extends SensorBase implements PIDSource,
* Get the factory scaling offset constant. The offset constant for the
* channel that was calibrated in manufacturing and stored in an eeprom in
* the module.
*
*
* Volts = ((LSB_Weight * 1e-9) * raw) - (Offset * 1e-9)
*
*
* @return Offset constant.
*/
public int getOffset() {
@@ -232,7 +232,7 @@ public class AnalogChannel extends SensorBase implements PIDSource,
/**
* Get the channel number.
*
*
* @return The channel number.
*/
public int getChannel() {
@@ -241,7 +241,7 @@ public class AnalogChannel extends SensorBase implements PIDSource,
/**
* Gets the number of the analog module this channel is on.
*
*
* @return The module number of the analog module this channel is on.
*/
public int getModuleNumber() {
@@ -252,7 +252,7 @@ public class AnalogChannel extends SensorBase implements PIDSource,
* Set the number of averaging bits. This sets the number of averaging bits.
* The actual number of averaged samples is 2**bits. The averaging is done
* automatically in the FPGA.
*
*
* @param bits
* The number of averaging bits.
*/
@@ -268,7 +268,7 @@ public class AnalogChannel extends SensorBase implements PIDSource,
* Get the number of averaging bits. This gets the number of averaging bits
* from the FPGA. The actual number of averaged samples is 2**bits. The
* averaging is done automatically in the FPGA.
*
*
* @return The number of averaging bits.
*/
public int getAverageBits() {
@@ -284,7 +284,7 @@ public class AnalogChannel extends SensorBase implements PIDSource,
* Set the number of oversample bits. This sets the number of oversample
* bits. The actual number of oversampled values is 2**bits. The
* oversampling is done automatically in the FPGA.
*
*
* @param bits
* The number of oversample bits.
*/
@@ -300,7 +300,7 @@ public class AnalogChannel extends SensorBase implements PIDSource,
* Get the number of oversample bits. This gets the number of oversample
* bits from the FPGA. The actual number of oversampled values is 2**bits.
* The oversampling is done automatically in the FPGA.
*
*
* @return The number of oversample bits.
*/
public int getOversampleBits() {
@@ -333,9 +333,9 @@ public class AnalogChannel extends SensorBase implements PIDSource,
/**
* Set an inital value for the accumulator.
*
*
* This will be added to all values returned to the user.
*
*
* @param initialValue
* The value that the accumulator should start from when reset.
*/
@@ -356,12 +356,12 @@ public class AnalogChannel extends SensorBase implements PIDSource,
/**
* Set the center value of the accumulator.
*
*
* The center value is subtracted from each A/D value before it is added to
* the accumulator. This is used for the center value of devices like gyros
* and accelerometers to make integration work and to take the device offset
* into account when integrating.
*
*
* This center value is based on the output of the oversampled and averaged
* source from channel 1. Because of this, any non-zero oversample bits will
* affect the size of the value for this field.
@@ -387,10 +387,10 @@ public class AnalogChannel extends SensorBase implements PIDSource,
/**
* Read the accumulated value.
*
*
* Read the value that has been accumulating on channel 1. The accumulator
* is attached after the oversample and average engine.
*
*
* @return The 64-bit value accumulated since the last Reset().
*/
public long getAccumulatorValue() {
@@ -404,10 +404,10 @@ public class AnalogChannel extends SensorBase implements PIDSource,
/**
* Read the number of accumulated values.
*
*
* Read the count of the accumulated values since the accumulator was last
* Reset().
*
*
* @return The number of times samples from the channel were accumulated.
*/
public long getAccumulatorCount() {
@@ -422,10 +422,10 @@ public class AnalogChannel extends SensorBase implements PIDSource,
/**
* Read the accumulated value and the number of accumulated values
* atomically.
*
*
* This function reads the value and count from the FPGA atomically. This
* can be used for averaging.
*
*
* @param result
* AccumulatorResult object to store the results in.
*/
@@ -455,7 +455,7 @@ public class AnalogChannel extends SensorBase implements PIDSource,
/**
* Is the channel attached to an accumulator.
*
*
* @return The analog channel is attached to an accumulator.
*/
public boolean isAccumulatorChannel() {
@@ -482,7 +482,7 @@ public class AnalogChannel extends SensorBase implements PIDSource,
/**
* Get the average value for usee with PIDController
*
*
* @return the average value
*/
public double pidGet() {

View File

@@ -70,7 +70,7 @@ public class AnalogModule extends Module {
super(tModuleType.kModuleType_Analog, moduleNumber);
m_ports = new ByteBuffer[8];
for (int i = 0; i < SensorBase.kAnalogChannels; i++) {
for (int i = 0; i < SensorBase.kAnalogInputChannels; i++) {
ByteBuffer port_pointer = AnalogJNI.getPortWithModule((byte) moduleNumber, (byte) i);
ByteBuffer status = ByteBuffer.allocateDirect(4);
// set the byte order

View File

@@ -0,0 +1,140 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 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 java.nio.ByteOrder;
import java.nio.IntBuffer;
import java.nio.LongBuffer;
import java.nio.ByteBuffer;
//import com.sun.jna.Pointer;
import edu.wpi.first.wpilibj.communication.FRCNetworkCommunicationsLibrary.tResourceType;
import edu.wpi.first.wpilibj.communication.UsageReporting;
import edu.wpi.first.wpilibj.hal.AnalogJNI;
import edu.wpi.first.wpilibj.hal.HALUtil;
import edu.wpi.first.wpilibj.livewindow.LiveWindow;
import edu.wpi.first.wpilibj.livewindow.LiveWindowSendable;
import edu.wpi.first.wpilibj.tables.ITable;
import edu.wpi.first.wpilibj.util.AllocationException;
import edu.wpi.first.wpilibj.util.CheckedAllocationException;
/**
* Analog output class.
*/
public class AnalogOutput extends SensorBase implements LiveWindowSendable {
private static Resource channels = new Resource(kAnalogOutputChannels);
private ByteBuffer m_port;
private int m_channel;
/**
* Construct an analog output on a specified MXP channel.
*
* @param channel
* The channel number to represent.
*/
public AnalogOutput(final int channel) {
m_channel = channel;
if (AnalogJNI.checkAnalogOutputChannel(channel) == 0) {
throw new AllocationException("Analog output channel " + m_channel
+ " cannot be allocated. Channel is not present.");
}
try {
channels.allocate(channel);
} catch (CheckedAllocationException e) {
throw new AllocationException("Analog output channel " + m_channel
+ " is already allocated");
}
ByteBuffer port_pointer = AnalogJNI.getPortWithModule((byte) 1, (byte) channel);
ByteBuffer status = ByteBuffer.allocateDirect(4);
// set the byte order
status.order(ByteOrder.LITTLE_ENDIAN);
m_port = AnalogJNI.initializeAnalogOutputPort(port_pointer, status.asIntBuffer());
HALUtil.checkStatus(status.asIntBuffer());
LiveWindow.addSensor("AnalogOutput", 1, channel, this);
UsageReporting.report(tResourceType.kResourceType_AnalogChannel, channel, 1);
}
/**
* Channel destructor.
*/
public void free() {
channels.free(m_channel);
m_channel = 0;
}
public void setVoltage(double voltage) {
ByteBuffer status = ByteBuffer.allocateDirect(4);
status.order(ByteOrder.LITTLE_ENDIAN);
AnalogJNI.setAnalogOutput(m_port, voltage, status.asIntBuffer());
HALUtil.checkStatus(status.asIntBuffer());
}
public double getVoltage() {
ByteBuffer status = ByteBuffer.allocateDirect(4);
status.order(ByteOrder.LITTLE_ENDIAN);
double voltage = AnalogJNI.getAnalogOutput(m_port, status.asIntBuffer());
HALUtil.checkStatus(status.asIntBuffer());
return voltage;
}
/*
* Live Window code, only does anything if live window is activated.
*/
public String getSmartDashboardType() {
return "Analog Output";
}
private ITable m_table;
/**
* {@inheritDoc}
*/
public void initTable(ITable subtable) {
m_table = subtable;
updateTable();
}
/**
* {@inheritDoc}
*/
public void updateTable() {
if (m_table != null) {
m_table.putNumber("Value", getVoltage());
}
}
/**
* {@inheritDoc}
*/
public ITable getTable() {
return m_table;
}
/**
* Analog Channels don't have to do anything special when entering the
* LiveWindow. {@inheritDoc}
*/
public void startLiveWindowMode() {
}
/**
* Analog Channels don't have to do anything special when exiting the
* LiveWindow. {@inheritDoc}
*/
public void stopLiveWindowMode() {
}
}

View File

@@ -106,7 +106,7 @@ public class AnalogTrigger implements IInputOutput {
* @param channel
* the AnalogChannel to use for the analog trigger
*/
public AnalogTrigger(AnalogChannel channel) {
public AnalogTrigger(AnalogInput channel) {
initTrigger(channel.getModuleNumber(), channel.getChannel());
}

View File

@@ -86,7 +86,7 @@ public class DriverStation implements IInputOutput {
private static DriverStation instance = new DriverStation();
private FRCCommonControlData m_controlData;
private AnalogChannel m_batteryChannel;
private AnalogInput m_batteryChannel;
private Thread m_thread;
private final Object m_semaphore;
private final Object m_dataSem;

View File

@@ -31,7 +31,7 @@ public class Gyro extends SensorBase implements PIDSource, ISensor,
static final double kSamplesPerSecond = 50.0;
static final double kCalibrationSampleTime = 5.0;
static final double kDefaultVoltsPerDegreePerSecond = 0.007;
AnalogChannel m_analog;
AnalogInput m_analog;
double m_voltsPerDegreePerSecond;
double m_offset;
int m_center;
@@ -94,7 +94,7 @@ public class Gyro extends SensorBase implements PIDSource, ISensor,
* The analog channel the gyro is connected to.
*/
public Gyro(int slot, int channel) {
m_analog = new AnalogChannel(slot, channel);
m_analog = new AnalogInput(slot, channel);
m_channelAllocated = true;
initGyro();
}
@@ -108,7 +108,7 @@ public class Gyro extends SensorBase implements PIDSource, ISensor,
* The analog channel the gyro is connected to.
*/
public Gyro(int channel) {
m_analog = new AnalogChannel(channel);
m_analog = new AnalogInput(channel);
m_channelAllocated = true;
initGyro();
}
@@ -121,7 +121,7 @@ public class Gyro extends SensorBase implements PIDSource, ISensor,
* @param channel
* The AnalogChannel object that the gyro is connected to.
*/
public Gyro(AnalogChannel channel) {
public Gyro(AnalogInput channel) {
m_analog = channel;
if (m_analog == null) {
System.err

View File

@@ -14,13 +14,13 @@ import edu.wpi.first.wpilibj.hal.AnalogJNI;
* Base class for all sensors.
* Stores most recent status information as well as containing utility functions for checking
* channels and error processing.
*
*
* XXX: Wait, there's no exception thrown if we try to allocate a non-existent module? It that behavior correct?
*/
public abstract class SensorBase { // TODO: Refactor
// TODO: Move this to the HAL
/**
* Ticks per microsecond
*/
@@ -35,9 +35,13 @@ public abstract class SensorBase { // TODO: Refactor
*/
public static final int kDigitalModules = 1;
/**
* Number of analog channels per module
* Number of analog input channels per module
*/
public static final int kAnalogChannels = 4;
public static final int kAnalogInputChannels = 8;
/**
* Number of analog output channels per module
*/
public static final int kAnalogOutputChannels = 2;
/**
* Number of analog modules
*/
@@ -206,14 +210,27 @@ public abstract class SensorBase { // TODO: Refactor
}
/**
* Check that the analog channel number is value.
* Verify that the analog channel number is one of the legal channel numbers. Channel numbers
* are 1-based.
* Check that the analog input number is value.
* Verify that the analog input number is one of the legal channel numbers. Channel numbers
* are 0-based.
*
* @param channel The channel number to check.
*/
protected static void checkAnalogChannel(final int channel) {
if (channel <= 0 || channel > kAnalogChannels) {
protected static void checkAnalogInputChannel(final int channel) {
if (channel <= 0 || channel > kAnalogInputChannels) {
System.err.println("Requested analog channel number is out of range.");
}
}
/**
* Check that the analog input number is value.
* Verify that the analog input number is one of the legal channel numbers. Channel numbers
* are 0-based.
*
* @param channel The channel number to check.
*/
protected static void checkAnalogOutputChannel(final int channel) {
if (channel <= 0 || channel > kAnalogOutputChannels) {
System.err.println("Requested analog channel number is out of range.");
}
}
@@ -231,7 +248,7 @@ public abstract class SensorBase { // TODO: Refactor
}
/**
* Verify that the power distribution channel number is within limits.
* Verify that the power distribution channel number is within limits.
* Channel numbers are 1-based.
*
* @param channel The channel number to check.