Skip to content

Commit

Permalink
1. Added more intermediary tests
Browse files Browse the repository at this point in the history
2. Added release and debug modes
  • Loading branch information
Shobuj-Paul committed Feb 24, 2024
1 parent 384c878 commit b70ac88
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 23 deletions.
12 changes: 11 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ project(bemf_observer LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wpedantic -Werror")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wpedantic -Werror -Wold-style-cast")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS} -g")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} -O3")

include(CTest)
enable_testing()
Expand All @@ -31,6 +33,14 @@ add_executable(bemf_observer_test tests/bemf_observer_test.cpp)
target_link_libraries(bemf_observer_test ${PROJECT_NAME})
add_test(NAME bemf_observer_test COMMAND "${CMAKE_SOURCE_DIR}/bin/bemf_observer_test")

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

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

# Include Header files
include_directories(
include
Expand Down
49 changes: 35 additions & 14 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ PROJECT_DIR="$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)"

# Path to the build directory.
BUILD_DIR=${PROJECT_DIR}/build
BIN_DIR=${PROJECT_DIR}/bin
if [ -d "$BUILD_DIR" ]; then
echo "Build directory exists."
else
echo "Build directory does not exist."
echo "Creating build directory..."
mkdir -p ${BUILD_DIR}
fi

# Check if --clean flag is passed
for arg in "$@"
Expand All @@ -20,28 +26,43 @@ do
rm -rf ${BIN_DIR}
exit 0
;;
--show-failed)
SHOW_FAILED=1
;;
--debug)
DEBUG=1
;;
--release)
RELEASE=1
;;
esac
done

# Path to the build directory.
BUILD_DIR=${PROJECT_DIR}/build
if [ -d "$BUILD_DIR" ]; then
echo "Build directory exists."
# Configure and build the project.
cd ${BUILD_DIR}
if [ -z "$DEBUG" ]; then
cmake .. -DCMAKE_BUILD_TYPE=Debug ..
make -j4
elif [ -z "$RELEASE" ]; then
cmake .. -DCMAKE_BUILD_TYPE=Release ..
make -j4
else
echo "Build directory does not exist."
echo "Creating build directory..."
mkdir -p ${BUILD_DIR}
cmake ..
make -j4
fi
cd ${BUILD_DIR}

# Configure and build the project.
cmake ..
make -j4

# If make completes successfully, run ctest
if [ $? -eq 0 ]; then
cd ${BUILD_DIR}
ctest -C Release
if [ -z "$SHOW_FAILED" ]; then
ctest -C Debug --rerun-failed --output-on-failure
elif [ -z "$DEBUG" ]; then
ctest -C Debug --output-on-failure
elif [ -z "$RELEASE" ]; then
ctest -C Release
else
ctest
fi
else
echo -e "\nMake failed, so ctest will not be run."
fi
Expand Down
13 changes: 9 additions & 4 deletions src/observers/dq_update.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@

observers::DQUpdate::DQUpdate() : I_prev(0, 0), X_prev(0, 0), E(0, 0)
{
d_axis = std::make_unique<controllers::PIController>();
q_axis = std::make_unique<controllers::PIController>();
}

observers::DQUpdate::DQUpdate(controllers::PIConfig config, BemfGains gains) : I_prev(0, 0), X_prev(0, 0), E(0, 0), config(config), gains(gains)
observers::DQUpdate::DQUpdate(controllers::PIConfig config, BemfGains gains)
: I_prev(0, 0), X_prev(0, 0), E(0, 0), config(config), gains(gains)
{
d_axis = std::make_unique<controllers::PIController>();
q_axis = std::make_unique<controllers::PIController>();
}

float observers::DQUpdate::loop(math::FrameAlphaBeta currents, math::FrameAlphaBeta voltages, float angular_velocity,
float rotor_angle, const SetBemfParams& set_params, const ExtBemfParams& ext_params)
float rotor_angle, const SetBemfParams& set_params, const ExtBemfParams& ext_params)
{
math::FrameDQ I_est, error, X;

Expand All @@ -36,8 +41,8 @@ float observers::DQUpdate::loop(math::FrameAlphaBeta currents, math::FrameAlphaB
if (set_params.error_q)
error.q = ext_params.error_q;

E.d = q_axis.loop(error.d, config, set_params.error_sum_d, ext_params.error_sum_d);
E.q = d_axis.loop(error.q, config, set_params.error_sum_q, ext_params.error_sum_q);
E.d = q_axis->loop(error.d, config, set_params.error_sum_d, ext_params.error_sum_d);
E.q = d_axis->loop(error.q, config, set_params.error_sum_q, ext_params.error_sum_q);

X_prev = X;
I_prev = I_est;
Expand Down
9 changes: 5 additions & 4 deletions src/observers/tracker.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#include <observers/tracker.hpp>

float observers::Tracker::loop(float phase_error, const SetTrackerParams& set_params,
const ExtTrackerParams& ext_params)
float observers::Tracker::loop(float phase_error, const SetTrackerParams& set_params, const ExtTrackerParams& ext_params)
{
if (set_params.error)
phase_error = ext_params.error;
angle_est += config.Ts * angle_controller.loop(phase_error, config, set_params.speed, ext_params.speed);
angle_est += config.Ts * angle_controller->loop(phase_error, config, set_params.speed, ext_params.speed);
if (set_params.etheta)
angle_est = ext_params.etheta;
angle_est = math::wrapAngle(angle_est);
Expand All @@ -14,9 +13,11 @@ float observers::Tracker::loop(float phase_error, const SetTrackerParams& set_pa

observers::Tracker::Tracker(controllers::PIConfig config) : config(config)
{
angle_controller = std::make_unique<controllers::PIController>();
angle_integrator = std::make_unique<math::integrator>();
}

float observers::Tracker::speed_tracker(float angle_est, float Ts)
{
return angle_integrator.loop(angle_est, Ts);
return angle_integrator->loop(angle_est, Ts);
}

0 comments on commit b70ac88

Please sign in to comment.