mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-21 01:01:43 +00:00
The camera fixture tests now include an SPI accelerometer test
Change-Id: I5dae746541d1d0e29f7d7c140a8fed8418502f45
This commit is contained in:
@@ -9,46 +9,56 @@
|
||||
#include "gtest/gtest.h"
|
||||
#include "TestBench.h"
|
||||
|
||||
static constexpr double kResetTime = 1.0;
|
||||
static constexpr double kServoResetTime = 1.0;
|
||||
|
||||
static constexpr double kTestAngle = 180.0;
|
||||
|
||||
|
||||
static constexpr double kTiltSetpoint0 = 0.16;
|
||||
static constexpr double kTiltSetpoint45 = 0.385;
|
||||
static constexpr double kTiltSetpoint90 = 0.61;
|
||||
static constexpr double kTiltTime = 0.5;
|
||||
static constexpr double kAccelerometerTolerance = 0.1;
|
||||
|
||||
/**
|
||||
* A fixture for the camera with two servos and a gyro
|
||||
* @author Thomas Clark
|
||||
*/
|
||||
class TiltPanCameraTest : public testing::Test {
|
||||
protected:
|
||||
static Gyro *m_gyro;
|
||||
Servo *m_tilt, *m_pan;
|
||||
Gyro *m_gyro;
|
||||
ADXL345_SPI *m_spiAccel;
|
||||
|
||||
static void SetUpTestCase() {
|
||||
// The gyro object blocks for 5 seconds in the constructor, so only
|
||||
// construct it once for the whole test case
|
||||
m_gyro = new Gyro(TestBench::kCameraGyroChannel);
|
||||
}
|
||||
|
||||
static void TearDownTestCase() {
|
||||
delete m_gyro;
|
||||
}
|
||||
|
||||
virtual void SetUp() {
|
||||
m_tilt = new Servo(TestBench::kCameraTiltChannel);
|
||||
m_pan = new Servo(TestBench::kCameraPanChannel);
|
||||
m_gyro = new Gyro(TestBench::kCameraGyroChannel);
|
||||
m_spiAccel = new ADXL345_SPI(SPI::kOnboardCS0);
|
||||
}
|
||||
|
||||
virtual void TearDown() {
|
||||
delete m_tilt;
|
||||
delete m_pan;
|
||||
delete m_gyro;
|
||||
}
|
||||
|
||||
void Reset() {
|
||||
m_tilt->Set(0.0);
|
||||
m_pan->Set(0.0);
|
||||
|
||||
Wait(kResetTime);
|
||||
|
||||
m_gyro->Reset();
|
||||
delete m_spiAccel;
|
||||
}
|
||||
};
|
||||
|
||||
Gyro *TiltPanCameraTest::m_gyro = 0;
|
||||
|
||||
/**
|
||||
* Test if the servo turns 180 degrees and the gyroscope measures this angle
|
||||
*/
|
||||
TEST_F(TiltPanCameraTest, GyroAngle) {
|
||||
Reset();
|
||||
Wait(kServoResetTime);
|
||||
|
||||
for(int i = 0; i < 100; i++) {
|
||||
m_pan->Set(i / 100.0);
|
||||
@@ -61,4 +71,26 @@ TEST_F(TiltPanCameraTest, GyroAngle) {
|
||||
<< " degrees, servo should have turned " << kTestAngle << " degrees";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test if the accelerometer measures gravity along the correct axes when the
|
||||
* camera rotates
|
||||
*/
|
||||
TEST_F(TiltPanCameraTest, SPIAccelerometer) {
|
||||
m_tilt->Set(kTiltSetpoint0);
|
||||
Wait(kTiltTime);
|
||||
EXPECT_NEAR(-1.0, m_spiAccel->GetAcceleration(ADXL345_SPI::kAxis_X), kAccelerometerTolerance);
|
||||
EXPECT_NEAR(0.0, m_spiAccel->GetAcceleration(ADXL345_SPI::kAxis_Y), kAccelerometerTolerance);
|
||||
EXPECT_NEAR(0.0, m_spiAccel->GetAcceleration(ADXL345_SPI::kAxis_Z), kAccelerometerTolerance);
|
||||
|
||||
m_tilt->Set(kTiltSetpoint45);
|
||||
Wait(kTiltTime);
|
||||
EXPECT_NEAR(-std::sqrt(0.5), m_spiAccel->GetAcceleration(ADXL345_SPI::kAxis_X), kAccelerometerTolerance);
|
||||
EXPECT_NEAR(0.0, m_spiAccel->GetAcceleration(ADXL345_SPI::kAxis_Y), kAccelerometerTolerance);
|
||||
EXPECT_NEAR(std::sqrt(0.5), m_spiAccel->GetAcceleration(ADXL345_SPI::kAxis_Z), kAccelerometerTolerance);
|
||||
|
||||
m_tilt->Set(kTiltSetpoint90);
|
||||
Wait(kTiltTime);
|
||||
EXPECT_NEAR(0.0, m_spiAccel->GetAcceleration(ADXL345_SPI::kAxis_X), kAccelerometerTolerance);
|
||||
EXPECT_NEAR(0.0, m_spiAccel->GetAcceleration(ADXL345_SPI::kAxis_Y), kAccelerometerTolerance);
|
||||
EXPECT_NEAR(1.0, m_spiAccel->GetAcceleration(ADXL345_SPI::kAxis_Z), kAccelerometerTolerance);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user