Add encoder indexing in Java

Change-Id: I7d6a7da4301703aafab9d63ce67b6c88c62647c9
This commit is contained in:
Thomas Clark
2015-01-08 00:23:32 -05:00
parent a2dfffeddc
commit e13720bb94
3 changed files with 79 additions and 1 deletions

View File

@@ -33,6 +33,9 @@ import edu.wpi.first.wpilibj.util.BoundaryException;
* to be zeroed before use.
*/
public class Encoder extends SensorBase implements CounterBase, PIDSource, LiveWindowSendable {
public enum IndexingType {
kResetWhileHigh, kResetWhileLow, kResetOnFallingEdge, kResetOnRisingEdge
}
/**
* The a source
@@ -727,6 +730,59 @@ public class Encoder extends SensorBase implements CounterBase, PIDSource, LiveW
}
}
/**
* Set the index source for the encoder. When this source rises, the encoder count automatically resets.
*
* @param channel A DIO channel to set as the encoder index
* @param type The state that will cause the encoder to reset
*/
public void setIndexSource(int channel, IndexingType type) {
ByteBuffer status = ByteBuffer.allocateDirect(4);
status.order(ByteOrder.LITTLE_ENDIAN);
boolean activeHigh = (type == IndexingType.kResetWhileHigh) || (type == IndexingType.kResetOnRisingEdge);
boolean edgeSensitive = (type == IndexingType.kResetOnFallingEdge) || (type == IndexingType.kResetOnRisingEdge);
EncoderJNI.setEncoderIndexSource(m_encoder, channel, false, activeHigh, edgeSensitive, status.asIntBuffer());
HALUtil.checkStatus(status.asIntBuffer());
}
/**
* Set the index source for the encoder. When this source is activated, the encoder count automatically resets.
*
* @param channel A DIO channel to set as the encoder index
*/
public void setIndexSource(int channel) {
this.setIndexSource(channel, IndexingType.kResetOnRisingEdge);
}
/**
* Set the index source for the encoder. When this source rises, the encoder count automatically resets.
*
* @param source A digital source to set as the encoder index
* @param type The state that will cause the encoder to reset
*/
public void setIndexSource(DigitalSource source, IndexingType type) {
ByteBuffer status = ByteBuffer.allocateDirect(4);
status.order(ByteOrder.LITTLE_ENDIAN);
boolean activeHigh = (type == IndexingType.kResetWhileHigh) || (type == IndexingType.kResetOnRisingEdge);
boolean edgeSensitive = (type == IndexingType.kResetOnFallingEdge) || (type == IndexingType.kResetOnRisingEdge);
EncoderJNI.setEncoderIndexSource(m_encoder, source.getChannelForRouting(), source.getAnalogTriggerForRouting(),
activeHigh, edgeSensitive, status.asIntBuffer());
HALUtil.checkStatus(status.asIntBuffer());
}
/**
* Set the index source for the encoder. When this source is activated, the encoder count automatically resets.
*
* @param source A digital source to set as the encoder index
*/
public void setIndexSource(DigitalSource source) {
this.setIndexSource(source, IndexingType.kResetOnRisingEdge);
}
/*
* Live Window code, only does anything if live window is activated.
*/

View File

@@ -15,4 +15,5 @@ public class EncoderJNI extends JNIWrapper {
public static native void setEncoderReverseDirection(ByteBuffer encoder_pointer, byte reverseDirection, IntBuffer status);
public static native void setEncoderSamplesToAverage(ByteBuffer encoder_pointer, int samplesToAverage, IntBuffer status);
public static native int getEncoderSamplesToAverage(ByteBuffer encoder_pointer, IntBuffer status);
public static native void setEncoderIndexSource(ByteBuffer digital_port, int pin, boolean analogTrigger, boolean activeHigh, boolean edgeSensitive, IntBuffer status);
}

View File

@@ -7,7 +7,7 @@
#include "HAL/Digital.hpp"
// set the logging level
TLogLevel encoderJNILogLevel = logWARNING;
TLogLevel encoderJNILogLevel = logDEBUG;
#define ENCODERJNI_LOG(level) \
if (level > encoderJNILogLevel) ; \
@@ -223,3 +223,24 @@ JNIEXPORT jint JNICALL Java_edu_wpi_first_wpilibj_hal_EncoderJNI_getEncoderSampl
ENCODERJNI_LOG(logDEBUG) << "getEncoderSamplesToAverageResult = " << returnValue;
return returnValue;
}
/*
* Class: edu_wpi_first_wpilibj_hal_EncoderJNI
* Method: setEncoderIndexSource
* Signature: (Ljava/nio/ByteBuffer;IZZZLjava/nio/IntBuffer;)V
*/
JNIEXPORT void JNICALL Java_edu_wpi_first_wpilibj_hal_EncoderJNI_setEncoderIndexSource
(JNIEnv * env, jclass, jobject id, jint pin, jboolean analogTrigger, jboolean activeHigh, jboolean edgeSensitive, jobject status)
{
ENCODERJNI_LOG(logDEBUG) << "Calling ENCODERJNI setEncoderIndexSource";
void ** javaId = (void**)env->GetDirectBufferAddress(id);
ENCODERJNI_LOG(logDEBUG) << "Encoder Ptr = " << *javaId;
jint * statusPtr = (jint*)env->GetDirectBufferAddress(status);
ENCODERJNI_LOG(logDEBUG) << "Pin = " << pin;
ENCODERJNI_LOG(logDEBUG) << "Analog Trigger = " << (analogTrigger?"true":"false");
ENCODERJNI_LOG(logDEBUG) << "Active High = " << (activeHigh?"true":"false");
ENCODERJNI_LOG(logDEBUG) << "Edge Sensitive = " << (edgeSensitive?"true":"false");
ENCODERJNI_LOG(logDEBUG) << "Status Ptr = " << statusPtr;
setEncoderIndexSource(*javaId, pin, analogTrigger, activeHigh, edgeSensitive, statusPtr);
ENCODERJNI_LOG(logDEBUG) << "Status = " << *statusPtr;
}