Skip to content

Commit 4b814f0

Browse files
authored
fix(bldc_driver): fix driver level inversion (#100)
* fix(bldc_driver): fix driver level inversion * Update to set output to low when counting up, and high when counting down. This means that setting a value of 25% will actually have the output high for 25% of the time. Fixes #97 * feat(bldc): updated examples * Update examples to always calibrate by setting zero electrical offset to 0. * example(bldc_motor): update * Update bldc motor example to 1) always calibrate sensor direction, 2) always calibrate electrical angle, and 3) enable the motor so it actually runs * fix(bldc_driver): configure low side * Update bldc driver to configure the compare actions for the low side generators * Check error of tiemr functions to not enable if they fail (since it wont work anyway). Havent seen those errors, but just in queso. * example(bldc_haptics): update * Update example to always calibrate sensor direction (since its a test) * Updated example to debug log since its a test
1 parent 0aaa986 commit 4b814f0

3 files changed

Lines changed: 38 additions & 19 deletions

File tree

components/bldc_driver/include/bldc_driver.hpp

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,16 @@ class BldcDriver {
7272
return;
7373
}
7474
logger_.info("Enabling");
75-
mcpwm_timer_enable(timer_);
76-
mcpwm_timer_start_stop(timer_, MCPWM_TIMER_START_NO_STOP);
75+
auto err = mcpwm_timer_enable(timer_);
76+
if (err != ESP_OK) {
77+
logger_.error("Failed to enable timer: {}", esp_err_to_name(err));
78+
return;
79+
}
80+
err = mcpwm_timer_start_stop(timer_, MCPWM_TIMER_START_NO_STOP);
81+
if (err != ESP_OK) {
82+
logger_.error("Failed to start timer: {}", esp_err_to_name(err));
83+
return;
84+
}
7785
enabled_ = true;
7886
if (gpio_en_ >= 0) {
7987
gpio_set_level((gpio_num_t)gpio_en_, 1);
@@ -294,22 +302,28 @@ class BldcDriver {
294302
}
295303

296304
// A high / low
297-
configure_generator_action(generators_[0], comparators_[0]);
305+
configure_generator_action(generators_[0], generators_[1], comparators_[0]);
298306
configure_generator_deadtime(generators_[0], generators_[1]);
299307
// B high / low
300-
configure_generator_action(generators_[2], comparators_[1]);
308+
configure_generator_action(generators_[2], generators_[3], comparators_[1]);
301309
configure_generator_deadtime(generators_[2], generators_[3]);
302310
// C high / low
303-
configure_generator_action(generators_[4], comparators_[2]);
311+
configure_generator_action(generators_[4], generators_[5], comparators_[2]);
304312
configure_generator_deadtime(generators_[4], generators_[5]);
305313
}
306314

307-
void configure_generator_action(mcpwm_gen_handle_t &gen_high, mcpwm_cmpr_handle_t comp) {
315+
void configure_generator_action(mcpwm_gen_handle_t &gen_high, mcpwm_gen_handle_t &gen_low,
316+
mcpwm_cmpr_handle_t comp) {
308317
logger_.info("Setup generator action");
309318
ESP_ERROR_CHECK(mcpwm_generator_set_actions_on_compare_event(
310319
gen_high,
311-
MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, comp, MCPWM_GEN_ACTION_HIGH),
312-
MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_DOWN, comp, MCPWM_GEN_ACTION_LOW),
320+
MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, comp, MCPWM_GEN_ACTION_LOW),
321+
MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_DOWN, comp, MCPWM_GEN_ACTION_HIGH),
322+
MCPWM_GEN_COMPARE_EVENT_ACTION_END()));
323+
ESP_ERROR_CHECK(mcpwm_generator_set_actions_on_compare_event(
324+
gen_low,
325+
MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, comp, MCPWM_GEN_ACTION_LOW),
326+
MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_DOWN, comp, MCPWM_GEN_ACTION_HIGH),
313327
MCPWM_GEN_COMPARE_EVENT_ACTION_END()));
314328
}
315329

