2014-05-28 13:21:35 -04:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2018-01-02 09:20:21 -08:00
|
|
|
/* Copyright (c) 2008-2018 FIRST. All Rights Reserved. */
|
2016-01-02 03:02:34 -08:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
2014-05-28 13:21:35 -04:00
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
2016-01-02 03:02:34 -08:00
|
|
|
/* the project. */
|
2014-05-28 13:21:35 -04:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2016-01-02 03:02:34 -08:00
|
|
|
|
2014-05-28 13:21:35 -04:00
|
|
|
package edu.wpi.first.wpilibj;
|
|
|
|
|
|
2018-05-24 00:31:04 -04:00
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
|
2014-05-28 13:21:35 -04:00
|
|
|
import org.junit.After;
|
|
|
|
|
import org.junit.Before;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
|
|
import edu.wpi.first.wpilibj.fixtures.TiltPanCameraFixture;
|
|
|
|
|
import edu.wpi.first.wpilibj.test.AbstractComsSetup;
|
|
|
|
|
import edu.wpi.first.wpilibj.test.TestBench;
|
|
|
|
|
|
2016-05-20 12:07:40 -04:00
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Tests that the {@link TiltPanCameraFixture}.
|
|
|
|
|
*/
|
2014-07-31 16:08:14 -04:00
|
|
|
public class GyroTest extends AbstractComsSetup {
|
2015-06-25 15:07:55 -04:00
|
|
|
private static final Logger logger = Logger.getLogger(GyroTest.class.getName());
|
|
|
|
|
|
|
|
|
|
public static final double TEST_ANGLE = 90.0;
|
|
|
|
|
|
2016-05-20 12:07:40 -04:00
|
|
|
private TiltPanCameraFixture m_tpcam;
|
2015-06-25 15:07:55 -04:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected Logger getClassLogger() {
|
|
|
|
|
return logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Before
|
|
|
|
|
public void setUp() throws Exception {
|
|
|
|
|
logger.fine("Setup: TiltPan camera");
|
2016-05-20 12:07:40 -04:00
|
|
|
m_tpcam = TestBench.getInstance().getTiltPanCam();
|
|
|
|
|
m_tpcam.setup();
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@After
|
|
|
|
|
public void tearDown() throws Exception {
|
2016-05-20 12:07:40 -04:00
|
|
|
m_tpcam.teardown();
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
2015-07-28 11:57:22 -04:00
|
|
|
public void testAllGyroTests() {
|
2016-05-20 12:07:40 -04:00
|
|
|
testInitial(m_tpcam.getGyro());
|
|
|
|
|
testDeviationOverTime(m_tpcam.getGyro());
|
|
|
|
|
testGyroAngle(m_tpcam.getGyro());
|
2015-07-28 11:57:22 -04:00
|
|
|
testGyroAngleCalibratedParameters();
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-02 13:38:51 -05:00
|
|
|
public void testInitial(AnalogGyro gyro) {
|
|
|
|
|
double angle = gyro.getAngle();
|
2015-06-25 15:07:55 -04:00
|
|
|
assertEquals(errorMessage(angle, 0), 0, angle, .5);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-20 12:07:40 -04:00
|
|
|
* Test to see if the Servo and the gyroscope is turning 90 degrees Note servo on TestBench is not
|
|
|
|
|
* the same type of servo that servo class was designed for so setAngle is significantly off. This
|
|
|
|
|
* has been calibrated for the servo on the rig.
|
2015-06-25 15:07:55 -04:00
|
|
|
*/
|
2016-02-02 13:38:51 -05:00
|
|
|
public void testGyroAngle(AnalogGyro gyro) {
|
2015-06-25 15:07:55 -04:00
|
|
|
// Set angle
|
|
|
|
|
for (int i = 0; i < 5; i++) {
|
2016-05-20 12:07:40 -04:00
|
|
|
m_tpcam.getPan().set(0);
|
2015-06-25 15:07:55 -04:00
|
|
|
Timer.delay(.1);
|
|
|
|
|
}
|
2015-04-10 16:25:20 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
Timer.delay(0.5);
|
|
|
|
|
// Reset for setup
|
2016-02-02 13:38:51 -05:00
|
|
|
gyro.reset();
|
2015-06-25 15:07:55 -04:00
|
|
|
Timer.delay(0.5);
|
2015-04-10 16:25:20 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
// Perform test
|
|
|
|
|
for (int i = 0; i < 53; i++) {
|
2016-05-20 12:07:40 -04:00
|
|
|
m_tpcam.getPan().set(i / 100.0);
|
2015-06-25 15:07:55 -04:00
|
|
|
Timer.delay(0.05);
|
2015-04-10 16:25:20 -04:00
|
|
|
}
|
2015-06-25 15:07:55 -04:00
|
|
|
Timer.delay(1.2);
|
2015-04-10 16:25:20 -04:00
|
|
|
|
2016-02-02 13:38:51 -05:00
|
|
|
double angle = gyro.getAngle();
|
2015-04-10 16:25:20 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
double difference = TEST_ANGLE - angle;
|
2015-04-10 16:25:20 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
double diff = Math.abs(difference);
|
2015-04-10 16:25:20 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
assertEquals(errorMessage(diff, TEST_ANGLE), TEST_ANGLE, angle, 10);
|
|
|
|
|
}
|
2015-04-10 16:25:20 -04:00
|
|
|
|
2016-05-20 12:07:40 -04:00
|
|
|
|
|
|
|
|
protected void testDeviationOverTime(AnalogGyro gyro) {
|
2015-06-25 15:07:55 -04:00
|
|
|
// Make sure that the test isn't influenced by any previous motions.
|
2015-07-28 11:57:22 -04:00
|
|
|
Timer.delay(0.5);
|
2016-02-02 13:38:51 -05:00
|
|
|
gyro.reset();
|
2015-06-25 15:07:55 -04:00
|
|
|
Timer.delay(0.25);
|
2016-02-02 13:38:51 -05:00
|
|
|
double angle = gyro.getAngle();
|
2015-06-25 15:07:55 -04:00
|
|
|
assertEquals(errorMessage(angle, 0), 0, angle, .5);
|
|
|
|
|
Timer.delay(5);
|
2016-02-02 13:38:51 -05:00
|
|
|
angle = gyro.getAngle();
|
2016-02-08 15:03:51 -05:00
|
|
|
assertEquals("After 5 seconds " + errorMessage(angle, 0), 0, angle, 2.0);
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
2015-04-10 16:25:20 -04:00
|
|
|
|
2015-07-28 11:57:22 -04:00
|
|
|
/**
|
2016-05-20 12:07:40 -04:00
|
|
|
* Gets calibrated parameters from already calibrated gyro, allocates a new gyro with the center
|
|
|
|
|
* and offset parameters, and re-runs the test.
|
2015-07-28 11:57:22 -04:00
|
|
|
*/
|
|
|
|
|
public void testGyroAngleCalibratedParameters() {
|
|
|
|
|
// Get calibrated parameters to make new Gyro with parameters
|
2016-05-20 12:07:40 -04:00
|
|
|
final double calibratedOffset = m_tpcam.getGyro().getOffset();
|
|
|
|
|
final int calibratedCenter = m_tpcam.getGyro().getCenter();
|
|
|
|
|
m_tpcam.freeGyro();
|
|
|
|
|
m_tpcam.setupGyroParam(calibratedCenter, calibratedOffset);
|
2016-02-02 13:38:51 -05:00
|
|
|
Timer.delay(TiltPanCameraFixture.RESET_TIME);
|
2015-07-28 11:57:22 -04:00
|
|
|
// Repeat tests
|
2016-05-20 12:07:40 -04:00
|
|
|
testInitial(m_tpcam.getGyroParam());
|
|
|
|
|
testDeviationOverTime(m_tpcam.getGyroParam());
|
|
|
|
|
testGyroAngle(m_tpcam.getGyroParam());
|
2015-07-28 11:57:22 -04:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
private String errorMessage(double difference, double target) {
|
|
|
|
|
return "Gyro angle skewed " + difference + " deg away from target " + target;
|
|
|
|
|
}
|
2014-05-28 13:21:35 -04:00
|
|
|
|
|
|
|
|
}
|