Skip to content

Commit

Permalink
Added PID test
Browse files Browse the repository at this point in the history
  • Loading branch information
Shobuj-Paul committed Feb 13, 2024
1 parent 1c12543 commit fde25f3
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 6 deletions.
14 changes: 9 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ enable_testing()

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)

## Add C++ executables here
add_executable(pi_test tests/pi_test.cpp)
target_link_libraries(pi_test ${PROJECT_NAME})
add_test(NAME pi_test COMMAND "${CMAKE_SOURCE_DIR}/bin/pi_test")

# Declare a C++ library
add_library(bemf_observer
src/controllers/pid_controller.cpp
Expand All @@ -27,6 +22,15 @@ add_library(bemf_observer
src/observers/dq_update.cpp
)

## Add C++ tests here
add_executable(pi_test tests/pi_test.cpp)
target_link_libraries(pi_test ${PROJECT_NAME})
add_test(NAME pi_test COMMAND "${CMAKE_SOURCE_DIR}/bin/pi_test")

add_executable(pid_test tests/pid_test.cpp)
target_link_libraries(pid_test ${PROJECT_NAME})
add_test(NAME pid_test COMMAND "${CMAKE_SOURCE_DIR}/bin/pid_test")

# Include Header files
include_directories(
include
Expand Down
1 change: 1 addition & 0 deletions include/math/functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace math
{
float abs(float input);
float pow(float input, int power);
float wrap_angle(float input);
float sin(float angle);
Expand Down
8 changes: 8 additions & 0 deletions src/math/functions.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
#include <math/functions.hpp>

float math::abs(float input)
{
if (input < 0)
return -input;
else
return input;
}

float math::pow(float input, int power)
{
float output = 1;
Expand Down
6 changes: 5 additions & 1 deletion tests/pi_test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <iostream>
#include <controllers/pi_controller.hpp>
#include <math/functions.hpp>
#define Ts 0.01

using namespace controllers;
Expand All @@ -12,7 +13,10 @@ int main()
std::cout << " Starting loop" << std::endl;
for (float t = 0; t < 10; t += Ts)
{
x_meas += Ts * controller.loop(x_ref - x_meas, config);
float error = x_ref - x_meas;
if(math::abs(error) < 0.1 && t>5)
break;
x_meas += Ts * controller.loop(error, config);
std::cout << "t = " << t << " x = " << x_meas << std::endl;
}
}
22 changes: 22 additions & 0 deletions tests/pid_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <iostream>
#include <controllers/pid_controller.hpp>
#include <math/functions.hpp>
#define Ts 0.01

using namespace controllers;

int main()
{
float x_meas = 0, x_ref = 10;
pid_config config = { 2, 1, 0.01, Ts, 10, -10 };
pid_controller controller;
std::cout << " Starting loop" << std::endl;
for (float t = 0; t < 10; t += Ts)
{
float error = x_ref - x_meas;
if(math::abs(error) < 0.1 && t>5)
break;
x_meas += Ts * controller.loop(error, config);
std::cout << "t = " << t << " x = " << x_meas << std::endl;
}
}

0 comments on commit fde25f3

Please sign in to comment.