Updated the C++ TiltPanCameraTest

The gyro sensitivity is fixed, the Accelerometer interface is
now used, and some tolerances are adjusted.

Change-Id: Iac1f3c4fbae3be923bd97692684ff72cd2f623f9
This commit is contained in:
thomasclark
2014-07-23 09:36:27 -04:00
parent 41c2b9402c
commit 5eddb69aa0

View File

@@ -16,8 +16,8 @@ static constexpr double kTestAngle = 180.0;
static constexpr double kTiltSetpoint0 = 0.16; static constexpr double kTiltSetpoint0 = 0.16;
static constexpr double kTiltSetpoint45 = 0.385; static constexpr double kTiltSetpoint45 = 0.385;
static constexpr double kTiltSetpoint90 = 0.61; static constexpr double kTiltSetpoint90 = 0.61;
static constexpr double kTiltTime = 0.5; static constexpr double kTiltTime = 1.0;
static constexpr double kAccelerometerTolerance = 0.1; static constexpr double kAccelerometerTolerance = 0.2;
/** /**
* A fixture for the camera with two servos and a gyro * A fixture for the camera with two servos and a gyro
@@ -27,24 +27,25 @@ class TiltPanCameraTest : public testing::Test {
protected: protected:
static Gyro *m_gyro; static Gyro *m_gyro;
Servo *m_tilt, *m_pan; Servo *m_tilt, *m_pan;
ADXL345_SPI *m_spiAccel; Accelerometer *m_spiAccel;
static void SetUpTestCase() { static void SetUpTestCase() {
// The gyro object blocks for 5 seconds in the constructor, so only // The gyro object blocks for 5 seconds in the constructor, so only
// construct it once for the whole test case // construct it once for the whole test case
m_gyro = new Gyro(TestBench::kCameraGyroChannel); m_gyro = new Gyro(TestBench::kCameraGyroChannel);
m_gyro->SetSensitivity(0.0134897058674);
} }
static void TearDownTestCase() { static void TearDownTestCase() {
delete m_gyro; delete m_gyro;
} }
virtual void SetUp() { virtual void SetUp() {
m_tilt = new Servo(TestBench::kCameraTiltChannel); m_tilt = new Servo(TestBench::kCameraTiltChannel);
m_pan = new Servo(TestBench::kCameraPanChannel); m_pan = new Servo(TestBench::kCameraPanChannel);
m_spiAccel = new ADXL345_SPI(SPI::kOnboardCS0); m_spiAccel = new ADXL345_SPI(SPI::kOnboardCS0);
} }
virtual void TearDown() { virtual void TearDown() {
delete m_tilt; delete m_tilt;
delete m_pan; delete m_pan;
@@ -58,17 +59,21 @@ Gyro *TiltPanCameraTest::m_gyro = 0;
* Test if the servo turns 180 degrees and the gyroscope measures this angle * Test if the servo turns 180 degrees and the gyroscope measures this angle
*/ */
TEST_F(TiltPanCameraTest, GyroAngle) { TEST_F(TiltPanCameraTest, GyroAngle) {
m_pan->Set(0.0f);
Wait(kServoResetTime); Wait(kServoResetTime);
for(int i = 0; i < 100; i++) { m_gyro->Reset();
m_pan->Set(i / 100.0);
for(int i = 0; i < 180; i++) {
m_pan->SetAngle(i);
Wait(0.05); Wait(0.05);
} }
double gyroAngle = m_gyro->GetAngle(); double gyroAngle = m_gyro->GetAngle();
EXPECT_NEAR(gyroAngle, kTestAngle, 20.0) << "Gyro measured " << gyroAngle EXPECT_NEAR(gyroAngle, kTestAngle, 20.0) << "Gyro measured " << gyroAngle
<< " degrees, servo should have turned " << kTestAngle << " degrees"; << " degrees, servo should have turned " << kTestAngle << " degrees";
} }
/** /**
@@ -78,19 +83,19 @@ TEST_F(TiltPanCameraTest, GyroAngle) {
TEST_F(TiltPanCameraTest, SPIAccelerometer) { TEST_F(TiltPanCameraTest, SPIAccelerometer) {
m_tilt->Set(kTiltSetpoint0); m_tilt->Set(kTiltSetpoint0);
Wait(kTiltTime); Wait(kTiltTime);
EXPECT_NEAR(-1.0, m_spiAccel->GetAcceleration(ADXL345_SPI::kAxis_X), kAccelerometerTolerance); EXPECT_NEAR(-1.0, m_spiAccel->GetX(), kAccelerometerTolerance);
EXPECT_NEAR(0.0, m_spiAccel->GetAcceleration(ADXL345_SPI::kAxis_Y), kAccelerometerTolerance); EXPECT_NEAR(0.0, m_spiAccel->GetY(), kAccelerometerTolerance);
EXPECT_NEAR(0.0, m_spiAccel->GetAcceleration(ADXL345_SPI::kAxis_Z), kAccelerometerTolerance); EXPECT_NEAR(0.0, m_spiAccel->GetZ(), kAccelerometerTolerance);
m_tilt->Set(kTiltSetpoint45); m_tilt->Set(kTiltSetpoint45);
Wait(kTiltTime); Wait(kTiltTime);
EXPECT_NEAR(-std::sqrt(0.5), m_spiAccel->GetAcceleration(ADXL345_SPI::kAxis_X), kAccelerometerTolerance); EXPECT_NEAR(-std::sqrt(0.5), m_spiAccel->GetX(), kAccelerometerTolerance);
EXPECT_NEAR(0.0, m_spiAccel->GetAcceleration(ADXL345_SPI::kAxis_Y), kAccelerometerTolerance); EXPECT_NEAR(0.0, m_spiAccel->GetY(), kAccelerometerTolerance);
EXPECT_NEAR(std::sqrt(0.5), m_spiAccel->GetAcceleration(ADXL345_SPI::kAxis_Z), kAccelerometerTolerance); EXPECT_NEAR(std::sqrt(0.5), m_spiAccel->GetZ(), kAccelerometerTolerance);
m_tilt->Set(kTiltSetpoint90); m_tilt->Set(kTiltSetpoint90);
Wait(kTiltTime); Wait(kTiltTime);
EXPECT_NEAR(0.0, m_spiAccel->GetAcceleration(ADXL345_SPI::kAxis_X), kAccelerometerTolerance); EXPECT_NEAR(0.0, m_spiAccel->GetX(), kAccelerometerTolerance);
EXPECT_NEAR(0.0, m_spiAccel->GetAcceleration(ADXL345_SPI::kAxis_Y), kAccelerometerTolerance); EXPECT_NEAR(0.0, m_spiAccel->GetY(), kAccelerometerTolerance);
EXPECT_NEAR(1.0, m_spiAccel->GetAcceleration(ADXL345_SPI::kAxis_Z), kAccelerometerTolerance); EXPECT_NEAR(1.0, m_spiAccel->GetZ(), kAccelerometerTolerance);
} }