From 39e4fdc8696159b3d83dfde3eecc8319c3d4b418 Mon Sep 17 00:00:00 2001 From: EmanuelFeru Date: Tue, 24 Mar 2020 19:57:08 +0100 Subject: [PATCH] Variant_PWM implemented --- Inc/BLDC_controller.h | 4 +- Inc/config.h | 17 +- Inc/defines.h | 8 +- Inc/rtwtypes.h | 4 +- MDK-ARM/mainboard-hack.uvoptx | 157 ++++++++- MDK-ARM/mainboard-hack.uvprojx | 619 +++++++++++++++++++++++++++++++++ README.md | 5 +- Src/BLDC_controller.c | 9 +- Src/BLDC_controller_data.c | 4 +- Src/control.c | 83 ++--- Src/stm32f1xx_it.c | 5 +- Src/util.c | 24 +- platformio.ini | 2 +- 13 files changed, 857 insertions(+), 84 deletions(-) diff --git a/Inc/BLDC_controller.h b/Inc/BLDC_controller.h index 2bf7c56..d7a2841 100644 --- a/Inc/BLDC_controller.h +++ b/Inc/BLDC_controller.h @@ -3,9 +3,9 @@ * * Code generated for Simulink model 'BLDC_controller'. * - * Model version : 1.1256 + * Model version : 1.1260 * Simulink Coder version : 8.13 (R2017b) 24-Jul-2017 - * C/C++ source code generated on : Mon Dec 30 21:36:12 2019 + * C/C++ source code generated on : Tue Mar 24 11:01:08 2020 * * Target selection: ert.tlc * Embedded hardware selection: ARM Compatible->ARM Cortex diff --git a/Inc/config.h b/Inc/config.h index 23b7866..64f8f4e 100644 --- a/Inc/config.h +++ b/Inc/config.h @@ -284,14 +284,13 @@ * Channel 1: steering, Channel 2: speed. */ #define CONTROL_PWM // use RC PWM as input. disable DEBUG_SERIAL_USART2! + // #define SUPPORT_BUTTONS // use right sensor board cable for button inputs. Disable DEBUG_SERIAL_USART3! #define PWM_DEADBAND 100 // How much of the center position is considered 'center' (100 = values -100 to 100 are considered 0) // Min / Max values of each channel (use DEBUG to determine these values) #define PWM_CH1_MAX 1000 // (0 - 1000) #define PWM_CH1_MIN -1000 // (-1000 - 0) #define PWM_CH2_MAX 1000 // (0 - 1000) - #define PWM_CH2_MIN -1000 // (-1000 - 0) - // right sensor board cable. Only read once during startup - #define BUTTONS_RIGHT // use right sensor board cable for button inputs. Disable DEBUG_SERIAL_USART3! + #define PWM_CH2_MIN -1000 // (-1000 - 0) #define FILTER 6553 // 0.1f [-] fixdt(0,16,16) lower value == softer filter [0, 65535] = [0.0 - 1.0]. #define SPEED_COEFFICIENT 16384 // 1.0f [-] fixdt(1,16,14) higher value == stronger. [0, 65535] = [-2.0 - 2.0]. In this case 16384 = 1.0 * 2^14 #define STEER_COEFFICIENT 0 // 0.0f [-] fixdt(1,16,14) higher value == stronger. [0, 65535] = [-2.0 - 2.0]. In this case 0 = 0.0 * 2^14. If you do not want any steering, set it to 0. @@ -464,6 +463,14 @@ #error CONTROL_PPM and SERIAL_USART2 not allowed. It is on the same cable. #endif +#if (defined(CONTROL_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART2) || defined(DEBUG_SERIAL_USART2)) && defined(CONTROL_PWM) + #error CONTROL_PWM and SERIAL_USART2 not allowed. It is on the same cable. +#endif + +#if (defined(CONTROL_SERIAL_USART3) || defined(SIDEBOARD_SERIAL_USART3) || defined(DEBUG_SERIAL_USART3)) && defined(CONTROL_PWM) && defined(SUPPORT_BUTTONS) + #error SUPPORT_BUTTONS and SERIAL_USART3 not allowed for VARIANT_PWM. It is on the same cable. +#endif + #if (defined(CONTROL_SERIAL_USART3) || defined(SIDEBOARD_SERIAL_USART3) || defined(DEBUG_SERIAL_USART3)) && defined(CONTROL_NUNCHUK) #error CONTROL_NUNCHUK and SERIAL_USART3 not allowed. It is on the same cable. #endif @@ -472,8 +479,8 @@ #error DEBUG_I2C_LCD and SERIAL_USART3 not allowed. It is on the same cable. #endif -#if defined(CONTROL_PPM) && defined(CONTROL_ADC) && defined(CONTROL_NUNCHUK) || defined(CONTROL_PPM) && defined(CONTROL_ADC) || defined(CONTROL_ADC) && defined(CONTROL_NUNCHUK) || defined(CONTROL_PPM) && defined(CONTROL_NUNCHUK) - #error only 1 input method allowed. use CONTROL_PPM or CONTROL_ADC or CONTROL_NUNCHUK. +#if defined(CONTROL_ADC) && (defined(CONTROL_PPM) || defined(CONTROL_PWM) || defined(CONTROL_NUNCHUK)) || defined(CONTROL_PPM) && (defined(CONTROL_PWM) || defined(CONTROL_NUNCHUK)) || defined(CONTROL_PWM) && defined(CONTROL_NUNCHUK) + #error only 1 input method allowed. use CONTROL_ADC or CONTROL_PPM or CONTROL_PWM or CONTROL_NUNCHUK. #endif #if defined(ADC_PROTECT_ENA) && ((ADC1_MIN - ADC_PROTECT_THRESH) <= 0 || (ADC1_MAX + ADC_PROTECT_THRESH) >= 4095) diff --git a/Inc/defines.h b/Inc/defines.h index 024b63e..ddcbd6c 100644 --- a/Inc/defines.h +++ b/Inc/defines.h @@ -124,6 +124,11 @@ #define CHARGER_PIN GPIO_PIN_12 #define CHARGER_PORT GPIOA +#define BUTTON1_RIGHT_PIN GPIO_PIN_10 +#define BUTTON1_RIGHT_PORT GPIOB +#define BUTTON2_RIGHT_PIN GPIO_PIN_11 +#define BUTTON2_RIGHT_PORT GPIOB + #define DELAY_TIM_FREQUENCY_US 1000000 #define MILLI_R (R * 1000) @@ -141,6 +146,7 @@ #define RAD(a) ((a)*180.0f / M_PI) #define SIGN(a) (((a) < 0) ? (-1) : (((a) > 0) ? (1) : (0))) #define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x))) +#define IN_RANGE(x, low, high) (((x) >= (low)) && ((x) <= (high))) #define SCALE(value, high, max) MIN(MAX(((max) - (value)) / ((max) - (high)), 0.0f), 1.0f) #define MIN(a, b) (((a) < (b)) ? (a) : (b)) #define MAX(a, b) (((a) > (b)) ? (a) : (b)) @@ -168,7 +174,7 @@ uint8_t Nunchuk_Ping(void); void PPM_Init(void); void PPM_ISR_Callback(void); void PWM_Init(void); -//void PWM_ISR_CH1_Callback(void); +void PWM_ISR_CH1_Callback(void); void PWM_ISR_CH2_Callback(void); // Sideboard definitions diff --git a/Inc/rtwtypes.h b/Inc/rtwtypes.h index 6c93fd9..21d3fc2 100644 --- a/Inc/rtwtypes.h +++ b/Inc/rtwtypes.h @@ -3,9 +3,9 @@ * * Code generated for Simulink model 'BLDC_controller'. * - * Model version : 1.1256 + * Model version : 1.1260 * Simulink Coder version : 8.13 (R2017b) 24-Jul-2017 - * C/C++ source code generated on : Mon Dec 30 21:36:12 2019 + * C/C++ source code generated on : Tue Mar 24 11:01:08 2020 * * Target selection: ert.tlc * Embedded hardware selection: ARM Compatible->ARM Cortex diff --git a/MDK-ARM/mainboard-hack.uvoptx b/MDK-ARM/mainboard-hack.uvoptx index 0dc5c18..93cd70d 100644 --- a/MDK-ARM/mainboard-hack.uvoptx +++ b/MDK-ARM/mainboard-hack.uvoptx @@ -75,7 +75,7 @@ 1 0 - 0 + 1 18 @@ -633,6 +633,159 @@ + + VARIANT_PWM + 0x4 + ARM-ADS + + 8000000 + + 1 + 0 + 0 + 1 + 0 + + + 1 + 65535 + 0 + 0 + 0 + + + 79 + 66 + 8 + .\Listing\ + + + 1 + 1 + 1 + 0 + 1 + 1 + 0 + 1 + 0 + 0 + 0 + 0 + + + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 0 + 0 + + + 1 + 0 + 0 + + 18 + + 0 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 0 + 1 + 1 + 1 + 1 + 0 + 0 + 1 + 5 + + + + + + + + + + + STLink\ST-LINKIII-KEIL_SWO.dll + + + + 0 + ST-LINKIII-KEIL_SWO + -U -O206 -S0 -C0 -A0 -TO18 -TC10000000 -TP21 -TDS8004 -TDT0 -TDC1F -TIEFFFFFFFF -TIP8 -FO15 -FD20000000 -FC1000 -FN1 -FF0STM32F10x_512.FLM -FS08000000 -FL080000 -FP0($$Device:STM32F103RC$Flash\STM32F10x_512.FLM) + + + 0 + UL2CM3 + UL2CM3(-S0 -C0 -P0 -FD20000000 -FC1000 -FN1 -FF0STM32F10x_512 -FS08000000 -FL080000 -FP0($$Device:STM32F103RC$Flash\STM32F10x_512.FLM)) + + + + + 0 + + + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + + + + 0 + 0 + 0 + + + + + + 1 + 0 + 2 + 10000000 + + + + VARIANT_IBUS 0x4 @@ -840,7 +993,7 @@ 1 0 - 1 + 0 18 diff --git a/MDK-ARM/mainboard-hack.uvprojx b/MDK-ARM/mainboard-hack.uvprojx index 8958233..4fcbab9 100644 --- a/MDK-ARM/mainboard-hack.uvprojx +++ b/MDK-ARM/mainboard-hack.uvprojx @@ -2410,6 +2410,624 @@ + + VARIANT_PWM + 0x4 + ARM-ADS + 5060422::V5.06 update 4 (build 422)::ARMCC + + + STM32F103RC + STMicroelectronics + Keil.STM32F1xx_DFP.2.3.0 + http://www.keil.com/pack/ + IRAM(0x20000000-0x2000BFFF) IROM(0x8000000-0x803FFFF) CLOCK(8000000) CPUTYPE("Cortex-M3") + + + + + + + + + + + + + + + $$Device:STM32F103RC$SVD\STM32F103xx.svd + 0 + 0 + + + + + + + 0 + 0 + 0 + 0 + 1 + + .\Objects\ + firmware + 1 + 0 + 1 + 1 + 1 + .\Listing\ + 1 + 0 + 0 + + 0 + 0 + + + 0 + 0 + 0 + 0 + + + 0 + 0 + + + 0 + 0 + 0 + 0 + + + 1 + 0 + $K\ARM\ARMCC\bin\fromelf.exe --bin --output=.\Objects\@L.bin !L + + 0 + 0 + 0 + 0 + + 0 + + + + 0 + 0 + 0 + 0 + 0 + 1 + 0 + 0 + 0 + 0 + 3 + + + 0 + + + SARMCM3.DLL + -REMAP + DCM.DLL + -pCM3 + SARMCM3.DLL + + TCM.DLL + -pCM3 + + + + 1 + 0 + 0 + 0 + 16 + + + + + 1 + 0 + 0 + 1 + 1 + 4096 + + 1 + BIN\UL2CM3.DLL + "" () + + + + + 0 + + + + 0 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 0 + 1 + 1 + 0 + 1 + 1 + 0 + 0 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 0 + 0 + "Cortex-M3" + + 0 + 0 + 0 + 1 + 1 + 0 + 0 + 0 + 0 + 0 + 8 + 1 + 0 + 0 + 0 + 3 + 3 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 1 + 0 + 0 + 0 + 0 + 1 + 0 + + + 0 + 0x0 + 0x0 + + + 0 + 0x0 + 0x0 + + + 0 + 0x0 + 0x0 + + + 0 + 0x0 + 0x0 + + + 0 + 0x0 + 0x0 + + + 0 + 0x0 + 0x0 + + + 0 + 0x20000000 + 0xc000 + + + 1 + 0x8000000 + 0x40000 + + + 0 + 0x0 + 0x0 + + + 1 + 0x0 + 0x0 + + + 1 + 0x0 + 0x0 + + + 1 + 0x0 + 0x0 + + + 1 + 0x8000000 + 0x40000 + + + 1 + 0x0 + 0x0 + + + 0 + 0x0 + 0x0 + + + 0 + 0x0 + 0x0 + + + 0 + 0x0 + 0x0 + + + 0 + 0x20000000 + 0xc000 + + + 0 + 0x0 + 0x0 + + + + + + 1 + 4 + 0 + 0 + 1 + 0 + 0 + 0 + 0 + 0 + 2 + 0 + 0 + 1 + 0 + 1 + 1 + 1 + 1 + 0 + 0 + 0 + + + USE_HAL_DRIVER,STM32F103xE,VARIANT_PWM + + ..\Inc;..\Drivers\STM32F1xx_HAL_Driver\Inc;..\Drivers\STM32F1xx_HAL_Driver\Inc\Legacy;..\Drivers\CMSIS\Device\ST\STM32F1xx\Include;..\Drivers\CMSIS\Include + + + + 1 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + + + + + + + + + 1 + 0 + 0 + 0 + 1 + 0 + 0x08000000 + 0x20000000 + + + + + + + + + + + + + Startup + + + startup_stm32f103xe.s + 2 + .\startup_stm32f103xe.s + + + + + Src + + + bldc.c + 1 + ..\Src\bldc.c + + + BLDC_controller.c + 1 + ..\Src\BLDC_controller.c + + + BLDC_controller_data.c + 1 + ..\Src\BLDC_controller_data.c + + + comms.c + 1 + ..\Src\comms.c + + + control.c + 1 + ..\Src\control.c + + + eeprom.c + 1 + ..\Src\eeprom.c + + + hd44780.c + 1 + ..\Src\hd44780.c + + + main.c + 1 + ..\Src\main.c + + + pcf8574.c + 1 + ..\Src\pcf8574.c + + + setup.c + 1 + ..\Src\setup.c + + + stm32f1xx_it.c + 1 + ..\Src\stm32f1xx_it.c + + + util.c + 1 + ..\Src\util.c + + + config.h + 5 + ..\Inc\config.h + + + + + HAL_Driver + + + stm32f1xx_hal.c + 1 + ..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal.c + + + stm32f1xx_hal_adc.c + 1 + ..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_adc.c + + + stm32f1xx_hal_adc_ex.c + 1 + ..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_adc_ex.c + + + stm32f1xx_hal_cortex.c + 1 + ..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_cortex.c + + + stm32f1xx_hal_dma.c + 1 + ..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_dma.c + + + stm32f1xx_hal_flash.c + 1 + ..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_flash.c + + + stm32f1xx_hal_flash_ex.c + 1 + ..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_flash_ex.c + + + stm32f1xx_hal_gpio.c + 1 + ..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_gpio.c + + + stm32f1xx_hal_gpio_ex.c + 1 + ..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_gpio_ex.c + + + stm32f1xx_hal_i2c.c + 1 + ..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_i2c.c + + + stm32f1xx_hal_pwr.c + 1 + ..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_pwr.c + + + stm32f1xx_hal_rcc.c + 1 + ..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_rcc.c + + + stm32f1xx_hal_rcc_ex.c + 1 + ..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_rcc_ex.c + + + stm32f1xx_hal_tim.c + 1 + ..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_tim.c + + + stm32f1xx_hal_tim_ex.c + 1 + ..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_tim_ex.c + + + stm32f1xx_hal_uart.c + 1 + ..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_uart.c + + + + + CMSIS + + + system_stm32f1xx.c + 1 + ../Src/system_stm32f1xx.c + + + + + ::CMSIS + + + 0 + 0 + 0 + 0 + 0 + 1 + 2 + 2 + 2 + 2 + 11 + + + 1 + + + + 2 + 0 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 0 + 2 + 2 + 2 + 2 + 0 + 0 + 2 + 2 + 2 + 2 + 2 + + + + + + + + + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + + + + + + + + + + + + VARIANT_IBUS 0x4 @@ -4760,6 +5378,7 @@ + diff --git a/README.md b/README.md index 0e8da5d..7dfe37a 100644 --- a/README.md +++ b/README.md @@ -104,8 +104,9 @@ This firmware offers currently these variants (selectable in [platformio.ini](/p - **VARIANT_ADC**: In this variant the motors are controlled by two potentiometers connected to the Left sensor cable (long wired) - **VARIANT_USART**: In this variant the motors are controlled via serial protocol (e.g. on USART3 right sensor cable, the short wired cable). The commands can be sent from an Arduino. Check out the [hoverserial.ino](/02_Arduino/hoverserial) as an example sketch. - **VARIANT_NUNCHUK**: Wii Nunchuk offers one hand control for throttle, braking and steering. This was one of the first input device used for electric armchairs or bottle crates. -- **VARIANT_PPM**: This is when you want to use a RC remote control with PPM Sum signal -- **VARIANT_IBUS**: This is when you want to use a RC remote control with Flysky IBUS protocol connected to the Left sensor cable. +- **VARIANT_PPM**: This is when you want to use an RC remote control with PPM Sum signal. +- **VARIANT_PWM**: This is when you want to use an RC remote control with PWM signal. +- **VARIANT_IBUS**: This is when you want to use an RC remote control with Flysky IBUS protocol connected to the Left sensor cable. - **VARIANT_HOVERCAR**: In this variant the motors are controlled by two pedals brake and throttle. Reverse is engaged by double tapping on the brake pedal at standstill. - **VARIANT_HOVERBOARD**: In this variant the mainboard reads the sideboards data. The sideboards need to be flashed with the hacked version. Only balancing controller is still to be implemented. - **VARIANT_TRANSPOTTER**: This build is for transpotter which is a hoverboard based transportation system. For more details on how to build it check [here](https://github.com/NiklasFauth/hoverboard-firmware-hack/wiki/Build-Instruction:-TranspOtter) and [here](https://hackaday.io/project/161891-transpotter-ng). diff --git a/Src/BLDC_controller.c b/Src/BLDC_controller.c index 6bd2bd1..0df807e 100644 --- a/Src/BLDC_controller.c +++ b/Src/BLDC_controller.c @@ -3,9 +3,9 @@ * * Code generated for Simulink model 'BLDC_controller'. * - * Model version : 1.1256 + * Model version : 1.1260 * Simulink Coder version : 8.13 (R2017b) 24-Jul-2017 - * C/C++ source code generated on : Mon Dec 30 21:36:12 2019 + * C/C++ source code generated on : Tue Mar 24 11:01:08 2020 * * Target selection: ert.tlc * Embedded hardware selection: ARM Compatible->ARM Cortex @@ -1012,6 +1012,7 @@ void BLDC_controller_step(RT_MODEL *const rtM) * Abs: '/Abs4' * Constant: '/CTRL_COMM4' * Constant: '/r_errInpTgtThres' + * Inport: '/b_motEna' * Logic: '/Logical Operator1' * RelationalOperator: '/Relational Operator7' * S-Function (sfix_bitop): '/Bitwise Operator1' @@ -1033,8 +1034,8 @@ void BLDC_controller_step(RT_MODEL *const rtM) rtb_Merge_f_idx_1 = rtDW->UnitDelay4_DSTATE_eu; } - rtb_RelationalOperator1_m = ((rtb_Merge_f_idx_1 > rtP->r_errInpTgtThres) && - rtb_RelationalOperator9); + rtb_RelationalOperator1_m = (rtU->b_motEna && rtb_RelationalOperator9 && + (rtb_Merge_f_idx_1 > rtP->r_errInpTgtThres)); } /* End of Switch: '/Switch3' */ diff --git a/Src/BLDC_controller_data.c b/Src/BLDC_controller_data.c index 9ce9b3a..8171ac0 100644 --- a/Src/BLDC_controller_data.c +++ b/Src/BLDC_controller_data.c @@ -3,9 +3,9 @@ * * Code generated for Simulink model 'BLDC_controller'. * - * Model version : 1.1256 + * Model version : 1.1260 * Simulink Coder version : 8.13 (R2017b) 24-Jul-2017 - * C/C++ source code generated on : Mon Dec 30 21:36:12 2019 + * C/C++ source code generated on : Tue Mar 24 11:01:08 2020 * * Target selection: ert.tlc * Embedded hardware selection: ARM Compatible->ARM Cortex diff --git a/Src/control.c b/Src/control.c index a85b3df..b0e9022 100644 --- a/Src/control.c +++ b/Src/control.c @@ -26,8 +26,6 @@ uint32_t ppm_timeout = 0; bool ppm_valid = true; -#define IN_RANGE(x, low, up) (((x) >= (low)) && ((x) <= (up))) - void PPM_ISR_Callback(void) { // Dummy loop with 16 bit count wrap around uint16_t rc_delay = TIM2->CNT; @@ -87,84 +85,54 @@ void PPM_Init(void) { #endif -#ifdef BUTTONS_RIGHT - -uint8_t btn1 = 0; -uint8_t btn2 = 0; - -void BUTTONS_RIGHT_Init() { - GPIO_InitTypeDef GPIO_InitStruct; - /*Configure GPIO pin : PB10 */ - GPIO_InitStruct.Pin = GPIO_PIN_10; - GPIO_InitStruct.Mode = GPIO_MODE_INPUT; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM; - GPIO_InitStruct.Pull = GPIO_PULLUP; - HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); - - btn1 = !HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_10); - - GPIO_InitTypeDef GPIO_InitStruct2; - /*Configure GPIO pin : PB11 */ - GPIO_InitStruct2.Pin = GPIO_PIN_11; - GPIO_InitStruct2.Mode = GPIO_MODE_INPUT; - GPIO_InitStruct2.Speed = GPIO_SPEED_FREQ_MEDIUM; - GPIO_InitStruct2.Pull = GPIO_PULLUP; - HAL_GPIO_Init(GPIOB, &GPIO_InitStruct2); - - btn2 = !HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_11); -} - -#endif - #ifdef CONTROL_PWM -//uint16_t pwm_captured_ch1_value = 500; +uint16_t pwm_captured_ch1_value = 500; uint16_t pwm_captured_ch2_value = 500; -uint32_t pwm_timeout = 0; +uint32_t pwm_timeout_ch1 = 0; +uint32_t pwm_timeout_ch2 = 0; -#define IN_RANGE(x, low, up) (((x) >= (low)) && ((x) <= (up))) - -/* void PWM_ISR_CH1_Callback(void) { // Dummy loop with 16 bit count wrap around uint16_t rc_signal = TIM3->CNT; TIM3->CNT = 0; - // The interval check below should be larger than the feasible PWM interval of ~[500, 2500] ms - if (IN_RANGE(rc_signal, 200, 4000)){ + if (IN_RANGE(rc_signal, 900, 2100)){ timeout = 0; - pwm_timeout = 0; + pwm_timeout_ch1 = 0; pwm_captured_ch1_value = CLAMP(rc_signal, 1000, 2000) - 1000; } } -*/ + void PWM_ISR_CH2_Callback(void) { // Dummy loop with 16 bit count wrap around uint16_t rc_signal = TIM2->CNT; TIM2->CNT = 0; - // The interval check below should be larger than the feasible PWM interval of ~[900, 2100] ms - if (IN_RANGE(rc_signal, 200, 3000)){ + if (IN_RANGE(rc_signal, 900, 2100)){ timeout = 0; - pwm_timeout = 0; + pwm_timeout_ch2 = 0; pwm_captured_ch2_value = CLAMP(rc_signal, 1000, 2000) - 1000; } } // SysTick executes once each ms void PWM_SysTick_Callback(void) { - pwm_timeout++; + pwm_timeout_ch1++; + pwm_timeout_ch2++; // Stop after 500 ms without PWM signal - if(pwm_timeout > 500) { - //pwm_captured_ch1_value = 500; + if(pwm_timeout_ch1 > 500) { + pwm_captured_ch1_value = 500; + pwm_timeout_ch1 = 500; // limit the timeout to max timeout value of 500 ms + } + if(pwm_timeout_ch2 > 500) { pwm_captured_ch2_value = 500; - pwm_timeout = 500; // limit the timeout to max timeout value of 500 ms + pwm_timeout_ch2 = 500; // limit the timeout to max timeout value of 500 ms } } void PWM_Init(void) { // Channel 1 (steering) - /* GPIO_InitTypeDef GPIO_InitStruct2; // Configure GPIO pin : PA2 GPIO_InitStruct2.Pin = GPIO_PIN_2; @@ -185,10 +153,8 @@ void PWM_Init(void) { HAL_NVIC_SetPriority(EXTI2_IRQn, 0, 0); HAL_NVIC_EnableIRQ(EXTI2_IRQn); HAL_TIM_Base_Start(&TimHandle2); -*/ // Channel 2 (speed) - GPIO_InitTypeDef GPIO_InitStruct; /*Configure GPIO pin : PA3 */ GPIO_InitStruct.Pin = GPIO_PIN_3; @@ -209,10 +175,25 @@ void PWM_Init(void) { HAL_NVIC_SetPriority(EXTI3_IRQn, 0, 0); HAL_NVIC_EnableIRQ(EXTI3_IRQn); HAL_TIM_Base_Start(&TimHandle); + + #ifdef SUPPORT_BUTTONS + /*Configure GPIO pin : PB10 */ + GPIO_InitStruct.Pin = BUTTON1_RIGHT_PIN; + GPIO_InitStruct.Mode = GPIO_MODE_INPUT; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM; + GPIO_InitStruct.Pull = GPIO_PULLUP; + HAL_GPIO_Init(BUTTON1_RIGHT_PORT, &GPIO_InitStruct); + + /*Configure GPIO pin : PB11 */ + GPIO_InitStruct2.Pin = BUTTON2_RIGHT_PIN; + GPIO_InitStruct2.Mode = GPIO_MODE_INPUT; + GPIO_InitStruct2.Speed = GPIO_SPEED_FREQ_MEDIUM; + GPIO_InitStruct2.Pull = GPIO_PULLUP; + HAL_GPIO_Init(BUTTON2_RIGHT_PORT, &GPIO_InitStruct2); + #endif } #endif - uint8_t Nunchuk_Ping(void) { if (HAL_I2C_Master_Receive(&hi2c2,0xA4,(uint8_t*)nunchuk_data, 1, 10) == HAL_OK) { return 1; diff --git a/Src/stm32f1xx_it.c b/Src/stm32f1xx_it.c index 2f69f7c..5c1dbcb 100644 --- a/Src/stm32f1xx_it.c +++ b/Src/stm32f1xx_it.c @@ -242,13 +242,12 @@ void EXTI3_IRQHandler(void) PWM_ISR_CH2_Callback(); __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_3); } -/* + void EXTI2_IRQHandler(void) { PWM_ISR_CH1_Callback(); __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_2); -}*/ - +} #endif #ifdef CONTROL_SERIAL_USART2 diff --git a/Src/util.c b/Src/util.c index 9fb8e82..69ed6df 100644 --- a/Src/util.c +++ b/Src/util.c @@ -64,7 +64,7 @@ extern volatile uint16_t ppm_captured_value[PPM_NUM_CHANNELS+1]; #endif #ifdef CONTROL_PWM -//extern volatile uint16_t pwm_captured_ch1_value; +extern volatile uint16_t pwm_captured_ch1_value; extern volatile uint16_t pwm_captured_ch2_value; #endif @@ -152,7 +152,6 @@ uint16_t VirtAddVarTab[NB_OF_VAR] = {0x1300}; // Dummy virtual address to av //------------------------------------------------------------------------ static int16_t INPUT_MAX; // [-] Input target maximum limitation static int16_t INPUT_MIN; // [-] Input target minimum limitation -static int16_t INPUT_MID; // [-] Input target middle #if defined(CONTROL_ADC) && defined(ADC_PROTECT_ENA) static int16_t timeoutCntADC = 0; // Timeout counter for ADC Protection @@ -238,7 +237,6 @@ void Input_Lim_Init(void) { // Input Limitations - ! Do NOT touch ! INPUT_MAX = 1000; INPUT_MIN = -1000; } - INPUT_MID = INPUT_MAX / 2; } void Input_Init(void) { @@ -639,18 +637,22 @@ void readCommand(void) { #endif #ifdef CONTROL_PPM - cmd1 = CLAMP((ppm_captured_value[0] - INPUT_MID) * 2, INPUT_MIN, INPUT_MAX); - cmd2 = CLAMP((ppm_captured_value[1] - INPUT_MID) * 2, INPUT_MIN, INPUT_MAX); + cmd1 = CLAMP((ppm_captured_value[0] - 500) * 2, INPUT_MIN, INPUT_MAX); + cmd2 = CLAMP((ppm_captured_value[1] - 500) * 2, INPUT_MIN, INPUT_MAX); #ifdef SUPPORT_BUTTONS - button1 = ppm_captured_value[5] > INPUT_MID; + button1 = ppm_captured_value[5] > 500; button2 = 0; #endif // float scale = ppm_captured_value[2] / 1000.0f; // not used for now, uncomment if needed #endif #ifdef CONTROL_PWM - cmd1 = 0; // CLAMP(PWM_Signal_Correct((pwm_captured_ch1_value - 500) * 2, PWM_CH1_MIN, PWM_CH1_MAX), INPUT_MIN, INPUT_MAX); + cmd1 = CLAMP(PWM_Signal_Correct((pwm_captured_ch1_value - 500) * 2, PWM_CH1_MIN, PWM_CH1_MAX), INPUT_MIN, INPUT_MAX); cmd2 = CLAMP(PWM_Signal_Correct((pwm_captured_ch2_value - 500) * 2, PWM_CH2_MIN, PWM_CH2_MAX), INPUT_MIN, INPUT_MAX); + #ifdef SUPPORT_BUTTONS + button1 = !HAL_GPIO_ReadPin(BUTTON1_RIGHT_PORT, BUTTON1_RIGHT_PIN); + button2 = !HAL_GPIO_ReadPin(BUTTON2_RIGHT_PORT, BUTTON2_RIGHT_PIN); + #endif #endif #ifdef CONTROL_ADC @@ -718,8 +720,8 @@ void readCommand(void) { 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, 0, INPUT_MAX); // 1000-2000 -> 0-1000 } - 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); + cmd1 = CLAMP((ibus_captured_value[0] - 500) * 2, INPUT_MIN, INPUT_MAX); + cmd2 = CLAMP((ibus_captured_value[1] - 500) * 2, INPUT_MIN, INPUT_MAX); command.start = 0xFF; // Change the Start Frame for timeout detection in the next cycle timeoutCntSerial = 0; // Reset the timeout counter } @@ -844,6 +846,7 @@ void readCommand(void) { * This function realizes a dead-band around 0 and scales the input within a min and a max */ int PWM_Signal_Correct(int16_t u, int16_t min, int16_t max) { +#ifdef CONTROL_PWM int outVal = 0; if(u > -PWM_DEADBAND && u < PWM_DEADBAND) { outVal = 0; @@ -853,6 +856,9 @@ int PWM_Signal_Correct(int16_t u, int16_t min, int16_t max) { outVal = (INPUT_MIN * CLAMP(u + PWM_DEADBAND, min + PWM_DEADBAND, 0)) / (min + PWM_DEADBAND); } return outVal; +#else + return 0; +#endif } diff --git a/platformio.ini b/platformio.ini index a910d93..2de82ae 100644 --- a/platformio.ini +++ b/platformio.ini @@ -13,7 +13,7 @@ src_dir = Src ;default_envs = VARIANT_USART ; Variant for Serial control via USART3 input ;default_envs = VARIANT_NUNCHUK ; Variant for Nunchuk controlled vehicle build ;default_envs = VARIANT_PPM ; Variant for RC-Remotes with PPM-Sum signal -default_envs = VARIANT_PWM ; Variant for RC-Remotes with PWM signal +;default_envs = VARIANT_PWM ; Variant for RC-Remotes with PWM signal ;default_envs = VARIANT_IBUS ; Variant for RC-Remotes with FLYSKY IBUS ;default_envs = VARIANT_HOVERCAR ; Variant for HOVERCAR build ;default_envs = VARIANT_HOVERBOARD ; Variant for HOVERBOARD