Skip to content

Commit

Permalink
Made unique pointers out of class objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Shobuj-Paul committed Feb 24, 2024
1 parent 384c878 commit 27b6b2c
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 36 deletions.
4 changes: 3 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 Down
16 changes: 7 additions & 9 deletions include/observers/bemf_observer.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#pragma once

#include <cstdint>
#include <memory>
#include <observers/dq_update.hpp>
#include <observers/tracker.hpp>
#include <math/foc.hpp>
#include <cstdint>

namespace observers
{
Expand All @@ -18,18 +19,15 @@ class BemfObserver
float speed_prev, angle_prev, Vbus_prev;

public:
DQUpdate dq_update;
Tracker tracker;
std::unique_ptr<DQUpdate> dq_update;
std::unique_ptr<Tracker> 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
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
5 changes: 3 additions & 2 deletions include/observers/tracker.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <memory>
#include <controllers/pi_controller.hpp>
#include <math/operators.hpp>
#include <math/functions.hpp>
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: 12 additions & 15 deletions src/observers/bemf_observer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down
9 changes: 5 additions & 4 deletions src/observers/dq_update.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down
7 changes: 3 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 @@ -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);
}

0 comments on commit 27b6b2c

Please sign in to comment.