From 5196976ba1b43bc69d17b89a11ef4ca06a4247a1 Mon Sep 17 00:00:00 2001 From: William Emfinger Date: Tue, 1 Aug 2023 08:35:49 -0500 Subject: [PATCH 1/5] 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 --- components/bldc_driver/include/bldc_driver.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/components/bldc_driver/include/bldc_driver.hpp b/components/bldc_driver/include/bldc_driver.hpp index 59daa9a63..0f1285ec8 100644 --- a/components/bldc_driver/include/bldc_driver.hpp +++ b/components/bldc_driver/include/bldc_driver.hpp @@ -308,8 +308,8 @@ class BldcDriver { logger_.info("Setup generator action"); ESP_ERROR_CHECK(mcpwm_generator_set_actions_on_compare_event( gen_high, - MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, comp, MCPWM_GEN_ACTION_HIGH), - MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_DOWN, comp, MCPWM_GEN_ACTION_LOW), + MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, comp, MCPWM_GEN_ACTION_LOW), + MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_DOWN, comp, MCPWM_GEN_ACTION_HIGH), MCPWM_GEN_COMPARE_EVENT_ACTION_END())); } From 837d708af260314d9050e8997f17ceada345b932 Mon Sep 17 00:00:00 2001 From: William Emfinger Date: Tue, 1 Aug 2023 08:38:48 -0500 Subject: [PATCH 2/5] feat(bldc): updated examples * Update examples to always calibrate by setting zero electrical offset to 0. --- .../bldc_haptics/example/main/bldc_haptics_example.cpp | 8 +++++--- components/bldc_motor/example/main/bldc_motor_example.cpp | 8 +++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/components/bldc_haptics/example/main/bldc_haptics_example.cpp b/components/bldc_haptics/example/main/bldc_haptics_example.cpp index e10b89b94..52e7b94bc 100644 --- a/components/bldc_haptics/example/main/bldc_haptics_example.cpp +++ b/components/bldc_haptics/example/main/bldc_haptics_example.cpp @@ -109,9 +109,11 @@ extern "C" void app_main(void) { 5.0f, // tested by running velocity_openloop and seeing if the veloicty is ~correct .kv_rating = 320, // tested by running velocity_openloop and seeing if the velocity is ~correct - .current_limit = 1.0f, // Amps - .zero_electric_offset = 2.3914752, // gotten from previously running without providing this - // and it will be logged. + .current_limit = 1.0f, // Amps + .zero_electric_offset = 0.0f, // set to zero to always calibrate, since this is a test + // .zero_electric_offset = 2.3914752, // gotten from previously running without providing + // this + // // and it will be logged. .sensor_direction = espp::detail::SensorDirection::COUNTER_CLOCKWISE, .foc_type = espp::detail::FocType::SPACE_VECTOR_PWM, .driver = driver, diff --git a/components/bldc_motor/example/main/bldc_motor_example.cpp b/components/bldc_motor/example/main/bldc_motor_example.cpp index 57c7f0700..9f317a2fd 100644 --- a/components/bldc_motor/example/main/bldc_motor_example.cpp +++ b/components/bldc_motor/example/main/bldc_motor_example.cpp @@ -111,9 +111,11 @@ extern "C" void app_main(void) { 5.0f, // tested by running velocity_openloop and seeing if the veloicty is ~correct .kv_rating = 320, // tested by running velocity_openloop and seeing if the velocity is ~correct - .current_limit = 1.0f, // Amps - .zero_electric_offset = 2.3914752, // gotten from previously running without providing this - // and it will be logged. + .current_limit = 1.0f, // Amps + .zero_electric_offset = 0.0f, // set to zero to always calibrate, since this is a test + // .zero_electric_offset = 2.3914752, // gotten from previously running without providing + // this + // // and it will be logged. .sensor_direction = espp::detail::SensorDirection::COUNTER_CLOCKWISE, .foc_type = espp::detail::FocType::SPACE_VECTOR_PWM, .driver = driver, From 6bfa6facbab67ae607e600d3237de7af4c2fca0a Mon Sep 17 00:00:00 2001 From: William Emfinger Date: Tue, 1 Aug 2023 09:10:14 -0500 Subject: [PATCH 3/5] 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 --- .../bldc_motor/example/main/bldc_motor_example.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/components/bldc_motor/example/main/bldc_motor_example.cpp b/components/bldc_motor/example/main/bldc_motor_example.cpp index 9f317a2fd..ef9c6637f 100644 --- a/components/bldc_motor/example/main/bldc_motor_example.cpp +++ b/components/bldc_motor/example/main/bldc_motor_example.cpp @@ -113,10 +113,9 @@ extern "C" void app_main(void) { 320, // tested by running velocity_openloop and seeing if the velocity is ~correct .current_limit = 1.0f, // Amps .zero_electric_offset = 0.0f, // set to zero to always calibrate, since this is a test - // .zero_electric_offset = 2.3914752, // gotten from previously running without providing - // this - // // and it will be logged. - .sensor_direction = espp::detail::SensorDirection::COUNTER_CLOCKWISE, + .sensor_direction = espp::detail::SensorDirection::UNKNOWN, // set to unknown to always + // calibrate, since this is a + // test .foc_type = espp::detail::FocType::SPACE_VECTOR_PWM, .driver = driver, .sensor = mt6701, @@ -140,7 +139,7 @@ extern "C" void app_main(void) { .output_min = -20.0, // angle pid works on velocity (rad/s) .output_max = 20.0, // angle pid works on velocity (rad/s) }, - .log_level = espp::Logger::Verbosity::INFO}); + .log_level = espp::Logger::Verbosity::DEBUG}); static const auto motion_control_type = espp::detail::MotionControlType::VELOCITY; // static const auto motion_control_type = espp::detail::MotionControlType::ANGLE; @@ -152,6 +151,9 @@ extern "C" void app_main(void) { motor.set_motion_control_type(motion_control_type); std::atomic target = 0; + // enable the motor + motor.enable(); + auto motor_task_fn = [&motor, &target](std::mutex &m, std::condition_variable &cv) { static auto delay = std::chrono::duration(core_update_period); auto start = std::chrono::high_resolution_clock::now(); From 8353b9d410e7276a99ad8857b7e3b66f664e1d8e Mon Sep 17 00:00:00 2001 From: William Emfinger Date: Tue, 1 Aug 2023 09:40:48 -0500 Subject: [PATCH 4/5] 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. --- .../bldc_driver/include/bldc_driver.hpp | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/components/bldc_driver/include/bldc_driver.hpp b/components/bldc_driver/include/bldc_driver.hpp index 0f1285ec8..0ed9b5621 100644 --- a/components/bldc_driver/include/bldc_driver.hpp +++ b/components/bldc_driver/include/bldc_driver.hpp @@ -72,8 +72,16 @@ class BldcDriver { return; } logger_.info("Enabling"); - mcpwm_timer_enable(timer_); - mcpwm_timer_start_stop(timer_, MCPWM_TIMER_START_NO_STOP); + auto err = mcpwm_timer_enable(timer_); + if (err != ESP_OK) { + logger_.error("Failed to enable timer: {}", esp_err_to_name(err)); + return; + } + err = mcpwm_timer_start_stop(timer_, MCPWM_TIMER_START_NO_STOP); + if (err != ESP_OK) { + logger_.error("Failed to start timer: {}", esp_err_to_name(err)); + return; + } enabled_ = true; if (gpio_en_ >= 0) { gpio_set_level((gpio_num_t)gpio_en_, 1); @@ -294,23 +302,29 @@ class BldcDriver { } // A high / low - configure_generator_action(generators_[0], comparators_[0]); + configure_generator_action(generators_[0], generators_[1], comparators_[0]); configure_generator_deadtime(generators_[0], generators_[1]); // B high / low - configure_generator_action(generators_[2], comparators_[1]); + configure_generator_action(generators_[2], generators_[3], comparators_[1]); configure_generator_deadtime(generators_[2], generators_[3]); // C high / low - configure_generator_action(generators_[4], comparators_[2]); + configure_generator_action(generators_[4], generators_[5], comparators_[2]); configure_generator_deadtime(generators_[4], generators_[5]); } - void configure_generator_action(mcpwm_gen_handle_t &gen_high, mcpwm_cmpr_handle_t comp) { + void configure_generator_action(mcpwm_gen_handle_t &gen_high, mcpwm_gen_handle_t &gen_low, + mcpwm_cmpr_handle_t comp) { logger_.info("Setup generator action"); ESP_ERROR_CHECK(mcpwm_generator_set_actions_on_compare_event( gen_high, MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, comp, MCPWM_GEN_ACTION_LOW), MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_DOWN, comp, MCPWM_GEN_ACTION_HIGH), MCPWM_GEN_COMPARE_EVENT_ACTION_END())); + ESP_ERROR_CHECK(mcpwm_generator_set_actions_on_compare_event( + gen_low, + MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, comp, MCPWM_GEN_ACTION_LOW), + MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_DOWN, comp, MCPWM_GEN_ACTION_HIGH), + MCPWM_GEN_COMPARE_EVENT_ACTION_END())); } void configure_generator_deadtime(mcpwm_gen_handle_t &gen_high, mcpwm_gen_handle_t &gen_low) { From 0a7d004eb1ddbc49bbf0450b2bd3ffd1b17ed946 Mon Sep 17 00:00:00 2001 From: William Emfinger Date: Tue, 1 Aug 2023 09:41:42 -0500 Subject: [PATCH 5/5] example(bldc_haptics): update * Update example to always calibrate sensor direction (since its a test) * Updated example to debug log since its a test --- .../example/main/bldc_haptics_example.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/components/bldc_haptics/example/main/bldc_haptics_example.cpp b/components/bldc_haptics/example/main/bldc_haptics_example.cpp index 52e7b94bc..f2449d0d1 100644 --- a/components/bldc_haptics/example/main/bldc_haptics_example.cpp +++ b/components/bldc_haptics/example/main/bldc_haptics_example.cpp @@ -97,7 +97,7 @@ extern "C" void app_main(void) { .gpio_fault = 36, // connected to the nFAULT pin of TMC6300-BOB .power_supply_voltage = 5.0f, .limit_voltage = 5.0f, - .log_level = espp::Logger::Verbosity::WARN}); + .log_level = espp::Logger::Verbosity::DEBUG}); // now make the bldc motor using BldcMotor = espp::BldcMotor; @@ -111,10 +111,9 @@ extern "C" void app_main(void) { 320, // tested by running velocity_openloop and seeing if the velocity is ~correct .current_limit = 1.0f, // Amps .zero_electric_offset = 0.0f, // set to zero to always calibrate, since this is a test - // .zero_electric_offset = 2.3914752, // gotten from previously running without providing - // this - // // and it will be logged. - .sensor_direction = espp::detail::SensorDirection::COUNTER_CLOCKWISE, + .sensor_direction = + espp::detail::SensorDirection::UNKNOWN, // set to unknown to always calibrate, since + // this is a test .foc_type = espp::detail::FocType::SPACE_VECTOR_PWM, .driver = driver, .sensor = mt6701, @@ -138,7 +137,7 @@ extern "C" void app_main(void) { .output_min = -20.0, // angle pid works on velocity (rad/s) .output_max = 20.0, // angle pid works on velocity (rad/s) }, - .log_level = espp::Logger::Verbosity::INFO}); + .log_level = espp::Logger::Verbosity::DEBUG}); auto print_detent_config = [&logger](const auto &detent_config) { if (detent_config == espp::detail::UNBOUNDED_NO_DETENTS) {