Skip to content

Commit

Permalink
Made class objects into unique_ptr for heap allocation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Shobuj-Paul committed Feb 24, 2024
1 parent b70ac88 commit de2e6d7
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 34 deletions.
18 changes: 8 additions & 10 deletions include/observers/bemf_observer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,18 @@ struct BemfOutput
class BemfObserver
{
float speed_prev, angle_prev, Vbus_prev;
const uint8_t num_rotor_poles;

public:
DQUpdate dq_update;
Tracker tracker;
std::unique_ptr<DQUpdate> dq_update;
std::unique_ptr<Tracker> tracker;
float Ts;

BemfObserver(const float Ts = 0);
BemfObserver(const float Ts = 0, const uint8_t num_rotor_poles = 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 bool force_bemf, const bool en_dis_6_step_comm);
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 bool force_bemf,
const bool en_dis_6_step_comm);
};
} // namespace observers
3 changes: 2 additions & 1 deletion include/observers/dq_update.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <memory>
#include <math/foc.hpp>
#include <controllers/pi_controller.hpp>

Expand Down Expand Up @@ -68,7 +69,7 @@ struct BemfGains
class DQUpdate
{
math::FrameDQ I_prev, X_prev, E;
controllers::PIController d_axis, q_axis;
std::unique_ptr<controllers::PIController> d_axis, q_axis;

public:
controllers::PIConfig config;
Expand Down
7 changes: 4 additions & 3 deletions include/observers/tracker.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#pragma once

#include <controllers/pi_controller.hpp>
#include <memory>
#include <math/operators.hpp>
#include <math/functions.hpp>
#include <controllers/pi_controller.hpp>

namespace observers
{
Expand Down Expand Up @@ -31,8 +32,8 @@ struct ExtTrackerParams
class Tracker
{
float speed_est, angle_est;
controllers::PIController angle_controller;
math::integrator angle_integrator;
std::unique_ptr<controllers::PIController> angle_controller;
std::unique_ptr<math::integrator> angle_integrator;

public:
controllers::PIConfig config;
Expand Down
27 changes: 14 additions & 13 deletions src/observers/bemf_observer.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
#include <observers/bemf_observer.hpp>

observers::BemfObserver::BemfObserver(const float Ts) : speed_prev(0), angle_prev(0), Ts(Ts)
observers::BemfObserver::BemfObserver(const float Ts, const uint8_t num_rotor_poles)
: speed_prev(0), angle_prev(0), Vbus_prev(0), num_rotor_poles(num_rotor_poles), Ts(Ts)
{
tracker = std::make_unique<observers::Tracker>(controllers::PIConfig());
dq_update = std::make_unique<observers::DQUpdate>();
}

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,
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 ExtTrackerParams& ext_tracker_params, const uint8_t idle_mode,
const bool force_bemf, const bool en_dis_6_step_comm)
{
observers::BemfOutput output;
Expand Down Expand Up @@ -38,20 +40,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;
Expand Down
15 changes: 8 additions & 7 deletions tests/bemf_observer_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,27 @@

int main()
{
observers::BemfGains gains(1, 1, 1, Ts, 0);
observers::BemfObserver bemf_observer;
uint8_t idle_mode = 0;
bool force_bemf = false, en_dis_6_step_comm = false;
float angle = 0, Vbus = 12;

math::FrameABC line_currents, line_voltages;
math::FrameDQ U;
float angle = 0, Vbus = 12;

observers::BemfGains gains(1, 1, 1, Ts, 0);
observers::BemfObserver bemf_observer(Ts, 16);

observers::SetBemfParams set_bemf_params;
observers::SetTrackerParams set_tracker_params;
observers::ExtBemfParams ext_bemf_params;
observers::ExtTrackerParams ext_tracker_params;
uint8_t pos_obs_mode = 2, idle_mode = 0, opmode = 3, num_rotor_poles = 8, freq_mode = 2;
bool force_bemf = false, en_dis_6_step_comm = false;

for (float t = 0; t < 10; t += Ts)
{
math::FrameABC duties = math::inverse_clarke_transform(math::inverse_park_transform(U, angle));
observers::BemfOutput output =
bemf_observer.loop(line_currents, line_voltages, duties, Vbus, set_bemf_params, set_tracker_params,
ext_bemf_params, ext_tracker_params, pos_obs_mode, idle_mode, opmode, num_rotor_poles,
freq_mode, force_bemf, en_dis_6_step_comm);
ext_bemf_params, ext_tracker_params, idle_mode, force_bemf, en_dis_6_step_comm);
angle = output.e_theta_deg;
std::cout << "Duties: " << duties.a << " " << duties.b << " " << duties.c << std::endl;
}
Expand Down
17 changes: 17 additions & 0 deletions tests/dq_update_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <observers/dq_update.hpp>

int main()
{
observers::BemfGains gains(1, 1, 1, 1e-4, 0);
controllers::PIConfig config = { 0.5, 100, 1e-4, 100, -100 };
auto dq_update = observers::DQUpdate(config, gains);
for (float t = 0; t < 10; t += 1e-4)
{
math::FrameAlphaBeta currents(0, 0);
math::FrameAlphaBeta voltages(0, 0);
float angular_velocity = 0;
float rotor_angle = 0;
dq_update.loop(currents, voltages, angular_velocity, rotor_angle);
}
return 0;
}
14 changes: 14 additions & 0 deletions tests/tracker_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <observers/tracker.hpp>

int main()
{
controllers::PIConfig config = { 0.5, 100, 1e-4, 180, -180 };
auto tracker = observers::Tracker(config);
float phase_error = 10;

for (float t = 0; t < 10; t += 1e-4)
{
tracker.loop(phase_error);
}
return 0;
}

0 comments on commit de2e6d7

Please sign in to comment.