Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 1 | // 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. |
| 4 | |
| 5 | #include <hal/HAL.h> |
| 6 | |
| 7 | #include "CrossConnects.h" |
| 8 | #include "LifetimeWrappers.h" |
| 9 | #include "gtest/gtest.h" |
| 10 | |
| 11 | using namespace hlt; |
| 12 | |
| 13 | class DutyCycleTest : public ::testing::TestWithParam<std::pair<int, int>> {}; |
| 14 | |
| 15 | TEST_P(DutyCycleTest, DutyCycle) { |
| 16 | auto param = GetParam(); |
| 17 | |
| 18 | int32_t status = 0; |
| 19 | PWMHandle pwmHandle(param.first, &status); |
| 20 | ASSERT_NE(pwmHandle, HAL_kInvalidHandle); |
| 21 | ASSERT_EQ(0, status); |
| 22 | |
| 23 | // Ensure our PWM is disabled, and set up properly |
| 24 | HAL_SetPWMRaw(pwmHandle, 0, &status); |
| 25 | ASSERT_EQ(0, status); |
| 26 | HAL_SetPWMConfig(pwmHandle, 2.0, 1.0, 1.0, 0, 0, &status); |
| 27 | HAL_SetPWMConfig(pwmHandle, 5.05, 2.525, 2.525, 2.525, 0, &status); |
| 28 | ASSERT_EQ(0, status); |
| 29 | HAL_SetPWMPeriodScale(pwmHandle, 0, &status); |
| 30 | ASSERT_EQ(0, status); |
| 31 | |
| 32 | DIOHandle dioHandle{param.second, true, &status}; |
| 33 | ASSERT_EQ(0, status); |
| 34 | |
| 35 | DutyCycleHandle dutyCycle{dioHandle, &status}; |
| 36 | ASSERT_EQ(0, status); |
| 37 | |
| 38 | HAL_SetPWMSpeed(pwmHandle, 0.5, &status); |
| 39 | ASSERT_EQ(0, status); |
| 40 | |
| 41 | // Sleep enough time for the frequency to converge |
| 42 | usleep(3500000); |
| 43 | |
| 44 | ASSERT_NEAR(1000 / 5.05, |
| 45 | (double)HAL_GetDutyCycleFrequency(dutyCycle, &status), 1); |
| 46 | |
| 47 | // TODO measure output |
| 48 | } |
| 49 | |
| 50 | INSTANTIATE_TEST_SUITE_P(DutyCycleCrossConnTests, DutyCycleTest, |
| 51 | ::testing::ValuesIn(PWMCrossConnects)); |