2020-12-26 14:12:05 -08: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.
|
2017-10-16 22:54:36 -04:00
|
|
|
|
2021-04-17 11:27:16 -07:00
|
|
|
#include "frc/motorcontrol/MotorControllerGroup.h" // NOLINT(build/include_order)
|
2017-10-16 22:54:36 -04:00
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
2023-08-28 15:13:34 -07:00
|
|
|
#include <gtest/gtest.h>
|
2024-01-01 13:37:51 -08:00
|
|
|
#include <wpi/deprecated.h>
|
2023-08-28 15:13:34 -07:00
|
|
|
|
2022-05-21 16:20:26 -07:00
|
|
|
#include "motorcontrol/MockMotorController.h"
|
2017-10-16 22:54:36 -04:00
|
|
|
|
|
|
|
|
using namespace frc;
|
|
|
|
|
|
2021-04-17 11:27:16 -07:00
|
|
|
enum MotorControllerGroupTestType { TEST_ONE, TEST_TWO, TEST_THREE };
|
2017-10-16 22:54:36 -04:00
|
|
|
|
|
|
|
|
std::ostream& operator<<(std::ostream& os,
|
2021-04-17 11:27:16 -07:00
|
|
|
const MotorControllerGroupTestType& type) {
|
2017-10-16 22:54:36 -04:00
|
|
|
switch (type) {
|
|
|
|
|
case TEST_ONE:
|
2022-12-05 23:06:43 -05:00
|
|
|
os << "MotorControllerGroup with one motor controller";
|
2017-10-16 22:54:36 -04:00
|
|
|
break;
|
|
|
|
|
case TEST_TWO:
|
2022-12-05 23:06:43 -05:00
|
|
|
os << "MotorControllerGroup with two motor controllers";
|
2017-10-16 22:54:36 -04:00
|
|
|
break;
|
|
|
|
|
case TEST_THREE:
|
2022-12-05 23:06:43 -05:00
|
|
|
os << "MotorControllerGroup with three motor controllers";
|
2017-10-16 22:54:36 -04:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return os;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-01 13:37:51 -08:00
|
|
|
WPI_IGNORE_DEPRECATED
|
|
|
|
|
|
2017-10-16 22:54:36 -04:00
|
|
|
/**
|
2021-04-17 11:27:16 -07:00
|
|
|
* A fixture used for MotorControllerGroup testing.
|
2017-10-16 22:54:36 -04:00
|
|
|
*/
|
2021-04-17 11:27:16 -07:00
|
|
|
class MotorControllerGroupTest
|
|
|
|
|
: public testing::TestWithParam<MotorControllerGroupTestType> {
|
2017-10-16 22:54:36 -04:00
|
|
|
protected:
|
2022-12-05 23:06:43 -05:00
|
|
|
std::vector<MockMotorController> m_motorControllers;
|
2021-04-17 11:27:16 -07:00
|
|
|
std::unique_ptr<MotorControllerGroup> m_group;
|
2017-10-16 22:54:36 -04:00
|
|
|
|
|
|
|
|
void SetUp() override {
|
|
|
|
|
switch (GetParam()) {
|
|
|
|
|
case TEST_ONE: {
|
2022-12-05 23:06:43 -05:00
|
|
|
m_motorControllers.emplace_back();
|
|
|
|
|
m_group = std::make_unique<MotorControllerGroup>(m_motorControllers[0]);
|
2017-10-16 22:54:36 -04:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case TEST_TWO: {
|
2022-12-05 23:06:43 -05:00
|
|
|
m_motorControllers.emplace_back();
|
|
|
|
|
m_motorControllers.emplace_back();
|
|
|
|
|
m_group = std::make_unique<MotorControllerGroup>(m_motorControllers[0],
|
|
|
|
|
m_motorControllers[1]);
|
2017-10-16 22:54:36 -04:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case TEST_THREE: {
|
2022-12-05 23:06:43 -05:00
|
|
|
m_motorControllers.emplace_back();
|
|
|
|
|
m_motorControllers.emplace_back();
|
|
|
|
|
m_motorControllers.emplace_back();
|
|
|
|
|
m_group = std::make_unique<MotorControllerGroup>(m_motorControllers[0],
|
|
|
|
|
m_motorControllers[1],
|
|
|
|
|
m_motorControllers[2]);
|
2017-10-16 22:54:36 -04:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2021-04-17 11:27:16 -07:00
|
|
|
TEST_P(MotorControllerGroupTest, Set) {
|
2017-10-16 22:54:36 -04:00
|
|
|
m_group->Set(1.0);
|
|
|
|
|
|
2022-12-05 23:06:43 -05:00
|
|
|
for (auto& motorController : m_motorControllers) {
|
|
|
|
|
EXPECT_FLOAT_EQ(motorController.Get(), 1.0);
|
2017-10-16 22:54:36 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-17 11:27:16 -07:00
|
|
|
TEST_P(MotorControllerGroupTest, GetInverted) {
|
2017-10-16 22:54:36 -04:00
|
|
|
m_group->SetInverted(true);
|
|
|
|
|
|
|
|
|
|
EXPECT_TRUE(m_group->GetInverted());
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-17 11:27:16 -07:00
|
|
|
TEST_P(MotorControllerGroupTest, SetInvertedDoesNotModifyMotorControllers) {
|
2022-12-05 23:06:43 -05:00
|
|
|
for (auto& motorController : m_motorControllers) {
|
|
|
|
|
motorController.SetInverted(false);
|
2017-10-16 22:54:36 -04:00
|
|
|
}
|
|
|
|
|
m_group->SetInverted(true);
|
|
|
|
|
|
2022-12-05 23:06:43 -05:00
|
|
|
for (auto& motorController : m_motorControllers) {
|
|
|
|
|
EXPECT_EQ(motorController.GetInverted(), false);
|
2017-10-16 22:54:36 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-17 11:27:16 -07:00
|
|
|
TEST_P(MotorControllerGroupTest, SetInvertedDoesInvert) {
|
2017-10-16 22:54:36 -04:00
|
|
|
m_group->SetInverted(true);
|
|
|
|
|
m_group->Set(1.0);
|
|
|
|
|
|
2022-12-05 23:06:43 -05:00
|
|
|
for (auto& motorController : m_motorControllers) {
|
|
|
|
|
EXPECT_FLOAT_EQ(motorController.Get(), -1.0);
|
2017-10-16 22:54:36 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-17 11:27:16 -07:00
|
|
|
TEST_P(MotorControllerGroupTest, Disable) {
|
2017-10-16 22:54:36 -04:00
|
|
|
m_group->Set(1.0);
|
|
|
|
|
m_group->Disable();
|
|
|
|
|
|
2022-12-05 23:06:43 -05:00
|
|
|
for (auto& motorController : m_motorControllers) {
|
|
|
|
|
EXPECT_FLOAT_EQ(motorController.Get(), 0.0);
|
2017-10-16 22:54:36 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-17 11:27:16 -07:00
|
|
|
TEST_P(MotorControllerGroupTest, StopMotor) {
|
2017-10-16 22:54:36 -04:00
|
|
|
m_group->Set(1.0);
|
|
|
|
|
m_group->StopMotor();
|
|
|
|
|
|
2022-12-05 23:06:43 -05:00
|
|
|
for (auto& motorController : m_motorControllers) {
|
|
|
|
|
EXPECT_FLOAT_EQ(motorController.Get(), 0.0);
|
2017-10-16 22:54:36 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-21 06:12:50 -07:00
|
|
|
INSTANTIATE_TEST_SUITE_P(Tests, MotorControllerGroupTest,
|
2019-05-31 13:43:32 -07:00
|
|
|
testing::Values(TEST_ONE, TEST_TWO, TEST_THREE));
|
2024-01-01 13:37:51 -08:00
|
|
|
|
|
|
|
|
WPI_UNIGNORE_DEPRECATED
|