From 21eaf7e9fc34d72674e946fd27167ef72874eb3d Mon Sep 17 00:00:00 2001 From: Shobuj Paul <72087882+Shobuj-Paul@users.noreply.github.com> Date: Thu, 22 Feb 2024 11:26:44 +0000 Subject: [PATCH] Made gains and configs as member variables rather than function inputs --- include/observers/bemf_observer.hpp | 2 +- include/observers/dq_update.hpp | 17 +++++++++++------ include/observers/tracker.hpp | 3 ++- src/observers/bemf_observer.cpp | 10 +++++----- src/observers/dq_update.cpp | 7 +++---- 5 files changed, 22 insertions(+), 17 deletions(-) diff --git a/include/observers/bemf_observer.hpp b/include/observers/bemf_observer.hpp index 77a8c75..63443c5 100644 --- a/include/observers/bemf_observer.hpp +++ b/include/observers/bemf_observer.hpp @@ -15,7 +15,7 @@ struct BemfOutput class BemfObserver { - BemfSolver dq_update; + DQUpdate dq_update; Tracker tracker; float speed_prev, angle_prev, Vbus_prev; diff --git a/include/observers/dq_update.hpp b/include/observers/dq_update.hpp index d0df65f..3eb9cfc 100644 --- a/include/observers/dq_update.hpp +++ b/include/observers/dq_update.hpp @@ -43,7 +43,7 @@ struct ExtBemfParams struct BemfGains { float VOLTAGE_GAIN, CURRENT_GAIN, EMF_GAIN, SPEED_CURRENT_GAIN; - BemfGains(float Ld, float Lq, float Rs, float Ts, int axis) + BemfGains(float Ld, float Lq, float Rs, float Ts, bool axis) { if (axis == 0) { @@ -52,7 +52,7 @@ struct BemfGains EMF_GAIN = Ts / (2 * Ld + Rs * Ts); SPEED_CURRENT_GAIN = (2 * Ld - Rs * Ts) / (2 * Ld + Rs * Ts); } - if (axis == 1) + else if (axis == 1) { VOLTAGE_GAIN = Ts / (2 * Lq + Rs * Ts); CURRENT_GAIN = -Ld * Ts / (2 * Lq + Rs * Ts); @@ -60,17 +60,22 @@ struct BemfGains SPEED_CURRENT_GAIN = (2 * Lq - Rs * Ts) / (2 * Lq + Rs * Ts); } } + BemfGains() : VOLTAGE_GAIN(0), CURRENT_GAIN(0), EMF_GAIN(0), SPEED_CURRENT_GAIN(0) + { + } }; -class BemfSolver +class DQUpdate { math::FrameDQ I_prev, X_prev, E; controllers::PIController d_axis, q_axis; public: - BemfSolver(); - float loop(math::FrameAlphaBeta currents, math::FrameAlphaBeta voltages, const controllers::PIConfig& config, - const BemfGains& gains, float angular_velocity, float rotor_angle, + DQUpdate(); + controllers::PIConfig config; + BemfGains gains; + + float loop(math::FrameAlphaBeta currents, math::FrameAlphaBeta voltages, float angular_velocity, float rotor_angle, const SetBemfParams& set_params = SetBemfParams(), const ExtBemfParams& ext_params = ExtBemfParams()); math::FrameDQ get_emfs() const; }; diff --git a/include/observers/tracker.hpp b/include/observers/tracker.hpp index f2c5672..a9dde5e 100644 --- a/include/observers/tracker.hpp +++ b/include/observers/tracker.hpp @@ -35,8 +35,9 @@ class Tracker math::integrator angle_integrator; public: + controllers::PIConfig config; Tracker() = default; - float loop(float phase_error, controllers::PIConfig config, const SetTrackerParams& params = SetTrackerParams(), + float loop(float phase_error, const SetTrackerParams& params = SetTrackerParams(), const ExtTrackerParams& ext_params = ExtTrackerParams()); float speed_tracker(float angle_est, float Ts); }; diff --git a/src/observers/bemf_observer.cpp b/src/observers/bemf_observer.cpp index ac22867..eb581e3 100644 --- a/src/observers/bemf_observer.cpp +++ b/src/observers/bemf_observer.cpp @@ -38,19 +38,19 @@ observers::BemfOutput observers::BemfObserver::loop(math::FrameABC line_currents voltages.beta = voltages.beta * Vbus; } - const observers::BemfGains bemf_gains(0.0001, 0.0001, 0.1, Ts, 0); - const controllers::PIConfig update_config = [freq_mode, opmode, Ts]() { + dq_update.gains = observers::BemfGains(0.0001, 0.0001, 0.1, Ts, 0); + dq_update.config = [freq_mode, opmode, Ts]() { controllers::PIConfig config = { 0.1, 0.1, Ts, -180, 180 }; return config; }(); - const controllers::PIConfig track_config = [freq_mode, opmode, Ts]() { + tracker.config = [freq_mode, opmode, Ts]() { controllers::PIConfig config = { 0.1, 0.1, Ts, -180, 180 }; return config; }(); - float phase_error = dq_update.loop(currents, voltages, update_config, bemf_gains, speed_prev, angle_prev, + float phase_error = dq_update.loop(currents, voltages, speed_prev, angle_prev, set_bemf_params, ext_bemf_params); - float angle = tracker.loop(phase_error, track_config, set_tracker_params, ext_tracker_params); + float angle = tracker.loop(phase_error, set_tracker_params, ext_tracker_params); float speed = tracker.speed_tracker(angle, Ts); // Process Tracker Output diff --git a/src/observers/dq_update.cpp b/src/observers/dq_update.cpp index 48404c3..e06f629 100644 --- a/src/observers/dq_update.cpp +++ b/src/observers/dq_update.cpp @@ -1,12 +1,11 @@ #include #include -observers::BemfSolver::BemfSolver() : I_prev(0, 0), X_prev(0, 0), E(0, 0) +observers::DQUpdate::DQUpdate() : I_prev(0, 0), X_prev(0, 0), E(0, 0) { } -float observers::BemfSolver::loop(math::FrameAlphaBeta currents, math::FrameAlphaBeta voltages, - const controllers::PIConfig& config, const BemfGains& gains, float angular_velocity, +float observers::DQUpdate::loop(math::FrameAlphaBeta currents, math::FrameAlphaBeta voltages, float angular_velocity, float rotor_angle, const SetBemfParams& set_params, const ExtBemfParams& ext_params) { math::FrameDQ I_est, error, X; @@ -42,7 +41,7 @@ float observers::BemfSolver::loop(math::FrameAlphaBeta currents, math::FrameAlph return math::atan2(E.d, E.q); } -math::FrameDQ observers::BemfSolver::get_emfs() const +math::FrameDQ observers::DQUpdate::get_emfs() const { return E; }