/* Robot Controls Generate PWM signals to control motors. By: Alexander Bobkov Date: Dec 21, 2024 built-in LED GPIO: 10 build-in push button GPIO: 3 */ #include #include #include #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/queue.h" #include "driver/gpio.h" #include "driver/ledc.h" //#include "driver/mcpwm.h" #include "esp_log.h" #include "led_strip.h" #include "sdkconfig.h" /* ESP-NOW */ #include "esp_now.h" #include "esp_mac.h" //#include "espnow_utils.h" #include "esp_wifi.h" #include "esp_system.h" #include "espnow_config.h" static const char *TAG = "ESP IDF Robot"; // LED #define LEDC_TIMER LEDC_TIMER_0 #define LEDC_MODE LEDC_LOW_SPEED_MODE // LEDC_LOW_SPEED_MODE #define LEDC_OUTPUT_IO (5) // Define the output GPIO #define LEDC_CHANNEL LEDC_CHANNEL_0 #define LEDC_DUTY_RES LEDC_TIMER_13_BIT // /* TIMER RESOLUTION MAX VALUE HALF-DUTY 10 1023 511 13 8191 4095 */ #define LEDC_DUTY (7820) // 8068, 7944, 7820, 7696, 7572, *7680*, 7424, 7168, 6144, 512, 768 #define LEDC_FREQUENCY (4000) // For LED the freuqncy of 500Hz seems to be sufficient. // Frequency in Hertz. For DC motor, set frequency at 5 kHz /* Use project configuration menu (idf.py menuconfig) to choose the GPIO to blink, or you can edit the following line and set a number here. */ // Retrieve values from configuration menu #define BLINK_GPIO CONFIG_BLINK_GPIO // 10 GPIO of on-board LED #define PUSH_BTN_GPIO CONFIG_BUTTON_GPIO // 3 GPIO of on-board push-button #define MTR_FL_GPIO 0 //CONFIG_MOTOR_FRONT_LEFT_GPIO #define ESP_INTR_FLAG_DEFAULT 0 #define GPIO_INPUT_PIN_SEL ((1ULL<channel = CONFIG_ESPNOW_CHANNEL; peer->ifidx = ESPNOW_WIFI_IF; peer->encrypt = false; memcpy(peer->peer_addr, broadcast_mac, ESP_NOW_ETH_ALEN); ESP_ERROR_CHECK( esp_now_add_peer(peer) ); free(peer); /* Initialize sending parameters. */ send_param = malloc(sizeof(espnow_send_param_t)); if (send_param == NULL) { ESP_LOGE(TAG, "Malloc send parameter fail"); vSemaphoreDelete(espnow_queue); esp_now_deinit(); return ESP_FAIL; } memset(send_param, 0, sizeof(espnow_send_param_t)); send_param->unicast = false; send_param->broadcast = true; send_param->state = 0; send_param->magic = esp_random(); send_param->count = CONFIG_ESPNOW_SEND_COUNT; send_param->delay = CONFIG_ESPNOW_SEND_DELAY; send_param->len = CONFIG_ESPNOW_SEND_LEN; send_param->buffer = malloc(CONFIG_ESPNOW_SEND_LEN); if (send_param->buffer == NULL) { ESP_LOGE(TAG, "Malloc send buffer fail"); free(send_param); vSemaphoreDelete(espnow_queue); esp_now_deinit(); return ESP_FAIL; } memcpy(send_param->dest_mac, broadcast_mac, ESP_NOW_ETH_ALEN); example_espnow_data_prepare(send_param); xTaskCreate(espnow_task, "example_espnow_task", 2048, send_param, 4, NULL); return ESP_OK; } void app_main(void) { // Initialize LED ledc_init(); // Initialize the config structure. gpio_config_t io_conf = {}; /* Configure the peripheral according to the LED type */ //configure_led(); // Configure on-board LED io_conf.intr_type = GPIO_INTR_DISABLE; io_conf.mode = GPIO_MODE_OUTPUT; io_conf.pin_bit_mask = GPIO_OUTPUT_PIN_SEL; io_conf.pull_down_en = 0; io_conf.pull_up_en = 0; gpio_config(&io_conf); // Configure on-board push button io_conf.intr_type = GPIO_INTR_POSEDGE; io_conf.pin_bit_mask = GPIO_INPUT_PIN_SEL; io_conf.mode = GPIO_MODE_INPUT; io_conf.pull_up_en = 1; gpio_config(&io_conf); // Set push button interrupt gpio_set_intr_type(PUSH_BTN_GPIO, GPIO_INTR_NEGEDGE);//ANYEDGE); gpio_evt_queue = xQueueCreate(10, sizeof(uint32_t)); xTaskCreate(gpio_task, "GPIO task", 2048, NULL, 10, NULL); gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT); gpio_isr_handler_add(PUSH_BTN_GPIO, gpio_isr_handler, (void*) PUSH_BTN_GPIO); configure_button(); //configure_dc_mc(); printf("Added button interrupt"); // ESP-NOW wifi_init(); espnow_init(); //esp_now_add_peer(&peerInfo); while (1) { ESP_LOGI(TAG, "Turning the LED %s!", s_led_state == true ? "ON" : "OFF"); //blink_led(); /* Toggle the LED state */ gpio_set_level(BLINK_GPIO, s_led_state); s_led_state = !s_led_state; vTaskDelay(CONFIG_BLINK_PERIOD / portTICK_PERIOD_MS); } }