diff --git a/wpilibc/wpilibC++Devices/include/Counter.h b/wpilibc/wpilibC++Devices/include/Counter.h index 7543e5fb62..1148c54e10 100644 --- a/wpilibc/wpilibC++Devices/include/Counter.h +++ b/wpilibc/wpilibC++Devices/include/Counter.h @@ -67,7 +67,7 @@ public: bool GetDirection(); void SetSamplesToAverage(int samplesToAverage); int GetSamplesToAverage(); - uint32_t GetIndex() + uint32_t GetFPGAIndex() { return m_index; } diff --git a/wpilibc/wpilibC++Devices/include/Encoder.h b/wpilibc/wpilibC++Devices/include/Encoder.h index d18eccf816..4166c0735a 100644 --- a/wpilibc/wpilibC++Devices/include/Encoder.h +++ b/wpilibc/wpilibC++Devices/include/Encoder.h @@ -41,6 +41,7 @@ public: // CounterBase interface int32_t Get(); int32_t GetRaw(); + int32_t GetEncodingScale(); void Reset(); double GetPeriod(); void SetMaxPeriod(double maxPeriod); @@ -63,6 +64,11 @@ public: void InitTable(ITable *subTable); ITable * GetTable(); + int32_t GetFPGAIndex() + { + return m_index; + } + private: void InitEncoder(bool _reverseDirection, EncodingType encodingType); double DecodingScaleFactor(); @@ -72,9 +78,11 @@ private: bool m_allocatedASource; // was the A source allocated locally? bool m_allocatedBSource; // was the B source allocated locally? void* m_encoder; + int32_t m_index; // The encoder's FPGA index. double m_distancePerPulse; // distance of travel for each encoder tick Counter *m_counter; // Counter object for 1x and 2x encoding EncodingType m_encodingType; // Encoding type + int32_t m_encodingScale; // 1x, 2x, or 4x, per the encodingType PIDSourceParameter m_pidSource; // Encoder parameter that sources a PID controller ITable *m_table; diff --git a/wpilibc/wpilibC++Devices/src/Counter.cpp b/wpilibc/wpilibC++Devices/src/Counter.cpp index 09a194b416..dbe7ab2670 100644 --- a/wpilibc/wpilibC++Devices/src/Counter.cpp +++ b/wpilibc/wpilibC++Devices/src/Counter.cpp @@ -23,8 +23,8 @@ void Counter::InitCounter(Mode mode) m_table = NULL; int32_t status = 0; - uint32_t index = 0; - m_counter = initializeCounter(mode, &index, &status); + m_index = 0; + m_counter = initializeCounter(mode, &m_index, &status); wpi_setErrorWithContext(status, getHALErrorMessage(status)); m_upSource = NULL; @@ -34,7 +34,7 @@ void Counter::InitCounter(Mode mode) SetMaxPeriod(.5); - HALReport(HALUsageReporting::kResourceType_Counter, index, mode); + HALReport(HALUsageReporting::kResourceType_Counter, m_index, mode); } /** diff --git a/wpilibc/wpilibC++Devices/src/Encoder.cpp b/wpilibc/wpilibC++Devices/src/Encoder.cpp index 8d6bb5d5ef..567af9a319 100644 --- a/wpilibc/wpilibC++Devices/src/Encoder.cpp +++ b/wpilibc/wpilibC++Devices/src/Encoder.cpp @@ -28,11 +28,12 @@ void Encoder::InitEncoder(bool reverseDirection, EncodingType encodingType) { m_table = NULL; m_encodingType = encodingType; - int32_t index = 0; + m_index = 0; switch (encodingType) { case k4X: { + m_encodingScale = 4; if (m_aSource->StatusIsFatal()) { CloneError(m_aSource); @@ -44,12 +45,11 @@ void Encoder::InitEncoder(bool reverseDirection, EncodingType encodingType) return; } int32_t status = 0; - int32_t index = 0; m_encoder = initializeEncoder(m_aSource->GetModuleForRouting(), m_aSource->GetChannelForRouting(), m_aSource->GetAnalogTriggerForRouting(), m_bSource->GetModuleForRouting(), m_bSource->GetChannelForRouting(), m_bSource->GetAnalogTriggerForRouting(), - reverseDirection, &index, &status); + reverseDirection, &m_index, &status); wpi_setErrorWithContext(status, getHALErrorMessage(status)); m_counter = NULL; SetMaxPeriod(.5); @@ -58,15 +58,19 @@ void Encoder::InitEncoder(bool reverseDirection, EncodingType encodingType) case k1X: case k2X: { + m_encodingScale = encodingType == k1X ? 1 : 2; m_counter = new Counter(m_encodingType, m_aSource, m_bSource, reverseDirection); - index = m_counter->GetIndex(); + m_index = m_counter->GetFPGAIndex(); break; } + default: + wpi_setErrorWithContext(-1, "Invalid encodingType argument"); + break; } m_distancePerPulse = 1.0; m_pidSource = kDistance; - HALReport(HALUsageReporting::kResourceType_Encoder, index, encodingType); + HALReport(HALUsageReporting::kResourceType_Encoder, m_index, encodingType); LiveWindow::GetInstance()->AddSensor("Encoder", m_aSource->GetChannelForRouting(), this); } @@ -178,6 +182,12 @@ Encoder::~Encoder() } } +/** + * The encoding scale factor 1x, 2x, or 4x, per the requested encodingType. + * Used to divide raw edge counts down to spec'd counts. + */ +int32_t Encoder::GetEncodingScale() { return m_encodingScale; } + /** * Gets the raw value from the encoder. * The raw value is the actual count unscaled by the 1x, 2x, or 4x scale @@ -232,7 +242,7 @@ void Encoder::Reset() /** * Returns the period of the most recent pulse. * Returns the period of the most recent Encoder pulse in seconds. - * This method compenstates for the decoding type. + * This method compensates for the decoding type. * * @deprecated Use GetRate() in favor of this method. This returns unscaled periods and GetRate() scales using value from SetDistancePerPulse(). * diff --git a/wpilibc/wpilibC++Sim/include/Encoder.h b/wpilibc/wpilibC++Sim/include/Encoder.h index 4932c801ff..1076cb1169 100644 --- a/wpilibc/wpilibC++Sim/include/Encoder.h +++ b/wpilibc/wpilibC++Sim/include/Encoder.h @@ -37,6 +37,7 @@ public: // CounterBase interface int32_t Get(); int32_t GetRaw(); + int32_t GetEncodingScale(); void Reset(); double GetPeriod(); void SetMaxPeriod(double maxPeriod); @@ -59,6 +60,11 @@ public: void InitTable(ITable *subTable); ITable * GetTable(); + int32_t FPGAEncoderIndex() + { + return 0; + } + private: void InitEncoder(int channelA, int channelB, bool _reverseDirection, EncodingType encodingType); double DecodingScaleFactor(); @@ -70,6 +76,7 @@ private: int channelA, channelB; double m_distancePerPulse; // distance of travel for each encoder tick EncodingType m_encodingType; // Encoding type + int32_t m_encodingScale; // 1x, 2x, or 4x, per the encodingType PIDSourceParameter m_pidSource; // Encoder parameter that sources a PID controller bool m_reverseDirection; SimEncoder* impl; diff --git a/wpilibc/wpilibC++Sim/src/Encoder.cpp b/wpilibc/wpilibC++Sim/src/Encoder.cpp index b45adcd518..fd64cf84b6 100644 --- a/wpilibc/wpilibC++Sim/src/Encoder.cpp +++ b/wpilibc/wpilibC++Sim/src/Encoder.cpp @@ -28,6 +28,9 @@ void Encoder::InitEncoder(int channelA, int channelB, bool reverseDirection, Enc this->channelA = channelA; this->channelB = channelB; m_encodingType = encodingType; + m_encodingScale = encodingType == k4X ? 4 + : encodingType == k2X ? 2 + : 1; int32_t index = 0; m_distancePerPulse = 1.0; @@ -188,6 +191,12 @@ double Encoder::DecodingScaleFactor() } } +/** + * The encoding scale factor 1x, 2x, or 4x, per the requested encodingType. + * Used to divide raw edge counts down to spec'd counts. + */ +int32_t Encoder::GetEncodingScale() { return m_encodingScale; } + /** * Gets the raw value from the encoder. * The raw value is the actual count unscaled by the 1x, 2x, or 4x scale diff --git a/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/Counter.java b/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/Counter.java index 7670056bfc..ffb895d6cc 100644 --- a/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/Counter.java +++ b/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/Counter.java @@ -216,6 +216,13 @@ public class Counter extends SensorBase implements CounterBase, m_counter = null; } + /** + * @return the Counter's FPGA index + */ + public int getFPGAIndex() { + return m_index; + } + /** * Set the upsource for the counter as a digital input channel. * diff --git a/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/Encoder.java b/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/Encoder.java index cc3ceaa2fe..18e78b126f 100644 --- a/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/Encoder.java +++ b/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/Encoder.java @@ -52,6 +52,7 @@ public class Encoder extends SensorBase implements CounterBase, PIDSource, LiveW // tick private Counter m_counter; // Counter object for 1x and 2x encoding private EncodingType m_encodingType = EncodingType.k4X; + private int m_encodingScale; // 1x, 2x, or 4x, per the encodingType private boolean m_allocatedA; private boolean m_allocatedB; private boolean m_allocatedI; @@ -77,6 +78,7 @@ public class Encoder extends SensorBase implements CounterBase, PIDSource, LiveW private void initEncoder(boolean reverseDirection) { switch (m_encodingType.value) { case EncodingType.k4X_val: + m_encodingScale = 4; ByteBuffer status = ByteBuffer.allocateDirect(4); // set the byte order status.order(ByteOrder.LITTLE_ENDIAN); @@ -98,8 +100,10 @@ public class Encoder extends SensorBase implements CounterBase, PIDSource, LiveW break; case EncodingType.k2X_val: case EncodingType.k1X_val: + m_encodingScale = m_encodingType == EncodingType.k1X ? 1 : 2; m_counter = new Counter(m_encodingType, m_aSource, m_bSource, reverseDirection); + m_index = m_counter.getFPGAIndex(); break; } m_distancePerPulse = 1.0; @@ -375,6 +379,21 @@ public class Encoder extends SensorBase implements CounterBase, PIDSource, LiveW this(aSource, bSource, indexSource, false); } + /** + * @return the Encoder's FPGA index + */ + public int getFPGAIndex() { + return m_index; + } + + /** + * @return the encoding scale factor 1x, 2x, or 4x, per the requested + * encodingType. Used to divide raw edge counts down to spec'd counts. + */ + public int getEncodingScale() { + return m_encodingScale; + } + public void free() { if (m_aSource != null && m_allocatedA) { m_aSource.free(); diff --git a/wpilibj/wpilibJavaSim/src/main/java/edu/wpi/first/wpilibj/Encoder.java b/wpilibj/wpilibJavaSim/src/main/java/edu/wpi/first/wpilibj/Encoder.java index 1ed003ed61..d9ad52834d 100644 --- a/wpilibj/wpilibJavaSim/src/main/java/edu/wpi/first/wpilibj/Encoder.java +++ b/wpilibj/wpilibJavaSim/src/main/java/edu/wpi/first/wpilibj/Encoder.java @@ -29,6 +29,7 @@ public class Encoder extends SensorBase implements CounterBase, PIDSource, LiveW private int m_index; private double m_distancePerPulse; // distance of travel for each encoder tick private EncodingType m_encodingType = EncodingType.k4X; + private int m_encodingScale; // 1x, 2x, or 4x, per the encodingType private boolean m_allocatedA; private boolean m_allocatedB; private boolean m_allocatedI; @@ -56,6 +57,9 @@ public class Encoder extends SensorBase implements CounterBase, PIDSource, LiveW private void initEncoder(int aChannel, int bChannel, boolean reverseDirection) { m_distancePerPulse = 1.0; m_pidSource = PIDSourceParameter.kDistance; + m_encodingScale = m_encodingType == EncodingType.k4X ? 4 + : m_encodingType == EncodingType.k2X ? 2 + : 1; LiveWindow.addSensor("Encoder", aChannel, this); @@ -142,6 +146,21 @@ public class Encoder extends SensorBase implements CounterBase, PIDSource, LiveW initEncoder(aChannel, bChannel, reverseDirection); } + /** + * @return the Encoder's FPGA index + */ + public int getFPGAEncoderIndex() { + return m_index; + } + + /** + * @return the encoding scale factor 1x, 2x, or 4x, per the requested + * encodingType. Used to divide raw edge counts down to spec'd counts. + */ + public int getEncodingScale() { + return m_encodingScale; + } + public void free() {} /**