Skip to content

Commit

Permalink
Use fields everywhere.
Browse files Browse the repository at this point in the history
  • Loading branch information
Noah Gleason committed Oct 31, 2017
1 parent 396d44c commit dff5023
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public class MappedDigitalInput {
*/
@JsonIgnore
private final List<DigitalInput> digitalInputs;

/**
* Value of the inputs. Field to avoid garbage collection.
*/
private List<Boolean> digitalValues;

/**
* Construct a MappedDigitalInput.
Expand All @@ -41,7 +46,7 @@ public MappedDigitalInput(@NotNull @JsonProperty(required = true) int[] ports) {
@JsonIgnore
@NotNull
public List<Boolean> getStatus() {
List<Boolean> digitalValues = new ArrayList<>();
digitalValues = new ArrayList<>();
for (DigitalInput digitalInput : digitalInputs) {
//Negated because, by default, false means signal and true means no signal, and that's dumb.
digitalValues.add(!digitalInput.get());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,14 @@ public class OIOutreach implements OIUnidirectional {
@NotNull
private final Button button;

/**
* The cached outputs for the left and right sides of the drive.
*/
private double cachedLeftOutput, cachedRightOutput;

/**
* Whether the driver was trying to drive straight when values were cached.
*/
private boolean cachedCommandingStraight;

@JsonCreator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.WRAPPER_OBJECT, property = "@class")
public abstract class OIArcade implements OIUnidirectional {

/**
* Cached output values.
*/
private double rotCached, fwdCached, leftCached, rightCached;

/**
* Whether the driver was trying to drive straight when values were cached.
*/
private boolean commandingStraightCached;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,6 @@ public class OIArcadeWithDPad extends OIArcade implements Loggable {
*/
private final double turnInPlaceRotScale;

/**
* Cached forwards and rotation values.
*/
private double cachedFwd, cachedRot;

/**
* The time velocity and rotation were cached at.
*/
private long timeLastCached;

/**
* Default constructor
*
Expand Down Expand Up @@ -95,31 +85,7 @@ public OIArcadeWithDPad(
this.turnInPlaceRotScale = turnInPlaceRotScale;
timeLastCached = 0;
}

/**
* Calculate and cache the values of fwd and rot.
*/
private void cacheValues() {
if (Clock.currentTimeMillis() > timeLastCached) {
timeLastCached = Clock.currentTimeMillis();

//Forwards is simple
cachedFwd = fwdThrottle.getValue();

//If the gamepad is being pushed to the left or right
if (gamepad != null && !(gamepad.getPOV() == -1 || gamepad.getPOV() % 180 == 0)) {
//Output the shift value
cachedRot = gamepad.getPOV() < 180 ? dPadShift : -dPadShift;
} else if (cachedFwd == 0) { //Turning in place
cachedRot = rotThrottle.getValue() * turnInPlaceRotScale;
} else if (scaleRotByFwdPoly != null) { //If we're using Cheezy Drive
cachedRot = rotThrottle.getValue() * scaleRotByFwdPoly.get(Math.abs(cachedFwd));
} else { //Plain and simple
cachedRot = rotThrottle.getValue();
}
}
}


/**
* The output of the throttle controlling linear velocity, smoothed and adjusted according to what type of joystick
* it is.
Expand All @@ -128,8 +94,7 @@ private void cacheValues() {
*/
@Override
public double getFwd() {
cacheValues();
return cachedFwd;
return fwdThrottle.getValue();
}

/**
Expand All @@ -140,8 +105,17 @@ public double getFwd() {
*/
@Override
public double getRot() {
cacheValues();
return cachedRot;
//If the gamepad is being pushed to the left or right
if (gamepad != null && !(gamepad.getPOV() == -1 || gamepad.getPOV() % 180 == 0)) {
//Output the shift value
return gamepad.getPOV() < 180 ? dPadShift : -dPadShift;
} else if (getFwd() == 0) { //Turning in place
return rotThrottle.getValue() * turnInPlaceRotScale;
} else if (scaleRotByFwdPoly != null) { //If we're using Cheezy Drive
return rotThrottle.getValue() * scaleRotByFwdPoly.get(Math.abs(getFwd()));
} else { //Plain and simple
return rotThrottle.getValue();
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.WRAPPER_OBJECT, property = "@class")
public abstract class OITank implements OIUnidirectional {

/**
* Cached left and right throttle values.
*/
private double leftThrottleCached, rightThrottleCached;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,22 @@ public class UnidirectionalPoseEstimator <T extends SubsystemAHRS & DriveUnidire
* The percent the Noah method changed the wrong encoder reading by.
*/
private double percentChanged;

/**
* Angle and magnitude of vector being calculated. Field to avoid garbage collection.
*/
private double vectorAngle, vectorMagnitude;

/**
* Per-run variables for run(). Fields to avoid garbage collection.
*/
private double left, right, theta, deltaLeft, deltaRight, deltaTheta;
private long time;

/**
* Output vector from run(). Field to avoid garbage collection.
*/
private double[] vector;

/**
* Default constructor.
Expand Down Expand Up @@ -144,50 +160,38 @@ public UnidirectionalPoseEstimator(@Nullable Double robotDiameter,
}

private static double[] calcEliVector(double left, double right, double deltaTheta, double lastAngle) {
//The vector for how much the robot moves, element 0 is x and element 1 is y.
double[] vector = new double[2];

//If we're going in a straight line
if (deltaTheta == 0) {
//we could use deltaRight here, doesn't matter. Going straight means no change in angle and left and right are the same.
vector[0] = left * Math.cos(lastAngle);
vector[1] = left * Math.sin(lastAngle);
return new double[]{left * Math.cos(lastAngle), left * Math.sin(lastAngle)};
} else {
//This next part is too complicated to explain in comments. Read this wiki page instead:
// http://team449.shoutwiki.com/wiki/Pose_Estimation
double r = ((left + right) / 2.) / deltaTheta;
double vectorAngle = lastAngle + deltaTheta / 2.;
double vectorMagnitude = 2. * r * Math.sin(deltaTheta / 2.);
vector[0] = vectorMagnitude * Math.cos(vectorAngle);
vector[1] = vectorMagnitude * Math.sin(vectorAngle);
double vectorMagnitude = 2. * ((left + right) / 2.) / deltaTheta * Math.sin(deltaTheta / 2.);
return new double[]{vectorMagnitude * Math.cos(vectorAngle), vectorMagnitude * Math.sin(vectorAngle)};
}
return vector;
}

private static double[] calcVector(double left, double right, double robotDiameter, double deltaTheta, double lastAngle) {
//The vector for how much the robot moves, element 0 is x and element 1 is y.
double[] vector = new double[2];

//If we're going in a straight line
if (deltaTheta == 0) {
//we could use deltaRight here, doesn't matter. Going straight means no change in angle and left and right are the same.
vector[0] = left * Math.cos(lastAngle);
vector[1] = left * Math.sin(lastAngle);
return new double[]{left * Math.cos(lastAngle), left * Math.sin(lastAngle)};
} else {
//This next part is too complicated to explain in comments. Read this wiki page instead:
// http://team449.shoutwiki.com/wiki/Pose_Estimation
double r;
if (left - right == 0) {
r = left / deltaTheta;
vectorMagnitude = 2* left / deltaTheta * Math.sin(deltaTheta / 2.);
} else {
r = robotDiameter / 2. * (left + right) / (left - right);
vectorMagnitude = 2* robotDiameter / 2. * (left + right) / (left - right) * Math.sin(deltaTheta / 2.);
}
double vectorAngle = lastAngle + deltaTheta / 2.;
double vectorMagnitude = 2. * r * Math.sin(deltaTheta / 2.);
vector[0] = vectorMagnitude * Math.cos(vectorAngle);
vector[1] = vectorMagnitude * Math.sin(vectorAngle);
vectorAngle = lastAngle + deltaTheta / 2.;
return new double[]{vectorMagnitude * Math.cos(vectorAngle), vectorMagnitude * Math.sin(vectorAngle)};
}
return vector;
}

/**
Expand All @@ -196,24 +200,22 @@ private static double[] calcVector(double left, double right, double robotDiamet
@Override
public synchronized void run() {
//Record everything at the start, as it may change between executing lines of code and that would be bad.
double left = subsystem.getLeftPos();
double right = subsystem.getRightPos();
double theta = Math.toRadians(subsystem.getAngularDisplacement());
long time = Clock.currentTimeMillis();
left = subsystem.getLeftPos();
right = subsystem.getRightPos();
theta = Math.toRadians(subsystem.getAngularDisplacement());
time = Clock.currentTimeMillis();

//Calculate differences versus the last measurement
double deltaLeft = left - lastLeftPos;
double deltaRight = right - lastRightPos;
double deltaTheta = theta - lastTheta;
double robotDiameter;
deltaLeft = left - lastLeftPos;
deltaRight = right - lastRightPos;
deltaTheta = theta - lastTheta;
if (deltaTheta == 0) {
fudgedWheelbaseDiameter = -1;
} else {
fudgedWheelbaseDiameter = (deltaLeft - deltaRight) / deltaTheta;
}

double[] vector;
if (this.robotDiameter != null) {
if (robotDiameter != null) {
//Noah's Approach:

//For this next part, we assume that the gyro is 100% accurate at measuring the change in angle over the given
Expand All @@ -222,7 +224,6 @@ public synchronized void run() {
//Given those constraints, we have an overdetermined system because deltaTheta should be equal to
//(deltaLeft-deltaRight)/robotDiameter. We can use this to determine which wheel slipped more, and replace its
//reading with a value calculated from the other encoder and the gyro.
robotDiameter = this.robotDiameter;
if (deltaTheta < (deltaLeft - deltaRight) / robotDiameter) {
if (deltaLeft > 0) {
percentChanged = ((deltaRight + robotDiameter * deltaTheta) - deltaLeft) / deltaLeft;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ public class ClimberCurrentLimited extends YamlSubsystem implements Loggable, Su
*/
private boolean motorSpinning;

/**
* Whether the condition was met last time caching was done.
*/
private boolean conditionMetCached;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public Object[] getData() {
@NotNull
@Override
public String getName() {
return "multiSubsystem";
return "loggingShooter";
}

/**
Expand Down

0 comments on commit dff5023

Please sign in to comment.