Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions components/bldc_driver/include/bldc_driver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -294,22 +302,28 @@ 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_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()));
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()));
}

Expand Down
13 changes: 7 additions & 6 deletions components/bldc_haptics/example/main/bldc_haptics_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<espp::BldcDriver, espp::Mt6701>;
Expand All @@ -109,10 +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.
.sensor_direction = espp::detail::SensorDirection::COUNTER_CLOCKWISE,
.current_limit = 1.0f, // Amps
.zero_electric_offset = 0.0f, // set to zero to always calibrate, since this is a test
.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,
Expand All @@ -136,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) {
Expand Down
14 changes: 9 additions & 5 deletions components/bldc_motor/example/main/bldc_motor_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +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.
.sensor_direction = espp::detail::SensorDirection::COUNTER_CLOCKWISE,
.current_limit = 1.0f, // Amps
.zero_electric_offset = 0.0f, // set to zero to always calibrate, since this is a test
.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,
Expand All @@ -138,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;
Expand All @@ -150,6 +151,9 @@ extern "C" void app_main(void) {
motor.set_motion_control_type(motion_control_type);
std::atomic<float> 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<float>(core_update_period);
auto start = std::chrono::high_resolution_clock::now();
Expand Down