diff --git a/CMakeLists.txt b/CMakeLists.txt index 89dae27..67d3471 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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() diff --git a/include/observers/bemf_observer.hpp b/include/observers/bemf_observer.hpp index 284afd3..10ded9c 100644 --- a/include/observers/bemf_observer.hpp +++ b/include/observers/bemf_observer.hpp @@ -1,9 +1,10 @@ #pragma once +#include +#include #include #include #include -#include namespace observers { @@ -18,18 +19,15 @@ class BemfObserver float speed_prev, angle_prev, Vbus_prev; public: - DQUpdate dq_update; - Tracker tracker; + std::unique_ptr dq_update; + std::unique_ptr tracker; float Ts; BemfObserver(const float Ts = 0); BemfOutput loop(const math::FrameABC& line_currents, const math::FrameABC& line_voltages, - const math::FrameABC& duties, float Vbus, - const SetBemfParams& set_bemf_params, - const SetTrackerParams& set_tracker_params, - const ExtBemfParams& ext_bemf_params, - const ExtTrackerParams& ext_tracker_params, - const uint8_t idle_mode, const uint8_t num_rotor_poles, + const math::FrameABC& duties, float Vbus, const SetBemfParams& set_bemf_params, + const SetTrackerParams& set_tracker_params, const ExtBemfParams& ext_bemf_params, + const ExtTrackerParams& ext_tracker_params, const uint8_t idle_mode, const uint8_t num_rotor_poles, const bool force_bemf, const bool en_dis_6_step_comm); }; } // namespace observers diff --git a/include/observers/dq_update.hpp b/include/observers/dq_update.hpp index 18106e6..1ff92d6 100644 --- a/include/observers/dq_update.hpp +++ b/include/observers/dq_update.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include @@ -68,7 +69,7 @@ struct BemfGains class DQUpdate { math::FrameDQ I_prev, X_prev, E; - controllers::PIController d_axis, q_axis; + std::unique_ptr d_axis, q_axis; public: controllers::PIConfig config; diff --git a/include/observers/tracker.hpp b/include/observers/tracker.hpp index 1f0afab..9f0b3ba 100644 --- a/include/observers/tracker.hpp +++ b/include/observers/tracker.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -31,8 +32,8 @@ struct ExtTrackerParams class Tracker { float speed_est, angle_est; - controllers::PIController angle_controller; - math::integrator angle_integrator; + std::unique_ptr angle_controller; + std::unique_ptr angle_integrator; public: controllers::PIConfig config; diff --git a/src/observers/bemf_observer.cpp b/src/observers/bemf_observer.cpp index bfe4cf4..80176c4 100644 --- a/src/observers/bemf_observer.cpp +++ b/src/observers/bemf_observer.cpp @@ -4,14 +4,12 @@ observers::BemfObserver::BemfObserver(const float Ts) : speed_prev(0), angle_pre { } -observers::BemfOutput observers::BemfObserver::loop(const math::FrameABC& line_currents, const math::FrameABC& line_voltages, - const math::FrameABC& duties, float Vbus, - const SetBemfParams& set_bemf_params, - const SetTrackerParams& set_tracker_params, - const ExtBemfParams& ext_bemf_params, - const ExtTrackerParams& ext_tracker_params, - const uint8_t idle_mode, const uint8_t num_rotor_poles, - const bool force_bemf, const bool en_dis_6_step_comm) +observers::BemfOutput +observers::BemfObserver::loop(const math::FrameABC& line_currents, const math::FrameABC& line_voltages, + const math::FrameABC& duties, float Vbus, const SetBemfParams& set_bemf_params, + const SetTrackerParams& set_tracker_params, const ExtBemfParams& ext_bemf_params, + const ExtTrackerParams& ext_tracker_params, const uint8_t idle_mode, + const uint8_t num_rotor_poles, const bool force_bemf, const bool en_dis_6_step_comm) { observers::BemfOutput output; @@ -38,20 +36,19 @@ observers::BemfOutput observers::BemfObserver::loop(const math::FrameABC& line_c voltages.beta = voltages.beta * Vbus; } - dq_update.gains = observers::BemfGains(0.0001, 0.0001, 0.1, Ts, 0); - dq_update.config = [this]() { + dq_update->gains = observers::BemfGains(0.0001, 0.0001, 0.1, Ts, 0); + dq_update->config = [this]() { controllers::PIConfig config = { 0.1, 0.1, Ts, -180, 180 }; return config; }(); - tracker.config = [this]() { + tracker->config = [this]() { controllers::PIConfig config = { 0.1, 0.1, Ts, -180, 180 }; return config; }(); - float phase_error = dq_update.loop(currents, voltages, speed_prev, angle_prev, - set_bemf_params, ext_bemf_params); - float angle = tracker.loop(phase_error, set_tracker_params, ext_tracker_params); - float speed = tracker.speed_tracker(angle, Ts); + float phase_error = dq_update->loop(currents, voltages, speed_prev, angle_prev, set_bemf_params, ext_bemf_params); + float angle = tracker->loop(phase_error, set_tracker_params, ext_tracker_params); + float speed = tracker->speed_tracker(angle, Ts); // Process Tracker Output output.phase_error_rad = phase_error; diff --git a/src/observers/dq_update.cpp b/src/observers/dq_update.cpp index 0121f15..98f39da 100644 --- a/src/observers/dq_update.cpp +++ b/src/observers/dq_update.cpp @@ -5,12 +5,13 @@ observers::DQUpdate::DQUpdate() : I_prev(0, 0), X_prev(0, 0), E(0, 0) { } -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) { } 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; @@ -36,8 +37,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; diff --git a/src/observers/tracker.cpp b/src/observers/tracker.cpp index d3365d6..7d5c838 100644 --- a/src/observers/tracker.cpp +++ b/src/observers/tracker.cpp @@ -1,11 +1,10 @@ #include -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); @@ -18,5 +17,5 @@ observers::Tracker::Tracker(controllers::PIConfig config) : config(config) float observers::Tracker::speed_tracker(float angle_est, float Ts) { - return angle_integrator.loop(angle_est, Ts); + return angle_integrator->loop(angle_est, Ts); }