Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PID unit tests #217

Merged
merged 1 commit into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions andino_firmware/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ test_build_src = true
build_src_filter =
+<encoder.cpp>
+<motor.cpp>
+<pid.cpp>

; Environment for Arduino Uno.
[env:uno]
Expand Down
104 changes: 100 additions & 4 deletions andino_firmware/test/desktop/test_pid/pid_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,108 @@

#include <gtest/gtest.h>

// TODO(jballoffet): Add unit tests.
TEST(PidTest, ConstructOk) {
andino::PID pid_controller(0, 0, 0, 0, 0, 0);
EXPECT_EQ(1, 1);
namespace andino {
namespace test {
namespace {

TEST(PidTest, Initialize) {
andino::PID pid_controller(1, 0, 0, 1, -100, 100);
int output = 0;

// Controller is disabled by default, so output variable should remain unchanged.
pid_controller.compute(5, output);
EXPECT_EQ(output, 0);
}

TEST(PidTest, ComputeOutputProportionalGain) {
andino::PID pid_controller(3, 0, 0, 1, -100, 100);
int output = 0;
pid_controller.set_setpoint(15);
pid_controller.enable();

pid_controller.compute(5, output);
EXPECT_EQ(output, 30);

pid_controller.compute(10, output);
EXPECT_EQ(output, 60);
}

TEST(PidTest, ComputeOutputProportionalAndDerivativeGain) {
andino::PID pid_controller(3, 2, 0, 1, -100, 100);
int output = 0;
pid_controller.set_setpoint(15);
pid_controller.enable();

pid_controller.compute(5, output);
EXPECT_EQ(output, 20);

pid_controller.compute(10, output);
EXPECT_EQ(output, 50);
}

TEST(PidTest, ComputeOutputProportionalAndIntegralGain) {
andino::PID pid_controller(3, 0, 1, 1, -100, 100);
int output = 0;
pid_controller.set_setpoint(15);
pid_controller.enable();

pid_controller.compute(5, output);
EXPECT_EQ(output, 30);

pid_controller.compute(10, output);
EXPECT_EQ(output, 70);
}

TEST(PidTest, ComputeOutputProportionalDerivativeAndIntegralGain) {
andino::PID pid_controller(3, 2, 1, 1, -100, 100);
int output = 0;
pid_controller.set_setpoint(15);
pid_controller.enable();

pid_controller.compute(5, output);
EXPECT_EQ(output, 20);

pid_controller.compute(10, output);
EXPECT_EQ(output, 60);
}

TEST(PidTest, Reset) {
andino::PID pid_controller(3, 0, 0, 1, -100, 100);
int output = 0;
pid_controller.set_setpoint(15);
pid_controller.enable();

pid_controller.compute(5, output);
EXPECT_EQ(output, 30);

// Controller reset, obtained output should be the same as before.
pid_controller.reset(0);
pid_controller.set_setpoint(15);
pid_controller.compute(5, output);
EXPECT_EQ(output, 30);
}

TEST(PidTest, SetTunings) {
andino::PID pid_controller(3, 0, 0, 1, -100, 100);
int output = 0;
pid_controller.set_setpoint(15);
pid_controller.enable();

pid_controller.compute(5, output);
EXPECT_EQ(output, 30);

// Proportional gain doubled, obtained output should be doubled.
pid_controller.reset(0);
pid_controller.set_setpoint(15);
pid_controller.set_tunings(6, 0, 0, 1);
pid_controller.compute(5, output);
EXPECT_EQ(output, 60);
}

} // namespace
} // namespace test
} // namespace andino

int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
if (RUN_ALL_TESTS()) {
Expand Down
Loading