blob: bd7373919a27f3c172dd3c70c573adc0890fcab7 [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
Austin Schuh812d0d12021-11-04 20:16:48 -07005#include "frc/PowerDistribution.h" // NOLINT(build/include_order)
Brian Silverman8fce7482020-01-05 13:18:21 -08006
James Kuszmaulb13e13f2023-11-22 20:44:04 -08007#include <gtest/gtest.h>
Brian Silverman8fce7482020-01-05 13:18:21 -08008#include <hal/Ports.h>
Austin Schuh812d0d12021-11-04 20:16:48 -07009#include <units/time.h>
Brian Silverman8fce7482020-01-05 13:18:21 -080010
11#include "TestBench.h"
Brian Silverman8fce7482020-01-05 13:18:21 -080012#include "frc/Timer.h"
Austin Schuh812d0d12021-11-04 20:16:48 -070013#include "frc/motorcontrol/Talon.h"
Brian Silverman8fce7482020-01-05 13:18:21 -080014
Austin Schuh812d0d12021-11-04 20:16:48 -070015static constexpr auto kMotorTime = 0.25_s;
Brian Silverman8fce7482020-01-05 13:18:21 -080016
Austin Schuh812d0d12021-11-04 20:16:48 -070017class PowerDistributionTest : public testing::Test {
Brian Silverman8fce7482020-01-05 13:18:21 -080018 protected:
Austin Schuh812d0d12021-11-04 20:16:48 -070019 frc::PowerDistribution m_pdp;
20 frc::Talon m_talon{TestBench::kTalonChannel};
Brian Silverman8fce7482020-01-05 13:18:21 -080021};
22
Austin Schuh812d0d12021-11-04 20:16:48 -070023TEST_F(PowerDistributionTest, CheckRepeatedCalls) {
24 auto numChannels = HAL_GetNumCTREPDPChannels();
Brian Silverman8fce7482020-01-05 13:18:21 -080025 // 1 second
26 for (int i = 0; i < 50; i++) {
27 for (int j = 0; j < numChannels; j++) {
Austin Schuh812d0d12021-11-04 20:16:48 -070028 m_pdp.GetCurrent(j);
Brian Silverman8fce7482020-01-05 13:18:21 -080029 }
Austin Schuh812d0d12021-11-04 20:16:48 -070030 m_pdp.GetVoltage();
Brian Silverman8fce7482020-01-05 13:18:21 -080031 }
Austin Schuh812d0d12021-11-04 20:16:48 -070032 frc::Wait(20_ms);
Brian Silverman8fce7482020-01-05 13:18:21 -080033}
34
35/**
36 * Test if the current changes when the motor is driven using a talon
37 */
Austin Schuh812d0d12021-11-04 20:16:48 -070038TEST_F(PowerDistributionTest, CheckCurrentTalon) {
39 frc::Wait(kMotorTime);
Brian Silverman8fce7482020-01-05 13:18:21 -080040
41 /* The Current should be 0 */
Austin Schuh812d0d12021-11-04 20:16:48 -070042 EXPECT_FLOAT_EQ(0, m_pdp.GetCurrent(TestBench::kTalonPDPChannel))
Brian Silverman8fce7482020-01-05 13:18:21 -080043 << "The Talon current was non-zero";
44
45 /* Set the motor to full forward */
Austin Schuh812d0d12021-11-04 20:16:48 -070046 m_talon.Set(1.0);
47 frc::Wait(kMotorTime);
Brian Silverman8fce7482020-01-05 13:18:21 -080048
49 /* The current should now be positive */
Austin Schuh812d0d12021-11-04 20:16:48 -070050 ASSERT_GT(m_pdp.GetCurrent(TestBench::kTalonPDPChannel), 0)
Brian Silverman8fce7482020-01-05 13:18:21 -080051 << "The Talon current was not positive";
52}