Skip to content

Commit

Permalink
Update motor driver configuration to have macros for frequency and re…
Browse files Browse the repository at this point in the history
…solution
  • Loading branch information
SuperChamp234 committed Jan 31, 2024
1 parent bbc04fa commit 294b8ce
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
2 changes: 2 additions & 0 deletions include/motor_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
#define MOTOR_BACKWARD 201
#define MOTOR_STOP 202

#define MCPWM_FREQ 20000
#define MCPWM_RESOLUTION 10000000
/**
* @brief Enable motor driver
* @param motor_handle_t Motor object handle
Expand Down
8 changes: 4 additions & 4 deletions src/motor_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,26 @@ esp_err_t enable_motor_driver(motor_handle_t *motor, int motor_id) {
if(motor_id == MOTOR_A_0){
motor_config.pwma_gpio_num = MDA_NORMAL_IN_1,
motor_config.pwmb_gpio_num = MDA_NORMAL_IN_2,
motor_config.pwm_freq_hz = 20000; // 20kHz PWM frequency
motor_config.pwm_freq_hz = MCPWM_FREQ; // 20kHz PWM frequency
} else if(motor_id == MOTOR_A_1) {
motor_config.pwma_gpio_num = MDA_NORMAL_IN_3,
motor_config.pwmb_gpio_num = MDA_NORMAL_IN_4,
motor_config.pwm_freq_hz = 20000; // 20kHz PWM frequency
motor_config.pwm_freq_hz = MCPWM_FREQ; // 20kHz PWM frequency
} else {
ESP_LOGE(TAG, "Invalid motor id");
return ESP_FAIL;
}
motor_mcpwm_config_t motor_mcpwm_config;
motor_mcpwm_config.group_id = 0;
motor_mcpwm_config.resolution_hz = 10000000; // 10MHz
motor_mcpwm_config.resolution_hz = MCPWM_RESOLUTION; // 10MHz
ESP_ERROR_CHECK(motor_new_mcpwm_device(&motor_config, &motor_mcpwm_config, motor));
ESP_ERROR_CHECK((*motor)->enable(*motor));
enabled_motor_driver_flag = 1;
return ESP_OK;
}

esp_err_t set_motor_speed(motor_handle_t motor, int direction, float speed) {
speed = (uint32_t)map(speed, 0, 100, 0, 500);
speed = (uint32_t)map(speed, 0, 100, 0, MCPWM_RESOLUTION / MCPWM_FREQ);
if(direction == MOTOR_FORWARD){
motor->forward(motor);
motor->set_speed(motor, speed);
Expand Down

0 comments on commit 294b8ce

Please sign in to comment.