From 5c9dbdfebf764afe6faaf9f712faf75c2ecac775 Mon Sep 17 00:00:00 2001 From: Sahil Kale Date: Wed, 27 Dec 2023 17:29:30 +0100 Subject: [PATCH] [BLDC] Rename the Bldc6Step namespace --- .../bldc/brushless_6step_commutation.cpp | 4 ++-- .../bldc/brushless_6step_commutation.hpp | 4 ++-- control_loop/bldc/brushless_control_loop.cpp | 17 +++++++---------- control_loop/bldc/brushless_control_loop.hpp | 3 +-- control_loop/bldc/rotor_estimator.cpp | 15 +++++++-------- control_loop/bldc/rotor_estimator.hpp | 9 ++++----- test/brushless_6step_types_test.cpp | 16 ++++++++-------- test/brushless_control_loop_test.cpp | 2 +- test/rotor_estimator_test.cpp | 9 +++------ 9 files changed, 35 insertions(+), 44 deletions(-) diff --git a/control_loop/bldc/brushless_6step_commutation.cpp b/control_loop/bldc/brushless_6step_commutation.cpp index 02be1a5..57e73ba 100644 --- a/control_loop/bldc/brushless_6step_commutation.cpp +++ b/control_loop/bldc/brushless_6step_commutation.cpp @@ -3,7 +3,7 @@ #include "math_util.hpp" namespace control_loop { -namespace Bldc6StepCommutationTypes { +namespace Bldc6Step { commutation_step_t determine_commutation_step_from_theta(float electrical_theta) { commutation_step_t commutation_step = {CommutationSignal::LOW, CommutationSignal::LOW, CommutationSignal::LOW}; @@ -36,5 +36,5 @@ commutation_step_t determine_commutation_step_from_theta(float electrical_theta) return commutation_step; } -} // namespace Bldc6StepCommutationTypes +} // namespace Bldc6Step } // namespace control_loop \ No newline at end of file diff --git a/control_loop/bldc/brushless_6step_commutation.hpp b/control_loop/bldc/brushless_6step_commutation.hpp index 9d250cf..4a6a191 100644 --- a/control_loop/bldc/brushless_6step_commutation.hpp +++ b/control_loop/bldc/brushless_6step_commutation.hpp @@ -5,7 +5,7 @@ namespace control_loop { -namespace Bldc6StepCommutationTypes { +namespace Bldc6Step { enum class CommutationSignal { HIGH, LOW, @@ -39,7 +39,7 @@ constexpr commutation_step_t commutation_steps[num_commutation_steps] = { */ commutation_step_t determine_commutation_step_from_theta(float electrical_theta); -}; // namespace Bldc6StepCommutationTypes +}; // namespace Bldc6Step } // namespace control_loop diff --git a/control_loop/bldc/brushless_control_loop.cpp b/control_loop/bldc/brushless_control_loop.cpp index 25dde19..beca2ec 100644 --- a/control_loop/bldc/brushless_control_loop.cpp +++ b/control_loop/bldc/brushless_control_loop.cpp @@ -205,8 +205,7 @@ void BrushlessControlLoop::update_rotor_position_estimator( bridge_.read_current(estimator_inputs.phase_current); // Get the commutation step - estimator_inputs.current_commutation_step = - Bldc6StepCommutationTypes::determine_commutation_step_from_theta(rotor_position_); + estimator_inputs.current_commutation_step = Bldc6Step::determine_commutation_step_from_theta(rotor_position_); // Set the phase params estimator_inputs.phase_resistance = params_->foc_params.phase_resistance; @@ -302,7 +301,6 @@ void BrushlessControlLoop::run_foc(float speed, utime_t current_time_us, utime_t control_loop_type_ = get_desired_control_loop_type(is_primary_estimator_valid, is_secondary_estimator_valid); // Get the bus voltage float bus_voltage = 0.0f; - // TODO: ERROR CHECKING!! app_hal_status_E status = bridge_.read_bus_voltage(bus_voltage); status_.set_error(BrushlessControlLoopStatus::BrushlessControlLoopError::BUS_VOLTAGE_READ_FAILURE, static_cast(status != app_hal_status_E::APP_HAL_OK)); @@ -393,8 +391,7 @@ void BrushlessControlLoop::run_trap(float speed, hwbridge::Bridge3Phase::phase_c primary_rotor_position_estimator_.get_rotor_position(rotor_position_); // Get the commutation step - Bldc6StepCommutationTypes::commutation_step_t current_commutation_step = - Bldc6StepCommutationTypes::determine_commutation_step_from_theta(rotor_position_); + Bldc6Step::commutation_step_t current_commutation_step = Bldc6Step::determine_commutation_step_from_theta(rotor_position_); // Determine the duty cycles for the inverter determine_inverter_duty_cycles_trap(phase_commands, current_commutation_step, speed); @@ -465,14 +462,14 @@ BrushlessControlLoop::BrushlessControlLoopType BrushlessControlLoop::get_desired return desired_control_loop_type; } -void BrushlessControlLoop::determine_inverter_duty_cycles_trap( - hwbridge::Bridge3Phase::phase_command_t phase_command[3], - Bldc6StepCommutationTypes::commutation_step_t current_commutation_step, float motor_speed) { +void BrushlessControlLoop::determine_inverter_duty_cycles_trap(hwbridge::Bridge3Phase::phase_command_t phase_command[3], + Bldc6Step::commutation_step_t current_commutation_step, + float motor_speed) { for (int i = 0; i < 3; i++) { - if (current_commutation_step.signals[i] == Bldc6StepCommutationTypes::CommutationSignal::HIGH) { + if (current_commutation_step.signals[i] == Bldc6Step::CommutationSignal::HIGH) { phase_command[i].duty_cycle_high_side = fabs(motor_speed); phase_command[i].invert_low_side = true; - } else if (current_commutation_step.signals[i] == Bldc6StepCommutationTypes::CommutationSignal::LOW) { + } else if (current_commutation_step.signals[i] == Bldc6Step::CommutationSignal::LOW) { phase_command[i].duty_cycle_high_side = 0.0f; phase_command[i].invert_low_side = true; } else { diff --git a/control_loop/bldc/brushless_control_loop.hpp b/control_loop/bldc/brushless_control_loop.hpp index 3d19ca7..0d4cbdc 100644 --- a/control_loop/bldc/brushless_control_loop.hpp +++ b/control_loop/bldc/brushless_control_loop.hpp @@ -300,8 +300,7 @@ class BrushlessControlLoop : public ControlLoop { * @param motor_speed the motor speed to generate the duty cycles for. 0 - 1.0f */ void determine_inverter_duty_cycles_trap(hwbridge::Bridge3Phase::phase_command_t phase_command[3], - Bldc6StepCommutationTypes::commutation_step_t current_commutation_step, - float motor_speed); + Bldc6Step::commutation_step_t current_commutation_step, float motor_speed); }; } // namespace control_loop diff --git a/control_loop/bldc/rotor_estimator.cpp b/control_loop/bldc/rotor_estimator.cpp index 32ea7a0..7e86c90 100644 --- a/control_loop/bldc/rotor_estimator.cpp +++ b/control_loop/bldc/rotor_estimator.cpp @@ -27,8 +27,8 @@ app_hal_status_E BldcSensorlessRotorSectorSensor::get_electrical_angle(float& an } // Get the commutation step as a function of the electrical angle - control_loop::Bldc6StepCommutationTypes::commutation_step_t step = - control_loop::Bldc6StepCommutationTypes::determine_commutation_step_from_theta(estimated_electrical_angle_); + control_loop::Bldc6Step::commutation_step_t step = + control_loop::Bldc6Step::determine_commutation_step_from_theta(estimated_electrical_angle_); // Check if a zero crossing has occurred const bool zero_crossing = zero_crossing_detected(bemf_voltage, step); @@ -68,17 +68,16 @@ app_hal_status_E BldcSensorlessRotorSectorSensor::get_electrical_angle(float& an bool BldcSensorlessRotorSectorSensor::zero_crossing_detected( const hwbridge::Bridge3Phase::phase_voltage_t& bemf_voltage, - control_loop::Bldc6StepCommutationTypes::commutation_step_t current_commutation_step) { + control_loop::Bldc6Step::commutation_step_t current_commutation_step) { float phase_sum = 0.0f; - control_loop::Bldc6StepCommutationTypes::CommutationSignal zero_crossing_signal = - control_loop::Bldc6StepCommutationTypes::CommutationSignal::Z_RISING; + control_loop::Bldc6Step::CommutationSignal zero_crossing_signal = control_loop::Bldc6Step::CommutationSignal::Z_RISING; float undriven_phase_voltage = 0.0f; const float bemf_voltages[hwbridge::Bridge3Phase::NUM_PHASES] = {bemf_voltage.u, bemf_voltage.v, bemf_voltage.w}; for (uint8_t i = 0; i < hwbridge::Bridge3Phase::NUM_PHASES; i++) { - if ((current_commutation_step.signals[i] != control_loop::Bldc6StepCommutationTypes::CommutationSignal::Z_FALLING) && - (current_commutation_step.signals[i] != control_loop::Bldc6StepCommutationTypes::CommutationSignal::Z_RISING)) { + if ((current_commutation_step.signals[i] != control_loop::Bldc6Step::CommutationSignal::Z_FALLING) && + (current_commutation_step.signals[i] != control_loop::Bldc6Step::CommutationSignal::Z_RISING)) { phase_sum += bemf_voltages[i]; } else { zero_crossing_signal = current_commutation_step.signals[i]; @@ -88,7 +87,7 @@ bool BldcSensorlessRotorSectorSensor::zero_crossing_detected( float zero_crossing_threshold = phase_sum / 2.0f; // NOTE: This requires the bemf voltage to run when the PWM is ON bool return_value = false; - if (zero_crossing_signal == control_loop::Bldc6StepCommutationTypes::CommutationSignal::Z_RISING) { + if (zero_crossing_signal == control_loop::Bldc6Step::CommutationSignal::Z_RISING) { if (undriven_phase_voltage > zero_crossing_threshold) { return_value = true; } diff --git a/control_loop/bldc/rotor_estimator.hpp b/control_loop/bldc/rotor_estimator.hpp index fcab38c..890503f 100644 --- a/control_loop/bldc/rotor_estimator.hpp +++ b/control_loop/bldc/rotor_estimator.hpp @@ -18,10 +18,9 @@ class ElectricalRotorPosEstimator { public: utime_t time = 0; hwbridge::Bridge3Phase::phase_voltage_t phase_voltage = {0.0f, 0.0f, 0.0f}; - control_loop::Bldc6StepCommutationTypes::commutation_step_t current_commutation_step = { - control_loop::Bldc6StepCommutationTypes::CommutationSignal::Z_FALLING, - control_loop::Bldc6StepCommutationTypes::CommutationSignal::HIGH, - control_loop::Bldc6StepCommutationTypes::CommutationSignal::LOW}; + control_loop::Bldc6Step::commutation_step_t current_commutation_step = { + control_loop::Bldc6Step::CommutationSignal::Z_FALLING, control_loop::Bldc6Step::CommutationSignal::HIGH, + control_loop::Bldc6Step::CommutationSignal::LOW}; hwbridge::Bridge3Phase::phase_current_t phase_current = {0.0f, 0.0f, 0.0f}; float V_alpha = 0.0f; @@ -93,7 +92,7 @@ class BldcSensorlessRotorSectorSensor : public BldcRotorSectorSensor { protected: bool zero_crossing_detected(const hwbridge::Bridge3Phase::phase_voltage_t& bemf_voltage, - control_loop::Bldc6StepCommutationTypes::commutation_step_t current_commutation_step); + control_loop::Bldc6Step::commutation_step_t current_commutation_step); hwbridge::Bridge3Phase& bridge_; float estimated_electrical_angle_ = 0.0f; basilisk_hal::HAL_CLOCK& clock_; diff --git a/test/brushless_6step_types_test.cpp b/test/brushless_6step_types_test.cpp index 55d1685..e280b8b 100644 --- a/test/brushless_6step_types_test.cpp +++ b/test/brushless_6step_types_test.cpp @@ -7,28 +7,28 @@ using namespace ::testing; namespace control_loop { -void test_commutation_step(const Bldc6StepCommutationTypes::commutation_step_t& commutation_step, uint8_t expected_sector) { - EXPECT_EQ(commutation_step.signals[0], Bldc6StepCommutationTypes::commutation_steps[expected_sector].signals[0]); - EXPECT_EQ(commutation_step.signals[1], Bldc6StepCommutationTypes::commutation_steps[expected_sector].signals[1]); - EXPECT_EQ(commutation_step.signals[2], Bldc6StepCommutationTypes::commutation_steps[expected_sector].signals[2]); +void test_commutation_step(const Bldc6Step::commutation_step_t& commutation_step, uint8_t expected_sector) { + EXPECT_EQ(commutation_step.signals[0], Bldc6Step::commutation_steps[expected_sector].signals[0]); + EXPECT_EQ(commutation_step.signals[1], Bldc6Step::commutation_steps[expected_sector].signals[1]); + EXPECT_EQ(commutation_step.signals[2], Bldc6Step::commutation_steps[expected_sector].signals[2]); } // Test the angle to commutation step function TEST(Bldc6StepCommutationTest, test_angle_to_commutation) { // Get the commutation step from the angle 0 radians - auto commutation_step = Bldc6StepCommutationTypes::determine_commutation_step_from_theta(0.0f); + auto commutation_step = Bldc6Step::determine_commutation_step_from_theta(0.0f); test_commutation_step(commutation_step, 0); // Get the commutation step from the angle 1/12th of a revolution - commutation_step = Bldc6StepCommutationTypes::determine_commutation_step_from_theta(math::M_PI_FLOAT / 6.0f); + commutation_step = Bldc6Step::determine_commutation_step_from_theta(math::M_PI_FLOAT / 6.0f); test_commutation_step(commutation_step, 0); // Get the angle for 45 degrees - commutation_step = Bldc6StepCommutationTypes::determine_commutation_step_from_theta(math::M_PI_FLOAT / 4.0f); + commutation_step = Bldc6Step::determine_commutation_step_from_theta(math::M_PI_FLOAT / 4.0f); test_commutation_step(commutation_step, 1); // Get pi radians - commutation_step = Bldc6StepCommutationTypes::determine_commutation_step_from_theta(math::M_PI_FLOAT); + commutation_step = Bldc6Step::determine_commutation_step_from_theta(math::M_PI_FLOAT); test_commutation_step(commutation_step, 3); } } // namespace control_loop \ No newline at end of file diff --git a/test/brushless_control_loop_test.cpp b/test/brushless_control_loop_test.cpp index 6dff672..4232618 100644 --- a/test/brushless_control_loop_test.cpp +++ b/test/brushless_control_loop_test.cpp @@ -101,7 +101,7 @@ TEST(BrushlessControlLoopTest, test_6_step_duty_cycle) { // Check that the phase command is correct for a motor speed of 1 // Comm step U low, V Z, W high - test_control_loop.determine_inverter_duty_cycles_trap(phase_command, Bldc6StepCommutationTypes::commutation_steps[2], 1.0f); + test_control_loop.determine_inverter_duty_cycles_trap(phase_command, Bldc6Step::commutation_steps[2], 1.0f); EXPECT_FLOAT_EQ(phase_command[0].duty_cycle_high_side, 0.0f); EXPECT_FLOAT_EQ(phase_command[0].invert_low_side, true); EXPECT_FLOAT_EQ(phase_command[1].duty_cycle_high_side, 0.0f); diff --git a/test/rotor_estimator_test.cpp b/test/rotor_estimator_test.cpp index 2f2b936..81c3eda 100644 --- a/test/rotor_estimator_test.cpp +++ b/test/rotor_estimator_test.cpp @@ -347,23 +347,20 @@ TEST(RotorEstimatorTest, test_zero_crossing_detection) { bemf_voltage.w = 1.0f; // With this, we should not detect a zero crossing - EXPECT_FALSE( - sensorless_sensor.zero_crossing_detected(bemf_voltage, control_loop::Bldc6StepCommutationTypes::commutation_steps[4])); + EXPECT_FALSE(sensorless_sensor.zero_crossing_detected(bemf_voltage, control_loop::Bldc6Step::commutation_steps[4])); // Load the bemf with a voltage of 1.0f, 0.0f, and 0.51f bemf_voltage.w = 0.51f; // With this, we should not detect a zero crossing - EXPECT_FALSE( - sensorless_sensor.zero_crossing_detected(bemf_voltage, control_loop::Bldc6StepCommutationTypes::commutation_steps[4])); + EXPECT_FALSE(sensorless_sensor.zero_crossing_detected(bemf_voltage, control_loop::Bldc6Step::commutation_steps[4])); // Load the bemf with a voltage of 1.0f, 0.0f, and 0.49f bemf_voltage.w = 0.49f; // With this, we should detect a zero crossing - EXPECT_TRUE( - sensorless_sensor.zero_crossing_detected(bemf_voltage, control_loop::Bldc6StepCommutationTypes::commutation_steps[4])); + EXPECT_TRUE(sensorless_sensor.zero_crossing_detected(bemf_voltage, control_loop::Bldc6Step::commutation_steps[4])); } // Test the sensorless sector estimator reporting a sector change for the same duration of time as when a zero crossing was