components/bldc_haptics/example/main/bldc_haptics_example.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ extern "C" void app_main(void) {
9797
.gpio_fault = 36, // connected to the nFAULT pin of TMC6300-BOB
9898
.power_supply_voltage = 5.0f,
9999
.limit_voltage = 5.0f,
100-
.log_level = espp::Logger::Verbosity::WARN});
100+
.log_level = espp::Logger::Verbosity::DEBUG});
101101

102102
// now make the bldc motor
103103
using BldcMotor = espp::BldcMotor<espp::BldcDriver, espp::Mt6701>;
@@ -109,10 +109,11 @@ extern "C" void app_main(void) {
109109
5.0f, // tested by running velocity_openloop and seeing if the veloicty is ~correct
110110
.kv_rating =
111111
320, // tested by running velocity_openloop and seeing if the velocity is ~correct
112-
.current_limit = 1.0f, // Amps
113-
.zero_electric_offset = 2.3914752, // gotten from previously running without providing this
114-
// and it will be logged.
115-
.sensor_direction = espp::detail::SensorDirection::COUNTER_CLOCKWISE,
112+
.current_limit = 1.0f, // Amps
113+
.zero_electric_offset = 0.0f, // set to zero to always calibrate, since this is a test
114+
.sensor_direction =
115+
espp::detail::SensorDirection::UNKNOWN, // set to unknown to always calibrate, since
116+
// this is a test
116117
.foc_type = espp::detail::FocType::SPACE_VECTOR_PWM,
117118
.driver = driver,
118119
.sensor = mt6701,
@@ -136,7 +137,7 @@ extern "C" void app_main(void) {
136137
.output_min = -20.0, // angle pid works on velocity (rad/s)
137138
.output_max = 20.0, // angle pid works on velocity (rad/s)
138139
},
139-
.log_level = espp::Logger::Verbosity::INFO});
140+
.log_level = espp::Logger::Verbosity::DEBUG});
140141

141142
auto print_detent_config = [&logger](const auto &detent_config) {
142143
if (detent_config == espp::detail::UNBOUNDED_NO_DETENTS) {

components/bldc_motor/example/main/bldc_motor_example.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,11 @@ extern "C" void app_main(void) {
111111
5.0f, // tested by running velocity_openloop and seeing if the veloicty is ~correct
112112
.kv_rating =
113113
320, // tested by running velocity_openloop and seeing if the velocity is ~correct
114-
.current_limit = 1.0f, // Amps
115-
.zero_electric_offset = 2.3914752, // gotten from previously running without providing this
116-
// and it will be logged.
117-
.sensor_direction = espp::detail::SensorDirection::COUNTER_CLOCKWISE,
114+
.current_limit = 1.0f, // Amps
115+
.zero_electric_offset = 0.0f, // set to zero to always calibrate, since this is a test
116+
.sensor_direction = espp::detail::SensorDirection::UNKNOWN, // set to unknown to always
117+
// calibrate, since this is a
118+
// test
118119
.foc_type = espp::detail::FocType::SPACE_VECTOR_PWM,
119120
.driver = driver,
120121
.sensor = mt6701,
@@ -138,7 +139,7 @@ extern "C" void app_main(void) {
138139
.output_min = -20.0, // angle pid works on velocity (rad/s)
139140
.output_max = 20.0, // angle pid works on velocity (rad/s)
140141
},
141-
.log_level = espp::Logger::Verbosity::INFO});
142+
.log_level = espp::Logger::Verbosity::DEBUG});
142143

143144
static const auto motion_control_type = espp::detail::MotionControlType::VELOCITY;
144145
// static const auto motion_control_type = espp::detail::MotionControlType::ANGLE;
@@ -150,6 +151,9 @@ extern "C" void app_main(void) {
150151
motor.set_motion_control_type(motion_control_type);
151152
std::atomic<float> target = 0;
152153

154+
// enable the motor
155+
motor.enable();
156+
153157
auto motor_task_fn = [&motor, &target](std::mutex &m, std::condition_variable &cv) {
154158
static auto delay = std::chrono::duration<float>(core_update_period);
155159
auto start = std::chrono::high_resolution_clock::now();

0 commit comments

Comments
 (0)