From 733fd80362252f94d8f819d36c73c5c498ce8173 Mon Sep 17 00:00:00 2001 From: EmanuelFeru Date: Mon, 20 Jul 2020 20:33:42 +0200 Subject: [PATCH 1/4] created SKATEBOARD variant --- Inc/config.h | 37 ++++++++++++++++++++++++++++++++++++- Src/main.c | 8 +++++++- Src/util.c | 8 ++++++-- platformio.ini | 21 +++++++++++++++++++++ 4 files changed, 70 insertions(+), 4 deletions(-) diff --git a/Inc/config.h b/Inc/config.h index f4de14f..b59a001 100644 --- a/Inc/config.h +++ b/Inc/config.h @@ -19,6 +19,7 @@ //#define VARIANT_HOVERCAR // Variant for HOVERCAR build //#define VARIANT_HOVERBOARD // Variant for HOVERBOARD build //#define VARIANT_TRANSPOTTER // Variant for TRANSPOTTER build https://github.com/NiklasFauth/hoverboard-firmware-hack/wiki/Build-Instruction:-TranspOtter https://hackaday.io/project/161891-transpotter-ng + //#define VARIANT_SKATEBOARD // Variant for SKATEBOARD build #endif // ########################### END OF VARIANT SELECTION ############################ @@ -432,6 +433,40 @@ // ############################# END OF VARIANT_TRANSPOTTER SETTINGS ######################## +// ################################# VARIANT_PWM SETTINGS ############################## +#ifdef VARIANT_SKATEBOARD +/* ###### CONTROL VIA RC REMOTE ###### + * right sensor board cable. Connect PB10 to channel 1 and PB11 to channel 2 on receiver. + * Channel 1: steering, Channel 2: speed. +*/ + #undef CTRL_MOD_REQ + #define CTRL_MOD_REQ TRQ_MODE // SKATEBOARD works best in TORQUE Mode + #define CONTROL_PWM_LEFT // use RC PWM as input on the LEFT cable. disable DEBUG_SERIAL_USART2! + // #define CONTROL_PWM_RIGHT // use RC PWM as input on the RIGHT cable. disable DEBUG_SERIAL_USART3! + #ifdef CONTROL_PWM_RIGHT + #define DEBUG_SERIAL_USART2 // left sensor cable debug + #else + #define DEBUG_SERIAL_USART3 // right sensor cable debug + #endif + #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 700 // (0 - 1000) + #define PWM_CH2_MIN -800 // (-1000 - 0) + #define PWM_CH2_OUT_MIN -150 // (-1000 - 0) Change this value to adjust the braking amount + #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 // 1.0f [-] fixdt(1,16,14) higher value == stronger. [0, 65535] = [-2.0 - 2.0]. In this case 16384 = 1.0 * 2^14. If you do not want any steering, set it to 0. + #define INVERT_R_DIRECTION + #define INVERT_L_DIRECTION + // #define SUPPORT_BUTTONS_LEFT // use left sensor board cable for button inputs. Disable DEBUG_SERIAL_USART2! + // #define SUPPORT_BUTTONS_RIGHT // use right sensor board cable for button inputs. Disable DEBUG_SERIAL_USART3! + // #define STANDSTILL_HOLD_ENABLE // [-] Flag to hold the position when standtill is reached. Only available and makes sense for VOLTAGE or TORQUE mode. +#endif +// ############################# END OF VARIANT_PPM SETTINGS ############################ + + // ########################### UART SETIINGS ############################ #if defined(FEEDBACK_SERIAL_USART2) || defined(CONTROL_SERIAL_USART2) || defined(DEBUG_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART2) || \ @@ -473,7 +508,7 @@ // ############################### VALIDATE SETTINGS ############################### #if !defined(VARIANT_ADC) && !defined(VARIANT_USART) && !defined(VARIANT_NUNCHUK) && !defined(VARIANT_PPM) && !defined(VARIANT_PWM) && \ - !defined(VARIANT_IBUS) && !defined(VARIANT_HOVERCAR) && !defined(VARIANT_HOVERBOARD) && !defined(VARIANT_TRANSPOTTER) + !defined(VARIANT_IBUS) && !defined(VARIANT_HOVERCAR) && !defined(VARIANT_HOVERBOARD) && !defined(VARIANT_TRANSPOTTER) && !defined(VARIANT_SKATEBOARD) #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 e3eda39..8389ea9 100644 --- a/Src/main.c +++ b/Src/main.c @@ -213,7 +213,7 @@ int main(void) { } // ####### VARIANT_HOVERCAR ####### - #if defined(VARIANT_HOVERCAR) || defined(ELECTRIC_BRAKE_ENABLE) + #if defined(VARIANT_HOVERCAR) || defined(VARIANT_SKATEBOARD) || defined(ELECTRIC_BRAKE_ENABLE) uint16_t speedBlend; // Calculate speed Blend, a number between [0, 1] in fixdt(0,16,15) speedBlend = (uint16_t)(((CLAMP(speedAvgAbs,10,60) - 10) << 15) / 50); // speedBlend [0,1] is within [10 rpm, 60rpm] #endif @@ -240,6 +240,12 @@ int main(void) { } #endif + #ifdef VARIANT_SKATEBOARD + if (cmd2 < 0) { // When Throttle pedal is negative, it acts as brake. This condition is to make sure it goes to 0 as we reach standstill (to avoid Reverse driving) + cmd2 = (int16_t)((cmd2 * speedBlend) >> 15); + } + #endif + // ####### LOW-PASS FILTER ####### rateLimiter16(cmd1, RATE, &steerRateFixdt); rateLimiter16(cmd2, RATE, &speedRateFixdt); diff --git a/Src/util.c b/Src/util.c index 4277dbc..6111bd2 100644 --- a/Src/util.c +++ b/Src/util.c @@ -737,8 +737,8 @@ void readCommand(void) { #endif #if defined(CONTROL_PPM_LEFT) || defined(CONTROL_PPM_RIGHT) - cmd1 = CLAMP(addDeadBand((ppm_captured_value[0] - 500) * 2, PPM_DEADBAND, PPM_CH1_MIN, PPM_CH1_MAX), INPUT_MIN, INPUT_MAX); - cmd2 = CLAMP(addDeadBand((ppm_captured_value[1] - 500) * 2, PPM_DEADBAND, PPM_CH2_MIN, PPM_CH2_MAX), INPUT_MIN, INPUT_MAX); + cmd1 = addDeadBand((ppm_captured_value[0] - 500) * 2, PPM_DEADBAND, PPM_CH1_MIN, PPM_CH1_MAX, INPUT_MIN, INPUT_MAX); + cmd2 = addDeadBand((ppm_captured_value[1] - 500) * 2, PPM_DEADBAND, PPM_CH2_MIN, PPM_CH2_MAX, INPUT_MIN, INPUT_MAX); #ifdef SUPPORT_BUTTONS button1 = ppm_captured_value[5] > 500; button2 = 0; @@ -751,7 +751,11 @@ void readCommand(void) { #if defined(CONTROL_PWM_LEFT) || defined(CONTROL_PWM_RIGHT) cmd1 = addDeadBand((pwm_captured_ch1_value - 500) * 2, PWM_DEADBAND, PWM_CH1_MIN, PWM_CH1_MAX, INPUT_MIN, INPUT_MAX); + #if !defined(VARIANT_SKATEBOARD) cmd2 = addDeadBand((pwm_captured_ch2_value - 500) * 2, PWM_DEADBAND, PWM_CH2_MIN, PWM_CH2_MAX, INPUT_MIN, INPUT_MAX); + #else + cmd2 = addDeadBand((pwm_captured_ch2_value - 500) * 2, PWM_DEADBAND, PWM_CH2_MIN, PWM_CH2_MAX, PWM_CH2_OUT_MIN, INPUT_MAX); + #endif #if defined(SUPPORT_BUTTONS_LEFT) || defined(SUPPORT_BUTTONS_RIGHT) button1 = !HAL_GPIO_ReadPin(BUTTON1_PORT, BUTTON1_PIN); button2 = !HAL_GPIO_ReadPin(BUTTON2_PORT, BUTTON2_PIN); diff --git a/platformio.ini b/platformio.ini index 2de82ae..9926883 100644 --- a/platformio.ini +++ b/platformio.ini @@ -18,6 +18,7 @@ src_dir = Src ;default_envs = VARIANT_HOVERCAR ; Variant for HOVERCAR build ;default_envs = VARIANT_HOVERBOARD ; Variant for HOVERBOARD ;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 +;default_envs = VARIANT_SKATEBOARD ; Variant for SKATEBOARD build controlled via RC-Remotes with PWM signal ;================================================================ ;================================================================ @@ -208,3 +209,23 @@ build_flags = -D VARIANT_TRANSPOTTER ;================================================================ + + +[env:VARIANT_SKATEBOARD] +platform = ststm32 +framework = stm32cube +board = genericSTM32F103RC +debug_tool = stlink +upload_protocol = stlink + +build_flags = + -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_SKATEBOARD + +;================================================================ From 290ed307656fe3df155ceead7d6580f2530fac5e Mon Sep 17 00:00:00 2001 From: EmanuelFeru Date: Mon, 20 Jul 2020 21:06:39 +0200 Subject: [PATCH 2/4] Fixes Skateboard Variant - fixed to RIGHT cable - fixed braking relative to the direction of motion - adjusted the braking amount to -400 --- Inc/config.h | 32 ++++++++++++++++---------------- Src/main.c | 10 +++++++--- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/Inc/config.h b/Inc/config.h index b59a001..e94d201 100644 --- a/Inc/config.h +++ b/Inc/config.h @@ -324,11 +324,6 @@ */ #define CONTROL_PWM_LEFT // use RC PWM as input on the LEFT cable. disable DEBUG_SERIAL_USART2! // #define CONTROL_PWM_RIGHT // use RC PWM as input on the RIGHT cable. disable DEBUG_SERIAL_USART3! - #ifdef CONTROL_PWM_RIGHT - #define DEBUG_SERIAL_USART2 // left sensor cable debug - #else - #define DEBUG_SERIAL_USART3 // right sensor cable debug - #endif #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) @@ -342,8 +337,13 @@ // #define INVERT_L_DIRECTION // #define SUPPORT_BUTTONS_LEFT // use left sensor board cable for button inputs. Disable DEBUG_SERIAL_USART2! // #define SUPPORT_BUTTONS_RIGHT // use right sensor board cable for button inputs. Disable DEBUG_SERIAL_USART3! + #ifdef CONTROL_PWM_RIGHT + #define DEBUG_SERIAL_USART2 // left sensor cable debug + #else + #define DEBUG_SERIAL_USART3 // right sensor cable debug + #endif #endif -// ############################# END OF VARIANT_PPM SETTINGS ############################ +// ############################# END OF VARIANT_PWM SETTINGS ############################ @@ -433,7 +433,7 @@ // ############################# END OF VARIANT_TRANSPOTTER SETTINGS ######################## -// ################################# VARIANT_PWM SETTINGS ############################## +// ################################# VARIANT_SKATEBOARD SETTINGS ############################## #ifdef VARIANT_SKATEBOARD /* ###### CONTROL VIA RC REMOTE ###### * right sensor board cable. Connect PB10 to channel 1 and PB11 to channel 2 on receiver. @@ -441,20 +441,15 @@ */ #undef CTRL_MOD_REQ #define CTRL_MOD_REQ TRQ_MODE // SKATEBOARD works best in TORQUE Mode - #define CONTROL_PWM_LEFT // use RC PWM as input on the LEFT cable. disable DEBUG_SERIAL_USART2! - // #define CONTROL_PWM_RIGHT // use RC PWM as input on the RIGHT cable. disable DEBUG_SERIAL_USART3! - #ifdef CONTROL_PWM_RIGHT - #define DEBUG_SERIAL_USART2 // left sensor cable debug - #else - #define DEBUG_SERIAL_USART3 // right sensor cable debug - #endif + // #define CONTROL_PWM_LEFT // use RC PWM as input on the LEFT cable. disable DEBUG_SERIAL_USART2! + #define CONTROL_PWM_RIGHT // use RC PWM as input on the RIGHT cable. 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 700 // (0 - 1000) #define PWM_CH2_MIN -800 // (-1000 - 0) - #define PWM_CH2_OUT_MIN -150 // (-1000 - 0) Change this value to adjust the braking amount + #define PWM_CH2_OUT_MIN -400 // (-1000 - 0) Change this value to adjust the braking amount #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 // 1.0f [-] fixdt(1,16,14) higher value == stronger. [0, 65535] = [-2.0 - 2.0]. In this case 16384 = 1.0 * 2^14. If you do not want any steering, set it to 0. @@ -463,8 +458,13 @@ // #define SUPPORT_BUTTONS_LEFT // use left sensor board cable for button inputs. Disable DEBUG_SERIAL_USART2! // #define SUPPORT_BUTTONS_RIGHT // use right sensor board cable for button inputs. Disable DEBUG_SERIAL_USART3! // #define STANDSTILL_HOLD_ENABLE // [-] Flag to hold the position when standtill is reached. Only available and makes sense for VOLTAGE or TORQUE mode. + #ifdef CONTROL_PWM_RIGHT + #define DEBUG_SERIAL_USART2 // left sensor cable debug + #else + #define DEBUG_SERIAL_USART3 // right sensor cable debug + #endif #endif -// ############################# END OF VARIANT_PPM SETTINGS ############################ +// ############################# END OF VARIANT_SKATEBOARD SETTINGS ############################ diff --git a/Src/main.c b/Src/main.c index 8389ea9..c4b7f1b 100644 --- a/Src/main.c +++ b/Src/main.c @@ -236,13 +236,17 @@ int main(void) { if (speedAvg > 0) { // Make sure the Brake pedal is opposite to the direction of motion AND it goes to 0 as we reach standstill (to avoid Reverse driving by Brake pedal) cmd1 = (int16_t)((-cmd1 * speedBlend) >> 15); } else { - cmd1 = (int16_t)(( cmd1 * speedBlend) >> 15); + cmd1 = (int16_t)(( cmd1 * speedBlend) >> 15); } #endif #ifdef VARIANT_SKATEBOARD - if (cmd2 < 0) { // When Throttle pedal is negative, it acts as brake. This condition is to make sure it goes to 0 as we reach standstill (to avoid Reverse driving) - cmd2 = (int16_t)((cmd2 * speedBlend) >> 15); + if (cmd2 < 0) { // When Throttle is negative, it acts as brake. This condition is to make sure it goes to 0 as we reach standstill (to avoid Reverse driving) + if (speedAvg > 0) { // Make sure the braking is opposite to the direction of motion + cmd2 = (int16_t)(( cmd2 * speedBlend) >> 15); + } else { + cmd2 = (int16_t)((-cmd2 * speedBlend) >> 15); + } } #endif From 5a9b3cfbbc3741438de796a17f409bcaf54a5049 Mon Sep 17 00:00:00 2001 From: EmanuelFeru Date: Wed, 22 Jul 2020 15:56:14 +0200 Subject: [PATCH 3/4] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 4d2ff24..f43b654 100644 --- a/README.md +++ b/README.md @@ -125,6 +125,7 @@ This firmware offers currently these variants (selectable in [platformio.ini](/p - **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. See [HOVERCAR video](https://www.youtube.com/watch?v=IgHCcj0NgWQ&t=). - **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). +- **VARIANT_SKATEBOARD**: This is for skateboard build, controlled using an RC remote with PWM signal on right sensor cable. Of course the firmware can be further customized for other needs or projects. From 4d5c98d00aad4984175f4bafc31fbf33ac2691d4 Mon Sep 17 00:00:00 2001 From: EmanuelFeru Date: Wed, 22 Jul 2020 15:57:34 +0200 Subject: [PATCH 4/4] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f43b654..d066791 100644 --- a/README.md +++ b/README.md @@ -125,7 +125,7 @@ This firmware offers currently these variants (selectable in [platformio.ini](/p - **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. See [HOVERCAR video](https://www.youtube.com/watch?v=IgHCcj0NgWQ&t=). - **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). -- **VARIANT_SKATEBOARD**: This is for skateboard build, controlled using an RC remote with PWM signal on right sensor cable. +- **VARIANT_SKATEBOARD**: This is for skateboard build, controlled using an RC remote with PWM signal connected to the right sensor cable. Of course the firmware can be further customized for other needs or projects.