blob: 852429ba478f136f296d6514d3df8624e4499035 [file] [log] [blame]
Brian Silverman41cdd3e2019-01-19 19:48:58 -08001/*----------------------------------------------------------------------------*/
2/* Copyright (c) 2014-2018 FIRST. All Rights Reserved. */
3/* Open Source Software - may be modified and shared by FRC teams. The code */
4/* must be accompanied by the FIRST BSD license file in the root directory of */
5/* the project. */
6/*----------------------------------------------------------------------------*/
7
8#include <cmath>
9
10#include "TestBench.h"
11#include "frc/ADXL345_SPI.h"
12#include "frc/AnalogGyro.h"
13#include "frc/Servo.h"
14#include "frc/Timer.h"
15#include "gtest/gtest.h"
16
17using namespace frc;
18
19static constexpr double kServoResetTime = 2.0;
20
21static constexpr double kTestAngle = 90.0;
22
23static constexpr double kTiltSetpoint0 = 0.22;
24static constexpr double kTiltSetpoint45 = 0.45;
25static constexpr double kTiltSetpoint90 = 0.68;
26static constexpr double kTiltTime = 1.0;
27static constexpr double kAccelerometerTolerance = 0.2;
28static constexpr double kSensitivity = 0.013;
29
30/**
31 * A fixture for the camera with two servos and a gyro
32 */
33class TiltPanCameraTest : public testing::Test {
34 protected:
35 static AnalogGyro* m_gyro;
36 Servo *m_tilt, *m_pan;
37 Accelerometer* m_spiAccel;
38
39 static void SetUpTestCase() {
40 // The gyro object blocks for 5 seconds in the constructor, so only
41 // construct it once for the whole test case
42 m_gyro = new AnalogGyro(TestBench::kCameraGyroChannel);
43 m_gyro->SetSensitivity(kSensitivity);
44 }
45
46 static void TearDownTestCase() { delete m_gyro; }
47
48 void SetUp() override {
49 m_tilt = new Servo(TestBench::kCameraTiltChannel);
50 m_pan = new Servo(TestBench::kCameraPanChannel);
51 m_spiAccel = new ADXL345_SPI(SPI::kOnboardCS1);
52
53 m_tilt->Set(kTiltSetpoint45);
54 m_pan->SetAngle(0.0);
55
56 Wait(kServoResetTime);
57
58 m_gyro->Reset();
59 }
60
61 void DefaultGyroAngle();
62 void GyroAngle();
63 void GyroCalibratedParameters();
64
65 void TearDown() override {
66 delete m_tilt;
67 delete m_pan;
68 delete m_spiAccel;
69 }
70};
71
72AnalogGyro* TiltPanCameraTest::m_gyro = nullptr;
73
74/**
75 * Test if the gyro angle defaults to 0 immediately after being reset.
76 */
77void TiltPanCameraTest::DefaultGyroAngle() {
78 EXPECT_NEAR(0.0, m_gyro->GetAngle(), 1.0);
79}
80
81/**
82 * Test if the servo turns 90 degrees and the gyroscope measures this angle
83 * Note servo on TestBench is not the same type of servo that servo class
84 * was designed for so setAngle is significantly off. This has been calibrated
85 * for the servo on the rig.
86 */
87void TiltPanCameraTest::GyroAngle() {
88 // Make sure that the gyro doesn't get jerked when the servo goes to zero.
89 m_pan->SetAngle(0.0);
90 Wait(0.5);
91 m_gyro->Reset();
92
93 for (int32_t i = 0; i < 600; i++) {
94 m_pan->Set(i / 1000.0);
95 Wait(0.001);
96 }
97
98 double gyroAngle = m_gyro->GetAngle();
99
100 EXPECT_NEAR(gyroAngle, kTestAngle, 10.0)
101 << "Gyro measured " << gyroAngle << " degrees, servo should have turned "
102 << kTestAngle << " degrees";
103}
104
105/**
106 * Gets calibrated parameters from previously calibrated gyro, allocates a new
107 * gyro with the given parameters for center and offset, and re-runs tests on
108 * the new gyro.
109 */
110void TiltPanCameraTest::GyroCalibratedParameters() {
111 uint32_t cCenter = m_gyro->GetCenter();
112 double cOffset = m_gyro->GetOffset();
113 delete m_gyro;
114 m_gyro = new AnalogGyro(TestBench::kCameraGyroChannel, cCenter, cOffset);
115 m_gyro->SetSensitivity(kSensitivity);
116
117 // Default gyro angle test
118 // Accumulator needs a small amount of time to reset before being tested
119 m_gyro->Reset();
120 Wait(0.001);
121 EXPECT_NEAR(0.0, m_gyro->GetAngle(), 1.0);
122
123 // Gyro angle test
124 // Make sure that the gyro doesn't get jerked when the servo goes to zero.
125 m_pan->SetAngle(0.0);
126 Wait(0.5);
127 m_gyro->Reset();
128
129 for (int32_t i = 0; i < 600; i++) {
130 m_pan->Set(i / 1000.0);
131 Wait(0.001);
132 }
133
134 double gyroAngle = m_gyro->GetAngle();
135
136 EXPECT_NEAR(gyroAngle, kTestAngle, 10.0)
137 << "Gyro measured " << gyroAngle << " degrees, servo should have turned "
138 << kTestAngle << " degrees";
139}
140
141/**
142 * Run all gyro tests in one function to make sure they are run in order.
143 */
144TEST_F(TiltPanCameraTest, TestAllGyroTests) {
145 DefaultGyroAngle();
146 GyroAngle();
147 GyroCalibratedParameters();
148}
149
150/**
151 * Test if the accelerometer measures gravity along the correct axes when the
152 * camera rotates
153 */
154TEST_F(TiltPanCameraTest, SPIAccelerometer) {
155 m_tilt->Set(kTiltSetpoint0);
156 Wait(kTiltTime);
157 EXPECT_NEAR(-1.0, m_spiAccel->GetX(), kAccelerometerTolerance);
158 EXPECT_NEAR(0.0, m_spiAccel->GetY(), kAccelerometerTolerance);
159 EXPECT_NEAR(0.0, m_spiAccel->GetZ(), kAccelerometerTolerance);
160
161 m_tilt->Set(kTiltSetpoint45);
162 Wait(kTiltTime);
163 EXPECT_NEAR(-std::sqrt(0.5), m_spiAccel->GetX(), kAccelerometerTolerance);
164 EXPECT_NEAR(0.0, m_spiAccel->GetY(), kAccelerometerTolerance);
165 EXPECT_NEAR(std::sqrt(0.5), m_spiAccel->GetZ(), kAccelerometerTolerance);
166
167 m_tilt->Set(kTiltSetpoint90);
168 Wait(kTiltTime);
169 EXPECT_NEAR(0.0, m_spiAccel->GetX(), kAccelerometerTolerance);
170 EXPECT_NEAR(0.0, m_spiAccel->GetY(), kAccelerometerTolerance);
171 EXPECT_NEAR(1.0, m_spiAccel->GetZ(), kAccelerometerTolerance);
172}