From e668d2156937651251017f0c080900242af950bb Mon Sep 17 00:00:00 2001 From: purviyeshi <129578264+purviyeshi@users.noreply.github.com> Date: Fri, 5 Apr 2024 22:10:12 +0545 Subject: [PATCH 01/12] start porting servo.c code into esp idf v5.1 --- src/servo.c | 159 ++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 117 insertions(+), 42 deletions(-) diff --git a/src/servo.c b/src/servo.c index 9f8db4d4..182cb0f5 100644 --- a/src/servo.c +++ b/src/servo.c @@ -22,59 +22,139 @@ * SOFTWARE. */ +Online +YESTERDAY +YESTERDAY +Roll no wise change kela? +10:01 am +Ho +10:02 am +Suraj +Roll no wise change kela? +Sakshi la vichar na +10:13 am +Msg kr atach +10:13 am +Suraj +Roll no wise change kela? +Okumura nahi pahijey +10:29 am +Okay +10:30 am +1:10 pm +. +4:31 pm +ESP_LOGI(TAG, "Create comparators and generators from the operator"); + mcpwm_cmpr_handle_t comparators[5] = {NULL}; + mcpwm_gen_handle_t generators[5] = {NULL}; + + for (int i = 0; i < 5; i++) { + mcpwm_comparator_config_t comparator_config = { + .flags.update_cmp_on_tez = true, + }; + ESP_ERROR_CHECK(mcpwm_new_comparator(oper, &comparator_config, &comparators[i])); + + mcpwm_generator_config_t generator_config = { + .gen_gpio_num = i == 0 ? SERVO_PULSE_GPIO_1 : (i == 1 ? SERVO_PULSE_GPIO_2 : (i == 2 ? SERVO_PULSE_GPIO_3 : (i == 3 ? SERVO_PULSE_GPIO_4 : SERVO_PULSE_GPIO_5))), + }; + ESP_ERROR_CHECK(mcpwm_new_generator(oper, &generator_config, &generators[i])); + } + + for (int i = 0; i < 5; i++) { + ESP_ERROR_CHECK(mcpwm_comparator_set_compare_value(comparators[i], example_angle_to_compare(0))); + ESP_ERROR_CHECK(mcpwm_generator_set_action_on_timer_event(generators[i], + MCPWM_GEN_TIMER_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, MCPWM_TIMER_EVENT_EMPTY, MCPWM_GEN_ACTION_HIGH))); + } + + for (int i = 0; i < 5; i++) { + ESP_ERROR_CHECK(mcpwm_timer_enable(timer)); + ESP_ERROR_CHECK(mcpwm_timer_start_stop(timer, MCPWM_TIMER_START_NO_STOP)); + } +8:44 pm +mcpwm_generator_config_t generator_config = { + .gen_gpio_num = (i == 0 ? SERVO_PULSE_GPIO_2 : (i == 1 ? SERVO_PULSE_GPIO_3 : (i == 2 ? SERVO_PULSE_GPIO_4 : SERVO_PULSE_GPIO_5))), + }; +8:54 pm +MARIO.zip +ZIP•438 MB +9:03 pm #include "servo.h" static const char *TAG_SERVO = "servo"; static int enabled_servo_flag = 0; -#define STR(A) #A + +#define SERVO_TIMEBASE_RESOLUTION_HZ 1000000 // 1MHz, 1us per tick +#define SERVO_TIMEBASE_PERIOD 20000 // 20000 ticks, 20ms + +mcpwm_cmpr_handle_t comparators[4] = {NULL}; esp_err_t enable_servo() { - esp_err_t err; - CHECK_LOGE(err, mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM0A, SERVO_A), TAG_SERVO, "error: servo A: %s", esp_err_to_name(err)); - CHECK_LOGE(err, mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM0B, SERVO_B), TAG_SERVO, "error: servo B: %s", esp_err_to_name(err)); - CHECK_LOGE(err, mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM1A, SERVO_C), TAG_SERVO, "error: servo C: %s", esp_err_to_name(err)); - CHECK_LOGE(err, mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM1B, SERVO_D), TAG_SERVO, "error: servo D: %s", esp_err_to_name(err)); - - mcpwm_config_t pwm_config; - // sets the pwm frequency = 50 - pwm_config.frequency = 50; - // sets the initial duty cycle of PWMxA = 0 - pwm_config.cmpr_a = 0; - // sets the initial duty cycle of PWMxB = 0 - pwm_config.cmpr_b = 0; - // sets the pwm counter mode - pwm_config.counter_mode = MCPWM_UP_COUNTER; - // sets the pwm duty mode - pwm_config.duty_mode = MCPWM_DUTY_MODE_0; - - // init pwm 0a, 1a, 2a with the above settings - - esp_err_t err_A = mcpwm_init(MCPWM_UNIT_0, MCPWM_TIMER_0, &pwm_config); - esp_err_t err_B = mcpwm_init(MCPWM_UNIT_0, MCPWM_TIMER_1, &pwm_config); - - if (err_A == ESP_OK && err_B == ESP_OK) - { - enabled_servo_flag = 1; - ESP_LOGI(TAG_SERVO, "enabled servos"); + ESP_LOGI(TAG_SERVO, "Create timer and operator"); + mcpwm_timer_handle_t timer = NULL; + mcpwm_timer_config_t timer_config = { + .group_id = 0, + .clk_src = MCPWM_TIMER_CLK_SRC_DEFAULT, + .resolution_hz = SERVO_TIMEBASE_RESOLUTION_HZ, + .period_ticks = SERVO_TIMEBASE_PERIOD, + .count_mode = MCPWM_TIMER_COUNT_MODE_UP, + }; + + ESP_ERROR_CHECK(mcpwm_new_timer(&timer_config, &timer)); + + mcpwm_oper_handle_t oper = NULL; + mcpwm_operator_config_t operator_config = { + .group_id = 0, // operator must be in the same group as the timer + }; + + ESP_ERROR_CHECK(mcpwm_new_operator(&operator_config, &oper)); + + ESP_LOGI(TAG_SERVO, "Connect timer and operator"); + ESP_ERROR_CHECK(mcpwm_operator_connect_timer(oper, timer)); + + ESP_LOGI(TAG_SERVO, "Create comparators and generators from the operator"); + mcpwm_gen_handle_t generators[4] = {NULL}; - return ESP_OK; + for (int i = 0; i < 4; i++) { + // Check if the comparator is already initialized + if (comparators[i] == NULL) { + mcpwm_comparator_config_t comparator_config = { + .flags.update_cmp_on_tez = true, + }; + ESP_ERROR_CHECK(mcpwm_new_comparator(oper, &comparator_config, &comparators[i])); + } + + mcpwm_generator_config_t generator_config = { + .gen_gpio_num = (i == 0 ? SERVO_A : (i == 1 ? SERVO_B : (i == 2 ? SERVO_C : SERVO_D))), + }; + + ESP_ERROR_CHECK(mcpwm_new_generator(oper, &generator_config, &generators[i])); } - else - { - enabled_servo_flag = 0; - return ESP_FAIL; + + for (int i = 0; i < 4; i++) { + ESP_ERROR_CHECK(mcpwm_generator_set_action_on_timer_event(generators[i], + MCPWM_GEN_TIMER_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, MCPWM_TIMER_EVENT_EMPTY, MCPWM_GEN_ACTION_HIGH))); } -} -static esp_err_t set_angle_servo_helper(int servo_pin, int servo_max, int servo_min_pulsewidth, int servo_max_pulsewidth, unsigned int degree_of_rotation, mcpwm_unit_t mcpwm_num, mcpwm_timer_t timer_num, mcpwm_generator_t gen) + for (int i = 0; i < 4; i++) { + ESP_ERROR_CHECK(mcpwm_timer_enable(timer)); + ESP_ERROR_CHECK(mcpwm_timer_start_stop(timer, MCPWM_TIMER_START_NO_STOP)); + } + + // Set the flag to indicate that servos are enabled + enabled_servo_flag = 1; + + return ESP_OK; +} + +static esp_err_t set_angle_servo_helper(int servo_pin, int servo_max, int servo_min_pulsewidth, int servo_max_pulsewidth, int cmp_num, unsigned int degree_of_rotation) { degree_of_rotation = degree_of_rotation > servo_max ? servo_max : degree_of_rotation; uint32_t cal_pulsewidth = 0; cal_pulsewidth = (servo_min_pulsewidth + ((servo_max_pulsewidth - servo_min_pulsewidth) * (degree_of_rotation)) / (servo_max)); - esp_err_t err = mcpwm_set_duty_in_us(mcpwm_num, timer_num, gen, cal_pulsewidth); + esp_err_t err = mcpwm_comparator_set_compare_value(comparators[cmp_num], cal_pulsewidth); if (err == ESP_OK) { ESP_LOGI(TAG_SERVO, "set servo at pin %d: %ud", servo_pin, degree_of_rotation); @@ -94,7 +174,7 @@ esp_err_t set_angle_servo(servo_config *config, unsigned int degree_of_rotation) if (config->servo_pin) { config->angle = degree_of_rotation; - return set_angle_servo_helper(config->servo_pin, config->max_degree, config->min_pulse_width, config->max_pulse_width, degree_of_rotation, config->mcpwm_num, config->timer_num, config->gen); + return set_angle_servo_helper(config->servo_pin, config->max_degree, config->min_pulse_width, config->max_pulse_width, config->cmp_num, degree_of_rotation); } else { @@ -108,8 +188,3 @@ esp_err_t set_angle_servo(servo_config *config, unsigned int degree_of_rotation) return ESP_FAIL; } } - -int read_servo(servo_config *config) -{ - return config->angle; -} From 5505b86e3b21a8b8a6047391b8c3a808604f62b1 Mon Sep 17 00:00:00 2001 From: purviyeshi <129578264+purviyeshi@users.noreply.github.com> Date: Fri, 5 Apr 2024 22:35:30 +0545 Subject: [PATCH 02/12] start porting servo.h code into esp idf v5.1 --- include/servo.h | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/include/servo.h b/include/servo.h index e3a75361..91ffce70 100644 --- a/include/servo.h +++ b/include/servo.h @@ -27,9 +27,8 @@ #include #include "freertos/FreeRTOS.h" -#include "driver/mcpwm.h" -#include "soc/mcpwm_periph.h" #include "esp_attr.h" +#include "driver/mcpwm_prelude.h" #include "sdkconfig.h" #include "esp_log.h" @@ -43,9 +42,7 @@ typedef struct int max_pulse_width; int max_degree; int angle; - mcpwm_unit_t mcpwm_num; - mcpwm_timer_t timer_num; - mcpwm_generator_t gen; + int cmp_num; } servo_config; /** @struct servo_config @@ -88,4 +85,4 @@ esp_err_t set_angle_servo(servo_config *config, unsigned int degree_of_rotation) */ int read_servo(servo_config *config); -#endif \ No newline at end of file +#endif From 47fee7ad3edb58ce668102434a8da5900bcdfa80 Mon Sep 17 00:00:00 2001 From: purviyeshi <129578264+purviyeshi@users.noreply.github.com> Date: Fri, 5 Apr 2024 22:51:43 +0545 Subject: [PATCH 03/12] start porting servo.c code into esp idf v5.1 --- src/servo.c | 58 +---------------------------------------------------- 1 file changed, 1 insertion(+), 57 deletions(-) diff --git a/src/servo.c b/src/servo.c index 182cb0f5..ae83e8e6 100644 --- a/src/servo.c +++ b/src/servo.c @@ -22,62 +22,6 @@ * SOFTWARE. */ -Online -YESTERDAY -YESTERDAY -Roll no wise change kela? -10:01 am -Ho -10:02 am -Suraj -Roll no wise change kela? -Sakshi la vichar na -10:13 am -Msg kr atach -10:13 am -Suraj -Roll no wise change kela? -Okumura nahi pahijey -10:29 am -Okay -10:30 am -1:10 pm -. -4:31 pm -ESP_LOGI(TAG, "Create comparators and generators from the operator"); - mcpwm_cmpr_handle_t comparators[5] = {NULL}; - mcpwm_gen_handle_t generators[5] = {NULL}; - - for (int i = 0; i < 5; i++) { - mcpwm_comparator_config_t comparator_config = { - .flags.update_cmp_on_tez = true, - }; - ESP_ERROR_CHECK(mcpwm_new_comparator(oper, &comparator_config, &comparators[i])); - - mcpwm_generator_config_t generator_config = { - .gen_gpio_num = i == 0 ? SERVO_PULSE_GPIO_1 : (i == 1 ? SERVO_PULSE_GPIO_2 : (i == 2 ? SERVO_PULSE_GPIO_3 : (i == 3 ? SERVO_PULSE_GPIO_4 : SERVO_PULSE_GPIO_5))), - }; - ESP_ERROR_CHECK(mcpwm_new_generator(oper, &generator_config, &generators[i])); - } - - for (int i = 0; i < 5; i++) { - ESP_ERROR_CHECK(mcpwm_comparator_set_compare_value(comparators[i], example_angle_to_compare(0))); - ESP_ERROR_CHECK(mcpwm_generator_set_action_on_timer_event(generators[i], - MCPWM_GEN_TIMER_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, MCPWM_TIMER_EVENT_EMPTY, MCPWM_GEN_ACTION_HIGH))); - } - - for (int i = 0; i < 5; i++) { - ESP_ERROR_CHECK(mcpwm_timer_enable(timer)); - ESP_ERROR_CHECK(mcpwm_timer_start_stop(timer, MCPWM_TIMER_START_NO_STOP)); - } -8:44 pm -mcpwm_generator_config_t generator_config = { - .gen_gpio_num = (i == 0 ? SERVO_PULSE_GPIO_2 : (i == 1 ? SERVO_PULSE_GPIO_3 : (i == 2 ? SERVO_PULSE_GPIO_4 : SERVO_PULSE_GPIO_5))), - }; -8:54 pm -MARIO.zip -ZIP•438 MB -9:03 pm #include "servo.h" static const char *TAG_SERVO = "servo"; @@ -169,7 +113,7 @@ static esp_err_t set_angle_servo_helper(int servo_pin, int servo_max, int servo_ esp_err_t set_angle_servo(servo_config *config, unsigned int degree_of_rotation) { - if (enabled_servo_flag) + if (enabled_servo_flag) { if (config->servo_pin) { From ad773320b63a9a868f3777e82417829d0afc6085 Mon Sep 17 00:00:00 2001 From: purviyeshi <129578264+purviyeshi@users.noreply.github.com> Date: Mon, 8 Apr 2024 00:13:03 +0545 Subject: [PATCH 04/12] Port servo.c code into esp idf v5.1 --- src/servo.c | 146 ++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 113 insertions(+), 33 deletions(-) diff --git a/src/servo.c b/src/servo.c index ae83e8e6..cc27b047 100644 --- a/src/servo.c +++ b/src/servo.c @@ -30,11 +30,12 @@ static int enabled_servo_flag = 0; #define SERVO_TIMEBASE_RESOLUTION_HZ 1000000 // 1MHz, 1us per tick #define SERVO_TIMEBASE_PERIOD 20000 // 20000 ticks, 20ms -mcpwm_cmpr_handle_t comparators[4] = {NULL}; +mcpwm_cmpr_handle_t comparator_0= NULL; +mcpwm_cmpr_handle_t comparator_1= NULL; esp_err_t enable_servo() { - ESP_LOGI(TAG_SERVO, "Create timer and operator"); + ESP_LOGI(TAG_SERVO, "Create timer and operator for servos A and B"); mcpwm_timer_handle_t timer = NULL; mcpwm_timer_config_t timer_config = { .group_id = 0, @@ -43,53 +44,118 @@ esp_err_t enable_servo() .period_ticks = SERVO_TIMEBASE_PERIOD, .count_mode = MCPWM_TIMER_COUNT_MODE_UP, }; - ESP_ERROR_CHECK(mcpwm_new_timer(&timer_config, &timer)); mcpwm_oper_handle_t oper = NULL; mcpwm_operator_config_t operator_config = { .group_id = 0, // operator must be in the same group as the timer }; - ESP_ERROR_CHECK(mcpwm_new_operator(&operator_config, &oper)); - ESP_LOGI(TAG_SERVO, "Connect timer and operator"); + ESP_LOGI(TAG_SERVO, "Connect timer and operator for servos A and B"); ESP_ERROR_CHECK(mcpwm_operator_connect_timer(oper, timer)); - ESP_LOGI(TAG_SERVO, "Create comparators and generators from the operator"); - mcpwm_gen_handle_t generators[4] = {NULL}; + ESP_LOGI(TAG_SERVO, "Create comparator_0 and generator from the operator for servos A and B"); - for (int i = 0; i < 4; i++) { - // Check if the comparator is already initialized - if (comparators[i] == NULL) { - mcpwm_comparator_config_t comparator_config = { - .flags.update_cmp_on_tez = true, - }; - ESP_ERROR_CHECK(mcpwm_new_comparator(oper, &comparator_config, &comparators[i])); - } + mcpwm_comparator_config_t comparator_config = { + .flags.update_cmp_on_tez = true, + }; + ESP_ERROR_CHECK(mcpwm_new_comparator(oper, &comparator_config, &comparator_0)); - mcpwm_generator_config_t generator_config = { - .gen_gpio_num = (i == 0 ? SERVO_A : (i == 1 ? SERVO_B : (i == 2 ? SERVO_C : SERVO_D))), - }; + mcpwm_gen_handle_t generator = NULL; + mcpwm_generator_config_t generator_config = { + .gen_gpio_num = SERVO_A, + }; + ESP_ERROR_CHECK(mcpwm_new_generator(oper, &generator_config, &generator)); - ESP_ERROR_CHECK(mcpwm_new_generator(oper, &generator_config, &generators[i])); - } - - for (int i = 0; i < 4; i++) { - ESP_ERROR_CHECK(mcpwm_generator_set_action_on_timer_event(generators[i], - MCPWM_GEN_TIMER_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, MCPWM_TIMER_EVENT_EMPTY, MCPWM_GEN_ACTION_HIGH))); - } + // Similarly, create generator B for SERVO_B + mcpwm_gen_handle_t generator_b = NULL; + mcpwm_generator_config_t generator_config_b = { + .gen_gpio_num = SERVO_B, + }; + ESP_ERROR_CHECK(mcpwm_new_generator(oper, &generator_config_b, &generator_b)); - for (int i = 0; i < 4; i++) { - ESP_ERROR_CHECK(mcpwm_timer_enable(timer)); - ESP_ERROR_CHECK(mcpwm_timer_start_stop(timer, MCPWM_TIMER_START_NO_STOP)); - } + // Set actions for generator A and B + ESP_ERROR_CHECK(mcpwm_generator_set_action_on_timer_event(generator, + MCPWM_GEN_TIMER_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, MCPWM_TIMER_EVENT_EMPTY, MCPWM_GEN_ACTION_HIGH))); + ESP_ERROR_CHECK(mcpwm_generator_set_action_on_compare_event(generator, + MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, comparator_0, MCPWM_GEN_ACTION_LOW))); + + ESP_ERROR_CHECK(mcpwm_generator_set_action_on_timer_event(generator_b, + MCPWM_GEN_TIMER_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, MCPWM_TIMER_EVENT_EMPTY, MCPWM_GEN_ACTION_HIGH))); + ESP_ERROR_CHECK(mcpwm_generator_set_action_on_compare_event(generator_b, + MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, comparator_0, MCPWM_GEN_ACTION_LOW))); + + + // Now, create a new timer, operator, and generators for servos C and D + ESP_LOGI(TAG_SERVO, "Create timer and operator for servos C and D"); + mcpwm_timer_handle_t timer_1 = NULL; + mcpwm_timer_config_t timer_config_1 = { + .group_id = 1, + .clk_src = MCPWM_TIMER_CLK_SRC_DEFAULT, + .resolution_hz = SERVO_TIMEBASE_RESOLUTION_HZ, + .period_ticks = SERVO_TIMEBASE_PERIOD, + .count_mode = MCPWM_TIMER_COUNT_MODE_UP, + }; + ESP_ERROR_CHECK(mcpwm_new_timer(&timer_config_1, &timer_1)); + + mcpwm_oper_handle_t oper_1 = NULL; + mcpwm_operator_config_t operator_config_1 = { + .group_id = 1, // operator must be in the same group as the timer + }; + ESP_ERROR_CHECK(mcpwm_new_operator(&operator_config_1, &oper_1)); + + ESP_LOGI(TAG_SERVO, "Connect timer and operator for servos C and D"); + ESP_ERROR_CHECK(mcpwm_operator_connect_timer(oper_1, timer_1)); + + ESP_LOGI(TAG_SERVO, "Create comparator and generator from the operator for servos C and D"); + + mcpwm_comparator_config_t comparator_config_1 = { + .flags.update_cmp_on_tez = true, + }; + ESP_ERROR_CHECK(mcpwm_new_comparator(oper_1, &comparator_config_1, &comparator_1)); + + + // Similarly, create generator C for SERVO_C + mcpwm_gen_handle_t generator_c = NULL; + mcpwm_generator_config_t generator_config_c = { + .gen_gpio_num = SERVO_C, + }; + + ESP_ERROR_CHECK(mcpwm_new_generator(oper_1, &generator_config_c, &generator_c)); + + // Similarly, create generator D for SERVO_D + mcpwm_gen_handle_t generator_d = NULL; + mcpwm_generator_config_t generator_config_d = { + .gen_gpio_num = SERVO_D, + }; + ESP_ERROR_CHECK(mcpwm_new_generator(oper_1, &generator_config_d, &generator_d)); + + // Set actions for generator C and D + ESP_ERROR_CHECK(mcpwm_generator_set_action_on_timer_event(generator_c, + MCPWM_GEN_TIMER_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, MCPWM_TIMER_EVENT_EMPTY, MCPWM_GEN_ACTION_HIGH))); + ESP_ERROR_CHECK(mcpwm_generator_set_action_on_compare_event(generator_c, + MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, comparator_1, MCPWM_GEN_ACTION_LOW))); + + ESP_ERROR_CHECK(mcpwm_generator_set_action_on_timer_event(generator_d, + MCPWM_GEN_TIMER_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, MCPWM_TIMER_EVENT_EMPTY, MCPWM_GEN_ACTION_HIGH))); + ESP_ERROR_CHECK(mcpwm_generator_set_action_on_compare_event(generator_d, + MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, comparator_1, MCPWM_GEN_ACTION_LOW))); + + + // Enable and start both timers + ESP_LOGI(TAG_SERVO, "Enable and start timers"); + ESP_ERROR_CHECK(mcpwm_timer_enable(timer)); + ESP_ERROR_CHECK(mcpwm_timer_enable(timer_1)); + + ESP_ERROR_CHECK(mcpwm_timer_start_stop(timer, MCPWM_TIMER_START_NO_STOP)); + ESP_ERROR_CHECK(mcpwm_timer_start_stop(timer_1, MCPWM_TIMER_START_NO_STOP)); // Set the flag to indicate that servos are enabled enabled_servo_flag = 1; return ESP_OK; -} +} static esp_err_t set_angle_servo_helper(int servo_pin, int servo_max, int servo_min_pulsewidth, int servo_max_pulsewidth, int cmp_num, unsigned int degree_of_rotation) { @@ -98,10 +164,23 @@ static esp_err_t set_angle_servo_helper(int servo_pin, int servo_max, int servo_ uint32_t cal_pulsewidth = 0; cal_pulsewidth = (servo_min_pulsewidth + ((servo_max_pulsewidth - servo_min_pulsewidth) * (degree_of_rotation)) / (servo_max)); - esp_err_t err = mcpwm_comparator_set_compare_value(comparators[cmp_num], cal_pulsewidth); + + + esp_err_t err; + if (cmp_num == 0) + { + err = mcpwm_comparator_set_compare_value(comparator_0, cal_pulsewidth); + } + else + { + err = mcpwm_comparator_set_compare_value(comparator_1, cal_pulsewidth); + } + + + if (err == ESP_OK) { - ESP_LOGI(TAG_SERVO, "set servo at pin %d: %ud", servo_pin, degree_of_rotation); + ESP_LOGI(TAG_SERVO, "set servo at pin %d: %d", servo_pin, degree_of_rotation); } else { @@ -113,7 +192,7 @@ static esp_err_t set_angle_servo_helper(int servo_pin, int servo_max, int servo_ esp_err_t set_angle_servo(servo_config *config, unsigned int degree_of_rotation) { - if (enabled_servo_flag) + if (enabled_servo_flag) { if (config->servo_pin) { @@ -132,3 +211,4 @@ esp_err_t set_angle_servo(servo_config *config, unsigned int degree_of_rotation) return ESP_FAIL; } } + From 70fa202f746f282a6e384c51dc616e3d6cf26c26 Mon Sep 17 00:00:00 2001 From: purviyeshi <129578264+purviyeshi@users.noreply.github.com> Date: Mon, 8 Apr 2024 00:14:20 +0545 Subject: [PATCH 05/12] Port servo.h code into esp idf v5.1 --- include/servo.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/include/servo.h b/include/servo.h index 91ffce70..ce164684 100644 --- a/include/servo.h +++ b/include/servo.h @@ -30,6 +30,7 @@ #include "esp_attr.h" #include "driver/mcpwm_prelude.h" + #include "sdkconfig.h" #include "esp_log.h" #include "esp_err.h" @@ -45,6 +46,9 @@ typedef struct int cmp_num; } servo_config; + + + /** @struct servo_config * @brief This structure contains the configuration of servos * @var servo_config::servo_pin From 98e2a5730cf1fc9edebe4617870ca352d4a06902 Mon Sep 17 00:00:00 2001 From: purviyeshi <129578264+purviyeshi@users.noreply.github.com> Date: Fri, 12 Apr 2024 00:54:29 +0530 Subject: [PATCH 06/12] modify servo.c code to identify generators for 4 servos --- src/servo.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/servo.c b/src/servo.c index cc27b047..697f669c 100644 --- a/src/servo.c +++ b/src/servo.c @@ -62,11 +62,11 @@ esp_err_t enable_servo() }; ESP_ERROR_CHECK(mcpwm_new_comparator(oper, &comparator_config, &comparator_0)); - mcpwm_gen_handle_t generator = NULL; - mcpwm_generator_config_t generator_config = { + mcpwm_gen_handle_t generator_a = NULL; + mcpwm_generator_config_t generator_config_a = { .gen_gpio_num = SERVO_A, }; - ESP_ERROR_CHECK(mcpwm_new_generator(oper, &generator_config, &generator)); + ESP_ERROR_CHECK(mcpwm_new_generator(oper, &generator_config_a, &generator_a)); // Similarly, create generator B for SERVO_B mcpwm_gen_handle_t generator_b = NULL; @@ -76,9 +76,9 @@ esp_err_t enable_servo() ESP_ERROR_CHECK(mcpwm_new_generator(oper, &generator_config_b, &generator_b)); // Set actions for generator A and B - ESP_ERROR_CHECK(mcpwm_generator_set_action_on_timer_event(generator, + ESP_ERROR_CHECK(mcpwm_generator_set_action_on_timer_event(generator_a, MCPWM_GEN_TIMER_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, MCPWM_TIMER_EVENT_EMPTY, MCPWM_GEN_ACTION_HIGH))); - ESP_ERROR_CHECK(mcpwm_generator_set_action_on_compare_event(generator, + ESP_ERROR_CHECK(mcpwm_generator_set_action_on_compare_event(generator_a, MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, comparator_0, MCPWM_GEN_ACTION_LOW))); ESP_ERROR_CHECK(mcpwm_generator_set_action_on_timer_event(generator_b, @@ -211,4 +211,3 @@ esp_err_t set_angle_servo(servo_config *config, unsigned int degree_of_rotation) return ESP_FAIL; } } - From c15ace8b2bee54b5d6f6dffcd209a9dc3967c437 Mon Sep 17 00:00:00 2001 From: purviyeshi <129578264+purviyeshi@users.noreply.github.com> Date: Sat, 13 Apr 2024 08:15:59 +0545 Subject: [PATCH 07/12] add separate comparator for each servo --- src/servo.c | 63 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 41 insertions(+), 22 deletions(-) diff --git a/src/servo.c b/src/servo.c index 697f669c..a129f814 100644 --- a/src/servo.c +++ b/src/servo.c @@ -1,4 +1,4 @@ -/* +/ * MIT License * * Copyright (c) 2021 Society of Robotics and Automation @@ -30,8 +30,10 @@ static int enabled_servo_flag = 0; #define SERVO_TIMEBASE_RESOLUTION_HZ 1000000 // 1MHz, 1us per tick #define SERVO_TIMEBASE_PERIOD 20000 // 20000 ticks, 20ms -mcpwm_cmpr_handle_t comparator_0= NULL; -mcpwm_cmpr_handle_t comparator_1= NULL; +mcpwm_cmpr_handle_t comparator_a= NULL; +mcpwm_cmpr_handle_t comparator_b= NULL; +mcpwm_cmpr_handle_t comparator_c= NULL; +mcpwm_cmpr_handle_t comparator_d= NULL; esp_err_t enable_servo() { @@ -57,10 +59,15 @@ esp_err_t enable_servo() ESP_LOGI(TAG_SERVO, "Create comparator_0 and generator from the operator for servos A and B"); - mcpwm_comparator_config_t comparator_config = { + mcpwm_comparator_config_t comparator_config_a = { .flags.update_cmp_on_tez = true, }; - ESP_ERROR_CHECK(mcpwm_new_comparator(oper, &comparator_config, &comparator_0)); + ESP_ERROR_CHECK(mcpwm_new_comparator(oper, &comparator_config_a, &comparator_a)); + + mcpwm_comparator_config_t comparator_config_b = { + .flags.update_cmp_on_tez = true, + }; + ESP_ERROR_CHECK(mcpwm_new_comparator(oper, &comparator_config_b, &comparator_b)); mcpwm_gen_handle_t generator_a = NULL; mcpwm_generator_config_t generator_config_a = { @@ -79,12 +86,12 @@ esp_err_t enable_servo() ESP_ERROR_CHECK(mcpwm_generator_set_action_on_timer_event(generator_a, MCPWM_GEN_TIMER_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, MCPWM_TIMER_EVENT_EMPTY, MCPWM_GEN_ACTION_HIGH))); ESP_ERROR_CHECK(mcpwm_generator_set_action_on_compare_event(generator_a, - MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, comparator_0, MCPWM_GEN_ACTION_LOW))); + MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, comparator_a, MCPWM_GEN_ACTION_LOW))); ESP_ERROR_CHECK(mcpwm_generator_set_action_on_timer_event(generator_b, MCPWM_GEN_TIMER_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, MCPWM_TIMER_EVENT_EMPTY, MCPWM_GEN_ACTION_HIGH))); ESP_ERROR_CHECK(mcpwm_generator_set_action_on_compare_event(generator_b, - MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, comparator_0, MCPWM_GEN_ACTION_LOW))); + MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, comparator_b, MCPWM_GEN_ACTION_LOW))); // Now, create a new timer, operator, and generators for servos C and D @@ -110,10 +117,15 @@ esp_err_t enable_servo() ESP_LOGI(TAG_SERVO, "Create comparator and generator from the operator for servos C and D"); - mcpwm_comparator_config_t comparator_config_1 = { + mcpwm_comparator_config_t comparator_config_c = { + .flags.update_cmp_on_tez = true, + }; + ESP_ERROR_CHECK(mcpwm_new_comparator(oper_1, &comparator_config_1, &comparator_c)); + + mcpwm_comparator_config_t comparator_config_d = { .flags.update_cmp_on_tez = true, }; - ESP_ERROR_CHECK(mcpwm_new_comparator(oper_1, &comparator_config_1, &comparator_1)); + ESP_ERROR_CHECK(mcpwm_new_comparator(oper_1, &comparator_config_1, &comparator_d)); // Similarly, create generator C for SERVO_C @@ -135,12 +147,12 @@ esp_err_t enable_servo() ESP_ERROR_CHECK(mcpwm_generator_set_action_on_timer_event(generator_c, MCPWM_GEN_TIMER_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, MCPWM_TIMER_EVENT_EMPTY, MCPWM_GEN_ACTION_HIGH))); ESP_ERROR_CHECK(mcpwm_generator_set_action_on_compare_event(generator_c, - MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, comparator_1, MCPWM_GEN_ACTION_LOW))); + MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, comparator_c, MCPWM_GEN_ACTION_LOW))); ESP_ERROR_CHECK(mcpwm_generator_set_action_on_timer_event(generator_d, MCPWM_GEN_TIMER_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, MCPWM_TIMER_EVENT_EMPTY, MCPWM_GEN_ACTION_HIGH))); ESP_ERROR_CHECK(mcpwm_generator_set_action_on_compare_event(generator_d, - MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, comparator_1, MCPWM_GEN_ACTION_LOW))); + MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, comparator_d, MCPWM_GEN_ACTION_LOW))); // Enable and start both timers @@ -157,7 +169,7 @@ esp_err_t enable_servo() return ESP_OK; } -static esp_err_t set_angle_servo_helper(int servo_pin, int servo_max, int servo_min_pulsewidth, int servo_max_pulsewidth, int cmp_num, unsigned int degree_of_rotation) +static esp_err_t set_angle_servo_helper(int servo_pin, int servo_max, int servo_min_pulsewidth, int servo_max_pulsewidth, unsigned int degree_of_rotation) { degree_of_rotation = degree_of_rotation > servo_max ? servo_max : degree_of_rotation; @@ -167,17 +179,24 @@ static esp_err_t set_angle_servo_helper(int servo_pin, int servo_max, int servo_ esp_err_t err; - if (cmp_num == 0) - { - err = mcpwm_comparator_set_compare_value(comparator_0, cal_pulsewidth); - } - else - { - err = mcpwm_comparator_set_compare_value(comparator_1, cal_pulsewidth); + switch(servo_pin) { + case SERVO_A: + err = mcpwm_comparator_set_compare_value(comparator_a, cal_pulsewidth); + break; + case SERVO_B: + err = mcpwm_comparator_set_compare_value(comparator_b, cal_pulsewidth); + break; + case SERVO_C: + err = mcpwm_comparator_set_compare_value(comparator_c, cal_pulsewidth); + break; + case SERVO_D: + err = mcpwm_comparator_set_compare_value(comparator_d, cal_pulsewidth); + break; + default: + err = ESP_ERR_INVALID_ARG; + break; } - - if (err == ESP_OK) { ESP_LOGI(TAG_SERVO, "set servo at pin %d: %d", servo_pin, degree_of_rotation); @@ -197,7 +216,7 @@ esp_err_t set_angle_servo(servo_config *config, unsigned int degree_of_rotation) if (config->servo_pin) { config->angle = degree_of_rotation; - return set_angle_servo_helper(config->servo_pin, config->max_degree, config->min_pulse_width, config->max_pulse_width, config->cmp_num, degree_of_rotation); + return set_angle_servo_helper(config->servo_pin, config->max_degree, config->min_pulse_width, config->max_pulse_width, degree_of_rotation); } else { From 4dbfa517d471d4a2eafdaa4704e2bf0bb0f33fe8 Mon Sep 17 00:00:00 2001 From: purviyeshi <129578264+purviyeshi@users.noreply.github.com> Date: Sat, 13 Apr 2024 08:23:05 +0545 Subject: [PATCH 08/12] remove cmp_num variable and #endif line --- include/servo.h | 3 --- 1 file changed, 3 deletions(-) diff --git a/include/servo.h b/include/servo.h index ce164684..145197e2 100644 --- a/include/servo.h +++ b/include/servo.h @@ -43,7 +43,6 @@ typedef struct int max_pulse_width; int max_degree; int angle; - int cmp_num; } servo_config; @@ -88,5 +87,3 @@ esp_err_t set_angle_servo(servo_config *config, unsigned int degree_of_rotation) * @return esp_err_t */ int read_servo(servo_config *config); - -#endif From 360695893aa55437a9fd7cb1756ab69bf58e9a61 Mon Sep 17 00:00:00 2001 From: purviyeshi <129578264+purviyeshi@users.noreply.github.com> Date: Sat, 13 Apr 2024 14:56:30 +0545 Subject: [PATCH 09/12] correct a silly mistake --- include/servo.h | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/include/servo.h b/include/servo.h index 145197e2..64942365 100644 --- a/include/servo.h +++ b/include/servo.h @@ -42,12 +42,8 @@ typedef struct int min_pulse_width; int max_pulse_width; int max_degree; - int angle; } servo_config; - - - /** @struct servo_config * @brief This structure contains the configuration of servos * @var servo_config::servo_pin @@ -87,3 +83,5 @@ esp_err_t set_angle_servo(servo_config *config, unsigned int degree_of_rotation) * @return esp_err_t */ int read_servo(servo_config *config); + +#endif From a3144e54b20cd432eaaf47b45c551a1b4f201b31 Mon Sep 17 00:00:00 2001 From: purviyeshi <129578264+purviyeshi@users.noreply.github.com> Date: Sat, 13 Apr 2024 22:57:34 +0530 Subject: [PATCH 10/12] Add tested code --- src/servo.c | 142 ++++++++++++++++++++++------------------------------ 1 file changed, 61 insertions(+), 81 deletions(-) diff --git a/src/servo.c b/src/servo.c index a129f814..4208d8d5 100644 --- a/src/servo.c +++ b/src/servo.c @@ -1,40 +1,16 @@ -/ - * MIT License - * - * Copyright (c) 2021 Society of Robotics and Automation - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - #include "servo.h" - + static const char *TAG_SERVO = "servo"; static int enabled_servo_flag = 0; - + #define SERVO_TIMEBASE_RESOLUTION_HZ 1000000 // 1MHz, 1us per tick #define SERVO_TIMEBASE_PERIOD 20000 // 20000 ticks, 20ms - + mcpwm_cmpr_handle_t comparator_a= NULL; mcpwm_cmpr_handle_t comparator_b= NULL; mcpwm_cmpr_handle_t comparator_c= NULL; mcpwm_cmpr_handle_t comparator_d= NULL; - + esp_err_t enable_servo() { ESP_LOGI(TAG_SERVO, "Create timer and operator for servos A and B"); @@ -47,53 +23,53 @@ esp_err_t enable_servo() .count_mode = MCPWM_TIMER_COUNT_MODE_UP, }; ESP_ERROR_CHECK(mcpwm_new_timer(&timer_config, &timer)); - + mcpwm_oper_handle_t oper = NULL; mcpwm_operator_config_t operator_config = { .group_id = 0, // operator must be in the same group as the timer }; ESP_ERROR_CHECK(mcpwm_new_operator(&operator_config, &oper)); - + ESP_LOGI(TAG_SERVO, "Connect timer and operator for servos A and B"); ESP_ERROR_CHECK(mcpwm_operator_connect_timer(oper, timer)); - - ESP_LOGI(TAG_SERVO, "Create comparator_0 and generator from the operator for servos A and B"); - + + ESP_LOGI(TAG_SERVO, "Create comparator_a and generator from the operator for servos A and B"); + mcpwm_comparator_config_t comparator_config_a = { .flags.update_cmp_on_tez = true, }; ESP_ERROR_CHECK(mcpwm_new_comparator(oper, &comparator_config_a, &comparator_a)); - + mcpwm_comparator_config_t comparator_config_b = { .flags.update_cmp_on_tez = true, }; ESP_ERROR_CHECK(mcpwm_new_comparator(oper, &comparator_config_b, &comparator_b)); - + mcpwm_gen_handle_t generator_a = NULL; mcpwm_generator_config_t generator_config_a = { .gen_gpio_num = SERVO_A, }; ESP_ERROR_CHECK(mcpwm_new_generator(oper, &generator_config_a, &generator_a)); - + // Similarly, create generator B for SERVO_B mcpwm_gen_handle_t generator_b = NULL; mcpwm_generator_config_t generator_config_b = { .gen_gpio_num = SERVO_B, }; ESP_ERROR_CHECK(mcpwm_new_generator(oper, &generator_config_b, &generator_b)); - + // Set actions for generator A and B ESP_ERROR_CHECK(mcpwm_generator_set_action_on_timer_event(generator_a, MCPWM_GEN_TIMER_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, MCPWM_TIMER_EVENT_EMPTY, MCPWM_GEN_ACTION_HIGH))); ESP_ERROR_CHECK(mcpwm_generator_set_action_on_compare_event(generator_a, MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, comparator_a, MCPWM_GEN_ACTION_LOW))); - + ESP_ERROR_CHECK(mcpwm_generator_set_action_on_timer_event(generator_b, MCPWM_GEN_TIMER_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, MCPWM_TIMER_EVENT_EMPTY, MCPWM_GEN_ACTION_HIGH))); ESP_ERROR_CHECK(mcpwm_generator_set_action_on_compare_event(generator_b, MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, comparator_b, MCPWM_GEN_ACTION_LOW))); - - + + // Now, create a new timer, operator, and generators for servos C and D ESP_LOGI(TAG_SERVO, "Create timer and operator for servos C and D"); mcpwm_timer_handle_t timer_1 = NULL; @@ -105,78 +81,76 @@ esp_err_t enable_servo() .count_mode = MCPWM_TIMER_COUNT_MODE_UP, }; ESP_ERROR_CHECK(mcpwm_new_timer(&timer_config_1, &timer_1)); - + mcpwm_oper_handle_t oper_1 = NULL; mcpwm_operator_config_t operator_config_1 = { .group_id = 1, // operator must be in the same group as the timer }; ESP_ERROR_CHECK(mcpwm_new_operator(&operator_config_1, &oper_1)); - + ESP_LOGI(TAG_SERVO, "Connect timer and operator for servos C and D"); ESP_ERROR_CHECK(mcpwm_operator_connect_timer(oper_1, timer_1)); ESP_LOGI(TAG_SERVO, "Create comparator and generator from the operator for servos C and D"); - + mcpwm_comparator_config_t comparator_config_c = { .flags.update_cmp_on_tez = true, }; - ESP_ERROR_CHECK(mcpwm_new_comparator(oper_1, &comparator_config_1, &comparator_c)); - - mcpwm_comparator_config_t comparator_config_d = { + ESP_ERROR_CHECK(mcpwm_new_comparator(oper_1, &comparator_config_c, &comparator_c)); + + mcpwm_comparator_config_t comparator_config_d = { .flags.update_cmp_on_tez = true, }; - ESP_ERROR_CHECK(mcpwm_new_comparator(oper_1, &comparator_config_1, &comparator_d)); - - - // Similarly, create generator C for SERVO_C + ESP_ERROR_CHECK(mcpwm_new_comparator(oper_1, &comparator_config_d, &comparator_d)); + +// Similarly, create generator C for SERVO_C mcpwm_gen_handle_t generator_c = NULL; mcpwm_generator_config_t generator_config_c = { .gen_gpio_num = SERVO_C, }; - ESP_ERROR_CHECK(mcpwm_new_generator(oper_1, &generator_config_c, &generator_c)); - - // Similarly, create generator D for SERVO_D - mcpwm_gen_handle_t generator_d = NULL; - mcpwm_generator_config_t generator_config_d = { - .gen_gpio_num = SERVO_D, - }; - ESP_ERROR_CHECK(mcpwm_new_generator(oper_1, &generator_config_d, &generator_d)); - - // Set actions for generator C and D - ESP_ERROR_CHECK(mcpwm_generator_set_action_on_timer_event(generator_c, - MCPWM_GEN_TIMER_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, MCPWM_TIMER_EVENT_EMPTY, MCPWM_GEN_ACTION_HIGH))); - ESP_ERROR_CHECK(mcpwm_generator_set_action_on_compare_event(generator_c, - MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, comparator_c, MCPWM_GEN_ACTION_LOW))); - - ESP_ERROR_CHECK(mcpwm_generator_set_action_on_timer_event(generator_d, - MCPWM_GEN_TIMER_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, MCPWM_TIMER_EVENT_EMPTY, MCPWM_GEN_ACTION_HIGH))); - ESP_ERROR_CHECK(mcpwm_generator_set_action_on_compare_event(generator_d, - MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, comparator_d, MCPWM_GEN_ACTION_LOW))); - - + +// Similarly, create generator D for SERVO_D +mcpwm_gen_handle_t generator_d = NULL; +mcpwm_generator_config_t generator_config_d = { + .gen_gpio_num = SERVO_D, +}; +ESP_ERROR_CHECK(mcpwm_new_generator(oper_1, &generator_config_d, &generator_d)); + +// Set actions for generator C and D +ESP_ERROR_CHECK(mcpwm_generator_set_action_on_timer_event(generator_c, + MCPWM_GEN_TIMER_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, MCPWM_TIMER_EVENT_EMPTY, MCPWM_GEN_ACTION_HIGH))); +ESP_ERROR_CHECK(mcpwm_generator_set_action_on_compare_event(generator_c, + MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, comparator_c, MCPWM_GEN_ACTION_LOW))); + +ESP_ERROR_CHECK(mcpwm_generator_set_action_on_timer_event(generator_d, + MCPWM_GEN_TIMER_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, MCPWM_TIMER_EVENT_EMPTY, MCPWM_GEN_ACTION_HIGH))); +ESP_ERROR_CHECK(mcpwm_generator_set_action_on_compare_event(generator_d, + MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, comparator_d, MCPWM_GEN_ACTION_LOW))); + + // Enable and start both timers ESP_LOGI(TAG_SERVO, "Enable and start timers"); ESP_ERROR_CHECK(mcpwm_timer_enable(timer)); ESP_ERROR_CHECK(mcpwm_timer_enable(timer_1)); - + ESP_ERROR_CHECK(mcpwm_timer_start_stop(timer, MCPWM_TIMER_START_NO_STOP)); ESP_ERROR_CHECK(mcpwm_timer_start_stop(timer_1, MCPWM_TIMER_START_NO_STOP)); - + // Set the flag to indicate that servos are enabled enabled_servo_flag = 1; - + return ESP_OK; } - + static esp_err_t set_angle_servo_helper(int servo_pin, int servo_max, int servo_min_pulsewidth, int servo_max_pulsewidth, unsigned int degree_of_rotation) { degree_of_rotation = degree_of_rotation > servo_max ? servo_max : degree_of_rotation; - + uint32_t cal_pulsewidth = 0; cal_pulsewidth = (servo_min_pulsewidth + ((servo_max_pulsewidth - servo_min_pulsewidth) * (degree_of_rotation)) / (servo_max)); - - + + esp_err_t err; switch(servo_pin) { @@ -196,7 +170,7 @@ static esp_err_t set_angle_servo_helper(int servo_pin, int servo_max, int servo_ err = ESP_ERR_INVALID_ARG; break; } - + if (err == ESP_OK) { ESP_LOGI(TAG_SERVO, "set servo at pin %d: %d", servo_pin, degree_of_rotation); @@ -205,10 +179,10 @@ static esp_err_t set_angle_servo_helper(int servo_pin, int servo_max, int servo_ { ESP_LOGE(TAG_SERVO, "error: servo at pin %d: %s", servo_pin, esp_err_to_name(err)); } - + return err; } - + esp_err_t set_angle_servo(servo_config *config, unsigned int degree_of_rotation) { if (enabled_servo_flag) @@ -230,3 +204,9 @@ esp_err_t set_angle_servo(servo_config *config, unsigned int degree_of_rotation) return ESP_FAIL; } } + + +int read_servo(servo_config *config) +{ + return config->angle; +} From cc7b4eac44798776652fd5724ca1235a763375f0 Mon Sep 17 00:00:00 2001 From: purviyeshi <129578264+purviyeshi@users.noreply.github.com> Date: Sat, 13 Apr 2024 23:01:04 +0530 Subject: [PATCH 11/12] Add tested code --- include/servo.h | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/include/servo.h b/include/servo.h index 64942365..5eec4ec9 100644 --- a/include/servo.h +++ b/include/servo.h @@ -21,29 +21,33 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ - + #ifndef SERVO_H #define SERVO_H - + #include #include "freertos/FreeRTOS.h" #include "esp_attr.h" #include "driver/mcpwm_prelude.h" - - + + #include "sdkconfig.h" #include "esp_log.h" #include "esp_err.h" #include "pin_defs.h" - + typedef struct { int servo_pin; int min_pulse_width; int max_pulse_width; int max_degree; + int angle; } servo_config; - + + + + /** @struct servo_config * @brief This structure contains the configuration of servos * @var servo_config::servo_pin @@ -61,14 +65,14 @@ typedef struct * @var servo_config::gen * Member 'gen' contains MCPWM operator to use */ - + /** * @brief Enables Servo port on the sra board, sets up PWM for the three pins in servo port. * * @return esp_err_t - returns ESP_OK if servo pins initialised, else it returns ESP_ERR_INVALID_ARG **/ esp_err_t enable_servo(); - + /** * @brief Set the angle of the servos attached to the servo port of SRA Board * @@ -77,11 +81,11 @@ esp_err_t enable_servo(); * @return esp_err_t */ esp_err_t set_angle_servo(servo_config *config, unsigned int degree_of_rotation); - + /** * @brief Get the angle of the servos * @return esp_err_t */ int read_servo(servo_config *config); - + #endif From a7637595a0c9f205e7bf40dca01afd3e898e533f Mon Sep 17 00:00:00 2001 From: purviyeshi <129578264+purviyeshi@users.noreply.github.com> Date: Sat, 13 Apr 2024 23:35:02 +0530 Subject: [PATCH 12/12] add license which was removed --- src/servo.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/servo.c b/src/servo.c index 4208d8d5..056b393d 100644 --- a/src/servo.c +++ b/src/servo.c @@ -1,3 +1,27 @@ +/* + * MIT License + * + * Copyright (c) 2021 Society of Robotics and Automation + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + #include "servo.h" static const char *TAG_SERVO = "servo";