Skip to content

Commit

Permalink
Made gains and configs as member variables rather than function inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
Shobuj-Paul committed Feb 22, 2024
1 parent d4346d8 commit 21eaf7e
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 17 deletions.
2 changes: 1 addition & 1 deletion include/observers/bemf_observer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct BemfOutput

class BemfObserver
{
BemfSolver dq_update;
DQUpdate dq_update;
Tracker tracker;
float speed_prev, angle_prev, Vbus_prev;

Expand Down
17 changes: 11 additions & 6 deletions include/observers/dq_update.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -52,25 +52,30 @@ 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);
EMF_GAIN = Ts / (2 * Lq + Rs * Ts);
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;
};
Expand Down
3 changes: 2 additions & 1 deletion include/observers/tracker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down
10 changes: 5 additions & 5 deletions src/observers/bemf_observer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions src/observers/dq_update.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#include <observers/dq_update.hpp>
#include <math/functions.hpp>

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;
Expand Down Expand Up @@ -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;
}

0 comments on commit 21eaf7e

Please sign in to comment.