From d8a30bc72055d9ac641a010eada9884d75682aae Mon Sep 17 00:00:00 2001 From: RicardoEA <ricardo@focus.uy> Date: Tue, 17 Jan 2023 23:20:58 -0300 Subject: [PATCH] Add uart comunication feature --- Robotito/.vscode/settings.json | 4 ++- Robotito/src/control/control.c | 5 +++ Robotito/src/main.c | 56 ++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 1 deletion(-) diff --git a/Robotito/.vscode/settings.json b/Robotito/.vscode/settings.json index c60fb00..a3e5f72 100644 --- a/Robotito/.vscode/settings.json +++ b/Robotito/.vscode/settings.json @@ -3,6 +3,8 @@ "encoders.h": "c", "motors.h": "c", "mcpwm.h": "c", - "control.h": "c" + "control.h": "c", + "queue.h": "c", + "uart.h": "c" } } \ No newline at end of file diff --git a/Robotito/src/control/control.c b/Robotito/src/control/control.c index 46891b7..c6c2176 100644 --- a/Robotito/src/control/control.c +++ b/Robotito/src/control/control.c @@ -1,5 +1,7 @@ #include <stdio.h> +#include <string.h> #include "control.h" +#include "driver/uart.h" /* * Helper functions @@ -115,6 +117,9 @@ static void callback_odometry(){ o->y_dot = v->y; o->phi += v->z * OMNI_ODOM_CTRL_TIMER; o->phi_dot = v->z; + char* Txdata = (char*) malloc(100); + sprintf (Txdata, "Hello world index \r\n"); + uart_write_bytes(UART_NUM_2, Txdata, strlen(Txdata)); free(v); } diff --git a/Robotito/src/main.c b/Robotito/src/main.c index ab74e75..1d31831 100644 --- a/Robotito/src/main.c +++ b/Robotito/src/main.c @@ -13,18 +13,74 @@ #include "freertos/task.h" #include "freertos/queue.h" #include "driver/gpio.h" +#include "driver/uart.h" #include "motors.h" #include "encoders.h" #include "hwTimer.h" #include "control.h" +#include "esp_log.h" #define OMNI_CTRL_TIMER 0.05 // s #define MAX_OUTPUT 100.0 #define NMOTORS 3 #define OMNI_NRO_TIMER 0 +static const char *tag = "UART EVENT"; +#define UART_NUM UART_NUM_2 +#define BUF_SIZE 1024 +#define TASK_MEMORY 1024 * 2 + +static QueueHandle_t uart_queue; + +static void uart_task(void *pvParameters) +{ + uart_event_t event; + uint8_t *data = (uint8_t *)malloc(BUF_SIZE); + int len; + while (1) + { + if (xQueueReceive(uart_queue, &event, portMAX_DELAY ) ) + { + bzero(data,BUF_SIZE); + switch (event.type) + { + case UART_DATA: + len = uart_read_bytes(UART_NUM, data, event.size, portMAX_DELAY); + if (len > 0){ + printf("%s \n",data); + } + // decodificar mensaje y hacer omni_drive + uart_flush(UART_NUM); + break; + default: + ESP_LOGI(tag, "uart event type: %d", event.type); + break; + } + } + } +} + +static void init_uart(void) +{ + uart_config_t uart_config = { + .baud_rate = 9600, + .data_bits = UART_DATA_8_BITS, + .parity = UART_PARITY_DISABLE, + .stop_bits = UART_STOP_BITS_1, + .flow_ctrl = UART_HW_FLOWCTRL_DISABLE, + .source_clk = UART_SCLK_APB, + }; + uart_param_config(UART_NUM, &uart_config); + + uart_set_pin(UART_NUM, 5, 4, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE); + + uart_driver_install(UART_NUM, BUF_SIZE, BUF_SIZE, 5, &uart_queue, 0); + xTaskCreate(uart_task, "uart_task", TASK_MEMORY, NULL, 5, NULL); +} + void app_main(void) { + init_uart(); printf("Configuring Initial Parameters of mcpwm...\n"); omni_init(); omni_set_enable(true); -- GitLab