-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added unit tests for the computed Gyroscopes in the HardwareTalonSRX …
…class. The unit tests verify the basic logic, but still don't match a real hardware test.
- Loading branch information
Showing
9 changed files
with
513 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
strongback-tests/src/org/strongback/hardware/HardwareTalonSRX_AnalogInputSensorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* Strongback | ||
* Copyright 2015, Strongback and individual contributors by the @authors tag. | ||
* See the COPYRIGHT.txt in the distribution for a full listing of individual | ||
* contributors. | ||
* | ||
* Licensed under the MIT License; you may not use this file except in | ||
* compliance with the License. You may obtain a copy of the License at | ||
* http://opensource.org/licenses/MIT | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.strongback.hardware; | ||
|
||
import static org.fest.assertions.Assertions.assertThat; | ||
|
||
import org.fest.assertions.Delta; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.strongback.hardware.HardwareTalonSRX.AnalogInputSensor; | ||
|
||
public class HardwareTalonSRX_AnalogInputSensorTest { | ||
|
||
private static final Delta DELTA = Delta.delta(1); | ||
|
||
private double analogPosition = 0.0; | ||
private double analogVelocity = 0.0; // changes in volts per cycle | ||
private double analogRange = 1023; // 10 bit values | ||
private double analogTurnsOverVoltageRange = 1; | ||
private double analogVoltageRange = 3.3; | ||
private double cyclePeriodInSeconds = 0.1; | ||
private AnalogInputSensor sensor; | ||
|
||
@Before | ||
public void beforeEach() { | ||
} | ||
|
||
protected AnalogInputSensor createSensor() { | ||
sensor = new AnalogInputSensor(() -> analogPosition, | ||
() -> analogVelocity, | ||
analogRange, | ||
analogTurnsOverVoltageRange / analogVoltageRange, | ||
analogVoltageRange, | ||
() -> cyclePeriodInSeconds); | ||
return sensor; | ||
} | ||
|
||
@Test | ||
public void shouldReturnZeroWhenMeasuredPositionAndVelocityAreZero() { | ||
analogPosition = 0; | ||
analogTurnsOverVoltageRange = 1; | ||
createSensor(); | ||
assertThat(sensor.getAngle()).isEqualTo(0.0d, DELTA); | ||
assertThat(sensor.getHeading()).isEqualTo(0.0d, DELTA); | ||
assertThat(sensor.getRate()).isEqualTo(0.0d, DELTA); | ||
assertThat(sensor.rawPositionForAngleInDegrees(0.0d)).isEqualTo(analogPosition, DELTA); | ||
} | ||
|
||
@Test | ||
public void shouldReturnZeroWhenMeasuredPositionIsHalfRotationAndVelocityAreZero() { | ||
analogPosition = 1024/2 - 1; | ||
analogTurnsOverVoltageRange = 1; | ||
createSensor(); | ||
assertThat(sensor.getAngle()).isEqualTo(180.0d, DELTA); | ||
assertThat(sensor.getHeading()).isEqualTo(180.0d, DELTA); | ||
assertThat(sensor.getRate()).isEqualTo(0.0d, DELTA); | ||
assertThat(sensor.rawPositionForAngleInDegrees(180.0d)).isEqualTo(analogPosition, DELTA); | ||
} | ||
|
||
@Test | ||
public void shouldReturnZeroWhenMeasuredPositionIsOneRotationAndVelocityAreZero() { | ||
analogPosition = 1024-1; | ||
analogTurnsOverVoltageRange = 1; | ||
createSensor(); | ||
assertThat(sensor.getAngle()).isEqualTo(360.0d, DELTA); | ||
assertThat(sensor.getHeading()).isEqualTo(0.0d, DELTA); | ||
assertThat(sensor.getRate()).isEqualTo(0.0d, DELTA); | ||
assertThat(sensor.rawPositionForAngleInDegrees(360.0d)).isEqualTo(analogPosition, DELTA); | ||
} | ||
|
||
@Test | ||
public void shouldReturnZeroWhenMeasuredPositionIsOneAndOneHalfRotationAndVelocityAreZero() { | ||
analogPosition = (int)(1024 * 1.5) - 1; | ||
analogTurnsOverVoltageRange = 1; | ||
createSensor(); | ||
assertThat(sensor.getAngle()).isEqualTo(540.0d, DELTA); | ||
assertThat(sensor.getHeading()).isEqualTo(180.0d, DELTA); | ||
assertThat(sensor.getRate()).isEqualTo(0.0d, DELTA); | ||
assertThat(sensor.rawPositionForAngleInDegrees(540.0d)).isEqualTo(analogPosition, DELTA); | ||
} | ||
|
||
@Test | ||
public void shouldReturnZeroWhenMeasuredPositionIsThreeAndOneHalfRotationAndVelocityAreZero() { | ||
analogPosition = (int)(1024 * 3.5) - 1; | ||
analogTurnsOverVoltageRange = 1; | ||
createSensor(); | ||
assertThat(sensor.getAngle()).isEqualTo(1260.0d, DELTA); | ||
assertThat(sensor.getHeading()).isEqualTo(180.0d, DELTA); | ||
assertThat(sensor.getRate()).isEqualTo(0.0d, DELTA); | ||
assertThat(sensor.rawPositionForAngleInDegrees(1260.0d)).isEqualTo(analogPosition, Delta.delta(5.0)); | ||
} | ||
|
||
@Test | ||
public void shouldHandleMeasuredPositionAndVelocity() { | ||
analogPosition = (int)(1024 * 3.5) - 1; | ||
analogVelocity = 1023; // starts from 0 and does full rotation | ||
analogTurnsOverVoltageRange = 1; | ||
createSensor(); | ||
assertThat(sensor.getAngle()).isEqualTo(1260.0d, DELTA); | ||
assertThat(sensor.getHeading()).isEqualTo(180.0d, DELTA); | ||
assertThat(sensor.getRate()).isEqualTo(360.0 / cyclePeriodInSeconds, DELTA); // degrees per second | ||
assertThat(sensor.rawPositionForAngleInDegrees(1260.0d)).isEqualTo(analogPosition, Delta.delta(5.0)); | ||
} | ||
|
||
} |
126 changes: 126 additions & 0 deletions
126
strongback-tests/src/org/strongback/hardware/HardwareTalonSRX_EncoderInputSensorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/* | ||
* Strongback | ||
* Copyright 2015, Strongback and individual contributors by the @authors tag. | ||
* See the COPYRIGHT.txt in the distribution for a full listing of individual | ||
* contributors. | ||
* | ||
* Licensed under the MIT License; you may not use this file except in | ||
* compliance with the License. You may obtain a copy of the License at | ||
* http://opensource.org/licenses/MIT | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.strongback.hardware; | ||
|
||
import static org.fest.assertions.Assertions.assertThat; | ||
|
||
import org.fest.assertions.Delta; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.strongback.hardware.HardwareTalonSRX.EncoderInputSensor; | ||
|
||
public class HardwareTalonSRX_EncoderInputSensorTest { | ||
|
||
private static final Delta DELTA = Delta.delta(0.00001); | ||
|
||
private double positionInEdges = 0.0; | ||
private double velocityInEdges = 0.0; | ||
private double cyclePeriodInSeconds = 0.1; | ||
private double rotationInDegrees = 0.0; | ||
private int encoderPulsesPerRotation = 360; | ||
private int encoderEdgesPerPulse = 4; // 4x mode, or 4 rising edges per pulse | ||
private EncoderInputSensor sensor; | ||
|
||
@Before | ||
public void beforeEach() { | ||
cyclePeriodInSeconds = 0.1; | ||
} | ||
|
||
protected EncoderInputSensor createSensor() { | ||
sensor = new EncoderInputSensor(() -> positionInEdges, () -> velocityInEdges, encoderPulsesPerRotation / 360.0, // pulses | ||
// per | ||
// degree | ||
encoderEdgesPerPulse, () -> cyclePeriodInSeconds); | ||
return sensor; | ||
} | ||
|
||
@Test | ||
public void shouldReturnZeroWhenMeasuredPositionAndVelocityAreZero() { | ||
encoderPulsesPerRotation = 360; | ||
positionInEdges = 0.0; | ||
velocityInEdges = 0.0; | ||
createSensor(); | ||
assertThat(sensor.getAngle()).isEqualTo(0.0d, DELTA); | ||
assertThat(sensor.getHeading()).isEqualTo(0.0d, DELTA); | ||
assertThat(sensor.getRate()).isEqualTo(0.0d, DELTA); | ||
assertThat(sensor.rawPositionForAngleInDegrees(0.0d)).isEqualTo(positionInEdges, DELTA); | ||
} | ||
|
||
@Test | ||
public void shouldReturnZeroWhenMeasuredPositionIsHalfRotationAndVelocityAreZero() { | ||
encoderPulsesPerRotation = 360; | ||
positionInEdges = 180 * 4; // edges for 180 degrees | ||
velocityInEdges = 0.0; | ||
createSensor(); | ||
assertThat(sensor.getAngle()).isEqualTo(180.0d, DELTA); | ||
assertThat(sensor.getHeading()).isEqualTo(180.0d, DELTA); | ||
assertThat(sensor.getRate()).isEqualTo(0.0d, DELTA); | ||
assertThat(sensor.rawPositionForAngleInDegrees(180.0d)).isEqualTo(positionInEdges, DELTA); | ||
} | ||
|
||
@Test | ||
public void shouldReturnZeroWhenMeasuredPositionIsOneAndOneHalfRotationAndVelocityAreZero() { | ||
encoderPulsesPerRotation = 360; | ||
positionInEdges = (180+360) * 4; // edges for 540 degrees | ||
velocityInEdges = 0.0; | ||
createSensor(); | ||
assertThat(sensor.getAngle()).isEqualTo(540.0d, DELTA); | ||
assertThat(sensor.getHeading()).isEqualTo(180.0d, DELTA); | ||
assertThat(sensor.getRate()).isEqualTo(0.0d, DELTA); | ||
assertThat(sensor.rawPositionForAngleInDegrees(540.0d)).isEqualTo(positionInEdges, DELTA); | ||
} | ||
|
||
@Test | ||
public void shouldReturnZeroWhenMeasuredPositionIsThreeAndOneHalfRotationAndVelocityAreZero() { | ||
encoderPulsesPerRotation = 360; | ||
positionInEdges = (180+360*3) * 4; // edges for 1260 degrees | ||
velocityInEdges = 0.0; | ||
createSensor(); | ||
assertThat(sensor.getAngle()).isEqualTo(1260.0d, DELTA); | ||
assertThat(sensor.getHeading()).isEqualTo(180.0d, DELTA); | ||
assertThat(sensor.getRate()).isEqualTo(0.0d, DELTA); | ||
assertThat(sensor.rawPositionForAngleInDegrees(1260.0d)).isEqualTo(positionInEdges, DELTA); | ||
} | ||
|
||
@Test | ||
public void shouldReturnZeroWhenMeasuredPositionIsThreeAndOneHalfRotationAndVelocityAreZeroWithPrecisionEncoder() { | ||
encoderPulsesPerRotation = 250; | ||
rotationInDegrees = 180+360*3; | ||
positionInEdges = rotationInDegrees * encoderPulsesPerRotation /360 * 4; | ||
velocityInEdges = 0.0; | ||
createSensor(); | ||
assertThat(sensor.getAngle()).isEqualTo(1260.0d, DELTA); | ||
assertThat(sensor.getHeading()).isEqualTo(180.0d, DELTA); | ||
assertThat(sensor.getRate()).isEqualTo(0.0d, DELTA); | ||
assertThat(sensor.rawPositionForAngleInDegrees(1260.0d)).isEqualTo(positionInEdges, DELTA); | ||
} | ||
|
||
@Test | ||
public void shouldHandleMeasuredPositionOfOneAndVelocityOfTwenty() { | ||
double rotationsPerSecond = 20; | ||
encoderPulsesPerRotation = 360; | ||
rotationInDegrees = 360; | ||
positionInEdges = rotationInDegrees * encoderPulsesPerRotation /360 * 4; | ||
velocityInEdges = rotationsPerSecond * 4 * 360.0 * cyclePeriodInSeconds; // per cycle, or 28800 position units / second | ||
createSensor(); | ||
assertThat(sensor.getAngle()).isEqualTo(360.0, DELTA); | ||
assertThat(sensor.getHeading()).isEqualTo(0.0d, DELTA); | ||
assertThat(sensor.getRate()).isEqualTo(rotationsPerSecond * 360.0, DELTA); // degrees per second | ||
assertThat(sensor.rawPositionForAngleInDegrees(360.0d)).isEqualTo(positionInEdges, DELTA); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.