From 55c3d3ff1f76a15c6156125718b39cb265ba6def Mon Sep 17 00:00:00 2001
From: RicardoEA <ricardo@focus.uy>
Date: Sat, 21 Jan 2023 14:40:18 -0300
Subject: [PATCH] Uart to send/receive floats

---
 Robotito/src/control/control.c | 16 +++++++++++++---
 Robotito/src/main.c            | 23 ++++++++++++++++-------
 2 files changed, 29 insertions(+), 10 deletions(-)

diff --git a/Robotito/src/control/control.c b/Robotito/src/control/control.c
index e78a2dc..0269346 100644
--- a/Robotito/src/control/control.c
+++ b/Robotito/src/control/control.c
@@ -117,9 +117,19 @@ 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));
+    
+    //char* Txdata = (char*) malloc(100);
+    //sprintf (Txdata, "Hello world index \r\n");
+    //uart_write_bytes(UART_NUM_2, Txdata, strlen(Txdata));
+    float float_buffer[6];
+    //float_buffer = o;
+    float_buffer[0] = o->x;
+    float_buffer[1] = o->x_dot;
+    float_buffer[2] = o->y;
+    float_buffer[3] = o->y_dot;
+    float_buffer[4] = o->phi;
+    float_buffer[5] = o->phi_dot;
+    uart_write_bytes(UART_NUM_2, float_buffer, sizeof(float_buffer));
     free(v);
 }
 
diff --git a/Robotito/src/main.c b/Robotito/src/main.c
index 1d31831..d95dd00 100644
--- a/Robotito/src/main.c
+++ b/Robotito/src/main.c
@@ -27,15 +27,17 @@
 
 static const char *tag = "UART EVENT";
 #define UART_NUM UART_NUM_2
-#define BUF_SIZE 1024
+#define FLOAT_BUF_SIZE 4
+#define BUF_SIZE FLOAT_BUF_SIZE * sizeof(float)
 #define TASK_MEMORY 1024 * 2
 
 static QueueHandle_t uart_queue;
+static uint8_t data[BUF_SIZE];
+static float fdata[FLOAT_BUF_SIZE];
 
 static void uart_task(void *pvParameters)
 {
     uart_event_t event;
-    uint8_t *data = (uint8_t *)malloc(BUF_SIZE);
     int len;
     while (1)
     {
@@ -45,12 +47,19 @@ static void uart_task(void *pvParameters)
             switch (event.type)
             {
             case UART_DATA:
-                len = uart_read_bytes(UART_NUM, data, event.size, portMAX_DELAY);
-                if (len > 0){
+                len = uart_read_bytes(UART_NUM, data, BUF_SIZE, portMAX_DELAY);
+                if (len == BUF_SIZE){
+                    memcpy(&fdata, data , sizeof(fdata));
                     printf("%s \n",data);
+                    for (int i = 0; i < 4; i++)
+                    {
+                        printf("El dato %d en float es: %f \n",i, fdata[i]);    
+                    }
+                    // decodificar mensaje y hacer omni_drive
+                    uart_flush(UART_NUM);
+                }else{
+                    printf("Todavia no se completo el mensaje o hay un error. \n");
                 }
-                // decodificar mensaje y hacer omni_drive
-                uart_flush(UART_NUM);
                 break;
             default:
                 ESP_LOGI(tag, "uart event type: %d", event.type);
@@ -74,7 +83,7 @@ static void init_uart(void)
 
     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);
+    uart_driver_install(UART_NUM, BUF_SIZE*2, BUF_SIZE*2, 5, &uart_queue, 0);
     xTaskCreate(uart_task, "uart_task", TASK_MEMORY, NULL, 5, NULL);
 }
 
-- 
GitLab