artf4700: Added DigitalGlitchFilter

Initial Java support from Tyler Veness.
Final java support done by Jerry Morrison.

Change-Id: I1f85eb555f9ea4c0250c4c6729d7c51a76f5bef4
This commit is contained in:
Austin Schuh
2015-11-22 21:18:59 -08:00
committed by Peter Johnson
parent 6d00b77ef3
commit b3b03c43c8
19 changed files with 751 additions and 5 deletions

View File

@@ -60,8 +60,8 @@ public class Counter extends SensorBase implements CounterBase, LiveWindowSendab
}
}
private DigitalSource m_upSource; // /< What makes the counter count up.
private DigitalSource m_downSource; // /< What makes the counter count down.
protected DigitalSource m_upSource; // /< What makes the counter count up.
protected DigitalSource m_downSource; // /< What makes the counter count down.
private boolean m_allocatedUpSource;
private boolean m_allocatedDownSource;
private long m_counter; // /< The FPGA counter object.

View File

@@ -0,0 +1,164 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2015. 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 $(WIND_BASE)/WPILib. */
/*----------------------------------------------------------------------------*/
package edu.wpi.first.wpilibj;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import edu.wpi.first.wpilibj.DigitalSource;
import edu.wpi.first.wpilibj.Encoder;
import edu.wpi.first.wpilibj.Counter;
import edu.wpi.first.wpilibj.hal.DigitalGlitchFilterJNI;
/**
* Class to enable glitch filtering on a set of digital inputs.
* This class will manage adding and removing digital inputs from a FPGA glitch
* filter. The filter lets the user configure the time that an input must remain
* high or low before it is classified as high or low.
*/
public class DigitalGlitchFilter extends SensorBase {
public DigitalGlitchFilter() {
synchronized(m_mutex) {
int i = 0;
while (m_filterAllocated[i] != false && i < m_filterAllocated.length) {
i++;
}
if (i != m_filterAllocated.length) {
m_channelIndex = i;
m_filterAllocated[i] = true;
}
}
}
public void free() {
if (m_channelIndex >= 0) {
synchronized(m_mutex) {
m_filterAllocated[m_channelIndex] = false;
}
}
}
private static void setFilter(DigitalSource input, int channelIndex) {
if (input != null) { // Counter might have just one input
DigitalGlitchFilterJNI.setFilterSelect(input.m_port, channelIndex);
int selected = DigitalGlitchFilterJNI.getFilterSelect(input.m_port);
if (selected != channelIndex) {
throw new IllegalStateException("DigitalGlitchFilterJNI.setFilterSelect("
+ channelIndex + ") failed -> " + selected);
}
}
}
/**
* Assigns the DigitalSource to this glitch filter.
*
* @param input The DigitalSource to add.
*/
public void add(DigitalSource input) {
setFilter(input, m_channelIndex + 1);
}
/**
* Assigns the Encoder to this glitch filter.
*
* @param input The Encoder to add.
*/
public void add(Encoder input) {
add(input.m_aSource);
add(input.m_bSource);
}
/**
* Assigns the Counter to this glitch filter.
*
* @param input The Counter to add.
*/
public void add(Counter input) {
add(input.m_upSource);
add(input.m_downSource);
}
/**
* Removes this filter from the given digital input.
*
* @param input The DigitalSource to stop filtering.
*/
public void remove(DigitalSource input) {
setFilter(input, 0);
}
/**
* Removes this filter from the given Encoder.
*
* @param input the Encoder to stop filtering.
*/
public void remove(Encoder input) {
remove(input.m_aSource);
remove(input.m_bSource);
}
/**
* Removes this filter from the given Counter.
*
* @param input The Counter to stop filtering.
*/
public void remove(Counter input) {
remove(input.m_upSource);
remove(input.m_downSource);
}
/**
* Sets the number of FPGA cycles that the input must hold steady to pass
* through this glitch filter.
*
* @param fpga_cycles The number of FPGA cycles.
*/
public void setPeriodCycles(int fpga_cycles) {
DigitalGlitchFilterJNI.setFilterPeriod(m_channelIndex, fpga_cycles);
}
/**
* Sets the number of nanoseconds that the input must hold steady to pass
* through this glitch filter.
*
* @param nanoseconds The number of nanoseconds.
*/
public void setPeriodNanoSeconds(long nanoseconds) {
int fpga_cycles = (int) (nanoseconds * kSystemClockTicksPerMicrosecond / 4 /
1000);
setPeriodCycles(fpga_cycles);
}
/**
* Gets the number of FPGA cycles that the input must hold steady to pass
* through this glitch filter.
*
* @return The number of cycles.
*/
public int getPeriodCycles() {
return DigitalGlitchFilterJNI.getFilterPeriod(m_channelIndex);
}
/**
* Gets the number of nanoseconds that the input must hold steady to pass
* through this glitch filter.
*
* @return The number of nanoseconds.
*/
public long getPeriodNanoSeconds() {
int fpga_cycles = getPeriodCycles();
return (long) fpga_cycles * 1000L /
(long) (kSystemClockTicksPerMicrosecond / 4);
}
private int m_channelIndex = -1;
private static final Lock m_mutex = new ReentrantLock(true);
private static final boolean[] m_filterAllocated = new boolean[3];
};

View File

@@ -379,6 +379,11 @@ public class FRCNetworkCommunicationsLibrary extends JNIWrapper {
* src\main\include\NetworkCommunication\UsageReporting.h:62</i>
*/
public static final int kResourceType_CANTalonSRX = 52;
/**
* <i>native declaration :
* src\main\include\NetworkCommunication\UsageReporting.h:63</i>
*/
public static final int kResourceType_DigitalGlitchFilter = 53;
};
/**
* <i>native declaration :

View File

@@ -0,0 +1,8 @@
package edu.wpi.first.wpilibj.hal;
public class DigitalGlitchFilterJNI extends JNIWrapper {
public static native void setFilterSelect(long digital_port_pointer, int filter_index);
public static native int getFilterSelect(long digital_port_pointer);
public static native void setFilterPeriod(int filter_index, int fpga_cycles);
public static native int getFilterPeriod(int filter_index);
}