2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2014-06-02 10:12:08 -04:00
|
|
|
|
2016-09-23 20:27:11 -07:00
|
|
|
#include <cmath>
|
|
|
|
|
|
2023-08-28 15:13:34 -07:00
|
|
|
#include <gtest/gtest.h>
|
2021-05-28 22:06:59 -07:00
|
|
|
#include <units/time.h>
|
|
|
|
|
|
2016-05-25 22:38:11 -07:00
|
|
|
#include "TestBench.h"
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/ADXL345_SPI.h"
|
|
|
|
|
#include "frc/AnalogGyro.h"
|
|
|
|
|
#include "frc/Servo.h"
|
|
|
|
|
#include "frc/Timer.h"
|
2016-05-25 22:38:11 -07:00
|
|
|
|
2014-11-20 16:39:00 -05:00
|
|
|
static constexpr double kTiltSetpoint0 = 0.22;
|
|
|
|
|
static constexpr double kTiltSetpoint45 = 0.45;
|
|
|
|
|
static constexpr double kTiltSetpoint90 = 0.68;
|
2014-07-17 15:43:25 -04:00
|
|
|
|
2014-06-07 17:37:51 -04:00
|
|
|
/**
|
|
|
|
|
* A fixture for the camera with two servos and a gyro
|
|
|
|
|
*/
|
2014-06-02 10:12:08 -04:00
|
|
|
class TiltPanCameraTest : public testing::Test {
|
2015-06-25 15:07:55 -04:00
|
|
|
protected:
|
2021-05-31 10:21:34 -07:00
|
|
|
frc::Servo m_tilt{TestBench::kCameraTiltChannel};
|
|
|
|
|
frc::Servo m_pan{TestBench::kCameraPanChannel};
|
|
|
|
|
frc::ADXL345_SPI m_spiAccel{frc::SPI::kOnboardCS1};
|
2015-06-25 15:07:55 -04:00
|
|
|
|
2021-05-31 10:21:34 -07:00
|
|
|
TiltPanCameraTest() {
|
|
|
|
|
m_tilt.Set(kTiltSetpoint45);
|
|
|
|
|
m_pan.SetAngle(0.0);
|
2015-06-25 15:07:55 -04:00
|
|
|
|
2021-05-31 10:21:34 -07:00
|
|
|
// Wait for servos to reset
|
|
|
|
|
frc::Wait(2_s);
|
|
|
|
|
}
|
2015-06-25 15:07:55 -04:00
|
|
|
|
2021-05-31 10:21:34 -07:00
|
|
|
/**
|
|
|
|
|
* Test if the servo turns 90 degrees and the gyroscope measures this angle
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
void GyroTests(frc::AnalogGyro& gyro) {
|
|
|
|
|
gyro.SetSensitivity(0.013);
|
2015-06-25 15:07:55 -04:00
|
|
|
|
2021-05-31 10:21:34 -07:00
|
|
|
gyro.Reset();
|
2015-06-25 15:07:55 -04:00
|
|
|
|
2021-05-31 10:21:34 -07:00
|
|
|
// Accumulator needs a small amount of time to reset before being tested
|
|
|
|
|
frc::Wait(1_ms);
|
2015-06-25 15:07:55 -04:00
|
|
|
|
2021-05-31 10:21:34 -07:00
|
|
|
// Verify the gyro angle defaults to 0 immediately after being reset
|
|
|
|
|
EXPECT_NEAR(0.0, gyro.GetAngle(), 1.0);
|
2015-07-28 11:57:22 -04:00
|
|
|
|
2021-05-31 10:21:34 -07:00
|
|
|
// Make sure that the gyro doesn't get jerked when the servo goes to zero.
|
|
|
|
|
m_pan.SetAngle(0.0);
|
|
|
|
|
frc::Wait(0.5_s);
|
|
|
|
|
gyro.Reset();
|
2014-06-02 10:12:08 -04:00
|
|
|
|
2021-05-31 10:21:34 -07:00
|
|
|
for (int32_t i = 0; i < 600; i++) {
|
|
|
|
|
m_pan.Set(i / 1000.0);
|
|
|
|
|
frc::Wait(1_ms);
|
|
|
|
|
}
|
2014-06-02 10:12:08 -04:00
|
|
|
|
2021-05-31 10:21:34 -07:00
|
|
|
double gyroAngle = gyro.GetAngle();
|
2014-07-31 15:46:14 -04:00
|
|
|
|
2021-05-31 10:21:34 -07:00
|
|
|
EXPECT_NEAR(gyroAngle, 90.0, 10.0)
|
|
|
|
|
<< "Gyro measured " << gyroAngle
|
|
|
|
|
<< " degrees, servo should have turned 90 degrees";
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
2021-05-31 10:21:34 -07:00
|
|
|
};
|
2014-07-23 09:36:27 -04:00
|
|
|
|
2021-05-31 10:21:34 -07:00
|
|
|
TEST_F(TiltPanCameraTest, Gyro) {
|
|
|
|
|
int cCenter;
|
|
|
|
|
double cOffset;
|
2014-07-23 09:36:27 -04:00
|
|
|
|
2021-05-31 10:21:34 -07:00
|
|
|
{
|
|
|
|
|
frc::AnalogGyro gyro{TestBench::kCameraGyroChannel};
|
|
|
|
|
GyroTests(gyro);
|
2014-06-02 10:12:08 -04:00
|
|
|
|
2021-05-31 10:21:34 -07:00
|
|
|
// Gets calibrated parameters from previously calibrated gyro, allocates a
|
|
|
|
|
// new gyro with the given parameters for center and offset, and re-runs
|
|
|
|
|
// tests on the new gyro.
|
|
|
|
|
cCenter = gyro.GetCenter();
|
|
|
|
|
cOffset = gyro.GetOffset();
|
2015-07-28 11:57:22 -04:00
|
|
|
}
|
|
|
|
|
|
2021-05-31 10:21:34 -07:00
|
|
|
frc::AnalogGyro gyro{TestBench::kCameraGyroChannel, cCenter, cOffset};
|
|
|
|
|
GyroTests(gyro);
|
2015-07-28 11:57:22 -04:00
|
|
|
}
|
|
|
|
|
|
2014-07-17 15:43:25 -04:00
|
|
|
/**
|
|
|
|
|
* Test if the accelerometer measures gravity along the correct axes when the
|
|
|
|
|
* camera rotates
|
|
|
|
|
*/
|
|
|
|
|
TEST_F(TiltPanCameraTest, SPIAccelerometer) {
|
2021-05-31 10:21:34 -07:00
|
|
|
static constexpr auto kTiltTime = 1_s;
|
|
|
|
|
static constexpr double kAccelerometerTolerance = 0.2;
|
|
|
|
|
|
|
|
|
|
m_tilt.Set(kTiltSetpoint0);
|
|
|
|
|
frc::Wait(kTiltTime);
|
|
|
|
|
EXPECT_NEAR(-1.0, m_spiAccel.GetX(), kAccelerometerTolerance);
|
|
|
|
|
EXPECT_NEAR(0.0, m_spiAccel.GetY(), kAccelerometerTolerance);
|
|
|
|
|
EXPECT_NEAR(0.0, m_spiAccel.GetZ(), kAccelerometerTolerance);
|
|
|
|
|
|
|
|
|
|
m_tilt.Set(kTiltSetpoint45);
|
|
|
|
|
frc::Wait(kTiltTime);
|
|
|
|
|
EXPECT_NEAR(-std::sqrt(0.5), m_spiAccel.GetX(), kAccelerometerTolerance);
|
|
|
|
|
EXPECT_NEAR(0.0, m_spiAccel.GetY(), kAccelerometerTolerance);
|
|
|
|
|
EXPECT_NEAR(std::sqrt(0.5), m_spiAccel.GetZ(), kAccelerometerTolerance);
|
|
|
|
|
|
|
|
|
|
m_tilt.Set(kTiltSetpoint90);
|
|
|
|
|
frc::Wait(kTiltTime);
|
|
|
|
|
EXPECT_NEAR(0.0, m_spiAccel.GetX(), kAccelerometerTolerance);
|
|
|
|
|
EXPECT_NEAR(0.0, m_spiAccel.GetY(), kAccelerometerTolerance);
|
|
|
|
|
EXPECT_NEAR(1.0, m_spiAccel.GetZ(), kAccelerometerTolerance);
|
2014-07-17 15:43:25 -04:00
|
|
|
}
|