mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-23 01:21:42 +00:00
Encoder/Counter Fixes
This fixes all encoder variants with the getRate and getPeriod methods. The clock speed in the HAL was updated, as was the scaling factors for setting the stall periods. A default of .5 seconds is now set for the max period. Additionally, a long standing bug was fixed with Java 2x encoders. Changed tests to take into account the increased default timeout on encoders Change-Id: I8b54c07ea467154be94d7ae7e9ada1775933dee4
This commit is contained in:
@@ -838,7 +838,7 @@ double getCounterPeriod(void* counter_pointer, int32_t *status) {
|
||||
// output.Period is a fixed point number that counts by 2 (24 bits, 25 integer bits)
|
||||
period = (double)(output.Period << 1) / (double)output.Count;
|
||||
}
|
||||
return period * 1.0e-6;
|
||||
return period * 2.5e-8; // result * timebase (currently 40ns)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -850,7 +850,7 @@ double getCounterPeriod(void* counter_pointer, int32_t *status) {
|
||||
*/
|
||||
void setCounterMaxPeriod(void* counter_pointer, double maxPeriod, int32_t *status) {
|
||||
Counter* counter = (Counter*) counter_pointer;
|
||||
counter->counter->writeTimerConfig_StallPeriod((uint32_t)(maxPeriod * 1.0e6), status);
|
||||
counter->counter->writeTimerConfig_StallPeriod((uint32_t)(maxPeriod * 4.0e8), status);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1000,7 +1000,7 @@ double getEncoderPeriod(void* encoder_pointer, int32_t *status) {
|
||||
// output.Period is a fixed point number that counts by 2 (24 bits, 25 integer bits)
|
||||
value = (double)(output.Period << 1) / (double)output.Count;
|
||||
}
|
||||
double measuredPeriod = value * 1.0e-6;
|
||||
double measuredPeriod = value * 2.5e-8;
|
||||
return measuredPeriod / DECODING_SCALING_FACTOR;
|
||||
}
|
||||
|
||||
@@ -1018,7 +1018,7 @@ double getEncoderPeriod(void* encoder_pointer, int32_t *status) {
|
||||
*/
|
||||
void setEncoderMaxPeriod(void* encoder_pointer, double maxPeriod, int32_t *status) {
|
||||
Encoder* encoder = (Encoder*) encoder_pointer;
|
||||
encoder->encoder->writeTimerConfig_StallPeriod((uint32_t)(maxPeriod * 1.0e6 * DECODING_SCALING_FACTOR), status);
|
||||
encoder->encoder->writeTimerConfig_StallPeriod((uint32_t)(maxPeriod * 4.0e8 * DECODING_SCALING_FACTOR), status);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -31,6 +31,8 @@ void Counter::InitCounter(Mode mode)
|
||||
m_allocatedUpSource = false;
|
||||
m_allocatedDownSource = false;
|
||||
|
||||
SetMaxPeriod(.5);
|
||||
|
||||
HALReport(HALUsageReporting::kResourceType_Counter, index, mode);
|
||||
}
|
||||
|
||||
|
||||
@@ -52,6 +52,7 @@ void Encoder::InitEncoder(bool reverseDirection, EncodingType encodingType)
|
||||
reverseDirection, &index, &status);
|
||||
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
||||
m_counter = NULL;
|
||||
SetMaxPeriod(.5);
|
||||
break;
|
||||
}
|
||||
case k1X:
|
||||
|
||||
@@ -86,6 +86,8 @@ public class Counter extends SensorBase implements CounterBase,
|
||||
m_upSource = null;
|
||||
m_downSource = null;
|
||||
|
||||
setMaxPeriod(.5);
|
||||
|
||||
UsageReporting.report(tResourceType.kResourceType_Counter, m_index,
|
||||
mode.value);
|
||||
}
|
||||
@@ -166,12 +168,16 @@ public class Counter extends SensorBase implements CounterBase,
|
||||
if (encodingType == null)
|
||||
throw new NullPointerException("Encoding type given was null");
|
||||
|
||||
ByteBuffer status = ByteBuffer.allocateDirect(4);
|
||||
if (encodingType == EncodingType.k1X) {
|
||||
setUpSourceEdge(true, false);
|
||||
CounterJNI.setCounterAverageSize(m_counter, 1, status.asIntBuffer());
|
||||
} else {
|
||||
setUpSourceEdge(true, true);
|
||||
CounterJNI.setCounterAverageSize(m_counter, 2, status.asIntBuffer());
|
||||
}
|
||||
|
||||
HALUtil.checkStatus(status.asIntBuffer());
|
||||
setDownSourceEdge(inverted, true);
|
||||
}
|
||||
|
||||
@@ -374,7 +380,7 @@ public class Counter extends SensorBase implements CounterBase,
|
||||
ByteBuffer status = ByteBuffer.allocateDirect(4);
|
||||
status.order(ByteOrder.LITTLE_ENDIAN);
|
||||
CounterJNI.setCounterDownSourceEdge(m_counter, (byte) (risingEdge ? 1
|
||||
: 0), (byte) (fallingEdge ? 0 : 1), status.asIntBuffer());
|
||||
: 0), (byte) (fallingEdge ? 1 : 0), status.asIntBuffer());
|
||||
HALUtil.checkStatus(status.asIntBuffer());
|
||||
}
|
||||
|
||||
|
||||
@@ -94,6 +94,7 @@ public class Encoder extends SensorBase implements CounterBase, PIDSource, LiveW
|
||||
HALUtil.checkStatus(status.asIntBuffer());
|
||||
m_index = index.asIntBuffer().get(0);
|
||||
m_counter = null;
|
||||
setMaxPeriod(.5);
|
||||
break;
|
||||
case EncodingType.k2X_val:
|
||||
case EncodingType.k1X_val:
|
||||
@@ -464,7 +465,7 @@ public class Encoder extends SensorBase implements CounterBase, PIDSource, LiveW
|
||||
public double getPeriod() {
|
||||
double measuredPeriod;
|
||||
if (m_counter != null) {
|
||||
measuredPeriod = m_counter.getPeriod();
|
||||
measuredPeriod = m_counter.getPeriod() / decodingScaleFactor();
|
||||
} else {
|
||||
ByteBuffer status = ByteBuffer.allocateDirect(4);
|
||||
// set the byte order
|
||||
@@ -472,7 +473,7 @@ public class Encoder extends SensorBase implements CounterBase, PIDSource, LiveW
|
||||
measuredPeriod = EncoderJNI.getEncoderPeriod(m_encoder, status.asIntBuffer());
|
||||
HALUtil.checkStatus(status.asIntBuffer());
|
||||
}
|
||||
return measuredPeriod / decodingScaleFactor();
|
||||
return measuredPeriod;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -196,6 +196,7 @@ public class MotorEncoderTest extends AbstractComsSetup {
|
||||
assertEquals(me.getType() + " Motor value was incorrect after reset.", me.getMotor().get(), 0, 0);
|
||||
assertEquals(me.getType() + " Counter1 value was incorrect after reset.", me.getCounters()[0].get(), 0);
|
||||
assertEquals(me.getType() + " Counter2 value was incorrect after reset.", me.getCounters()[1].get(), 0);
|
||||
Timer.delay(0.5); // so this doesn't fail with the 0.5 second default timeout on the encoders
|
||||
assertTrue(me.getType() + " Encoder.getStopped() returned false after the motor was reset.", me.getEncoder().getStopped());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user