// Firmware ESP32-S3: cau noi WT61PC (UART) -> host (USB). // // ⚠️ File nay la BAN SUA cua base_kit/src/hardware/IMU_uart/main/main.c. // Xem ../README.md muc "Sua gi so voi ban base_kit". // // Dinh dang xuat PHAI khop libs/vslam_drivers/include/vslam/mcu_protocol.h -- // file do tu nhan la DAC TA cho firmware. Do la ly do firmware nam trong repo // nay chu khong nam trong base_kit: dac ta va ban thuc thi de canh nhau thi // test_mcu_protocol.cc la bai kiem cho ca hai phia. #include #include #include #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "driver/uart.h" #include "driver/gpio.h" #include "esp_timer.h" #define UART_PORT_NUM UART_NUM_1 #define UART_BAUD_RATE 9600 #define UART_TX_PIN 16 #define UART_RX_PIN 15 #define BUF_SIZE 1024 // Dat 1 de in lai ba dong text "Acc/Gyro/Angle" cu thay vi ban ghi I. // Chi de lui ve khi can so sanh; host doc duoc ca hai (xem mcu_bridge.cc). #define EMIT_LEGACY_TEXT 0 #define DEG2RAD 0.017453292519943295 // 9.80665 chu khong phai 9.8 nhu ban goc: lech 0.07%, sua mien phi. #define G_MPS2 9.80665 static float acc_x, acc_y, acc_z; // m/s^2 static float gyro_x, gyro_y, gyro_z; // rad/s static bool acc_updated = false; #if EMIT_LEGACY_TEXT static float angle_x, angle_y, angle_z; static bool gyro_updated = false, angle_updated = false; #endif static uint8_t calculate_checksum(uint8_t *buf) { uint8_t sum = 0; for (int i = 0; i < 10; i++) sum += buf[i]; return sum; } // Doi tan so xuat cua WT61PC. // // Lenh {0xff,0xaa,0x03,,0x00} lay tu DFRobot_WT61PC::modifyFrequency(). // 0x07 = 20 Hz. // // ⚠️ KHONG dat cao hon. Cam bien van gui DU ca ba khung (Acc + Gyro + Angle = // 33 byte) du firmware nay chi dung hai khung dau -- khong co cach tat khung // Angle ma khong doan ma thanh ghi. O 9600 baud 8N1 = 960 byte/s thi tran la // 960/33 = 29 Hz. 20 Hz = 660 byte/s = 69% bus, con bien. 25 Hz la 86%, sat // qua: mot khung bi tre la keo theo ca chuoi. // // ⚠️ KHONG dung lenh doi baud (thanh ghi 0x04): thu vien DFRobot khong expose // no nen ma phai doan, ma doan sai thi cam bien chuyen sang baud khac va CAM // hoan toan -- phai thao ra noi lai moi cuu duoc. static void set_output_rate_20hz(void) { const uint8_t cmd[5] = {0xff, 0xaa, 0x03, 0x07, 0x00}; uart_write_bytes(UART_PORT_NUM, (const char *)cmd, sizeof(cmd)); vTaskDelay(pdMS_TO_TICKS(100)); // cho cam bien nhan lenh } static void uart_task(void *arg) { uart_config_t uart_config = { .baud_rate = UART_BAUD_RATE, .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_DEFAULT, }; uart_driver_install(UART_PORT_NUM, BUF_SIZE * 2, 0, 0, NULL, 0); uart_param_config(UART_PORT_NUM, &uart_config); uart_set_pin(UART_PORT_NUM, UART_TX_PIN, UART_RX_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE); set_output_rate_20hz(); uint8_t data[11]; while (1) { uint8_t ch; int len = uart_read_bytes(UART_PORT_NUM, &ch, 1, portMAX_DELAY); if (len != 1 || ch != 0x55) continue; data[0] = 0x55; len = uart_read_bytes(UART_PORT_NUM, &data[1], 10, 100 / portTICK_PERIOD_MS); if (len != 10) continue; if (calculate_checksum(data) != data[10]) continue; int16_t x = (data[3] << 8) | data[2]; int16_t y = (data[5] << 8) | data[4]; int16_t z = (data[7] << 8) | data[6]; switch (data[1]) { case 0x51: // Gia toc acc_x = (x / 32768.0f) * 16.0f * G_MPS2; acc_y = (y / 32768.0f) * 16.0f * G_MPS2; acc_z = (z / 32768.0f) * 16.0f * G_MPS2; acc_updated = true; break; case 0x52: { // Van toc goc // ⚠️ Dong dau NGAY DAY, truoc moi xu ly khac. Day la thoi diem gan // nhat voi luc DO ma firmware biet duoc. // // Van con lech: cam bien lay mau roi moi day byte, va den luc khung // gyro (khung thu hai) nhan xong thi mau da gia ~23 ms o 9600 baud // (2 khung x 11 byte x 10 bit / 9600). KHONG tru san o day -- tru // san la gia dinh cam bien phat ngay sau khi lay mau, dieu chua // kiem duoc. Phan du la HANG SO va host do duoc: xem // `wheel_odom/lag` va `mcu.imu_lag_sec` trong sensors.yaml. const int64_t t_us = esp_timer_get_time(); gyro_x = (x / 32768.0f) * 2000.0f * DEG2RAD; gyro_y = (y / 32768.0f) * 2000.0f * DEG2RAD; gyro_z = (z / 32768.0f) * 2000.0f * DEG2RAD; #if EMIT_LEGACY_TEXT gyro_updated = true; #else // Phat NGAY khi co du acc + gyro. KHONG doi khung 0x53 (Angle): // khong co ai doc imu.orientation trong libs/ va app/, ma doi no // ton them mot khung = 11.5 ms o 9600 baud. // // micros cat con 32 bit theo dung mcu_protocol.h -- no TRAN moi // 71.58 phut va McuTimeUnwrapper phia host go tran. if (acc_updated) { printf("I,%" PRIu32 ",%.5f,%.5f,%.5f,%.3f,%.3f,%.3f\n", (uint32_t)(t_us & 0xFFFFFFFF), gyro_x, gyro_y, gyro_z, acc_x, acc_y, acc_z); acc_updated = false; } #endif break; } case 0x53: // Goc -- doc de khong lech khung, khong dung toi #if EMIT_LEGACY_TEXT angle_x = (x / 32768.0f) * 180.0f; angle_y = (y / 32768.0f) * 180.0f; angle_z = (z / 32768.0f) * 180.0f; angle_updated = true; #endif break; default: break; } #if EMIT_LEGACY_TEXT if (acc_updated && gyro_updated && angle_updated) { printf("Acc\t%.2f\t%.2f\t%.2f\n", acc_x, acc_y, acc_z); printf("Gyro\t%.2f\t%.2f\t%.2f\n", gyro_x / (float)DEG2RAD, gyro_y / (float)DEG2RAD, gyro_z / (float)DEG2RAD); printf("Angle\t%.2f\t%.2f\t%.2f\n\n", angle_x, angle_y, angle_z); acc_updated = gyro_updated = angle_updated = false; } #endif } } void app_main(void) { // ⚠️ Dong nay KHONG toi duoc host qua USB Serial/JTAG: host chua kip nhan // thiet bi luc app_main chay. Da mat mot buoi chan doan vi tuong firmware // bi sai khi khong bat duoc banner. Giu lai vi no ra UART0. printf("imu_uart: WT61PC -> ban ghi I, xem mcu_protocol.h\n"); xTaskCreate(uart_task, "uart_task", 4096, NULL, 5, NULL); }