blob: 42e52fc9b0924b80883aa31bb5485640e4bb1e02 [file] [log] [blame]
Austin Schuh812d0d12021-11-04 20:16:48 -07001// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
Brian Silverman8fce7482020-01-05 13:18:21 -08004
5#include <cmath>
6
Austin Schuh812d0d12021-11-04 20:16:48 -07007#include <units/time.h>
8
Brian Silverman8fce7482020-01-05 13:18:21 -08009#include "TestBench.h"
10#include "frc/ADXL345_SPI.h"
11#include "frc/AnalogGyro.h"
12#include "frc/Servo.h"
13#include "frc/Timer.h"
14#include "gtest/gtest.h"
15
Brian Silverman8fce7482020-01-05 13:18:21 -080016static constexpr double kTiltSetpoint0 = 0.22;
17static constexpr double kTiltSetpoint45 = 0.45;
18static constexpr double kTiltSetpoint90 = 0.68;
Brian Silverman8fce7482020-01-05 13:18:21 -080019
20/**
21 * A fixture for the camera with two servos and a gyro
22 */
23class TiltPanCameraTest : public testing::Test {
24 protected:
Austin Schuh812d0d12021-11-04 20:16:48 -070025 frc::Servo m_tilt{TestBench::kCameraTiltChannel};
26 frc::Servo m_pan{TestBench::kCameraPanChannel};
27 frc::ADXL345_SPI m_spiAccel{frc::SPI::kOnboardCS1};
Brian Silverman8fce7482020-01-05 13:18:21 -080028
Austin Schuh812d0d12021-11-04 20:16:48 -070029 TiltPanCameraTest() {
30 m_tilt.Set(kTiltSetpoint45);
31 m_pan.SetAngle(0.0);
32
33 // Wait for servos to reset
34 frc::Wait(2_s);
Brian Silverman8fce7482020-01-05 13:18:21 -080035 }
36
Austin Schuh812d0d12021-11-04 20:16:48 -070037 /**
38 * Test if the servo turns 90 degrees and the gyroscope measures this angle
39 * Note servo on TestBench is not the same type of servo that servo class
40 * was designed for so setAngle is significantly off. This has been calibrated
41 * for the servo on the rig.
42 */
43 void GyroTests(frc::AnalogGyro& gyro) {
44 gyro.SetSensitivity(0.013);
Brian Silverman8fce7482020-01-05 13:18:21 -080045
Austin Schuh812d0d12021-11-04 20:16:48 -070046 gyro.Reset();
Brian Silverman8fce7482020-01-05 13:18:21 -080047
Austin Schuh812d0d12021-11-04 20:16:48 -070048 // Accumulator needs a small amount of time to reset before being tested
49 frc::Wait(1_ms);
Brian Silverman8fce7482020-01-05 13:18:21 -080050
Austin Schuh812d0d12021-11-04 20:16:48 -070051 // Verify the gyro angle defaults to 0 immediately after being reset
52 EXPECT_NEAR(0.0, gyro.GetAngle(), 1.0);
Brian Silverman8fce7482020-01-05 13:18:21 -080053
Austin Schuh812d0d12021-11-04 20:16:48 -070054 // Make sure that the gyro doesn't get jerked when the servo goes to zero.
55 m_pan.SetAngle(0.0);
56 frc::Wait(0.5_s);
57 gyro.Reset();
Brian Silverman8fce7482020-01-05 13:18:21 -080058
Austin Schuh812d0d12021-11-04 20:16:48 -070059 for (int32_t i = 0; i < 600; i++) {
60 m_pan.Set(i / 1000.0);
61 frc::Wait(1_ms);
62 }
Brian Silverman8fce7482020-01-05 13:18:21 -080063
Austin Schuh812d0d12021-11-04 20:16:48 -070064 double gyroAngle = gyro.GetAngle();
65
66 EXPECT_NEAR(gyroAngle, 90.0, 10.0)
67 << "Gyro measured " << gyroAngle
68 << " degrees, servo should have turned 90 degrees";
Brian Silverman8fce7482020-01-05 13:18:21 -080069 }
70};
71
Austin Schuh812d0d12021-11-04 20:16:48 -070072TEST_F(TiltPanCameraTest, Gyro) {
73 int cCenter;
74 double cOffset;
Brian Silverman8fce7482020-01-05 13:18:21 -080075
Austin Schuh812d0d12021-11-04 20:16:48 -070076 {
77 frc::AnalogGyro gyro{TestBench::kCameraGyroChannel};
78 GyroTests(gyro);
Brian Silverman8fce7482020-01-05 13:18:21 -080079
Austin Schuh812d0d12021-11-04 20:16:48 -070080 // Gets calibrated parameters from previously calibrated gyro, allocates a
81 // new gyro with the given parameters for center and offset, and re-runs
82 // tests on the new gyro.
83 cCenter = gyro.GetCenter();
84 cOffset = gyro.GetOffset();
Brian Silverman8fce7482020-01-05 13:18:21 -080085 }
86
Austin Schuh812d0d12021-11-04 20:16:48 -070087 frc::AnalogGyro gyro{TestBench::kCameraGyroChannel, cCenter, cOffset};
88 GyroTests(gyro);
Brian Silverman8fce7482020-01-05 13:18:21 -080089}
90
91/**
92 * Test if the accelerometer measures gravity along the correct axes when the
93 * camera rotates
94 */
95TEST_F(TiltPanCameraTest, SPIAccelerometer) {
Austin Schuh812d0d12021-11-04 20:16:48 -070096 static constexpr auto kTiltTime = 1_s;
97 static constexpr double kAccelerometerTolerance = 0.2;
Brian Silverman8fce7482020-01-05 13:18:21 -080098
Austin Schuh812d0d12021-11-04 20:16:48 -070099 m_tilt.Set(kTiltSetpoint0);
100 frc::Wait(kTiltTime);
101 EXPECT_NEAR(-1.0, m_spiAccel.GetX(), kAccelerometerTolerance);
102 EXPECT_NEAR(0.0, m_spiAccel.GetY(), kAccelerometerTolerance);
103 EXPECT_NEAR(0.0, m_spiAccel.GetZ(), kAccelerometerTolerance);
Brian Silverman8fce7482020-01-05 13:18:21 -0800104
Austin Schuh812d0d12021-11-04 20:16:48 -0700105 m_tilt.Set(kTiltSetpoint45);
106 frc::Wait(kTiltTime);
107 EXPECT_NEAR(-std::sqrt(0.5), m_spiAccel.GetX(), kAccelerometerTolerance);
108 EXPECT_NEAR(0.0, m_spiAccel.GetY(), kAccelerometerTolerance);
109 EXPECT_NEAR(std::sqrt(0.5), m_spiAccel.GetZ(), kAccelerometerTolerance);
110
111 m_tilt.Set(kTiltSetpoint90);
112 frc::Wait(kTiltTime);
113 EXPECT_NEAR(0.0, m_spiAccel.GetX(), kAccelerometerTolerance);
114 EXPECT_NEAR(0.0, m_spiAccel.GetY(), kAccelerometerTolerance);
115 EXPECT_NEAR(1.0, m_spiAccel.GetZ(), kAccelerometerTolerance);
Brian Silverman8fce7482020-01-05 13:18:21 -0800116}