mirror of
https://github.com/lucysrausch/hoverboard-firmware-hack.git
synced 2025-07-30 15:47:15 +02:00
Merge pull request #132 from hexagon5un/simple_serial_framing
minimalistic framing for UART command
This commit is contained in:
@ -9,6 +9,7 @@
|
|||||||
#define DELAY_IN_MAIN_LOOP 5 // in ms. default 5. it is independent of all the timing critical stuff. do not touch if you do not know what you are doing.
|
#define DELAY_IN_MAIN_LOOP 5 // in ms. default 5. it is independent of all the timing critical stuff. do not touch if you do not know what you are doing.
|
||||||
|
|
||||||
#define TIMEOUT 5 // number of wrong / missing input commands before emergency off
|
#define TIMEOUT 5 // number of wrong / missing input commands before emergency off
|
||||||
|
#define START_FRAME 0xAAAA // serial command start-of-frame magic word
|
||||||
|
|
||||||
// ############################### GENERAL ###############################
|
// ############################### GENERAL ###############################
|
||||||
|
|
||||||
|
19
Src/main.c
19
Src/main.c
@ -41,9 +41,10 @@ int cmd2;
|
|||||||
int cmd3;
|
int cmd3;
|
||||||
|
|
||||||
typedef struct{
|
typedef struct{
|
||||||
|
uint16_t start_of_frame;
|
||||||
int16_t steer;
|
int16_t steer;
|
||||||
int16_t speed;
|
int16_t speed;
|
||||||
//uint32_t crc;
|
uint16_t checksum;
|
||||||
} Serialcommand;
|
} Serialcommand;
|
||||||
|
|
||||||
volatile Serialcommand command;
|
volatile Serialcommand command;
|
||||||
@ -157,7 +158,7 @@ int main(void) {
|
|||||||
|
|
||||||
#ifdef CONTROL_SERIAL_USART2
|
#ifdef CONTROL_SERIAL_USART2
|
||||||
UART_Control_Init();
|
UART_Control_Init();
|
||||||
HAL_UART_Receive_DMA(&huart2, (uint8_t *)&command, 4);
|
HAL_UART_Receive_DMA(&huart2, (uint8_t *)&command, sizeof(command));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef DEBUG_I2C_LCD
|
#ifdef DEBUG_I2C_LCD
|
||||||
@ -218,12 +219,20 @@ int main(void) {
|
|||||||
timeout = 0;
|
timeout = 0;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef CONTROL_SERIAL_USART2
|
#ifdef CONTROL_SERIAL_USART2
|
||||||
|
if (command.start_of_frame == START_FRAME &&
|
||||||
|
command.checksum ==(uint16_t)(START_FRAME ^ command.steer ^ command.speed)) {
|
||||||
cmd1 = CLAMP((int16_t)command.steer, -1000, 1000);
|
cmd1 = CLAMP((int16_t)command.steer, -1000, 1000);
|
||||||
cmd2 = CLAMP((int16_t)command.speed, -1000, 1000);
|
cmd2 = CLAMP((int16_t)command.speed, -1000, 1000);
|
||||||
|
} else { // restart DMA to hopefully get back in sync
|
||||||
|
// Try a periodic reset
|
||||||
|
if (main_loop_counter % 25 == 0) {
|
||||||
|
HAL_UART_DMAStop(&huart2);
|
||||||
|
HAL_UART_Receive_DMA(&huart2, (uint8_t *)&command, sizeof(command));
|
||||||
|
}
|
||||||
|
}
|
||||||
timeout = 0;
|
timeout = 0;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef CONTROL_MOTOR_TEST
|
#ifdef CONTROL_MOTOR_TEST
|
||||||
if (motor_test_direction == 1) cmd2 += 1;
|
if (motor_test_direction == 1) cmd2 += 1;
|
||||||
|
Reference in New Issue
Block a user