Skip to content

Commit

Permalink
Add PID class tests
Browse files Browse the repository at this point in the history
Signed-off-by: Javier Balloffet <[email protected]>
  • Loading branch information
jballoffet committed Feb 10, 2024
1 parent 91e6029 commit e7b9ade
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 4 deletions.
5 changes: 5 additions & 0 deletions andino_firmware/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
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(true);

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(true);

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(true);

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(true);

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(true);

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(true);

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

0 comments on commit e7b9ade

Please sign in to comment.