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 | |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame] | 5 | #include <gtest/gtest.h> |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 6 | #include <hal/HAL.h> |
| 7 | |
| 8 | #include "CrossConnects.h" |
| 9 | #include "LifetimeWrappers.h" |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 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 |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame] | 24 | HAL_SetPWMPulseTimeMicroseconds(pwmHandle, 0, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 25 | ASSERT_EQ(0, status); |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame] | 26 | HAL_SetPWMConfigMicroseconds(pwmHandle, 2000, 1000, 1000, 0, 0, &status); |
| 27 | HAL_SetPWMConfigMicroseconds(pwmHandle, 5050, 2525, 2525, 2525, 0, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 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 | |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame] | 44 | ASSERT_NEAR( |
| 45 | 1000 / 5.05, |
| 46 | static_cast<double>(HAL_GetDutyCycleFrequency(dutyCycle, &status)), 1); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 47 | |
| 48 | // TODO measure output |
| 49 | } |
| 50 | |
| 51 | INSTANTIATE_TEST_SUITE_P(DutyCycleCrossConnTests, DutyCycleTest, |
| 52 | ::testing::ValuesIn(PWMCrossConnects)); |