From 9edd22b6b77e927c98dec86e7070947fbae79ea0 Mon Sep 17 00:00:00 2001 From: EmanuelFeru Date: Mon, 1 Jun 2020 20:27:38 +0200 Subject: [PATCH] Improved UART Timeout Major: - Improved UART Timeout: Apparently, the UART data Rx/Tx is quite unreliable (especially under motor load) leading to very often out-of-sync issues. This change allows to reset the DMA more often, leading to a faster re-sync of the UART transmission and thus avoiding UART timeout. Issue #1 Minor: - added #define MPU_SENSOR_ENABLE to allow to enable/disable the MPU6050 sensor and save code size if the sensors is not needed - fixed the orientation in the Processing sketch - updated ReadMe --- Inc/config.h | 3 ++- Inc/mpu6050.h | 4 +++- Inc/mpu6050_dmp.h | 4 +++- Inc/mpu6050_dmpKey.h | 2 ++ Inc/mpu6050_dmpmap.h | 3 +++ README.md | 11 +++++++---- Src/main.c | 11 +++++++++-- Src/mpu6050.c | 10 ++++++---- Src/mpu6050_dmp.c | 5 +++++ docs/sketch_processing/sketch_processing.pde | 4 ++-- 10 files changed, 42 insertions(+), 15 deletions(-) diff --git a/Inc/config.h b/Inc/config.h index c3581cc..bb5de7f 100644 --- a/Inc/config.h +++ b/Inc/config.h @@ -45,6 +45,7 @@ /* ==================================== SETTINGS MPU-6050 ==================================== */ +#define MPU_SENSOR_ENABLE // [-] Enable flag for MPU-6050 sensor. Comment-out this flag to Disable the MPU sensor and reduce code size. #define MPU_DMP_ENABLE // [-] Enable flag for MPU-6050 DMP (Digital Motion Processing) functionality. #define MPU_DEFAULT_HZ 20 // [Hz] Default MPU frequecy: must be between 1Hz and 200Hz. #define TEMP_READ_MS 500 // [ms] Temperature read time interval @@ -73,7 +74,7 @@ #define USART_MAIN_BAUD 38400 // [bit/s] MAIN Serial Tx/Rx baud rate #endif #define SERIAL_START_FRAME 0xABCD // [-] Start frame definition for reliable serial communication -#define SERIAL_TIMEOUT 500 // [-] Numer of wrong received data for Serial timeout detection +#define SERIAL_TIMEOUT 800 // [-] Numer of wrong received data for Serial timeout detection /* ==================================== VALIDATE SETTINGS ==================================== */ diff --git a/Inc/mpu6050.h b/Inc/mpu6050.h index eca3f07..df14a7f 100644 --- a/Inc/mpu6050.h +++ b/Inc/mpu6050.h @@ -23,6 +23,7 @@ // Define to prevent recursive inclusion #ifndef MPU6050_H #define MPU6050_H +#ifdef MPU_SENSOR_ENABLE #include #include "defines.h" @@ -143,4 +144,5 @@ void mpu_handle_input(char c); void mpu_print_to_console(void); -#endif +#endif // MPU_SENSOR_ENABLE +#endif // MPU6050_H diff --git a/Inc/mpu6050_dmp.h b/Inc/mpu6050_dmp.h index cdfdd55..a1e4ef5 100644 --- a/Inc/mpu6050_dmp.h +++ b/Inc/mpu6050_dmp.h @@ -23,6 +23,7 @@ // Define to prevent recursive inclusion #ifndef MPU6050_DMP_H #define MPU6050_DMP_H +#ifdef MPU_SENSOR_ENABLE #include @@ -103,5 +104,6 @@ int dmp_read_fifo(short *gyro, short *accel, long *quat, unsigned long *timestamp, short *sensors, unsigned char *more); -#endif +#endif // MPU_SENSOR_ENABLE +#endif // MPU6050_DMP_H diff --git a/Inc/mpu6050_dmpKey.h b/Inc/mpu6050_dmpKey.h index a67896c..c772726 100644 --- a/Inc/mpu6050_dmpKey.h +++ b/Inc/mpu6050_dmpKey.h @@ -5,6 +5,7 @@ */ #ifndef DMPKEY_H__ #define DMPKEY_H__ +#ifdef MPU_SENSOR_ENABLE #define KEY_CFG_25 (0) @@ -492,4 +493,5 @@ typedef struct { +#endif // MPU_SENSOR_ENABLE #endif // DMPKEY_H__ diff --git a/Inc/mpu6050_dmpmap.h b/Inc/mpu6050_dmpmap.h index 391ba14..9f4813e 100644 --- a/Inc/mpu6050_dmpmap.h +++ b/Inc/mpu6050_dmpmap.h @@ -5,6 +5,7 @@ */ #ifndef DMPMAP_H #define DMPMAP_H +#ifdef MPU_SENSOR_ENABLE #ifdef __cplusplus extern "C" @@ -261,4 +262,6 @@ extern "C" #ifdef __cplusplus } #endif + +#endif // MPU_SENSOR_ENABLE #endif // DMPMAP_H diff --git a/README.md b/README.md index 0d0cc4f..3b1f08a 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ If you have never flashed your sideboard before, the MCU is probably locked. To To build and flash choose one of the following methods: -### Method 1: Using Platformio +### Method 1: Using Platformio IDE - open the folder in the IDE of choice (vscode or Atom) - press the 'PlatformIO:Build' or the 'PlatformIO:Upload' button (bottom left in vscode). @@ -55,14 +55,17 @@ To build and flash choose one of the following methods: - click Build Target (or press F7) to build the firmware - click Load Code (or press F8) to flash the firmware. -### Method 3: Using Ubuntu +### Method 3: Using Linux CLI - prerequisites: install [ST-Flash utility](https://github.com/texane/stlink). - -- open a terminal in the repo check-out folder and type: +- open a terminal in the repo check-out folder and if you have definded the variant in [config.h](/Inc/config.h) type: ``` make ``` +or you can set the variant like this +``` +make -e VARIANT=VARIANT_#### +``` - flash the firmware by typing: ``` make flash diff --git a/Src/main.c b/Src/main.c index 0eb9d8b..bccfb5a 100644 --- a/Src/main.c +++ b/Src/main.c @@ -170,6 +170,8 @@ int main(void) #endif intro_demo_led(100); // Short LEDs intro demo with 100 ms delay. This also gives some time for the MPU-6050 to power-up. + + #ifdef MPU_SENSOR_ENABLE if(mpu_config()) { // IMU MPU-6050 config mpuStatus = ERROR; HAL_GPIO_WritePin(LED2_GPIO_Port, LED2_Pin, GPIO_PIN_SET); // Turn on RED LED @@ -179,6 +181,9 @@ int main(void) HAL_GPIO_WritePin(LED2_GPIO_Port, LED2_Pin, GPIO_PIN_SET); // Turn on GREEN LED } mpu_handle_input('h'); // Print the User Help commands to serial + #else + HAL_GPIO_WritePin(LED2_GPIO_Port, LED2_Pin, GPIO_PIN_SET); // Turn on GREEN LED + #endif /* USER CODE END 2 */ @@ -201,7 +206,7 @@ int main(void) #endif // ==================================== USER Handling ==================================== - #ifdef SERIAL_DEBUG + #if defined(MPU_SENSOR_ENABLE) && defined(SERIAL_DEBUG) // Get the user Input as one character from Serial if (userCommand != 0) { // Check the availability of a user command set by the UART DMA log_i("Command = %c\n", userCommand); @@ -212,6 +217,7 @@ int main(void) // ==================================== MPU-6050 Handling ==================================== + #if defined(MPU_SENSOR_ENABLE) && defined(SERIAL_DEBUG) // Get MPU data. Because the MPU-6050 interrupt pin is not wired we have to check DMP data by pooling periodically if (SUCCESS == mpuStatus) { mpu_get_data(); @@ -222,6 +228,7 @@ int main(void) if (main_loop_counter % 50 == 0) { mpu_print_to_console(); } + #endif // ==================================== SENSORS Handling ==================================== @@ -293,7 +300,7 @@ int main(void) timeoutCntSerial = SERIAL_TIMEOUT; // Limit timout counter value } // Most probably we are out-of-sync. Try to re-sync by reseting the DMA - if (main_loop_counter % 150 == 0) { + if (NewFeedback.start != SERIAL_START_FRAME && NewFeedback.start != 0xFFFF && main_loop_counter % 5 == 0) { HAL_UART_DMAStop(&huart2); HAL_UART_Receive_DMA(&huart2, (uint8_t *)&NewFeedback, sizeof(NewFeedback)); } diff --git a/Src/mpu6050.c b/Src/mpu6050.c index 683c0a5..a6eef22 100644 --- a/Src/mpu6050.c +++ b/Src/mpu6050.c @@ -44,7 +44,11 @@ */ -static signed char MPU_ORIENTATION[9] = {1, 0, 0, // [-] MPU Sensor orientation matrix: set this according to the sensor installation +MPU_Data mpu; // holds the MPU-6050 data + +#ifdef MPU_SENSOR_ENABLE + +static signed char MPU_ORIENTATION[9] = {1, 0, 0, // [-] MPU Sensor orientation matrix: set this according to the sensor installation 0, 1, 0, 0, 0, 1}; @@ -3469,8 +3473,6 @@ int mpu_config(void) /* =========================== MPU-6050 Get Packet Data =========================== */ -MPU_Data mpu; - void mpu_get_data(void) { @@ -3952,5 +3954,5 @@ void mpu_print_to_console(void) #endif } - +#endif // MPU_SENSOR_ENABLE diff --git a/Src/mpu6050_dmp.c b/Src/mpu6050_dmp.c index bc28906..91844cd 100644 --- a/Src/mpu6050_dmp.c +++ b/Src/mpu6050_dmp.c @@ -22,12 +22,15 @@ // Includes #include #include +#include "config.h" #include "mpu6050.h" #include "mpu6050_dmp.h" #include "mpu6050_dmpKey.h" #include "mpu6050_dmpmap.h" +#ifdef MPU_SENSOR_ENABLE + /* The following functions must be defined for this platform: * i2c_write(unsigned char slave_addr, unsigned char reg_addr, * unsigned char length, unsigned char const *data) @@ -1328,6 +1331,8 @@ int dmp_register_android_orient_cb(void (*func)(unsigned char)) return 0; } +#endif // MPU_SENSOR_ENABLE + /** * @} */ diff --git a/docs/sketch_processing/sketch_processing.pde b/docs/sketch_processing/sketch_processing.pde index 41a43bb..afbfb1c 100644 --- a/docs/sketch_processing/sketch_processing.pde +++ b/docs/sketch_processing/sketch_processing.pde @@ -40,8 +40,8 @@ void draw() { text("Roll: " + roll + " Pitch: " + pitch + " Yaw: " + yaw, -200, 300); // Rotate the object - rotateX(radians(roll)); - rotateZ(radians(-pitch)); + rotateZ(radians(roll)); + rotateX(radians(pitch)); rotateY(radians(yaw)); // 3D 0bject