mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-24 01:31:46 +00:00
[wpilib] Absolute Encoder API and behavior fixes (#4052)
SetPositionOffset was added. Been requested multiple times, and easy to implement. The javadocs mentioned GetPositionInRotation. It has tripped up many people how to get the absolute position from the encoder (You currently have to have precreated the DutyCycle object). Add this method (as GetAbsolutePostition) to make this easier to do. The checks for making sure a matching set of values was read was doing direct double comparisions. This worked ok in the DutyCycle case, but has problems in the analog case. Solve this by using an epsilon comparison. And finally, scale AnalogEncoders analog input to 0-1 instead of 0-5. This was reported a few years ago, but the issue was missed. This caused the encoder to count from 0-5, then 1-6, then 2-7 etc. This is solved and now works correctly. Closes #3188 Closes #4046 Closes #4051 And fixes the following issue on CD https://www.chiefdelphi.com/t/wpilib-analogencoder-java/372649
This commit is contained in:
@@ -7,6 +7,7 @@ package edu.wpi.first.wpilibj;
|
||||
import edu.wpi.first.hal.SimDevice;
|
||||
import edu.wpi.first.hal.SimDevice.Direction;
|
||||
import edu.wpi.first.hal.SimDouble;
|
||||
import edu.wpi.first.math.MathUtil;
|
||||
import edu.wpi.first.util.sendable.Sendable;
|
||||
import edu.wpi.first.util.sendable.SendableBuilder;
|
||||
import edu.wpi.first.util.sendable.SendableRegistry;
|
||||
@@ -23,6 +24,7 @@ public class AnalogEncoder implements Sendable, AutoCloseable {
|
||||
|
||||
protected SimDevice m_simDevice;
|
||||
protected SimDouble m_simPosition;
|
||||
protected SimDouble m_simAbsolutePosition;
|
||||
|
||||
/**
|
||||
* Construct a new AnalogEncoder attached to a specific AnalogIn channel.
|
||||
@@ -51,6 +53,7 @@ public class AnalogEncoder implements Sendable, AutoCloseable {
|
||||
|
||||
if (m_simDevice != null) {
|
||||
m_simPosition = m_simDevice.createDouble("Position", Direction.kInput, 0.0);
|
||||
m_simAbsolutePosition = m_simDevice.createDouble("absPosition", Direction.kInput, 0.0);
|
||||
}
|
||||
|
||||
// Limits need to be 25% from each end
|
||||
@@ -61,6 +64,11 @@ public class AnalogEncoder implements Sendable, AutoCloseable {
|
||||
SendableRegistry.addLW(this, "Analog Encoder", m_analogInput.getChannel());
|
||||
}
|
||||
|
||||
private boolean doubleEquals(double a, double b) {
|
||||
double epsilon = 0.00001d;
|
||||
return Math.abs(a - b) < epsilon;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the encoder value since the last reset.
|
||||
*
|
||||
@@ -80,7 +88,8 @@ public class AnalogEncoder implements Sendable, AutoCloseable {
|
||||
double pos = m_analogInput.getVoltage();
|
||||
double counter2 = m_counter.get();
|
||||
double pos2 = m_analogInput.getVoltage();
|
||||
if (counter == counter2 && pos == pos2) {
|
||||
if (counter == counter2 && doubleEquals(pos, pos2)) {
|
||||
pos = pos / RobotController.getVoltage5V();
|
||||
double position = counter + pos - m_positionOffset;
|
||||
m_lastPosition = position;
|
||||
return position;
|
||||
@@ -92,12 +101,29 @@ public class AnalogEncoder implements Sendable, AutoCloseable {
|
||||
return m_lastPosition;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the absolute position of the analog encoder.
|
||||
*
|
||||
* <p>getAbsolutePosition() - getPositionOffset() will give an encoder absolute position relative
|
||||
* to the last reset. This could potentially be negative, which needs to be accounted for.
|
||||
*
|
||||
* <p>This will not account for rollovers, and will always be just the raw absolute position.
|
||||
*
|
||||
* @return the absolute position
|
||||
*/
|
||||
public double getAbsolutePosition() {
|
||||
if (m_simAbsolutePosition != null) {
|
||||
return m_simAbsolutePosition.get();
|
||||
}
|
||||
|
||||
return m_analogInput.getVoltage() / RobotController.getVoltage5V();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the offset of position relative to the last reset.
|
||||
*
|
||||
* <p>getPositionInRotation() - getPositionOffset() will give an encoder absolute position
|
||||
* relative to the last reset. This could potentially be negative, which needs to be accounted
|
||||
* for.
|
||||
* <p>getAbsolutePosition() - getPositionOffset() will give an encoder absolute position relative
|
||||
* to the last reset. This could potentially be negative, which needs to be accounted for.
|
||||
*
|
||||
* @return the position offset
|
||||
*/
|
||||
@@ -105,6 +131,17 @@ public class AnalogEncoder implements Sendable, AutoCloseable {
|
||||
return m_positionOffset;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the position offset.
|
||||
*
|
||||
* <p>This must be in the range of 0-1.
|
||||
*
|
||||
* @param offset the offset
|
||||
*/
|
||||
public void setPositionOffset(double offset) {
|
||||
m_positionOffset = MathUtil.clamp(offset, 0.0, 1.0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the distance per rotation of the encoder. This sets the multiplier used to determine the
|
||||
* distance driven based on the rotation value from the encoder. Set this value based on the how
|
||||
@@ -148,7 +185,7 @@ public class AnalogEncoder implements Sendable, AutoCloseable {
|
||||
/** Reset the Encoder distance to zero. */
|
||||
public void reset() {
|
||||
m_counter.reset();
|
||||
m_positionOffset = m_analogInput.getVoltage();
|
||||
m_positionOffset = m_analogInput.getVoltage() / RobotController.getVoltage5V();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -32,6 +32,7 @@ public class DutyCycleEncoder implements Sendable, AutoCloseable {
|
||||
|
||||
protected SimDevice m_simDevice;
|
||||
protected SimDouble m_simPosition;
|
||||
protected SimDouble m_simAbsolutePosition;
|
||||
protected SimDouble m_simDistancePerRotation;
|
||||
protected SimBoolean m_simIsConnected;
|
||||
|
||||
@@ -75,6 +76,8 @@ public class DutyCycleEncoder implements Sendable, AutoCloseable {
|
||||
m_simPosition = m_simDevice.createDouble("position", SimDevice.Direction.kInput, 0.0);
|
||||
m_simDistancePerRotation =
|
||||
m_simDevice.createDouble("distance_per_rot", SimDevice.Direction.kOutput, 1.0);
|
||||
m_simAbsolutePosition =
|
||||
m_simDevice.createDouble("absPosition", SimDevice.Direction.kInput, 0.0);
|
||||
m_simIsConnected = m_simDevice.createBoolean("connected", SimDevice.Direction.kInput, true);
|
||||
} else {
|
||||
m_counter = new Counter();
|
||||
@@ -87,6 +90,23 @@ public class DutyCycleEncoder implements Sendable, AutoCloseable {
|
||||
SendableRegistry.addLW(this, "DutyCycle Encoder", m_dutyCycle.getSourceChannel());
|
||||
}
|
||||
|
||||
private double mapSensorRange(double pos) {
|
||||
// map sensor range
|
||||
if (pos < m_sensorMin) {
|
||||
pos = m_sensorMin;
|
||||
}
|
||||
if (pos > m_sensorMax) {
|
||||
pos = m_sensorMax;
|
||||
}
|
||||
pos = (pos - m_sensorMin) / (m_sensorMax - m_sensorMin);
|
||||
return pos;
|
||||
}
|
||||
|
||||
private boolean doubleEquals(double a, double b) {
|
||||
double epsilon = 0.00001d;
|
||||
return Math.abs(a - b) < epsilon;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the encoder value since the last reset.
|
||||
*
|
||||
@@ -107,15 +127,9 @@ public class DutyCycleEncoder implements Sendable, AutoCloseable {
|
||||
double pos = m_dutyCycle.getOutput();
|
||||
double counter2 = m_counter.get();
|
||||
double pos2 = m_dutyCycle.getOutput();
|
||||
if (counter == counter2 && pos == pos2) {
|
||||
if (counter == counter2 && doubleEquals(pos, pos2)) {
|
||||
// map sensor range
|
||||
if (pos < m_sensorMin) {
|
||||
pos = m_sensorMin;
|
||||
}
|
||||
if (pos > m_sensorMax) {
|
||||
pos = m_sensorMax;
|
||||
}
|
||||
pos = (pos - m_sensorMin) / (m_sensorMax - m_sensorMin);
|
||||
pos = mapSensorRange(pos);
|
||||
double position = counter + pos - m_positionOffset;
|
||||
m_lastPosition = position;
|
||||
return position;
|
||||
@@ -127,12 +141,29 @@ public class DutyCycleEncoder implements Sendable, AutoCloseable {
|
||||
return m_lastPosition;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the absolute position of the duty cycle encoder.
|
||||
*
|
||||
* <p>getAbsolutePosition() - getPositionOffset() will give an encoder absolute position relative
|
||||
* to the last reset. This could potentially be negative, which needs to be accounted for.
|
||||
*
|
||||
* <p>This will not account for rollovers, and will always be just the raw absolute position.
|
||||
*
|
||||
* @return the absolute position
|
||||
*/
|
||||
public double getAbsolutePosition() {
|
||||
if (m_simAbsolutePosition != null) {
|
||||
return m_simAbsolutePosition.get();
|
||||
}
|
||||
|
||||
return mapSensorRange(m_dutyCycle.getOutput());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the offset of position relative to the last reset.
|
||||
*
|
||||
* <p>getPositionInRotation() - getPositionOffset() will give an encoder absolute position
|
||||
* relative to the last reset. This could potentially be negative, which needs to be accounted
|
||||
* for.
|
||||
* <p>getAbsolutePosition() - getPositionOffset() will give an encoder absolute position relative
|
||||
* to the last reset. This could potentially be negative, which needs to be accounted for.
|
||||
*
|
||||
* @return the position offset
|
||||
*/
|
||||
@@ -140,6 +171,17 @@ public class DutyCycleEncoder implements Sendable, AutoCloseable {
|
||||
return m_positionOffset;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the position offset.
|
||||
*
|
||||
* <p>This must be in the range of 0-1.
|
||||
*
|
||||
* @param offset the offset
|
||||
*/
|
||||
public void setPositionOffset(double offset) {
|
||||
m_positionOffset = MathUtil.clamp(offset, 0.0, 1.0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the encoder duty cycle range. As the encoder needs to maintain a duty cycle, the duty cycle
|
||||
* cannot go all the way to 0% or all the way to 100%. For example, an encoder with a 4096 us
|
||||
|
||||
Reference in New Issue
Block a user