2021-08-05 22:04:51 -04:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
|
|
|
|
|
2025-11-11 18:05:12 -08:00
|
|
|
#include "wpi/simulation/PowerDistributionSim.hpp"
|
2021-08-05 22:04:51 -04:00
|
|
|
|
2024-09-20 17:43:39 -07:00
|
|
|
#include <vector>
|
|
|
|
|
|
2023-08-28 15:13:34 -07:00
|
|
|
#include <gtest/gtest.h>
|
2021-08-05 22:04:51 -04:00
|
|
|
|
2025-11-07 19:56:21 -05:00
|
|
|
#include "callback_helpers/TestCallbackHelpers.hpp"
|
2025-11-07 19:57:55 -05:00
|
|
|
#include "wpi/hal/HAL.h"
|
2025-11-07 19:56:21 -05:00
|
|
|
#include "wpi/hardware/power/PowerDistribution.hpp"
|
2021-08-05 22:04:51 -04:00
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
namespace wpi::sim {
|
2021-08-05 22:04:51 -04:00
|
|
|
|
|
|
|
|
TEST(PowerDistributionSimTest, Initialize) {
|
|
|
|
|
HAL_Initialize(500, 0);
|
|
|
|
|
PowerDistributionSim sim{2};
|
|
|
|
|
EXPECT_FALSE(sim.GetInitialized());
|
|
|
|
|
|
|
|
|
|
BooleanCallback callback;
|
|
|
|
|
|
|
|
|
|
auto cb = sim.RegisterInitializedCallback(callback.GetCallback(), false);
|
2025-11-07 20:00:05 -05:00
|
|
|
PowerDistribution pdp(0, 2, wpi::PowerDistribution::ModuleType::kCTRE);
|
2021-08-05 22:04:51 -04:00
|
|
|
EXPECT_TRUE(sim.GetInitialized());
|
|
|
|
|
EXPECT_TRUE(callback.WasTriggered());
|
|
|
|
|
EXPECT_TRUE(callback.GetLastValue());
|
|
|
|
|
|
|
|
|
|
callback.Reset();
|
|
|
|
|
sim.SetInitialized(false);
|
|
|
|
|
EXPECT_TRUE(callback.WasTriggered());
|
|
|
|
|
EXPECT_FALSE(callback.GetLastValue());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(PowerDistributionSimTest, SetTemperature) {
|
|
|
|
|
HAL_Initialize(500, 0);
|
2025-11-07 20:00:05 -05:00
|
|
|
PowerDistribution pdp{0, 2, wpi::PowerDistribution::ModuleType::kCTRE};
|
2021-08-05 22:04:51 -04:00
|
|
|
PowerDistributionSim sim(pdp);
|
|
|
|
|
|
|
|
|
|
DoubleCallback callback;
|
|
|
|
|
auto cb = sim.RegisterTemperatureCallback(callback.GetCallback(), false);
|
|
|
|
|
|
|
|
|
|
sim.SetTemperature(35.04);
|
|
|
|
|
EXPECT_EQ(35.04, sim.GetTemperature());
|
|
|
|
|
EXPECT_EQ(35.04, pdp.GetTemperature());
|
|
|
|
|
EXPECT_TRUE(callback.WasTriggered());
|
|
|
|
|
EXPECT_TRUE(callback.GetLastValue());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(PowerDistributionSimTest, SetVoltage) {
|
|
|
|
|
HAL_Initialize(500, 0);
|
2025-11-07 20:00:05 -05:00
|
|
|
PowerDistribution pdp{0, 2, wpi::PowerDistribution::ModuleType::kCTRE};
|
2021-08-05 22:04:51 -04:00
|
|
|
PowerDistributionSim sim(pdp);
|
|
|
|
|
|
|
|
|
|
DoubleCallback callback;
|
|
|
|
|
auto cb = sim.RegisterVoltageCallback(callback.GetCallback(), false);
|
|
|
|
|
|
|
|
|
|
sim.SetVoltage(35.04);
|
|
|
|
|
EXPECT_EQ(35.04, sim.GetVoltage());
|
|
|
|
|
EXPECT_EQ(35.04, pdp.GetVoltage());
|
|
|
|
|
EXPECT_TRUE(callback.WasTriggered());
|
|
|
|
|
EXPECT_TRUE(callback.GetLastValue());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(PowerDistributionSimTest, SetCurrent) {
|
|
|
|
|
HAL_Initialize(500, 0);
|
2025-11-07 20:00:05 -05:00
|
|
|
PowerDistribution pdp{0, 2, wpi::PowerDistribution::ModuleType::kCTRE};
|
2021-08-05 22:04:51 -04:00
|
|
|
PowerDistributionSim sim(pdp);
|
|
|
|
|
|
2021-08-14 11:44:56 -07:00
|
|
|
for (int channel = 0; channel < HAL_GetNumCTREPDPChannels(); ++channel) {
|
2021-08-05 22:04:51 -04:00
|
|
|
DoubleCallback callback;
|
|
|
|
|
auto cb =
|
|
|
|
|
sim.RegisterCurrentCallback(channel, callback.GetCallback(), false);
|
|
|
|
|
|
|
|
|
|
const double kTestCurrent = 35.04 + channel;
|
|
|
|
|
sim.SetCurrent(channel, kTestCurrent);
|
|
|
|
|
EXPECT_EQ(kTestCurrent, sim.GetCurrent(channel));
|
|
|
|
|
EXPECT_EQ(kTestCurrent, pdp.GetCurrent(channel));
|
|
|
|
|
EXPECT_TRUE(callback.WasTriggered());
|
|
|
|
|
EXPECT_TRUE(callback.GetLastValue());
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-05-24 19:31:19 -04:00
|
|
|
|
|
|
|
|
TEST(PowerDistributionSimTest, GetAllCurrents) {
|
|
|
|
|
HAL_Initialize(500, 0);
|
2025-11-07 20:00:05 -05:00
|
|
|
PowerDistribution pdp{0, 2, wpi::PowerDistribution::ModuleType::kRev};
|
2024-05-24 19:31:19 -04:00
|
|
|
PowerDistributionSim sim(pdp);
|
|
|
|
|
|
|
|
|
|
// setup
|
|
|
|
|
for (int channel = 0; channel < pdp.GetNumChannels(); ++channel) {
|
|
|
|
|
const double kTestCurrent = 24 - channel;
|
|
|
|
|
sim.SetCurrent(channel, kTestCurrent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// run it
|
|
|
|
|
std::vector<double> currents = pdp.GetAllCurrents();
|
|
|
|
|
|
|
|
|
|
// verify
|
|
|
|
|
for (int channel = 0; channel < pdp.GetNumChannels(); ++channel) {
|
|
|
|
|
const double kTestCurrent = 24 - channel;
|
|
|
|
|
EXPECT_EQ(kTestCurrent, currents[channel]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
} // namespace wpi::sim
|