diff --git a/Inc/config.h b/Inc/config.h index 20be07f..fc441d7 100644 --- a/Inc/config.h +++ b/Inc/config.h @@ -133,6 +133,21 @@ // #define DEBUG_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuck or lcd) is used! #endif +#ifdef VARIANT_IBUS + // ###### CONTROL VIA RC REMOTE WITH FLYSKY IBUS PROTOCOL ###### + // left sensor board cable. Channel 1: steering, Channel 2: speed. + #define CONTROL_IBUS // use IBUS as input + #define IBUS_NUM_CHANNELS 14 // total number of IBUS channels to receive, even if they are not used. + #define IBUS_LENGTH 0x20 + #define IBUS_COMMAND 0x40 + + #define CONTROL_SERIAL_USART2 // left sensor board cable, disable if ADC or PPM is used! For Arduino control check the hoverSerial.ino + #define FEEDBACK_SERIAL_USART2 // left sensor board cable, disable if ADC or PPM is used! + #undef USART2_BAUD + #define USART2_BAUD 115200 +#endif + + #if defined(FEEDBACK_SERIAL_USART2) || defined(DEBUG_SERIAL_USART2) #define UART_DMA_CHANNEL DMA1_Channel7 #endif @@ -142,11 +157,11 @@ #endif #ifdef VARIANT_PPM -// ###### CONTROL VIA RC REMOTE ###### -// left sensor board cable. Channel 1: steering, Channel 2: speed. -#define CONTROL_PPM // use PPM-Sum as input. disable CONTROL_SERIAL_USART2! -#define PPM_NUM_CHANNELS 6 // total number of PPM channels to receive, even if they are not used. -#endif + // ###### CONTROL VIA RC REMOTE ###### + // left sensor board cable. Channel 1: steering, Channel 2: speed. + #define CONTROL_PPM // use PPM-Sum as input. disable CONTROL_SERIAL_USART2! + #define PPM_NUM_CHANNELS 6 // total number of PPM channels to receive, even if they are not used. +#endif // ###### CONTROL VIA TWO POTENTIOMETERS ###### /* ADC-calibration to cover the full poti-range: @@ -317,7 +332,7 @@ // ############################### VALIDATE SETTINGS ############################### -#if !defined(VARIANT_ADC) && !defined(VARIANT_USART3) && !defined(VARIANT_HOVERCAR) && !defined(VARIANT_TRANSPOTTER) && !defined(VARIANT_NUNCHUCK) && !defined(VARIANT_PPM) +#if !defined(VARIANT_ADC) && !defined(VARIANT_USART3) && !defined(VARIANT_HOVERCAR) && !defined(VARIANT_TRANSPOTTER) && !defined(VARIANT_NUNCHUCK) && !defined(VARIANT_PPM)&& !defined(VARIANT_IBUS) #error Variant not defined! Please check platformio.ini or Inc/config.h for available variants. #endif diff --git a/Src/main.c b/Src/main.c index 6d632b5..c7a1c07 100644 --- a/Src/main.c +++ b/Src/main.c @@ -106,12 +106,25 @@ static int16_t timeoutCntADC = 0; // Timeout counter for ADC Protection static uint8_t timeoutFlagADC = 0; // Timeout Flag for for ADC Protection: 0 = OK, 1 = Problem detected (line disconnected or wrong ADC data) #if defined(CONTROL_SERIAL_USART2) || defined(CONTROL_SERIAL_USART3) -typedef struct{ - uint16_t start; - int16_t steer; - int16_t speed; - uint16_t checksum; -} Serialcommand; + #ifdef CONTROL_IBUS + static uint16_t ibus_chksum; + static uint16_t ibus_captured_value[IBUS_NUM_CHANNELS]; + + typedef struct{ + uint8_t start; + uint8_t type; + uint8_t channels[IBUS_NUM_CHANNELS*2]; + uint8_t checksuml; + uint8_t checksumh; + } Serialcommand; + #elif + typedef struct{ + uint16_t start; + int16_t steer; + int16_t speed; + uint16_t checksum; + } Serialcommand; + #endif static volatile Serialcommand command; static int16_t timeoutCntSerial = 0; // Timeout counter for Rx Serial command #endif @@ -510,13 +523,29 @@ int main(void) { #if defined CONTROL_SERIAL_USART2 || defined CONTROL_SERIAL_USART3 // Handle received data validity, timeout and fix out-of-sync if necessary - if (command.start == START_FRAME && command.checksum == (uint16_t)(command.start ^ command.steer ^ command.speed)) { + #ifdef CONTROL_IBUS + ibus_chksum = 0xFFFF - IBUS_LENGTH - IBUS_COMMAND; + for (uint8_t i = 0; i < (IBUS_NUM_CHANNELS * 2); i ++) { + ibus_chksum -= command.channels[i]; + } + if (command.start == IBUS_LENGTH && command.type == IBUS_COMMAND && ibus_chksum == ( command.checksumh << 8) + command.checksuml ) { + #elif + if (command.start == START_FRAME && command.checksum == (uint16_t)(command.start ^ command.steer ^ command.speed)) { + #endif if (timeoutFlagSerial) { // Check for previous timeout flag if (timeoutCntSerial-- <= 0) // Timeout de-qualification timeoutFlagSerial = 0; // Timeout flag cleared } else { + #ifdef CONTROL_IBUS + for (uint8_t i = 0; i < (IBUS_NUM_CHANNELS * 2); i +=2) { + ibus_captured_value[(i/2)] = CLAMP( command.channels[i] + (command.channels[i+1] << 8) - 1000, INPUT_MIN, INPUT_MAX); + } + cmd1 = CLAMP((ibus_captured_value[0] - INPUT_MID) * 2, INPUT_MIN, INPUT_MAX); + cmd2 = CLAMP((ibus_captured_value[1] - INPUT_MID) * 2, INPUT_MIN, INPUT_MAX); + #elif cmd1 = CLAMP((int16_t)command.steer, INPUT_MIN, INPUT_MAX); - cmd2 = CLAMP((int16_t)command.speed, INPUT_MIN, INPUT_MAX); + cmd2 = CLAMP((int16_t)command.speed, INPUT_MIN, INPUT_MAX); + #endif command.start = 0xFFFF; // Change the Start Frame for timeout detection in the next cycle timeoutCntSerial = 0; // Reset the timeout counter } diff --git a/platformio.ini b/platformio.ini index 7e2efea..619005d 100644 --- a/platformio.ini +++ b/platformio.ini @@ -13,6 +13,7 @@ src_dir = Src ;default_envs = VARIANT_USART3 ; Variant for Serial control via USART3 input ;default_envs = VARIANT_NUNCHUCK ; Variant for Nunchuck controlled vehicle build ;default_envs = VARIANT_PPM ; Variant for RC-Remotes with PPM-Sum signal +;default_envs = VARIANT_IBUS ; Variant for RC-Remotes with FLYSKY IBUS ;default_envs = VARIANT_HOVERCAR ; Variant for HOVERCAR build ;default_envs = VARIANT_TRANSPOTTER ; Variant for TRANSPOTTER build https://github.com/NiklasFauth/hoverboard-firmware-hack/wiki/Build-Instruction:-TranspOtter https://hackaday.io/project/161891-transpotter-ng ;================================================================ @@ -111,6 +112,27 @@ build_flags = ;================================================================ +[env:VARIANT_IBUS] +platform = ststm32 +framework = stm32cube +board = genericSTM32F103RC +debug_tool = stlink +upload_protocol = stlink + +build_flags = + -I${PROJECT_DIR}/inc/ + -DUSE_HAL_DRIVER + -DSTM32F103xE + -Wl,-T./STM32F103RCTx_FLASH.ld + -Wl,-lc + -Wl,-lm + -g -ggdb ; to generate correctly the 'firmware.elf' for STM STUDIO vizualization +# -Wl,-lnosys + -D VARIANT_IBUS + -D PALTFORMIO + +;================================================================ + [env:VARIANT_HOVERCAR] platform = ststm32 framework = stm32cube