Skip to content

Commit

Permalink
[BLDC] Rename the Bldc6Step namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
sahil-kale committed Dec 27, 2023
1 parent 5995fff commit 5c9dbdf
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 44 deletions.
4 changes: 2 additions & 2 deletions control_loop/bldc/brushless_6step_commutation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions control_loop/bldc/brushless_6step_commutation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace control_loop {

namespace Bldc6StepCommutationTypes {
namespace Bldc6Step {
enum class CommutationSignal {
HIGH,
LOW,
Expand Down Expand Up @@ -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

Expand Down
17 changes: 7 additions & 10 deletions control_loop/bldc/brushless_control_loop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<bool>(status != app_hal_status_E::APP_HAL_OK));
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions control_loop/bldc/brushless_control_loop.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 7 additions & 8 deletions control_loop/bldc/rotor_estimator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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];
Expand All @@ -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;
}
Expand Down
9 changes: 4 additions & 5 deletions control_loop/bldc/rotor_estimator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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_;
Expand Down
16 changes: 8 additions & 8 deletions test/brushless_6step_types_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion test/brushless_control_loop_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 3 additions & 6 deletions test/rotor_estimator_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5c9dbdf

Please sign in to comment.