mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
Add SpeedControllerGroup (#362)
This commit is contained in:
committed by
Peter Johnson
parent
24752a9751
commit
877a9eae1f
@@ -0,0 +1,28 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2017 FIRST. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#include "MockSpeedController.h"
|
||||
|
||||
using namespace frc;
|
||||
|
||||
void MockSpeedController::Set(double speed) {
|
||||
m_speed = m_isInverted ? -speed : speed;
|
||||
}
|
||||
|
||||
double MockSpeedController::Get() const { return m_speed; }
|
||||
|
||||
void MockSpeedController::SetInverted(bool isInverted) {
|
||||
m_isInverted = isInverted;
|
||||
}
|
||||
|
||||
bool MockSpeedController::GetInverted() const { return m_isInverted; }
|
||||
|
||||
void MockSpeedController::Disable() { m_speed = 0; }
|
||||
|
||||
void MockSpeedController::StopMotor() { Disable(); }
|
||||
|
||||
void MockSpeedController::PIDWrite(double output) { Set(output); }
|
||||
@@ -0,0 +1,137 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2017 FIRST. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#include "SpeedControllerGroup.h" // NOLINT(build/include_order)
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "MockSpeedController.h"
|
||||
#include "TestBench.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
using namespace frc;
|
||||
|
||||
enum SpeedControllerGroupTestType { TEST_ONE, TEST_TWO, TEST_THREE };
|
||||
|
||||
std::ostream& operator<<(std::ostream& os,
|
||||
const SpeedControllerGroupTestType& type) {
|
||||
switch (type) {
|
||||
case TEST_ONE:
|
||||
os << "SpeedControllerGroup with one speed controller";
|
||||
break;
|
||||
case TEST_TWO:
|
||||
os << "SpeedControllerGroup with two speed controllers";
|
||||
break;
|
||||
case TEST_THREE:
|
||||
os << "SpeedControllerGroup with three speed controllers";
|
||||
break;
|
||||
}
|
||||
|
||||
return os;
|
||||
}
|
||||
|
||||
/**
|
||||
* A fixture used for SpeedControllerGroup testing.
|
||||
*/
|
||||
class SpeedControllerGroupTest
|
||||
: public testing::TestWithParam<SpeedControllerGroupTestType> {
|
||||
protected:
|
||||
std::vector<MockSpeedController> m_speedControllers;
|
||||
std::unique_ptr<SpeedControllerGroup> m_group;
|
||||
|
||||
void SetUp() override {
|
||||
switch (GetParam()) {
|
||||
case TEST_ONE: {
|
||||
m_speedControllers.emplace_back();
|
||||
m_group = std::make_unique<SpeedControllerGroup>(m_speedControllers[0]);
|
||||
break;
|
||||
}
|
||||
|
||||
case TEST_TWO: {
|
||||
m_speedControllers.emplace_back();
|
||||
m_speedControllers.emplace_back();
|
||||
m_group = std::make_unique<SpeedControllerGroup>(m_speedControllers[0],
|
||||
m_speedControllers[1]);
|
||||
break;
|
||||
}
|
||||
|
||||
case TEST_THREE: {
|
||||
m_speedControllers.emplace_back();
|
||||
m_speedControllers.emplace_back();
|
||||
m_speedControllers.emplace_back();
|
||||
m_group = std::make_unique<SpeedControllerGroup>(m_speedControllers[0],
|
||||
m_speedControllers[1],
|
||||
m_speedControllers[2]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
TEST_P(SpeedControllerGroupTest, Set) {
|
||||
m_group->Set(1.0);
|
||||
|
||||
for (auto& speedController : m_speedControllers) {
|
||||
EXPECT_FLOAT_EQ(speedController.Get(), 1.0);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_P(SpeedControllerGroupTest, GetInverted) {
|
||||
m_group->SetInverted(true);
|
||||
|
||||
EXPECT_TRUE(m_group->GetInverted());
|
||||
}
|
||||
|
||||
TEST_P(SpeedControllerGroupTest, SetInvertedDoesNotModifySpeedControllers) {
|
||||
for (auto& speedController : m_speedControllers) {
|
||||
speedController.SetInverted(false);
|
||||
}
|
||||
m_group->SetInverted(true);
|
||||
|
||||
for (auto& speedController : m_speedControllers) {
|
||||
EXPECT_EQ(speedController.GetInverted(), false);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_P(SpeedControllerGroupTest, SetInvertedDoesInvert) {
|
||||
m_group->SetInverted(true);
|
||||
m_group->Set(1.0);
|
||||
|
||||
for (auto& speedController : m_speedControllers) {
|
||||
EXPECT_FLOAT_EQ(speedController.Get(), -1.0);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_P(SpeedControllerGroupTest, Disable) {
|
||||
m_group->Set(1.0);
|
||||
m_group->Disable();
|
||||
|
||||
for (auto& speedController : m_speedControllers) {
|
||||
EXPECT_FLOAT_EQ(speedController.Get(), 0.0);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_P(SpeedControllerGroupTest, StopMotor) {
|
||||
m_group->Set(1.0);
|
||||
m_group->StopMotor();
|
||||
|
||||
for (auto& speedController : m_speedControllers) {
|
||||
EXPECT_FLOAT_EQ(speedController.Get(), 0.0);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_P(SpeedControllerGroupTest, PIDWrite) {
|
||||
m_group->PIDWrite(1.0);
|
||||
|
||||
for (auto& speedController : m_speedControllers) {
|
||||
EXPECT_FLOAT_EQ(speedController.Get(), 1.0);
|
||||
}
|
||||
}
|
||||
|
||||
INSTANTIATE_TEST_CASE_P(Test, SpeedControllerGroupTest,
|
||||
testing::Values(TEST_ONE, TEST_TWO, TEST_THREE));
|
||||
Reference in New Issue
Block a user