From cd01b7345143e0c2b9785ac146a7cb123272f893 Mon Sep 17 00:00:00 2001 From: kai Date: Sat, 18 Jan 2020 01:05:06 +0100 Subject: [PATCH 1/9] corrected nunchuck to nunchuk --- Inc/config.h | 52 +++++++++++++++++++++++----------------------- Inc/defines.h | 8 +++---- README.md | 8 +++---- Src/control.c | 24 ++++++++++----------- Src/main.c | 44 +++++++++++++++++++-------------------- Src/stm32f1xx_it.c | 2 +- platformio.ini | 6 +++--- 7 files changed, 72 insertions(+), 72 deletions(-) diff --git a/Inc/config.h b/Inc/config.h index 78425f4..05df47c 100644 --- a/Inc/config.h +++ b/Inc/config.h @@ -8,7 +8,7 @@ #if !defined(PLATFORMIO) //#define VARIANT_ADC // Variant for control via ADC input //#define VARIANT_USART3 // Variant for Serial control via USART3 input - //#define VARIANT_NUNCHUCK // Variant for Nunchuck controlled vehicle build + //#define VARIANT_ NUNCHUK // Variant for Nunchuk controlled vehicle build //#define VARIANT_PPM // Variant for RC-Remote with PPM-Sum Signal //#define VARIANT_IBUS // Variant for RC-Remotes with FLYSKY IBUS //#define VARIANT_HOVERCAR // Variant for HOVERCAR build @@ -54,7 +54,7 @@ /* How to calibrate: connect GND and RX of a 3.3v uart-usb adapter to the right sensor board cable * Be careful not to use the red wire of the cable. 15v will destroye verything.). - * If you are using nunchuck, disable it temporarily. enable DEBUG_SERIAL_USART3 and DEBUG_SERIAL_ASCII use asearial terminal. + * If you are using nunchuk, disable it temporarily. enable DEBUG_SERIAL_USART3 and DEBUG_SERIAL_ASCII use asearial terminal. */ /* Battery voltage calibration: connect power source. see . @@ -121,17 +121,17 @@ // #define FEEDBACK_SERIAL_USART2 // left sensor board cable, disable if ADC or PPM is used! // #define DEBUG_SERIAL_USART2 // left sensor board cable, disable if ADC or PPM is used! - // #define CONTROL_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuck or lcd) is used! For Arduino control check the hoverSerial.ino - // #define FEEDBACK_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuck or lcd) is used! - #define DEBUG_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuck or lcd) is used! + // #define CONTROL_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuk or lcd) is used! For Arduino control check the hoverSerial.ino + // #define FEEDBACK_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuk or lcd) is used! + #define DEBUG_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuk or lcd) is used! #elif defined(VARIANT_USART3) // #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! // #define DEBUG_SERIAL_USART2 // left sensor board cable, disable if ADC or PPM is used! - #define CONTROL_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuck or lcd) is used! For Arduino control check the hoverSerial.ino - #define FEEDBACK_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuck or lcd) is used! - // #define DEBUG_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuck or lcd) is used! + #define CONTROL_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuk or lcd) is used! For Arduino control check the hoverSerial.ino + #define FEEDBACK_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuk or lcd) is used! + // #define DEBUG_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuk or lcd) is used! #endif // ###### CONTROL VIA RC REMOTE WITH FLYSKY IBUS PROTOCOL ###### @@ -197,15 +197,15 @@ #define ADC2_MAX 2200 // max ADC2-value while poti at maximum-position (0 - 4095) #endif -#ifdef VARIANT_NUNCHUCK - // ###### CONTROL VIA NINTENDO NUNCHUCK ###### +#ifdef VARIANT_ NUNCHUK + // ###### CONTROL VIA NINTENDO NUNCHUK ###### /* left sensor board cable. - * keep cable short, use shielded cable, use ferrits, stabalize voltage in nunchuck, - * use the right one of the 2 types of nunchucks, add i2c pullups. - * use original nunchuck. most clones does not work very well. - * Recommendation: Nunchuck Breakout Board https://github.com/Jan--Henrik/hoverboard-breakout + * keep cable short, use shielded cable, use ferrits, stabalize voltage in nunchuk, + * use the right one of the 2 types of nunchuks, add i2c pullups. + * use original nunchuk. most clones does not work very well. + * Recommendation: Nunchuk Breakout Board https://github.com/Jan--Henrik/hoverboard-breakout */ - #define CONTROL_NUNCHUCK // use nunchuck as input. disable FEEDBACK_SERIAL_USART3, DEBUG_SERIAL_USART3! + #define CONTROL_ NUNCHUK // use nunchuk as input. disable FEEDBACK_SERIAL_USART3, DEBUG_SERIAL_USART3! #endif // ############################### MOTOR CONTROL ######################### @@ -272,12 +272,12 @@ #define RATE 480 // 30.0f [-] lower value == slower rate [0, 32767] = [0.0, 2047.9375]. Do NOT make rate negative (>32767) // Value of FILTER is in fixdt(0,16,16): VAL_fixedPoint = VAL_floatingPoint * 2^16. In this case 6553 = 0.1 * 2^16 -#ifndef VARIANT_NUNCHUCK +#ifndef VARIANT_ NUNCHUK #define FILTER 6553 // 0.1f [-] lower value == softer filter [0, 65535] = [0.0 - 1.0]. #endif // ################################# DEFAULT SETTINGS ############################ -#if !defined(VARIANT_HOVERCAR) && !defined(VARIANT_TRANSPOTTER) && !defined(VARIANT_NUNCHUCK) +#if !defined(VARIANT_HOVERCAR) && !defined(VARIANT_TRANSPOTTER) && !defined(VARIANT_ NUNCHUK) // Value of COEFFICIENT is in fixdt(1,16,14) // If VAL_floatingPoint >= 0, VAL_fixedPoint = VAL_floatingPoint * 2^14 // If VAL_floatingPoint < 0, VAL_fixedPoint = 2^16 + floor(VAL_floatingPoint * 2^14). @@ -301,7 +301,7 @@ #ifdef VARIANT_TRANSPOTTER #define CONTROL_GAMETRAK #define SUPPORT_LCD - #define SUPPORT_NUNCHUCK + #define SUPPORT_ NUNCHUK #define GAMETRAK_CONNECTION_NORMAL // for normal wiring according to the wiki instructions //#define GAMETRAK_CONNECTION_ALTERNATE // use this define instead if you messed up the gametrak ADC wiring (steering is speed, and length of the wire is steering) @@ -311,13 +311,13 @@ #define INVERT_R_DIRECTION // Invert right motor #define INVERT_L_DIRECTION // Invert left motor - // during nunchuck control (only relevant when activated) + // during nunchuk control (only relevant when activated) #define SPEED_COEFFICIENT 14746 // 0.9f - higher value == stronger. 0.0 to ~2.0? #define STEER_COEFFICIENT 8192 // 0.5f - higher value == stronger. if you do not want any steering, set it to 0.0; 0.0 to 1.0 #endif -// ################################# VARIANT_NUNCHUCK SETTINGS ############################ -#ifdef VARIANT_NUNCHUCK +// ################################# VARIANT_ NUNCHUK SETTINGS ############################ +#ifdef VARIANT_ NUNCHUK // # ARMCHAIR # #define FILTER 3276 // 0.05f #define SPEED_COEFFICIENT 8192 // 0.5f @@ -333,7 +333,7 @@ // ############################### VALIDATE SETTINGS ############################### -#if !defined(VARIANT_ADC) && !defined(VARIANT_USART3) && !defined(VARIANT_HOVERCAR) && !defined(VARIANT_TRANSPOTTER) && !defined(VARIANT_NUNCHUCK) && !defined(VARIANT_PPM)&& !defined(VARIANT_IBUS) +#if !defined(VARIANT_ADC) && !defined(VARIANT_USART3) && !defined(VARIANT_HOVERCAR) && !defined(VARIANT_TRANSPOTTER) && !defined(VARIANT_ NUNCHUK) && !defined(VARIANT_PPM)&& !defined(VARIANT_IBUS) #error Variant not defined! Please check platformio.ini or Inc/config.h for available variants. #endif @@ -365,16 +365,16 @@ #error CONTROL_PPM and SERIAL_USART2 not allowed. It is on the same cable. #endif -#if (defined(DEBUG_SERIAL_USART3) || defined(CONTROL_SERIAL_USART3)) && defined(CONTROL_NUNCHUCK) - #error CONTROL_NUNCHUCK and SERIAL_USART3 not allowed. It is on the same cable. +#if (defined(DEBUG_SERIAL_USART3) || defined(CONTROL_SERIAL_USART3)) && defined(CONTROL_ NUNCHUK) + #error CONTROL_ NUNCHUK and SERIAL_USART3 not allowed. It is on the same cable. #endif #if (defined(DEBUG_SERIAL_USART3) || defined(CONTROL_SERIAL_USART3)) && defined(DEBUG_I2C_LCD) #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_NUNCHUCK) || defined(CONTROL_PPM) && defined(CONTROL_ADC) || defined(CONTROL_ADC) && defined(CONTROL_NUNCHUCK) || defined(CONTROL_PPM) && defined(CONTROL_NUNCHUCK) - #error only 1 input method allowed. use CONTROL_PPM or CONTROL_ADC or CONTROL_NUNCHUCK. +#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. #endif #if defined(ADC_PROTECT_ENA) && ((ADC1_MIN - ADC_PROTECT_THRESH) <= 0 || (ADC1_MAX + ADC_PROTECT_THRESH) >= 4096) diff --git a/Inc/defines.h b/Inc/defines.h index ec5f6bc..bdf0387 100644 --- a/Inc/defines.h +++ b/Inc/defines.h @@ -174,10 +174,10 @@ void mixerFcn(int16_t rtu_speed, int16_t rtu_steer, int16_t *rty_speedR, int16_t void rateLimiter16(int16_t u, int16_t rate, int16_t *y); void multipleTapDet(int16_t u, uint32_t timeNow, MultipleTap *x); -// Define I2C, Nunchuck, PPM functions +// Define I2C, Nunchuk, PPM functions void I2C_Init(void); -void Nunchuck_Init(void); -void Nunchuck_Read(void); -uint8_t Nunchuck_Ping(void); +void Nunchuk_Init(void); +void Nunchuk_Read(void); +uint8_t Nunchuk_Ping(void); void PPM_Init(void); void PPM_ISR_Callback(void); \ No newline at end of file diff --git a/README.md b/README.md index 7a8fd17..3941477 100644 --- a/README.md +++ b/README.md @@ -142,11 +142,11 @@ If the board draws more than 100mA in idle, it's probably broken. If the motors do something, but don't rotate smooth and quietly, try to use an alternative phase mapping. Usually, color-correct mapping (blue to blue, green to green, yellow to yellow) works fine. However, some hoverboards have a different layout then others, and this might be the reason your motor isn't spinning. -Nunchuck not working: Use the right one of the 2 types of nunchucks. Use i2c pullups. +Nunchuk not working: Use the right one of the 2 types of nunchuks. Use i2c pullups. -Nunchuck or PPM working bad: The i2c bus and PPM signal are very sensitive to emv distortions of the motor controller. They get stronger the faster you are. Keep cables short, use shielded cable, use ferrits, stabilize voltage in nunchuck or reviever, add i2c pullups. To many errors leads to very high accelerations which triggers the protection board within the battery to shut everything down. +Nunchuk or PPM working bad: The i2c bus and PPM signal are very sensitive to emv distortions of the motor controller. They get stronger the faster you are. Keep cables short, use shielded cable, use ferrits, stabilize voltage in nunchuk or reviever, add i2c pullups. To many errors leads to very high accelerations which triggers the protection board within the battery to shut everything down. -Recommendation: Nunchuck Breakout Board https://github.com/Jan--Henrik/hoverboard-breakout +Recommendation: Nunchuk Breakout Board https://github.com/Jan--Henrik/hoverboard-breakout Most robust way for input is to use the ADC and potis. It works well even on 1m unshielded cable. Solder ~100k Ohm resistors between ADC-inputs and gnd directly on the mainboard. Use potis as pullups to 3.3V. @@ -156,7 +156,7 @@ Most robust way for input is to use the ADC and potis. It works well even on 1m This firmware offers currently these variants (selectable in [platformio.ini](/platformio.ini) and / or [/Inc/config.h](/Inc/config.h)): - **VARIANT_ADC**: In this variant the motors are controlled by two potentiometers connected to the Left sensor cable (long wired) - **VARIANT_USART3**: In this variant the motors are controlled via serial protocol on USART3 right sensor cable (short wired). The commands can be sent from an Arduino. Check out the [hoverserial.ino](/02_Arduino/hoverserial) as an example sketch. -- **VARIANT_NUNCHUCK**: Wii Nunchuck 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_ 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_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. diff --git a/Src/control.c b/Src/control.c index b2f3e66..1c52dcd 100644 --- a/Src/control.c +++ b/Src/control.c @@ -9,7 +9,7 @@ TIM_HandleTypeDef TimHandle; uint8_t ppm_count = 0; uint32_t timeout = 100; -uint8_t nunchuck_data[6] = {0}; +uint8_t nunchuk_data[6] = {0}; uint8_t i2cBuffer[2]; @@ -84,15 +84,15 @@ void PPM_Init(void) { } #endif -uint8_t Nunchuck_Ping(void) { - if (HAL_I2C_Master_Receive(&hi2c2,0xA4,(uint8_t*)nunchuck_data, 1, 10) == HAL_OK) { +uint8_t Nunchuk_Ping(void) { + if (HAL_I2C_Master_Receive(&hi2c2,0xA4,(uint8_t*)nunchuk_data, 1, 10) == HAL_OK) { return 1; } return 0; } -void Nunchuck_Init(void) { - //-- START -- init WiiNunchuck +void Nunchuk_Init(void) { + //-- START -- init WiiNunchuk i2cBuffer[0] = 0xF0; i2cBuffer[1] = 0x55; @@ -106,23 +106,23 @@ void Nunchuck_Init(void) { HAL_Delay(10); } -void Nunchuck_Read(void) { +void Nunchuk_Read(void) { i2cBuffer[0] = 0x00; HAL_I2C_Master_Transmit(&hi2c2,0xA4,(uint8_t*)i2cBuffer, 1, 10); HAL_Delay(3); - if (HAL_I2C_Master_Receive(&hi2c2,0xA4,(uint8_t*)nunchuck_data, 6, 10) == HAL_OK) { + if (HAL_I2C_Master_Receive(&hi2c2,0xA4,(uint8_t*)nunchuk_data, 6, 10) == HAL_OK) { timeout = 0; } #ifndef TRANSPOTTER if (timeout > 3) { HAL_Delay(50); - Nunchuck_Init(); + Nunchuk_Init(); } #endif - //setScopeChannel(0, (int)nunchuck_data[0]); - //setScopeChannel(1, (int)nunchuck_data[1]); - //setScopeChannel(2, (int)nunchuck_data[5] & 1); - //setScopeChannel(3, ((int)nunchuck_data[5] >> 1) & 1); + //setScopeChannel(0, (int)nunchuk_data[0]); + //setScopeChannel(1, (int)nunchuk_data[1]); + //setScopeChannel(2, (int)nunchuk_data[5] & 1); + //setScopeChannel(3, ((int)nunchuk_data[5] >> 1) & 1); } diff --git a/Src/main.c b/Src/main.c index 873ba5e..27dda00 100644 --- a/Src/main.c +++ b/Src/main.c @@ -83,7 +83,7 @@ extern I2C_HandleTypeDef hi2c2; #endif #ifdef VARIANT_TRANSPOTTER - uint8_t nunchuck_connected = 0; + uint8_t nunchuk_connected = 0; float steering; int feedforward; @@ -97,7 +97,7 @@ extern I2C_HandleTypeDef hi2c2; uint16_t counter = 0; #else - uint8_t nunchuck_connected = 1; + uint8_t nunchuk_connected = 1; #endif #if defined(CONTROL_ADC) && defined(ADC_PROTECT_ENA) @@ -146,7 +146,7 @@ typedef struct{ static SerialFeedback Feedback; #endif -#if defined(CONTROL_NUNCHUCK) || defined(SUPPORT_NUNCHUCK) || defined(CONTROL_PPM) || defined(CONTROL_ADC) +#if defined(CONTROL_ NUNCHUK) || defined(SUPPORT_ NUNCHUK) || defined(CONTROL_PPM) || defined(CONTROL_ADC) static uint8_t button1, button2; #endif @@ -182,7 +182,7 @@ extern int16_t batVoltage; // global variable for battery voltage static uint32_t inactivity_timeout_counter; static uint32_t main_loop_counter; -extern uint8_t nunchuck_data[6]; +extern uint8_t nunchuk_data[6]; #ifdef CONTROL_PPM extern volatile uint16_t ppm_captured_value[PPM_NUM_CHANNELS+1]; #endif @@ -304,9 +304,9 @@ int main(void) { PPM_Init(); #endif - #ifdef CONTROL_NUNCHUCK + #ifdef CONTROL_ NUNCHUK I2C_Init(); - Nunchuck_Init(); + Nunchuk_Init(); #endif #if defined(CONTROL_SERIAL_USART2) || defined(FEEDBACK_SERIAL_USART2) || defined(DEBUG_SERIAL_USART2) @@ -414,7 +414,7 @@ int main(void) { feedforward = ((distance - (int)(setDistance * 1345))); - if (nunchuck_connected == 0) { + if (nunchuk_connected == 0) { speedL = speedL * 0.8f + (CLAMP(feedforward + ((steering)*((float)MAX(ABS(feedforward), 50)) * ROT_P), -850, 850) * -0.2f); speedR = speedR * 0.8f + (CLAMP(feedforward - ((steering)*((float)MAX(ABS(feedforward), 50)) * ROT_P), -850, 850) * -0.2f); if ((speedL < lastSpeedL + 50 && speedL > lastSpeedL - 50) && (speedR < lastSpeedR + 50 && speedR > lastSpeedR - 50)) { @@ -453,14 +453,14 @@ int main(void) { } #endif - #if defined(CONTROL_NUNCHUCK) || defined(SUPPORT_NUNCHUCK) - if (nunchuck_connected != 0) { - Nunchuck_Read(); - cmd1 = CLAMP((nunchuck_data[0] - 127) * 8, INPUT_MIN, INPUT_MAX); // x - axis. Nunchuck joystick readings range 30 - 230 - cmd2 = CLAMP((nunchuck_data[1] - 128) * 8, INPUT_MIN, INPUT_MAX); // y - axis + #if defined(CONTROL_ NUNCHUK) || defined(SUPPORT_ NUNCHUK) + if (nunchuk_connected != 0) { + Nunchuk_Read(); + cmd1 = CLAMP((nunchuk_data[0] - 127) * 8, INPUT_MIN, INPUT_MAX); // x - axis. Nunchuk joystick readings range 30 - 230 + cmd2 = CLAMP((nunchuk_data[1] - 128) * 8, INPUT_MIN, INPUT_MAX); // y - axis - button1 = (uint8_t)nunchuck_data[5] & 1; - button2 = (uint8_t)(nunchuck_data[5] >> 1) & 1; + button1 = (uint8_t)nunchuk_data[5] & 1; + button2 = (uint8_t)(nunchuk_data[5] >> 1) & 1; } #endif @@ -697,7 +697,7 @@ int main(void) { HAL_Delay(1000); - nunchuck_connected = 0; + nunchuk_connected = 0; } if ((distance / 1345.0) - setDistance > 0.5 && (lastDistance / 1345.0) - setDistance > 0.5) { // Error, robot too far away! @@ -714,19 +714,19 @@ int main(void) { poweroff(); } - #ifdef SUPPORT_NUNCHUCK + #ifdef SUPPORT_ NUNCHUK if (counter % 500 == 0) { - if (nunchuck_connected == 0 && enable == 0) { - if (Nunchuck_Ping()) { + if (nunchuk_connected == 0 && enable == 0) { + if (Nunchuk_Ping()) { HAL_Delay(500); - Nunchuck_Init(); + Nunchuk_Init(); #ifdef SUPPORT_LCD LCD_SetLocation(&lcd, 0, 0); - LCD_WriteString(&lcd, "Nunchuck Control"); + LCD_WriteString(&lcd, "Nunchuk Control"); #endif timeout = 0; HAL_Delay(1000); - nunchuck_connected = 1; + nunchuk_connected = 1; } } } @@ -737,7 +737,7 @@ int main(void) { if (LCDerrorFlag == 1 && enable == 0) { } else { - if (nunchuck_connected == 0) { + if (nunchuk_connected == 0) { LCD_SetLocation(&lcd, 4, 0); LCD_WriteFloat(&lcd,distance/1345.0,2); LCD_SetLocation(&lcd, 10, 0); diff --git a/Src/stm32f1xx_it.c b/Src/stm32f1xx_it.c index 9de1ee7..25dd6da 100644 --- a/Src/stm32f1xx_it.c +++ b/Src/stm32f1xx_it.c @@ -178,7 +178,7 @@ void SysTick_Handler(void) { /* USER CODE END SysTick_IRQn 1 */ } -#ifdef CONTROL_NUNCHUCK +#ifdef CONTROL_ NUNCHUK extern I2C_HandleTypeDef hi2c2; void I2C1_EV_IRQHandler(void) { diff --git a/platformio.ini b/platformio.ini index 1042175..8f45e2b 100644 --- a/platformio.ini +++ b/platformio.ini @@ -11,7 +11,7 @@ src_dir = Src ; ;default_envs = VARIANT_ADC ; Variant for control via ADC input ;default_envs = VARIANT_USART3 ; Variant for Serial control via USART3 input -;default_envs = VARIANT_NUNCHUCK ; Variant for Nunchuck controlled vehicle build +;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_IBUS ; Variant for RC-Remotes with FLYSKY IBUS ;default_envs = VARIANT_HOVERCAR ; Variant for HOVERCAR build @@ -70,7 +70,7 @@ build_flags = ;================================================================ -[env:VARIANT_NUNCHUCK] +[env:VARIANT_ NUNCHUK] platform = ststm32 framework = stm32cube board = genericSTM32F103RC @@ -86,7 +86,7 @@ build_flags = -Wl,-lm -g -ggdb ; to generate correctly the 'firmware.elf' for STM STUDIO vizualization # -Wl,-lnosys - -D VARIANT_NUNCHUCK + -D VARIANT_ NUNCHUK -D PLATFORMIO ;================================================================ From 04fc584f0a90b622c316c3169704bf6b29d36fdc Mon Sep 17 00:00:00 2001 From: kai Date: Sat, 18 Jan 2020 01:08:47 +0100 Subject: [PATCH 2/9] renamed USART3 to USART because you can also use USART2 not only USART3 --- Inc/config.h | 6 +++--- README.md | 2 +- platformio.ini | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Inc/config.h b/Inc/config.h index 05df47c..0f738db 100644 --- a/Inc/config.h +++ b/Inc/config.h @@ -7,7 +7,7 @@ #if !defined(PLATFORMIO) //#define VARIANT_ADC // Variant for control via ADC input - //#define VARIANT_USART3 // Variant for Serial control via USART3 input + //#define VARIANT_USART // Variant for Serial control via USART3 input //#define VARIANT_ NUNCHUK // Variant for Nunchuk controlled vehicle build //#define VARIANT_PPM // Variant for RC-Remote with PPM-Sum Signal //#define VARIANT_IBUS // Variant for RC-Remotes with FLYSKY IBUS @@ -124,7 +124,7 @@ // #define CONTROL_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuk or lcd) is used! For Arduino control check the hoverSerial.ino // #define FEEDBACK_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuk or lcd) is used! #define DEBUG_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuk or lcd) is used! -#elif defined(VARIANT_USART3) +#elif defined(VARIANT_USART) // #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! // #define DEBUG_SERIAL_USART2 // left sensor board cable, disable if ADC or PPM is used! @@ -333,7 +333,7 @@ // ############################### VALIDATE SETTINGS ############################### -#if !defined(VARIANT_ADC) && !defined(VARIANT_USART3) && !defined(VARIANT_HOVERCAR) && !defined(VARIANT_TRANSPOTTER) && !defined(VARIANT_ NUNCHUK) && !defined(VARIANT_PPM)&& !defined(VARIANT_IBUS) +#if !defined(VARIANT_ADC) && !defined(VARIANT_USART) && !defined(VARIANT_HOVERCAR) && !defined(VARIANT_TRANSPOTTER) && !defined(VARIANT_ NUNCHUK) && !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/README.md b/README.md index 3941477..6c2eaa0 100644 --- a/README.md +++ b/README.md @@ -155,7 +155,7 @@ Most robust way for input is to use the ADC and potis. It works well even on 1m This firmware offers currently these variants (selectable in [platformio.ini](/platformio.ini) and / or [/Inc/config.h](/Inc/config.h)): - **VARIANT_ADC**: In this variant the motors are controlled by two potentiometers connected to the Left sensor cable (long wired) -- **VARIANT_USART3**: In this variant the motors are controlled via serial protocol on USART3 right sensor cable (short wired). The commands can be sent from an Arduino. Check out the [hoverserial.ino](/02_Arduino/hoverserial) as an example sketch. +- **VARIANT_USART**: In this variant the motors are controlled via serial protocol on USART3 right sensor cable (short wired). 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. diff --git a/platformio.ini b/platformio.ini index 8f45e2b..ce6f229 100644 --- a/platformio.ini +++ b/platformio.ini @@ -10,7 +10,7 @@ src_dir = Src ; Choose one or all variants get built ; ;default_envs = VARIANT_ADC ; Variant for control via ADC input -;default_envs = VARIANT_USART3 ; Variant for Serial control via USART3 input +;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_IBUS ; Variant for RC-Remotes with FLYSKY IBUS @@ -45,7 +45,7 @@ build_flags = ;================================================================ -[env:VARIANT_USART3] +[env:VARIANT_USART] platform = ststm32 framework = stm32cube board = genericSTM32F103RC @@ -65,7 +65,7 @@ build_flags = -Wl,-lm -g -ggdb ; to generate correctly the 'firmware.elf' for STM STUDIO vizualization # -Wl,-lnosys - -D VARIANT_USART3 + -D VARIANT_USART -D PLATFORMIO ;================================================================ From e1aa42bad09f1dff6c00d786adc41037b14e1db3 Mon Sep 17 00:00:00 2001 From: kai Date: Sat, 18 Jan 2020 01:15:45 +0100 Subject: [PATCH 3/9] space removal --- Inc/config.h | 26 +++++++++++++------------- README.md | 2 +- Src/main.c | 8 ++++---- Src/stm32f1xx_it.c | 2 +- platformio.ini | 6 +++--- 5 files changed, 22 insertions(+), 22 deletions(-) diff --git a/Inc/config.h b/Inc/config.h index 0f738db..dd2c84e 100644 --- a/Inc/config.h +++ b/Inc/config.h @@ -8,7 +8,7 @@ #if !defined(PLATFORMIO) //#define VARIANT_ADC // Variant for control via ADC input //#define VARIANT_USART // Variant for Serial control via USART3 input - //#define VARIANT_ NUNCHUK // Variant for Nunchuk controlled vehicle build + //#define VARIANT_NUNCHUK // Variant for Nunchuk controlled vehicle build //#define VARIANT_PPM // Variant for RC-Remote with PPM-Sum Signal //#define VARIANT_IBUS // Variant for RC-Remotes with FLYSKY IBUS //#define VARIANT_HOVERCAR // Variant for HOVERCAR build @@ -197,7 +197,7 @@ #define ADC2_MAX 2200 // max ADC2-value while poti at maximum-position (0 - 4095) #endif -#ifdef VARIANT_ NUNCHUK +#ifdef VARIANT_NUNCHUK // ###### CONTROL VIA NINTENDO NUNCHUK ###### /* left sensor board cable. * keep cable short, use shielded cable, use ferrits, stabalize voltage in nunchuk, @@ -205,7 +205,7 @@ * use original nunchuk. most clones does not work very well. * Recommendation: Nunchuk Breakout Board https://github.com/Jan--Henrik/hoverboard-breakout */ - #define CONTROL_ NUNCHUK // use nunchuk as input. disable FEEDBACK_SERIAL_USART3, DEBUG_SERIAL_USART3! + #define CONTROL_NUNCHUK // use nunchuk as input. disable FEEDBACK_SERIAL_USART3, DEBUG_SERIAL_USART3! #endif // ############################### MOTOR CONTROL ######################### @@ -272,12 +272,12 @@ #define RATE 480 // 30.0f [-] lower value == slower rate [0, 32767] = [0.0, 2047.9375]. Do NOT make rate negative (>32767) // Value of FILTER is in fixdt(0,16,16): VAL_fixedPoint = VAL_floatingPoint * 2^16. In this case 6553 = 0.1 * 2^16 -#ifndef VARIANT_ NUNCHUK +#ifndef VARIANT_NUNCHUK #define FILTER 6553 // 0.1f [-] lower value == softer filter [0, 65535] = [0.0 - 1.0]. #endif // ################################# DEFAULT SETTINGS ############################ -#if !defined(VARIANT_HOVERCAR) && !defined(VARIANT_TRANSPOTTER) && !defined(VARIANT_ NUNCHUK) +#if !defined(VARIANT_HOVERCAR) && !defined(VARIANT_TRANSPOTTER) && !defined(VARIANT_NUNCHUK) // Value of COEFFICIENT is in fixdt(1,16,14) // If VAL_floatingPoint >= 0, VAL_fixedPoint = VAL_floatingPoint * 2^14 // If VAL_floatingPoint < 0, VAL_fixedPoint = 2^16 + floor(VAL_floatingPoint * 2^14). @@ -301,7 +301,7 @@ #ifdef VARIANT_TRANSPOTTER #define CONTROL_GAMETRAK #define SUPPORT_LCD - #define SUPPORT_ NUNCHUK + #define SUPPORT_NUNCHUK #define GAMETRAK_CONNECTION_NORMAL // for normal wiring according to the wiki instructions //#define GAMETRAK_CONNECTION_ALTERNATE // use this define instead if you messed up the gametrak ADC wiring (steering is speed, and length of the wire is steering) @@ -316,8 +316,8 @@ #define STEER_COEFFICIENT 8192 // 0.5f - higher value == stronger. if you do not want any steering, set it to 0.0; 0.0 to 1.0 #endif -// ################################# VARIANT_ NUNCHUK SETTINGS ############################ -#ifdef VARIANT_ NUNCHUK +// ################################# VARIANT_NUNCHUK SETTINGS ############################ +#ifdef VARIANT_NUNCHUK // # ARMCHAIR # #define FILTER 3276 // 0.05f #define SPEED_COEFFICIENT 8192 // 0.5f @@ -333,7 +333,7 @@ // ############################### VALIDATE SETTINGS ############################### -#if !defined(VARIANT_ADC) && !defined(VARIANT_USART) && !defined(VARIANT_HOVERCAR) && !defined(VARIANT_TRANSPOTTER) && !defined(VARIANT_ NUNCHUK) && !defined(VARIANT_PPM)&& !defined(VARIANT_IBUS) +#if !defined(VARIANT_ADC) && !defined(VARIANT_USART) && !defined(VARIANT_HOVERCAR) && !defined(VARIANT_TRANSPOTTER) && !defined(VARIANT_NUNCHUK) && !defined(VARIANT_PPM)&& !defined(VARIANT_IBUS) #error Variant not defined! Please check platformio.ini or Inc/config.h for available variants. #endif @@ -365,16 +365,16 @@ #error CONTROL_PPM and SERIAL_USART2 not allowed. It is on the same cable. #endif -#if (defined(DEBUG_SERIAL_USART3) || defined(CONTROL_SERIAL_USART3)) && defined(CONTROL_ NUNCHUK) - #error CONTROL_ NUNCHUK and SERIAL_USART3 not allowed. It is on the same cable. +#if (defined(DEBUG_SERIAL_USART3) || defined(CONTROL_SERIAL_USART3)) && defined(CONTROL_NUNCHUK) + #error CONTROL_NUNCHUK and SERIAL_USART3 not allowed. It is on the same cable. #endif #if (defined(DEBUG_SERIAL_USART3) || defined(CONTROL_SERIAL_USART3)) && defined(DEBUG_I2C_LCD) #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_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. #endif #if defined(ADC_PROTECT_ENA) && ((ADC1_MIN - ADC_PROTECT_THRESH) <= 0 || (ADC1_MAX + ADC_PROTECT_THRESH) >= 4096) diff --git a/README.md b/README.md index 6c2eaa0..8debc84 100644 --- a/README.md +++ b/README.md @@ -156,7 +156,7 @@ Most robust way for input is to use the ADC and potis. It works well even on 1m This firmware offers currently these variants (selectable in [platformio.ini](/platformio.ini) and / or [/Inc/config.h](/Inc/config.h)): - **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 on USART3 right sensor cable (short wired). 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_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_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. diff --git a/Src/main.c b/Src/main.c index 27dda00..de6370e 100644 --- a/Src/main.c +++ b/Src/main.c @@ -146,7 +146,7 @@ typedef struct{ static SerialFeedback Feedback; #endif -#if defined(CONTROL_ NUNCHUK) || defined(SUPPORT_ NUNCHUK) || defined(CONTROL_PPM) || defined(CONTROL_ADC) +#if defined(CONTROL_NUNCHUK) || defined(SUPPORT_NUNCHUK) || defined(CONTROL_PPM) || defined(CONTROL_ADC) static uint8_t button1, button2; #endif @@ -304,7 +304,7 @@ int main(void) { PPM_Init(); #endif - #ifdef CONTROL_ NUNCHUK + #ifdef CONTROL_NUNCHUK I2C_Init(); Nunchuk_Init(); #endif @@ -453,7 +453,7 @@ int main(void) { } #endif - #if defined(CONTROL_ NUNCHUK) || defined(SUPPORT_ NUNCHUK) + #if defined(CONTROL_NUNCHUK) || defined(SUPPORT_NUNCHUK) if (nunchuk_connected != 0) { Nunchuk_Read(); cmd1 = CLAMP((nunchuk_data[0] - 127) * 8, INPUT_MIN, INPUT_MAX); // x - axis. Nunchuk joystick readings range 30 - 230 @@ -714,7 +714,7 @@ int main(void) { poweroff(); } - #ifdef SUPPORT_ NUNCHUK + #ifdef SUPPORT_NUNCHUK if (counter % 500 == 0) { if (nunchuk_connected == 0 && enable == 0) { if (Nunchuk_Ping()) { diff --git a/Src/stm32f1xx_it.c b/Src/stm32f1xx_it.c index 25dd6da..40fb78d 100644 --- a/Src/stm32f1xx_it.c +++ b/Src/stm32f1xx_it.c @@ -178,7 +178,7 @@ void SysTick_Handler(void) { /* USER CODE END SysTick_IRQn 1 */ } -#ifdef CONTROL_ NUNCHUK +#ifdef CONTROL_NUNCHUK extern I2C_HandleTypeDef hi2c2; void I2C1_EV_IRQHandler(void) { diff --git a/platformio.ini b/platformio.ini index ce6f229..a28c724 100644 --- a/platformio.ini +++ b/platformio.ini @@ -11,7 +11,7 @@ src_dir = Src ; ;default_envs = VARIANT_ADC ; Variant for control via ADC input ;default_envs = VARIANT_USART ; Variant for Serial control via USART3 input -;default_envs = VARIANT_ NUNCHUK ; Variant for Nunchuk controlled vehicle build +;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_IBUS ; Variant for RC-Remotes with FLYSKY IBUS ;default_envs = VARIANT_HOVERCAR ; Variant for HOVERCAR build @@ -70,7 +70,7 @@ build_flags = ;================================================================ -[env:VARIANT_ NUNCHUK] +[env:VARIANT_NUNCHUK] platform = ststm32 framework = stm32cube board = genericSTM32F103RC @@ -86,7 +86,7 @@ build_flags = -Wl,-lm -g -ggdb ; to generate correctly the 'firmware.elf' for STM STUDIO vizualization # -Wl,-lnosys - -D VARIANT_ NUNCHUK + -D VARIANT_NUNCHUK -D PLATFORMIO ;================================================================ From 1ddcee2a5d47de7aba11b9fd97947f606f0b4d52 Mon Sep 17 00:00:00 2001 From: kai Date: Sat, 18 Jan 2020 02:00:48 +0100 Subject: [PATCH 4/9] prettified config.h --- Inc/config.h | 583 +++++++++++++++++++++++++++------------------------ 1 file changed, 312 insertions(+), 271 deletions(-) diff --git a/Inc/config.h b/Inc/config.h index dd2c84e..317755e 100644 --- a/Inc/config.h +++ b/Inc/config.h @@ -1,10 +1,11 @@ #pragma once #include "stm32f1xx_hal.h" -// ############################### DEFINE FIRMWARE VARIANT ############################### -// For variant selection, check platformio.ini -// or define the desired build variant here if you want to use make - +// ############################### GENERAL SETTINGS ############################### +// For variant selection, check platformio.ini +// or define the desired build variant here if you want to use make in console +// or use VARIANT environment variable for example like "make -e VARIANT=VARIANT_NUNCHUK" +// Only one at a time, choose wisely ;-) #if !defined(PLATFORMIO) //#define VARIANT_ADC // Variant for control via ADC input //#define VARIANT_USART // Variant for Serial control via USART3 input @@ -15,11 +16,287 @@ //#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 #endif -// For any particular needs, feel free to change this file according to your needs. +#define INACTIVITY_TIMEOUT 8 // Minutes of not driving until poweroff. it is not very precise. +// ########################### END OF GENERAL SETTINGS ############################ + + + +// ############################### BATTERY ############################### +/* Battery voltage calibration: connect power source. + * see How to calibrate. + * Write debug output value nr 5 to BAT_CALIB_ADC. make and flash firmware. + * Then you can verify voltage on debug output value 6 (to get calibrated voltage multiplied by 100). +*/ +#define BAT_FILT_COEF 655 // battery voltage filter coefficient in fixed-point. coef_fixedPoint = coef_floatingPoint * 2^16. In this case 655 = 0.01 * 2^16 +#define BAT_CALIB_REAL_VOLTAGE 3970 // input voltage measured by multimeter (multiplied by 100). In this case 43.00 V * 100 = 4300 +#define BAT_CALIB_ADC 1492 // adc-value measured by mainboard (value nr 5 on UART debug output) +#define BAT_CELLS 10 // battery number of cells. Normal Hoverboard battery: 10s +#define BAT_LOW_LVL1 (360 * BAT_CELLS * BAT_CALIB_ADC) / BAT_CALIB_REAL_VOLTAGE // gently beeps at this voltage level. [V*100/cell]. In this case 3.60 V/cell +#define BAT_LOW_LVL2 (350 * BAT_CELLS * BAT_CALIB_ADC) / BAT_CALIB_REAL_VOLTAGE // your battery is almost empty. Charge now! [V*100/cell]. In this case 3.50 V/cell +#define BAT_LOW_DEAD (337 * BAT_CELLS * BAT_CALIB_ADC) / BAT_CALIB_REAL_VOLTAGE // undervoltage poweroff. (while not driving) [V*100/cell]. In this case 3.37 V/cell +// ######################## END OF BATTERY ############################### + + + +// ############################### TEMPERATURE ############################### +/* Board overheat detection: the sensor is inside the STM/GD chip. + * It is very inaccurate without calibration (up to 45°C). So only enable this funcion after calibration! + * Let your board cool down. + * see = 0, VAL_fixedPoint = VAL_floatingPoint * 2^14 + * If VAL_floatingPoint < 0, VAL_fixedPoint = 2^16 + floor(VAL_floatingPoint * 2^14). +*/ +#define DEFAULT_FILTER 6553 // Default for FILTER 0.1f [-] lower value == softer filter [0, 65535] = [0.0 - 1.0]. +#define DEFAULT_SPEED_COEFFICIENT 16384 // Default for SPEED_COEFFICIENT 1.0f [-] higher value == stronger. [0, 65535] = [-2.0 - 2.0]. In this case 16384 = 1.0 * 2^14 +#define DEFAULT_STEER_COEFFICIENT 8192 // Defualt for STEER_COEFFICIENT 0.5f [-] higher value == stronger. [0, 65535] = [-2.0 - 2.0]. In this case 8192 = 0.5 * 2^14. If you do not want any steering, set it to 0. +// ######################### END OF VARIANT DEFAULT SETTINGS ########################## + + + +// ################################# VARIANT_ADC SETTINGS ############################ +#ifdef VARIANT_ADC +/* CONTROL VIA TWO POTENTIOMETERS + * ADC-calibration to cover the full poti-range: + * Connect potis to left sensor board cable (0 to 3.3V) (do NOT use the red 15V wire in the cable!). see . + * Turn the potis to minimum position, write value 1 to ADC1_MIN and value 2 to ADC2_MIN + * Turn the potis to maximum position, write value 1 to ADC1_MAX and value 2 to ADC2_MAX + * For middle resting potis: Let the potis in the middle resting position, write value 1 to ADC1_MID and value 2 to ADC2_MID + * Make, flash and test it. +*/ + #define CONTROL_ADC // use ADC as input. disable CONTROL_SERIAL_USART2, FEEDBACK_SERIAL_USART2, DEBUG_SERIAL_USART2! + // #define ADC_PROTECT_ENA // ADC Protection Enable flag. Use this flag to make sure the ADC is protected when GND or Vcc wire is disconnected + #define ADC_PROTECT_TIMEOUT 30 // ADC Protection: number of wrong / missing input commands before safety state is taken + #define ADC_PROTECT_THRESH 400 // ADC Protection threshold below/above the MIN/MAX ADC values + // #define ADC1_MID_POT // ADC1 middle resting poti: comment-out if NOT a middle resting poti + #define ADC1_MIN 0 // min ADC1-value while poti at minimum-position (0 - 4095) + #define ADC1_MID 2048 // mid ADC1-value while poti at minimum-position (ADC1_MIN - ADC1_MAX) + #define ADC1_MAX 4095 // max ADC1-value while poti at maximum-position (0 - 4095) + // #define ADC2_MID_POT // ADC2 middle resting poti: comment-out if NOT a middle resting poti + #define ADC2_MIN 0 // min ADC2-value while poti at minimum-position (0 - 4095) + #define ADC2_MID 2048 // mid ADC2-value while poti at minimum-position (ADC2_MIN - ADC2_MAX) + #define ADC2_MAX 4095 // max ADC2-value while poti at maximum-position (0 - 4095) +#endif +// ############################# END OF VARIANT_ADC SETTINGS ######################### + + + +// ############################ VARIANT_USART SETTINGS ############################ +#ifdef VARIANT_USART + // #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! + //#define CONTROL_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuk or lcd) is used! For Arduino control check the hoverSerial.ino + //#define FEEDBACK_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuk or lcd) is used! +#endif +// ######################## END OF VARIANT_USART SETTINGS ######################### + + + +// ################################# VARIANT_NUNCHUK SETTINGS ############################ +#ifdef VARIANT_NUNCHUK + /* left sensor board cable. USART3 + * keep cable short, use shielded cable, use ferrits, stabalize voltage in nunchuk, + * use the right one of the 2 types of nunchuks, add i2c pullups. + * use original nunchuk. most clones does not work very well. + * Recommendation: Nunchuk Breakout Board https://github.com/Jan--Henrik/hoverboard-breakout + */ + #define CONTROL_NUNCHUK // use nunchuk as input. disable FEEDBACK_SERIAL_USART3, DEBUG_SERIAL_USART3! + // # maybe good for ARMCHAIR # + #define FILTER 3276 // 0.05f + #define SPEED_COEFFICIENT 8192 // 0.5f + #define STEER_COEFFICIENT 62259 // -0.2f +#endif +// ############################# END OF VARIANT_NUNCHUK SETTINGS ######################### + + + +// ################################# VARIANT_PPM SETTINGS ############################## +#ifdef VARIANT_PPM +/* ###### CONTROL VIA RC REMOTE ###### + * left sensor board cable. Channel 1: steering, Channel 2: speed. + * https://gist.github.com/peterpoetzi/1b63a4a844162196613871767189bd05 +*/ + #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 +// ############################# END OF VARIANT_PPM SETTINGS ############################ + + + +// ################################# VARIANT_IBUS SETTINGS ############################## +#ifdef VARIANT_IBUS +/* CONTROL VIA RC REMOTE WITH FLYSKY IBUS PROTOCOL +* Connected to 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 + + #undef USART2_BAUD + #define USART2_BAUD 115200 + #define CONTROL_SERIAL_USART2 // left sensor board cable, disable if ADC or PPM is used! + #define FEEDBACK_SERIAL_USART2 // left sensor board cable, disable if ADC or PPM is used! +#endif +// ############################# END OF VARIANT_IBUS SETTINGS ############################ + + + +// ############################ VARIANT_HOVERCAR SETTINGS ############################ +#ifdef VARIANT_HOVERCAR + #define CONTROL_ADC // use ADC as input. disable CONTROL_SERIAL_USART2, FEEDBACK_SERIAL_USART2, DEBUG_SERIAL_USART2! + #define ADC_PROTECT_ENA // ADC Protection Enable flag. Use this flag to make sure the ADC is protected when GND or Vcc wire is disconnected + #define ADC_PROTECT_TIMEOUT 30 // ADC Protection: number of wrong / missing input commands before safety state is taken + #define ADC_PROTECT_THRESH 400 // ADC Protection threshold below/above the MIN/MAX ADC values + #define ADC1_MIN 1000 // min ADC1-value while poti at minimum-position (0 - 4095) + #define ADC1_MAX 2500 // max ADC1-value while poti at maximum-position (0 - 4095) + #define ADC2_MIN 500 // min ADC2-value while poti at minimum-position (0 - 4095) + #define ADC2_MAX 2200 // max ADC2-value while poti at maximum-position (0 - 4095) + #define SPEED_COEFFICIENT 16384 // 1.0f + #define STEER_COEFFICIENT 0 // 0.0f + //#define INVERT_R_DIRECTION // Invert rotation of right motor + //#define INVERT_L_DIRECTION // Invert rotation of left motor +#endif +// ######################## END OF VARIANT_HOVERCAR SETTINGS ######################### + + + +// ################################# VARIANT_TRANSPOTTER SETTINGS ############################ +//TODO ADD VALIDATION +#ifdef VARIANT_TRANSPOTTER + #define CONTROL_GAMETRAK + //#define SUPPORT_LCD + //#define SUPPORT_NUNCHUK + #define GAMETRAK_CONNECTION_NORMAL // for normal wiring according to the wiki instructions + //#define GAMETRAK_CONNECTION_ALTERNATE // use this define instead if you messed up the gametrak ADC wiring (steering is speed, and length of the wire is steering) + #define ROT_P 1.2 // P coefficient for the direction controller. Positive / Negative values to invert gametrak steering direction. + // during nunchuk control (only relevant when activated) + #define SPEED_COEFFICIENT 14746 // 0.9f - higher value == stronger. 0.0 to ~2.0? + #define STEER_COEFFICIENT 8192 // 0.5f - higher value == stronger. if you do not want any steering, set it to 0.0; 0.0 to 1.0 +#endif +// ############################# END OF VARIANT_TRANSPOTTER SETTINGS ######################## + + + +// ############################### MOTOR CONTROL ######################### +/* GENERAL NOTES: + * 1. The parameters are over-writing the default motor parameters. For all the available parameters check BLDC_controller_data.c + * 2. The parameters are represented in fixed point data type for a more efficient code execution + * 3. For calibrating the fixed-point parameters use the Fixed-Point Viewer tool (see ) + * 4. For more details regarding the parameters and the working principle of the controller please consult the Simulink model + * 5. A webview was created, so Matlab/Simulink installation is not needed, unless you want to regenerate the code. + * The webview is an html page that can be opened with browsers like: Microsoft Internet Explorer or Microsoft Edge + * + * NOTES Field Weakening / Phase Advance: + * 1. The Field Weakening is a linear interpolation from 0 to FIELD_WEAK_MAX or PHASE_ADV_MAX (depeding if FOC or SIN is selected, respectively) + * 2. The Field Weakening starts engaging at FIELD_WEAK_LO and reaches the maximum value at FIELD_WEAK_HI + * 3. If you re-calibrate the Field Weakening please take all the safety measures! The motors can spin very fast! + + Inputs: + - cmd1 and cmd2: analog normalized input values. INPUT_MIN to INPUT_MAX + - button1 and button2: digital input values. 0 or 1 + - adc_buffer.l_tx2 and adc_buffer.l_rx2: unfiltered ADC values (you do not need them). 0 to 4095 + Outputs: + - speedR and speedL: normal driving INPUT_MIN to INPUT_MAX +*/ +// Control selections +#define CTRL_TYP_SEL 2 // [-] Control type selection: 0 = Commutation , 1 = Sinusoidal, 2 = FOC Field Oriented Control (default) +#define CTRL_MOD_REQ 1 // [-] Control mode request: 0 = Open mode, 1 = VOLTAGE mode (default), 2 = SPEED mode, 3 = TORQUE mode. Note: SPEED and TORQUE modes are only available for FOC! +#define DIAG_ENA 1 // [-] Motor Diagnostics enable flag: 0 = Disabled, 1 = Enabled (default) + +// Limitation settings +#define I_MOT_MAX 15 // [A] Maximum motor current limit +#define I_DC_MAX 17 // [A] Maximum DC Link current limit (This is the final current protection. Above this value, current chopping is applied. To avoid this make sure that I_DC_MAX = I_MOT_MAX + 2A) +#define N_MOT_MAX 1000 // [rpm] Maximum motor speed limit + +// Field Weakening / Phase Advance +#define FIELD_WEAK_ENA 0 // [-] Field Weakening / Phase Advance enable flag: 0 = Disabled (default), 1 = Enabled +#define FIELD_WEAK_MAX 5 // [A] Maximum Field Weakening D axis current (only for FOC). Higher current results in higher maximum speed. +#define PHASE_ADV_MAX 25 // [deg] Maximum Phase Advance angle (only for SIN). Higher angle results in higher maximum speed. +#define FIELD_WEAK_HI 1500 // [-] Input target High threshold for reaching maximum Field Weakening / Phase Advance. Do NOT set this higher than 1500. +#define FIELD_WEAK_LO 1000 // [-] Input target Low threshold for starting Field Weakening / Phase Advance. Do NOT set this higher than 1000. + +// Data checks - Do NOT touch +#if (FIELD_WEAK_ENA == 0) + #undef FIELD_WEAK_HI + #define FIELD_WEAK_HI 1000 // [-] This prevents the input target going beyond 1000 when Field Weakening is not enabled +#endif +#define INPUT_MAX MAX( 1000, FIELD_WEAK_HI) // [-] Defines the Input target maximum limitation +#define INPUT_MIN MIN(-1000,-FIELD_WEAK_HI) // [-] Defines the Input target minimum limitation +#define INPUT_MID INPUT_MAX / 2 + +// Multiple tap detection: default DOUBLE Tap (4 pulses) +#define MULTIPLE_TAP_NR 2 * 2 // [-] Define tap number: MULTIPLE_TAP_NR = number_of_taps * 2, number_of_taps = 1 (for single taping), 2 (for double tapping), 3 (for triple tapping), etc... +#define MULTIPLE_TAP_HI 600 // [-] Multiple tap detection High hysteresis threshold +#define MULTIPLE_TAP_LO 200 // [-] Multiple tap detection Low hysteresis threshold +#define MULTIPLE_TAP_TIMEOUT 2000 // [ms] Multiple tap detection Timeout period. The taps need to happen within this time window to be accepted. + +// Value of RATE is in fixdt(1,16,4): VAL_fixedPoint = VAL_floatingPoint * 2^4. In this case 480 = 30 * 2^4 +#define RATE 480 // 30.0f [-] lower value == slower rate [0, 32767] = [0.0, 2047.9375]. Do NOT make rate negative (>32767) +// ########################### END OF MOTOR CONTROL ######################## + + // ############################### DO-NOT-TOUCH SETTINGS ############################### - -#define PWM_FREQ 16000 // PWM frequency in Hz +#define PWM_FREQ 16000 // PWM frequency in Hz / is also used for buzzer #define DEAD_TIME 48 // PWM deadtime #ifdef VARIANT_TRANSPOTTER #define DELAY_IN_MAIN_LOOP 2 @@ -49,106 +326,26 @@ // ADC Total conversion time: this will be used to offset TIM8 in advance of TIM1 to align the Phase current ADC measurement // This parameter is used in setup.c #define ADC_TOTAL_CONV_TIME (ADC_CLOCK_DIV * ADC_CONV_CLOCK_CYCLES) // = ((SystemCoreClock / ADC_CLOCK_HZ) * ADC_CONV_CLOCK_CYCLES), where ADC_CLOCK_HZ = SystemCoreClock/ADC_CLOCK_DIV - -// ############################### GENERAL ############################### - -/* How to calibrate: connect GND and RX of a 3.3v uart-usb adapter to the right sensor board cable - * Be careful not to use the red wire of the cable. 15v will destroye verything.). - * If you are using nunchuk, disable it temporarily. enable DEBUG_SERIAL_USART3 and DEBUG_SERIAL_ASCII use asearial terminal. - */ - -/* Battery voltage calibration: connect power source. see . - * Write value nr 5 to BAT_CALIB_ADC. make and flash firmware. - * Then you can verify voltage on value 6 (to get calibrated voltage multiplied by 100). - */ -#define BAT_FILT_COEF 655 // battery voltage filter coefficient in fixed-point. coef_fixedPoint = coef_floatingPoint * 2^16. In this case 655 = 0.01 * 2^16 -#define BAT_CALIB_REAL_VOLTAGE 3970 // input voltage measured by multimeter (multiplied by 100). For example 43.00 V * 100 = 4300 -#define BAT_CALIB_ADC 1492 // adc-value measured by mainboard (value nr 5 on UART debug output) - -#define BAT_CELLS 10 // battery number of cells. Normal Hoverboard battery: 10s -#define BAT_LOW_LVL1_ENABLE 0 // to beep or not to beep, 1 or 0 -#define BAT_LOW_LVL2_ENABLE 1 // to beep or not to beep, 1 or 0 -#define BAT_LOW_LVL1 (360 * BAT_CELLS * BAT_CALIB_ADC) / BAT_CALIB_REAL_VOLTAGE // gently beeps at this voltage level. [V*100/cell]. In this case 3.60 V/cell -#define BAT_LOW_LVL2 (350 * BAT_CELLS * BAT_CALIB_ADC) / BAT_CALIB_REAL_VOLTAGE // your battery is almost empty. Charge now! [V*100/cell]. In this case 3.50 V/cell -#define BAT_LOW_DEAD (337 * BAT_CELLS * BAT_CALIB_ADC) / BAT_CALIB_REAL_VOLTAGE // undervoltage poweroff. (while not driving) [V*100/cell]. In this case 3.37 V/cell +// ########################### END OF DO-NOT-TOUCH SETTINGS ############################ -/* Board overheat detection: the sensor is inside the STM/GD chip. - * It is very inaccurate without calibration (up to 45°C). So only enable this funcion after calibration! - * Let your board cool down. see . - * Get the real temp of the chip by thermo cam or another temp-sensor taped on top of the chip and write it to TEMP_CAL_LOW_DEG_C. - * Write debug value 8 to TEMP_CAL_LOW_ADC. drive around to warm up the board. it should be at least 20°C warmer. repeat it for the HIGH-values. - * Enable warning and/or poweroff and make and flash firmware. - */ -#define TEMP_FILT_COEF 655 // temperature filter coefficient in fixed-point. coef_fixedPoint = coef_floatingPoint * 2^16. In this case 655 = 0.01 * 2^16 -#define TEMP_CAL_LOW_ADC 1655 // temperature 1: ADC value -#define TEMP_CAL_LOW_DEG_C 358 // temperature 1: measured temperature [°C * 10]. Here 35.8 °C -#define TEMP_CAL_HIGH_ADC 1588 // temperature 2: ADC value -#define TEMP_CAL_HIGH_DEG_C 489 // temperature 2: measured temperature [°C * 10]. Here 48.9 °C -#define TEMP_WARNING_ENABLE 0 // to beep or not to beep, 1 or 0, DO NOT ACTIVITE WITHOUT CALIBRATION! -#define TEMP_WARNING 600 // annoying fast beeps [°C * 10]. Here 60.0 °C -#define TEMP_POWEROFF_ENABLE 0 // to poweroff or not to poweroff, 1 or 0, DO NOT ACTIVITE WITHOUT CALIBRATION! -#define TEMP_POWEROFF 650 // overheat poweroff. (while not driving) [°C * 10]. Here 65.0 °C -#define INACTIVITY_TIMEOUT 8 // minutes of not driving until poweroff. it is not very precise. - -// ############################### LCD DEBUG ############################### - -//#define DEBUG_I2C_LCD // standard 16x2 or larger text-lcd via i2c-converter on right sensor board cable - - -// ############################### SERIAL DEBUG ############################### - -#ifndef VARIANT_TRANSPOTTER - //#define DEBUG_SERIAL_SERVOTERM - //#define DEBUG_SERIAL_ASCII // "1:345 2:1337 3:0 4:0 5:0 6:0 7:0 8:0\r\n" +// ########################### UART SETIINGS ############################ +#if defined(FEEDBACK_SERIAL_USART2) || defined(CONTROL_SERIAL_USART2) || defined(DEBUG_SERIAL_USART2) || defined(FEEDBACK_SERIAL_USART3) || defined(CONTROL_SERIAL_USART3) || defined(DEBUG_SERIAL_USART3) + #define START_FRAME 0xAAAA // [-] Start frame definition for serial commands + #define SERIAL_TIMEOUT 160 // [-] Serial timeout duration for the received data. 160 ~= 0.8 sec. Calculation: 0.8 sec / 0.005 sec #endif - - -// ############################### INPUT ############################### - -// ###### CONTROL VIA UART (serial) ###### -#define START_FRAME 0xAAAA // [-] Start frame definition for serial commands -#define SERIAL_TIMEOUT 160 // [-] Serial timeout duration for the received data. 160 ~= 0.8 sec. Calculation: 0.8 sec / 0.005 sec - -#define USART2_BAUD 38400 // UART2 baud rate (long wired cable) -#define USART2_WORDLENGTH UART_WORDLENGTH_8B // UART_WORDLENGTH_8B or UART_WORDLENGTH_9B -#define USART3_BAUD 38400 // UART3 baud rate (short wired cable) -#define USART3_WORDLENGTH UART_WORDLENGTH_8B // UART_WORDLENGTH_8B or UART_WORDLENGTH_9B - -#if defined(VARIANT_ADC) || defined(VARIANT_HOVERCAR) - // #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! - // #define DEBUG_SERIAL_USART2 // left sensor board cable, disable if ADC or PPM is used! - - // #define CONTROL_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuk or lcd) is used! For Arduino control check the hoverSerial.ino - // #define FEEDBACK_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuk or lcd) is used! - #define DEBUG_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuk or lcd) is used! -#elif defined(VARIANT_USART) - // #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! - // #define DEBUG_SERIAL_USART2 // left sensor board cable, disable if ADC or PPM is used! - - #define CONTROL_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuk or lcd) is used! For Arduino control check the hoverSerial.ino - #define FEEDBACK_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuk or lcd) is used! - // #define DEBUG_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuk or lcd) is used! +#if defined(FEEDBACK_SERIAL_USART2) || defined(CONTROL_SERIAL_USART2) || defined(DEBUG_SERIAL_USART2) + #ifndef USART2_BAUD + #define USART2_BAUD 38400 // UART2 baud rate (long wired cable) + #endif + #define USART2_WORDLENGTH UART_WORDLENGTH_8B // UART_WORDLENGTH_8B or UART_WORDLENGTH_9B #endif - -// ###### CONTROL VIA RC REMOTE WITH FLYSKY IBUS PROTOCOL ###### -/* Connected to Left sensor board cable. Channel 1: steering, Channel 2: speed. */ -#ifdef VARIANT_IBUS - #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 - - #undef USART2_BAUD - #define USART2_BAUD 115200 - #define CONTROL_SERIAL_USART2 // left sensor board cable, disable if ADC or PPM is used! - #define FEEDBACK_SERIAL_USART2 // left sensor board cable, disable if ADC or PPM is used! +#if defined(FEEDBACK_SERIAL_USART3) || defined(CONTROL_SERIAL_USART3) || defined(DEBUG_SERIAL_USART3) + #define USART3_BAUD 38400 // UART3 baud rate (short wired cable) + #define USART3_WORDLENGTH UART_WORDLENGTH_8B // UART_WORDLENGTH_8B or UART_WORDLENGTH_9B #endif - #if defined(FEEDBACK_SERIAL_USART2) || defined(DEBUG_SERIAL_USART2) #define UART_DMA_CHANNEL DMA1_Channel7 #endif @@ -156,184 +353,26 @@ #if defined(FEEDBACK_SERIAL_USART3) || defined(DEBUG_SERIAL_USART3) #define UART_DMA_CHANNEL DMA1_Channel2 #endif +// ########################### UART SETIINGS ############################ -// ###### CONTROL VIA RC REMOTE ###### -#ifdef VARIANT_PPM - // 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. + + +// ############################### APPLY DEFAULT SETTINGS ############################### +#ifndef SPEED_COEFFICIENT + #define SPEED_COEFFICIENT DEFAULT_SPEED_COEFFICIENT #endif - -// ###### CONTROL VIA TWO POTENTIOMETERS ###### -/* ADC-calibration to cover the full poti-range: - * Connect potis to left sensor board cable (0 to 3.3V) (do NOT use the red 15V wire in the cable!). see . - * Turn the potis to minimum position, write value 1 to ADC1_MIN and value 2 to ADC2_MIN - * Turn the potis to maximum position, write value 1 to ADC1_MAX and value 2 to ADC2_MAX - * For middle resting potis: Let the potis in the middle resting position, write value 1 to ADC1_MID and value 2 to ADC2_MID - * Make, flash and test it. - */ -#ifdef VARIANT_ADC - #define CONTROL_ADC // use ADC as input. disable CONTROL_SERIAL_USART2, FEEDBACK_SERIAL_USART2, DEBUG_SERIAL_USART2! - // #define ADC_PROTECT_ENA // ADC Protection Enable flag. Use this flag to make sure the ADC is protected when GND or Vcc wire is disconnected - #define ADC_PROTECT_TIMEOUT 30 // ADC Protection: number of wrong / missing input commands before safety state is taken - #define ADC_PROTECT_THRESH 400 // ADC Protection threshold below/above the MIN/MAX ADC values - // #define ADC1_MID_POT // ADC1 middle resting poti: comment-out if NOT a middle resting poti - #define ADC1_MIN 0 // min ADC1-value while poti at minimum-position (0 - 4095) - #define ADC1_MID 2048 // mid ADC1-value while poti at minimum-position (ADC1_MIN - ADC1_MAX) - #define ADC1_MAX 4095 // max ADC1-value while poti at maximum-position (0 - 4095) - // #define ADC2_MID_POT // ADC2 middle resting poti: comment-out if NOT a middle resting poti - #define ADC2_MIN 0 // min ADC2-value while poti at minimum-position (0 - 4095) - #define ADC2_MID 2048 // mid ADC2-value while poti at minimum-position (ADC2_MIN - ADC2_MAX) - #define ADC2_MAX 4095 // max ADC2-value while poti at maximum-position (0 - 4095) +#ifndef STEER_COEFFICIENT + #define STEER_COEFFICIENT DEFAULT_STEER_COEFFICIENT #endif -#ifdef VARIANT_HOVERCAR - #define CONTROL_ADC // use ADC as input. disable CONTROL_SERIAL_USART2, FEEDBACK_SERIAL_USART2, DEBUG_SERIAL_USART2! - #define ADC_PROTECT_ENA // ADC Protection Enable flag. Use this flag to make sure the ADC is protected when GND or Vcc wire is disconnected - #define ADC_PROTECT_TIMEOUT 30 // ADC Protection: number of wrong / missing input commands before safety state is taken - #define ADC_PROTECT_THRESH 400 // ADC Protection threshold below/above the MIN/MAX ADC values - #define ADC1_MIN 1000 // min ADC1-value while poti at minimum-position (0 - 4095) - #define ADC1_MAX 2500 // max ADC1-value while poti at maximum-position (0 - 4095) - #define ADC2_MIN 500 // min ADC2-value while poti at minimum-position (0 - 4095) - #define ADC2_MAX 2200 // max ADC2-value while poti at maximum-position (0 - 4095) +#ifndef FILTER + #define FILTER DEFAULT_FILTER #endif - -#ifdef VARIANT_NUNCHUK - // ###### CONTROL VIA NINTENDO NUNCHUK ###### - /* left sensor board cable. - * keep cable short, use shielded cable, use ferrits, stabalize voltage in nunchuk, - * use the right one of the 2 types of nunchuks, add i2c pullups. - * use original nunchuk. most clones does not work very well. - * Recommendation: Nunchuk Breakout Board https://github.com/Jan--Henrik/hoverboard-breakout - */ - #define CONTROL_NUNCHUK // use nunchuk as input. disable FEEDBACK_SERIAL_USART3, DEBUG_SERIAL_USART3! -#endif - -// ############################### MOTOR CONTROL ######################### -// Control selections -#define CTRL_TYP_SEL 2 // [-] Control type selection: 0 = Commutation , 1 = Sinusoidal, 2 = FOC Field Oriented Control (default) -#define CTRL_MOD_REQ 1 // [-] Control mode request: 0 = Open mode, 1 = VOLTAGE mode (default), 2 = SPEED mode, 3 = TORQUE mode. Note: SPEED and TORQUE modes are only available for FOC! -#define DIAG_ENA 1 // [-] Motor Diagnostics enable flag: 0 = Disabled, 1 = Enabled (default) - -// Limitation settings -#define I_MOT_MAX 15 // [A] Maximum motor current limit -#define I_DC_MAX 17 // [A] Maximum DC Link current limit (This is the final current protection. Above this value, current chopping is applied. To avoid this make sure that I_DC_MAX = I_MOT_MAX + 2A) -#define N_MOT_MAX 1000 // [rpm] Maximum motor speed limit - -// Field Weakening / Phase Advance -#define FIELD_WEAK_ENA 0 // [-] Field Weakening / Phase Advance enable flag: 0 = Disabled (default), 1 = Enabled -#define FIELD_WEAK_MAX 5 // [A] Maximum Field Weakening D axis current (only for FOC). Higher current results in higher maximum speed. -#define PHASE_ADV_MAX 25 // [deg] Maximum Phase Advance angle (only for SIN). Higher angle results in higher maximum speed. -#define FIELD_WEAK_HI 1500 // [-] Input target High threshold for reaching maximum Field Weakening / Phase Advance. Do NOT set this higher than 1500. -#define FIELD_WEAK_LO 1000 // [-] Input target Low threshold for starting Field Weakening / Phase Advance. Do NOT set this higher than 1000. - -// Data checks - Do NOT touch -#if (FIELD_WEAK_ENA == 0) - #undef FIELD_WEAK_HI - #define FIELD_WEAK_HI 1000 // [-] This prevents the input target going beyond 1000 when Field Weakening is not enabled -#endif -#define INPUT_MAX MAX( 1000, FIELD_WEAK_HI) // [-] Defines the Input target maximum limitation -#define INPUT_MIN MIN(-1000,-FIELD_WEAK_HI) // [-] Defines the Input target minimum limitation -#define INPUT_MID INPUT_MAX / 2 - -/* GENERAL NOTES: - * 1. The above parameters are over-writing the default motor parameters. For all the available parameters check BLDC_controller_data.c - * 2. The parameters are represented in fixed point data type for a more efficient code execution - * 3. For calibrating the fixed-point parameters use the Fixed-Point Viewer tool (see ) - * 4. For more details regarding the parameters and the working principle of the controller please consult the Simulink model - * 5. A webview was created, so Matlab/Simulink installation is not needed, unless you want to regenerate the code. The webview is an html page that can be opened with browsers like: Microsoft Internet Explorer or Microsoft Edge - * - * NOTES Field Weakening / Phase Advance: - * 1. The Field Weakening is a linear interpolation from 0 to FIELD_WEAK_MAX or PHASE_ADV_MAX (depeding if FOC or SIN is selected, respectively) - * 2. The Field Weakening starts engaging at FIELD_WEAK_LO and reaches the maximum value at FIELD_WEAK_HI - * 3. If you re-calibrate the Field Weakening please take all the safety measures! The motors can spin very fast! - */ +// ########################### END OF APPLY DEFAULT SETTING ############################ -// ############################### DRIVING BEHAVIOR ############################### - -/* Inputs: - * - cmd1 and cmd2: analog normalized input values. INPUT_MIN to INPUT_MAX - * - button1 and button2: digital input values. 0 or 1 - * - adc_buffer.l_tx2 and adc_buffer.l_rx2: unfiltered ADC values (you do not need them). 0 to 4095 - * Outputs: - * - speedR and speedL: normal driving INPUT_MIN to INPUT_MAX - */ - -// Beep in Reverse -#define BEEPS_BACKWARD 1 // 0 or 1 - -// Multiple tap detection: default DOUBLE Tap (4 pulses) -#define MULTIPLE_TAP_NR 2 * 2 // [-] Define tap number: MULTIPLE_TAP_NR = number_of_taps * 2, number_of_taps = 1 (for single taping), 2 (for double tapping), 3 (for triple tapping), etc... -#define MULTIPLE_TAP_HI 600 // [-] Multiple tap detection High hysteresis threshold -#define MULTIPLE_TAP_LO 200 // [-] Multiple tap detection Low hysteresis threshold -#define MULTIPLE_TAP_TIMEOUT 2000 // [ms] Multiple tap detection Timeout period. The taps need to happen within this time window to be accepted. - -// Value of RATE is in fixdt(1,16,4): VAL_fixedPoint = VAL_floatingPoint * 2^4. In this case 480 = 30 * 2^4 -#define RATE 480 // 30.0f [-] lower value == slower rate [0, 32767] = [0.0, 2047.9375]. Do NOT make rate negative (>32767) - -// Value of FILTER is in fixdt(0,16,16): VAL_fixedPoint = VAL_floatingPoint * 2^16. In this case 6553 = 0.1 * 2^16 -#ifndef VARIANT_NUNCHUK - #define FILTER 6553 // 0.1f [-] lower value == softer filter [0, 65535] = [0.0 - 1.0]. -#endif - -// ################################# DEFAULT SETTINGS ############################ -#if !defined(VARIANT_HOVERCAR) && !defined(VARIANT_TRANSPOTTER) && !defined(VARIANT_NUNCHUK) - // Value of COEFFICIENT is in fixdt(1,16,14) - // If VAL_floatingPoint >= 0, VAL_fixedPoint = VAL_floatingPoint * 2^14 - // If VAL_floatingPoint < 0, VAL_fixedPoint = 2^16 + floor(VAL_floatingPoint * 2^14). - #define SPEED_COEFFICIENT 16384 // 1.0f [-] higher value == stronger. [0, 65535] = [-2.0 - 2.0]. In this case 16384 = 1.0 * 2^14 - #define STEER_COEFFICIENT 8192 // 0.5f [-] higher value == stronger. [0, 65535] = [-2.0 - 2.0]. In this case 8192 = 0.5 * 2^14. If you do not want any steering, set it to 0. - - #define INVERT_R_DIRECTION - #define INVERT_L_DIRECTION -#endif - -// ################################# VARIANT_HOVERCAR SETTINGS ############################ -#ifdef VARIANT_HOVERCAR - #define SPEED_COEFFICIENT 16384 // 1.0f - #define STEER_COEFFICIENT 0 // 0.0f - - // #define INVERT_R_DIRECTION - // #define INVERT_L_DIRECTION -#endif - -// ################################# VARIANT_TRANSPOTTER SETTINGS ############################ -#ifdef VARIANT_TRANSPOTTER - #define CONTROL_GAMETRAK - #define SUPPORT_LCD - #define SUPPORT_NUNCHUK - - #define GAMETRAK_CONNECTION_NORMAL // for normal wiring according to the wiki instructions - //#define GAMETRAK_CONNECTION_ALTERNATE // use this define instead if you messed up the gametrak ADC wiring (steering is speed, and length of the wire is steering) - - #define ROT_P 1.2 // P coefficient for the direction controller. Positive / Negative values to invert gametrak steering direction. - - #define INVERT_R_DIRECTION // Invert right motor - #define INVERT_L_DIRECTION // Invert left motor - - // during nunchuk control (only relevant when activated) - #define SPEED_COEFFICIENT 14746 // 0.9f - higher value == stronger. 0.0 to ~2.0? - #define STEER_COEFFICIENT 8192 // 0.5f - higher value == stronger. if you do not want any steering, set it to 0.0; 0.0 to 1.0 -#endif - -// ################################# VARIANT_NUNCHUK SETTINGS ############################ -#ifdef VARIANT_NUNCHUK - // # ARMCHAIR # - #define FILTER 3276 // 0.05f - #define SPEED_COEFFICIENT 8192 // 0.5f - #define STEER_COEFFICIENT 62259 // -0.2f - -#endif - -// ################################# SIMPLE BOBBYCAR ################################# -// for better bobbycar code see: https://github.com/larsmm/hoverboard-firmware-hack-bbcar -// #define FILTER 6553 // 0.1f -// #define SPEED_COEFFICIENT 49152 // -1.0f -// #define STEER_COEFFICIENT 0 // 0.0f // ############################### VALIDATE SETTINGS ############################### - -#if !defined(VARIANT_ADC) && !defined(VARIANT_USART) && !defined(VARIANT_HOVERCAR) && !defined(VARIANT_TRANSPOTTER) && !defined(VARIANT_NUNCHUK) && !defined(VARIANT_PPM)&& !defined(VARIANT_IBUS) +#if !defined(VARIANT_ADC) && !defined(VARIANT_USART) && !defined(VARIANT_HOVERCAR) && !defined(VARIANT_TRANSPOTTER) && !defined(VARIANT_NUNCHUK) && !defined(VARIANT_PPM) &&t !defined(VARIANT_IBUS) && !defined(DEBUG_SERIAL_USART3) && !defined(DEBUG_SERIAL_USART2) #error Variant not defined! Please check platformio.ini or Inc/config.h for available variants. #endif @@ -389,4 +428,6 @@ #if defined(CONTROL_PPM) && !defined(PPM_NUM_CHANNELS) #error Total number of PPM channels needs to be set -#endif \ No newline at end of file +#endif +// ############################# END OF VALIDATE SETTINGS ############################ + From 05e56eefd72cfd6f6cff8cf3b95af77e6b613dc3 Mon Sep 17 00:00:00 2001 From: kai Date: Sat, 18 Jan 2020 02:04:36 +0100 Subject: [PATCH 5/9] prettified config.h typo fix --- Inc/config.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Inc/config.h b/Inc/config.h index 317755e..326d7ea 100644 --- a/Inc/config.h +++ b/Inc/config.h @@ -372,7 +372,7 @@ // ############################### VALIDATE SETTINGS ############################### -#if !defined(VARIANT_ADC) && !defined(VARIANT_USART) && !defined(VARIANT_HOVERCAR) && !defined(VARIANT_TRANSPOTTER) && !defined(VARIANT_NUNCHUK) && !defined(VARIANT_PPM) &&t !defined(VARIANT_IBUS) && !defined(DEBUG_SERIAL_USART3) && !defined(DEBUG_SERIAL_USART2) +#if !defined(VARIANT_ADC) && !defined(VARIANT_USART) && !defined(VARIANT_HOVERCAR) && !defined(VARIANT_TRANSPOTTER) && !defined(VARIANT_NUNCHUK) && !defined(VARIANT_PPM) && !defined(VARIANT_IBUS) && !defined(DEBUG_SERIAL_USART3) && !defined(DEBUG_SERIAL_USART2) #error Variant not defined! Please check platformio.ini or Inc/config.h for available variants. #endif From 2c1572d8442bf4064ec5548027c8dfa99428adb6 Mon Sep 17 00:00:00 2001 From: kai Date: Sat, 18 Jan 2020 02:10:40 +0100 Subject: [PATCH 6/9] prettified config.h --- Inc/config.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Inc/config.h b/Inc/config.h index 326d7ea..f6f3358 100644 --- a/Inc/config.h +++ b/Inc/config.h @@ -17,6 +17,7 @@ #endif #define INACTIVITY_TIMEOUT 8 // Minutes of not driving until poweroff. it is not very precise. +#define BEEPS_BACKWARD 1 // 0 or 1a // ########################### END OF GENERAL SETTINGS ############################ @@ -31,6 +32,8 @@ #define BAT_CALIB_REAL_VOLTAGE 3970 // input voltage measured by multimeter (multiplied by 100). In this case 43.00 V * 100 = 4300 #define BAT_CALIB_ADC 1492 // adc-value measured by mainboard (value nr 5 on UART debug output) #define BAT_CELLS 10 // battery number of cells. Normal Hoverboard battery: 10s +#define BAT_LOW_LVL1_ENABLE 0 // to beep or not to beep, 1 or 0 +#define BAT_LOW_LVL2_ENABLE 1 // to beep or not to beep, 1 or 0 #define BAT_LOW_LVL1 (360 * BAT_CELLS * BAT_CALIB_ADC) / BAT_CALIB_REAL_VOLTAGE // gently beeps at this voltage level. [V*100/cell]. In this case 3.60 V/cell #define BAT_LOW_LVL2 (350 * BAT_CELLS * BAT_CALIB_ADC) / BAT_CALIB_REAL_VOLTAGE // your battery is almost empty. Charge now! [V*100/cell]. In this case 3.50 V/cell #define BAT_LOW_DEAD (337 * BAT_CELLS * BAT_CALIB_ADC) / BAT_CALIB_REAL_VOLTAGE // undervoltage poweroff. (while not driving) [V*100/cell]. In this case 3.37 V/cell @@ -52,6 +55,7 @@ #define TEMP_CAL_LOW_DEG_C 358 // temperature 1: measured temperature [°C * 10]. Here 35.8 °C #define TEMP_CAL_HIGH_ADC 1588 // temperature 2: ADC value #define TEMP_CAL_HIGH_DEG_C 489 // temperature 2: measured temperature [°C * 10]. Here 48.9 °C +#define TEMP_WARNING_ENABLE 0 // to beep or not to beep, 1 or 0, DO NOT ACTIVITE WITHOUT CALIBRATION! #define TEMP_WARNING 600 // annoying fast beeps [°C * 10]. Here 60.0 °C #define TEMP_POWEROFF_ENABLE 0 // to poweroff or not to poweroff, 1 or 0, DO NOT ACTIVITE WITHOUT CALIBRATION! #define TEMP_POWEROFF 650 // overheat poweroff. (while not driving) [°C * 10]. Here 65.0 °C @@ -372,7 +376,7 @@ // ############################### VALIDATE SETTINGS ############################### -#if !defined(VARIANT_ADC) && !defined(VARIANT_USART) && !defined(VARIANT_HOVERCAR) && !defined(VARIANT_TRANSPOTTER) && !defined(VARIANT_NUNCHUK) && !defined(VARIANT_PPM) && !defined(VARIANT_IBUS) && !defined(DEBUG_SERIAL_USART3) && !defined(DEBUG_SERIAL_USART2) +#if !defined(VARIANT_ADC) && !defined(VARIANT_USART) && !defined(VARIANT_HOVERCAR) && !defined(VARIANT_TRANSPOTTER) && !defined(VARIANT_NUNCHUK) && !defined(VARIANT_PPM) &&t !defined(VARIANT_IBUS) && !defined(DEBUG_SERIAL_USART3) && !defined(DEBUG_SERIAL_USART2) #error Variant not defined! Please check platformio.ini or Inc/config.h for available variants. #endif From 8098a314574494eccbae40a447e0fadbbb0d631d Mon Sep 17 00:00:00 2001 From: kai Date: Sat, 18 Jan 2020 02:14:42 +0100 Subject: [PATCH 7/9] prettified config.h --- Inc/config.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Inc/config.h b/Inc/config.h index f6f3358..4ba6065 100644 --- a/Inc/config.h +++ b/Inc/config.h @@ -376,7 +376,7 @@ // ############################### VALIDATE SETTINGS ############################### -#if !defined(VARIANT_ADC) && !defined(VARIANT_USART) && !defined(VARIANT_HOVERCAR) && !defined(VARIANT_TRANSPOTTER) && !defined(VARIANT_NUNCHUK) && !defined(VARIANT_PPM) &&t !defined(VARIANT_IBUS) && !defined(DEBUG_SERIAL_USART3) && !defined(DEBUG_SERIAL_USART2) +#if !defined(VARIANT_ADC) && !defined(VARIANT_USART) && !defined(VARIANT_HOVERCAR) && !defined(VARIANT_TRANSPOTTER) && !defined(VARIANT_NUNCHUK) && !defined(VARIANT_PPM) && !defined(VARIANT_IBUS) && !defined(DEBUG_SERIAL_USART3) && !defined(DEBUG_SERIAL_USART2) #error Variant not defined! Please check platformio.ini or Inc/config.h for available variants. #endif From 25f223a70331c83f8fa6159cc6d26a0b59cc87b6 Mon Sep 17 00:00:00 2001 From: Kai Hauser Date: Sun, 26 Jan 2020 21:19:58 +0100 Subject: [PATCH 8/9] fixes :0:0: warning: "PLATFORMIO" redefined no need to define -D PLATFORMIO since by platformio itself. --- platformio.ini | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/platformio.ini b/platformio.ini index a28c724..3c7b025 100644 --- a/platformio.ini +++ b/platformio.ini @@ -41,7 +41,6 @@ build_flags = -g -ggdb ; to generate correctly the 'firmware.elf' for STM STUDIO vizualization # -Wl,-lnosys -D VARIANT_ADC - -D PLATFORMIO ;================================================================ @@ -66,7 +65,6 @@ build_flags = -g -ggdb ; to generate correctly the 'firmware.elf' for STM STUDIO vizualization # -Wl,-lnosys -D VARIANT_USART - -D PLATFORMIO ;================================================================ @@ -87,7 +85,6 @@ build_flags = -g -ggdb ; to generate correctly the 'firmware.elf' for STM STUDIO vizualization # -Wl,-lnosys -D VARIANT_NUNCHUK - -D PLATFORMIO ;================================================================ @@ -108,7 +105,6 @@ build_flags = -g -ggdb ; to generate correctly the 'firmware.elf' for STM STUDIO vizualization # -Wl,-lnosys -D VARIANT_PPM - -D PLATFORMIO ;================================================================ @@ -129,7 +125,6 @@ build_flags = -g -ggdb ; to generate correctly the 'firmware.elf' for STM STUDIO vizualization # -Wl,-lnosys -D VARIANT_IBUS - -D PLATFORMIO ;================================================================ @@ -154,7 +149,6 @@ build_flags = -g -ggdb ; to generate correctly the 'firmware.elf' for STM STUDIO vizualization # -Wl,-lnosys -D VARIANT_HOVERCAR - -D PLATFORMIO ;================================================================ @@ -175,6 +169,5 @@ build_flags = -g -ggdb ; to generate correctly the 'firmware.elf' for STM STUDIO vizualization # -Wl,-lnosys -D VARIANT_TRANSPOTTER - -D PLATFORMIO -;================================================================ \ No newline at end of file +;================================================================ From a57b6002306b68b141b1e4f265380c0b7cf56575 Mon Sep 17 00:00:00 2001 From: EmanuelFeru Date: Wed, 29 Jan 2020 21:20:04 +0100 Subject: [PATCH 9/9] Minor changes for PR #18 - updated sligtly the order of defines and corrected defines for some variants in config.h - fixed checksum data type in Arduino example (Issue #19) --- 02_Arduino/hoverserial/hoverserial.ino | 2 +- Inc/config.h | 236 +++++++++++++------------ README.md | 4 +- platformio.ini | 2 +- 4 files changed, 125 insertions(+), 119 deletions(-) diff --git a/02_Arduino/hoverserial/hoverserial.ino b/02_Arduino/hoverserial/hoverserial.ino index d6908c3..df05d57 100644 --- a/02_Arduino/hoverserial/hoverserial.ino +++ b/02_Arduino/hoverserial/hoverserial.ino @@ -58,7 +58,7 @@ typedef struct{ int16_t speedL_meas; int16_t batVoltage; int16_t boardTemp; - int16_t checksum; + uint16_t checksum; } SerialFeedback; SerialFeedback Feedback; SerialFeedback NewFeedback; diff --git a/Inc/config.h b/Inc/config.h index 4ba6065..141ac42 100644 --- a/Inc/config.h +++ b/Inc/config.h @@ -9,18 +9,52 @@ #if !defined(PLATFORMIO) //#define VARIANT_ADC // Variant for control via ADC input //#define VARIANT_USART // Variant for Serial control via USART3 input - //#define VARIANT_NUNCHUK // Variant for Nunchuk controlled vehicle build + //#define VARIANT_NUNCHUK // Variant for Nunchuk controlled vehicle build //#define VARIANT_PPM // Variant for RC-Remote with PPM-Sum Signal //#define VARIANT_IBUS // Variant for RC-Remotes with FLYSKY IBUS //#define VARIANT_HOVERCAR // Variant for HOVERCAR 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 #endif -#define INACTIVITY_TIMEOUT 8 // Minutes of not driving until poweroff. it is not very precise. -#define BEEPS_BACKWARD 1 // 0 or 1a +#define INACTIVITY_TIMEOUT 8 // Minutes of not driving until poweroff. it is not very precise. +#define BEEPS_BACKWARD 1 // 0 or 1 // ########################### END OF GENERAL SETTINGS ############################ +// ############################### DO-NOT-TOUCH SETTINGS ############################### +#define PWM_FREQ 16000 // PWM frequency in Hz / is also used for buzzer +#define DEAD_TIME 48 // PWM deadtime +#ifdef VARIANT_TRANSPOTTER + #define DELAY_IN_MAIN_LOOP 2 +#else + #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. +#endif +#define TIMEOUT 5 // number of wrong / missing input commands before emergency off +#define A2BIT_CONV 50 // A to bit for current conversion on ADC. Example: 1 A = 50, 2 A = 100, etc + +// ADC conversion time definitions +#define ADC_CONV_TIME_1C5 (14) //Total ADC clock cycles / conversion = ( 1.5+12.5) +#define ADC_CONV_TIME_7C5 (20) //Total ADC clock cycles / conversion = ( 7.5+12.5) +#define ADC_CONV_TIME_13C5 (26) //Total ADC clock cycles / conversion = ( 13.5+12.5) +#define ADC_CONV_TIME_28C5 (41) //Total ADC clock cycles / conversion = ( 28.5+12.5) +#define ADC_CONV_TIME_41C5 (54) //Total ADC clock cycles / conversion = ( 41.5+12.5) +#define ADC_CONV_TIME_55C5 (68) //Total ADC clock cycles / conversion = ( 55.5+12.5) +#define ADC_CONV_TIME_71C5 (84) //Total ADC clock cycles / conversion = ( 71.5+12.5) +#define ADC_CONV_TIME_239C5 (252) //Total ADC clock cycles / conversion = (239.5+12.5) + +// This settings influences the actual sample-time. Only use definitions above +// This parameter needs to be the same as the ADC conversion for Current Phase of the FIRST Motor in setup.c +#define ADC_CONV_CLOCK_CYCLES (ADC_CONV_TIME_7C5) + +// Set the configured ADC divider. This parameter needs to be the same ADC divider as PeriphClkInit.AdcClockSelection (see main.c) +#define ADC_CLOCK_DIV (4) + +// ADC Total conversion time: this will be used to offset TIM8 in advance of TIM1 to align the Phase current ADC measurement +// This parameter is used in setup.c +#define ADC_TOTAL_CONV_TIME (ADC_CLOCK_DIV * ADC_CONV_CLOCK_CYCLES) // = ((SystemCoreClock / ADC_CLOCK_HZ) * ADC_CONV_CLOCK_CYCLES), where ADC_CLOCK_HZ = SystemCoreClock/ADC_CLOCK_DIV +// ########################### END OF DO-NOT-TOUCH SETTINGS ############################ + + // ############################### BATTERY ############################### /* Battery voltage calibration: connect power source. @@ -63,6 +97,56 @@ +// ############################### MOTOR CONTROL ######################### +/* GENERAL NOTES: + * 1. The parameters here are over-writing the default motor parameters. For all the available parameters check BLDC_controller_data.c + * 2. The parameters are represented in fixed point data type for a more efficient code execution + * 3. For calibrating the fixed-point parameters use the Fixed-Point Viewer tool (see ) + * 4. For more details regarding the parameters and the working principle of the controller please consult the Simulink model + * 5. A webview was created, so Matlab/Simulink installation is not needed, unless you want to regenerate the code. + * The webview is an html page that can be opened with browsers like: Microsoft Internet Explorer or Microsoft Edge + * + * NOTES Field Weakening / Phase Advance: + * 1. The Field Weakening is a linear interpolation from 0 to FIELD_WEAK_MAX or PHASE_ADV_MAX (depeding if FOC or SIN is selected, respectively) + * 2. The Field Weakening starts engaging at FIELD_WEAK_LO and reaches the maximum value at FIELD_WEAK_HI + * 3. If you re-calibrate the Field Weakening please take all the safety measures! The motors can spin very fast! + + Inputs: + - cmd1 and cmd2: analog normalized input values. INPUT_MIN to INPUT_MAX + - button1 and button2: digital input values. 0 or 1 + - adc_buffer.l_tx2 and adc_buffer.l_rx2: unfiltered ADC values (you do not need them). 0 to 4095 + Outputs: + - speedR and speedL: normal driving INPUT_MIN to INPUT_MAX +*/ +// Control selections +#define CTRL_TYP_SEL 2 // [-] Control type selection: 0 = Commutation , 1 = Sinusoidal, 2 = FOC Field Oriented Control (default) +#define CTRL_MOD_REQ 1 // [-] Control mode request: 0 = Open mode, 1 = VOLTAGE mode (default), 2 = SPEED mode, 3 = TORQUE mode. Note: SPEED and TORQUE modes are only available for FOC! +#define DIAG_ENA 1 // [-] Motor Diagnostics enable flag: 0 = Disabled, 1 = Enabled (default) + +// Limitation settings +#define I_MOT_MAX 15 // [A] Maximum motor current limit +#define I_DC_MAX 17 // [A] Maximum DC Link current limit (This is the final current protection. Above this value, current chopping is applied. To avoid this make sure that I_DC_MAX = I_MOT_MAX + 2A) +#define N_MOT_MAX 1000 // [rpm] Maximum motor speed limit + +// Field Weakening / Phase Advance +#define FIELD_WEAK_ENA 0 // [-] Field Weakening / Phase Advance enable flag: 0 = Disabled (default), 1 = Enabled +#define FIELD_WEAK_MAX 5 // [A] Maximum Field Weakening D axis current (only for FOC). Higher current results in higher maximum speed. +#define PHASE_ADV_MAX 25 // [deg] Maximum Phase Advance angle (only for SIN). Higher angle results in higher maximum speed. +#define FIELD_WEAK_HI 1500 // [-] Input target High threshold for reaching maximum Field Weakening / Phase Advance. Do NOT set this higher than 1500. +#define FIELD_WEAK_LO 1000 // [-] Input target Low threshold for starting Field Weakening / Phase Advance. Do NOT set this higher than 1000. + +// Data checks - Do NOT touch +#if (FIELD_WEAK_ENA == 0) + #undef FIELD_WEAK_HI + #define FIELD_WEAK_HI 1000 // [-] This prevents the input target going beyond 1000 when Field Weakening is not enabled +#endif +#define INPUT_MAX MAX( 1000, FIELD_WEAK_HI) // [-] Defines the Input target maximum limitation +#define INPUT_MIN MIN(-1000,-FIELD_WEAK_HI) // [-] Defines the Input target minimum limitation +#define INPUT_MID INPUT_MAX / 2 +// ########################### END OF MOTOR CONTROL ######################## + + + // ############################### DEBUG SERIAL ############################### /* Connect GND and RX of a 3.3v uart-usb adapter to the left (USART2) or right sensor board cable (USART3) * Be careful not to use the red wire of the cable. 15v will destroye evrything. @@ -86,7 +170,9 @@ */ // #define DEBUG_SERIAL_USART2 // left sensor board cable, disable if ADC or PPM is used! -// #define DEBUG_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuk or lcd) is used! +#if defined(VARIANT_ADC) || defined(VARIANT_HOVERCAR) +#define DEBUG_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuk or lcd) is used! +#endif #ifndef VARIANT_TRANSPOTTER //#define DEBUG_SERIAL_SERVOTERM @@ -109,6 +195,8 @@ * If VAL_floatingPoint >= 0, VAL_fixedPoint = VAL_floatingPoint * 2^14 * If VAL_floatingPoint < 0, VAL_fixedPoint = 2^16 + floor(VAL_floatingPoint * 2^14). */ +// Value of RATE is in fixdt(1,16,4): VAL_fixedPoint = VAL_floatingPoint * 2^4. In this case 480 = 30 * 2^4 +#define DEFAULT_RATE 480 // 30.0f [-] lower value == slower rate [0, 32767] = [0.0, 2047.9375]. Do NOT make rate negative (>32767) #define DEFAULT_FILTER 6553 // Default for FILTER 0.1f [-] lower value == softer filter [0, 65535] = [0.0 - 1.0]. #define DEFAULT_SPEED_COEFFICIENT 16384 // Default for SPEED_COEFFICIENT 1.0f [-] higher value == stronger. [0, 65535] = [-2.0 - 2.0]. In this case 16384 = 1.0 * 2^14 #define DEFAULT_STEER_COEFFICIENT 8192 // Defualt for STEER_COEFFICIENT 0.5f [-] higher value == stronger. [0, 65535] = [-2.0 - 2.0]. In this case 8192 = 0.5 * 2^14. If you do not want any steering, set it to 0. @@ -145,10 +233,10 @@ // ############################ VARIANT_USART SETTINGS ############################ #ifdef VARIANT_USART - // #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! - //#define CONTROL_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuk or lcd) is used! For Arduino control check the hoverSerial.ino - //#define FEEDBACK_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuk or lcd) is used! + // #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! + #define CONTROL_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuk or lcd) is used! For Arduino control check the hoverSerial.ino + #define FEEDBACK_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuk or lcd) is used! #endif // ######################## END OF VARIANT_USART SETTINGS ######################### @@ -214,11 +302,17 @@ #define ADC1_MAX 2500 // max ADC1-value while poti at maximum-position (0 - 4095) #define ADC2_MIN 500 // min ADC2-value while poti at minimum-position (0 - 4095) #define ADC2_MAX 2200 // max ADC2-value while poti at maximum-position (0 - 4095) - #define SPEED_COEFFICIENT 16384 // 1.0f - #define STEER_COEFFICIENT 0 // 0.0f + #define SPEED_COEFFICIENT 16384 // 1.0f + #define STEER_COEFFICIENT 0 // 0.0f //#define INVERT_R_DIRECTION // Invert rotation of right motor //#define INVERT_L_DIRECTION // Invert rotation of left motor #endif + +// Multiple tap detection: default DOUBLE Tap on Brake pedal (4 pulses) +#define MULTIPLE_TAP_NR 2 * 2 // [-] Define tap number: MULTIPLE_TAP_NR = number_of_taps * 2, number_of_taps = 1 (for single taping), 2 (for double tapping), 3 (for triple tapping), etc... +#define MULTIPLE_TAP_HI 600 // [-] Multiple tap detection High hysteresis threshold +#define MULTIPLE_TAP_LO 200 // [-] Multiple tap detection Low hysteresis threshold +#define MULTIPLE_TAP_TIMEOUT 2000 // [ms] Multiple tap detection Timeout period. The taps need to happen within this time window to be accepted. // ######################## END OF VARIANT_HOVERCAR SETTINGS ######################### @@ -227,121 +321,30 @@ //TODO ADD VALIDATION #ifdef VARIANT_TRANSPOTTER #define CONTROL_GAMETRAK - //#define SUPPORT_LCD - //#define SUPPORT_NUNCHUK - #define GAMETRAK_CONNECTION_NORMAL // for normal wiring according to the wiki instructions + #define SUPPORT_LCD + #define SUPPORT_NUNCHUK + #define GAMETRAK_CONNECTION_NORMAL // for normal wiring according to the wiki instructions //#define GAMETRAK_CONNECTION_ALTERNATE // use this define instead if you messed up the gametrak ADC wiring (steering is speed, and length of the wire is steering) - #define ROT_P 1.2 // P coefficient for the direction controller. Positive / Negative values to invert gametrak steering direction. + #define ROT_P 1.2 // P coefficient for the direction controller. Positive / Negative values to invert gametrak steering direction. // during nunchuk control (only relevant when activated) - #define SPEED_COEFFICIENT 14746 // 0.9f - higher value == stronger. 0.0 to ~2.0? - #define STEER_COEFFICIENT 8192 // 0.5f - higher value == stronger. if you do not want any steering, set it to 0.0; 0.0 to 1.0 + #define SPEED_COEFFICIENT 14746 // 0.9f - higher value == stronger. 0.0 to ~2.0? + #define STEER_COEFFICIENT 8192 // 0.5f - higher value == stronger. if you do not want any steering, set it to 0.0; 0.0 to 1.0 + #define INVERT_R_DIRECTION // Invert right motor + #define INVERT_L_DIRECTION // Invert left motor #endif // ############################# END OF VARIANT_TRANSPOTTER SETTINGS ######################## -// ############################### MOTOR CONTROL ######################### -/* GENERAL NOTES: - * 1. The parameters are over-writing the default motor parameters. For all the available parameters check BLDC_controller_data.c - * 2. The parameters are represented in fixed point data type for a more efficient code execution - * 3. For calibrating the fixed-point parameters use the Fixed-Point Viewer tool (see ) - * 4. For more details regarding the parameters and the working principle of the controller please consult the Simulink model - * 5. A webview was created, so Matlab/Simulink installation is not needed, unless you want to regenerate the code. - * The webview is an html page that can be opened with browsers like: Microsoft Internet Explorer or Microsoft Edge - * - * NOTES Field Weakening / Phase Advance: - * 1. The Field Weakening is a linear interpolation from 0 to FIELD_WEAK_MAX or PHASE_ADV_MAX (depeding if FOC or SIN is selected, respectively) - * 2. The Field Weakening starts engaging at FIELD_WEAK_LO and reaches the maximum value at FIELD_WEAK_HI - * 3. If you re-calibrate the Field Weakening please take all the safety measures! The motors can spin very fast! - - Inputs: - - cmd1 and cmd2: analog normalized input values. INPUT_MIN to INPUT_MAX - - button1 and button2: digital input values. 0 or 1 - - adc_buffer.l_tx2 and adc_buffer.l_rx2: unfiltered ADC values (you do not need them). 0 to 4095 - Outputs: - - speedR and speedL: normal driving INPUT_MIN to INPUT_MAX -*/ -// Control selections -#define CTRL_TYP_SEL 2 // [-] Control type selection: 0 = Commutation , 1 = Sinusoidal, 2 = FOC Field Oriented Control (default) -#define CTRL_MOD_REQ 1 // [-] Control mode request: 0 = Open mode, 1 = VOLTAGE mode (default), 2 = SPEED mode, 3 = TORQUE mode. Note: SPEED and TORQUE modes are only available for FOC! -#define DIAG_ENA 1 // [-] Motor Diagnostics enable flag: 0 = Disabled, 1 = Enabled (default) - -// Limitation settings -#define I_MOT_MAX 15 // [A] Maximum motor current limit -#define I_DC_MAX 17 // [A] Maximum DC Link current limit (This is the final current protection. Above this value, current chopping is applied. To avoid this make sure that I_DC_MAX = I_MOT_MAX + 2A) -#define N_MOT_MAX 1000 // [rpm] Maximum motor speed limit - -// Field Weakening / Phase Advance -#define FIELD_WEAK_ENA 0 // [-] Field Weakening / Phase Advance enable flag: 0 = Disabled (default), 1 = Enabled -#define FIELD_WEAK_MAX 5 // [A] Maximum Field Weakening D axis current (only for FOC). Higher current results in higher maximum speed. -#define PHASE_ADV_MAX 25 // [deg] Maximum Phase Advance angle (only for SIN). Higher angle results in higher maximum speed. -#define FIELD_WEAK_HI 1500 // [-] Input target High threshold for reaching maximum Field Weakening / Phase Advance. Do NOT set this higher than 1500. -#define FIELD_WEAK_LO 1000 // [-] Input target Low threshold for starting Field Weakening / Phase Advance. Do NOT set this higher than 1000. - -// Data checks - Do NOT touch -#if (FIELD_WEAK_ENA == 0) - #undef FIELD_WEAK_HI - #define FIELD_WEAK_HI 1000 // [-] This prevents the input target going beyond 1000 when Field Weakening is not enabled -#endif -#define INPUT_MAX MAX( 1000, FIELD_WEAK_HI) // [-] Defines the Input target maximum limitation -#define INPUT_MIN MIN(-1000,-FIELD_WEAK_HI) // [-] Defines the Input target minimum limitation -#define INPUT_MID INPUT_MAX / 2 - -// Multiple tap detection: default DOUBLE Tap (4 pulses) -#define MULTIPLE_TAP_NR 2 * 2 // [-] Define tap number: MULTIPLE_TAP_NR = number_of_taps * 2, number_of_taps = 1 (for single taping), 2 (for double tapping), 3 (for triple tapping), etc... -#define MULTIPLE_TAP_HI 600 // [-] Multiple tap detection High hysteresis threshold -#define MULTIPLE_TAP_LO 200 // [-] Multiple tap detection Low hysteresis threshold -#define MULTIPLE_TAP_TIMEOUT 2000 // [ms] Multiple tap detection Timeout period. The taps need to happen within this time window to be accepted. - -// Value of RATE is in fixdt(1,16,4): VAL_fixedPoint = VAL_floatingPoint * 2^4. In this case 480 = 30 * 2^4 -#define RATE 480 // 30.0f [-] lower value == slower rate [0, 32767] = [0.0, 2047.9375]. Do NOT make rate negative (>32767) -// ########################### END OF MOTOR CONTROL ######################## - - - -// ############################### DO-NOT-TOUCH SETTINGS ############################### -#define PWM_FREQ 16000 // PWM frequency in Hz / is also used for buzzer -#define DEAD_TIME 48 // PWM deadtime -#ifdef VARIANT_TRANSPOTTER - #define DELAY_IN_MAIN_LOOP 2 -#else - #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. -#endif -#define TIMEOUT 5 // number of wrong / missing input commands before emergency off -#define A2BIT_CONV 50 // A to bit for current conversion on ADC. Example: 1 A = 50, 2 A = 100, etc - -// ADC conversion time definitions -#define ADC_CONV_TIME_1C5 (14) //Total ADC clock cycles / conversion = ( 1.5+12.5) -#define ADC_CONV_TIME_7C5 (20) //Total ADC clock cycles / conversion = ( 7.5+12.5) -#define ADC_CONV_TIME_13C5 (26) //Total ADC clock cycles / conversion = ( 13.5+12.5) -#define ADC_CONV_TIME_28C5 (41) //Total ADC clock cycles / conversion = ( 28.5+12.5) -#define ADC_CONV_TIME_41C5 (54) //Total ADC clock cycles / conversion = ( 41.5+12.5) -#define ADC_CONV_TIME_55C5 (68) //Total ADC clock cycles / conversion = ( 55.5+12.5) -#define ADC_CONV_TIME_71C5 (84) //Total ADC clock cycles / conversion = ( 71.5+12.5) -#define ADC_CONV_TIME_239C5 (252) //Total ADC clock cycles / conversion = (239.5+12.5) - -// This settings influences the actual sample-time. Only use definitions above -// This parameter needs to be the same as the ADC conversion for Current Phase of the FIRST Motor in setup.c -#define ADC_CONV_CLOCK_CYCLES (ADC_CONV_TIME_7C5) - -// Set the configured ADC divider. This parameter needs to be the same ADC divider as PeriphClkInit.AdcClockSelection (see main.c) -#define ADC_CLOCK_DIV (4) - -// ADC Total conversion time: this will be used to offset TIM8 in advance of TIM1 to align the Phase current ADC measurement -// This parameter is used in setup.c -#define ADC_TOTAL_CONV_TIME (ADC_CLOCK_DIV * ADC_CONV_CLOCK_CYCLES) // = ((SystemCoreClock / ADC_CLOCK_HZ) * ADC_CONV_CLOCK_CYCLES), where ADC_CLOCK_HZ = SystemCoreClock/ADC_CLOCK_DIV -// ########################### END OF DO-NOT-TOUCH SETTINGS ############################ - - - // ########################### UART SETIINGS ############################ -#if defined(FEEDBACK_SERIAL_USART2) || defined(CONTROL_SERIAL_USART2) || defined(DEBUG_SERIAL_USART2) || defined(FEEDBACK_SERIAL_USART3) || defined(CONTROL_SERIAL_USART3) || defined(DEBUG_SERIAL_USART3) +#if defined(FEEDBACK_SERIAL_USART2) || defined(CONTROL_SERIAL_USART2) || defined(DEBUG_SERIAL_USART2) || \ + defined(FEEDBACK_SERIAL_USART3) || defined(CONTROL_SERIAL_USART3) || defined(DEBUG_SERIAL_USART3) #define START_FRAME 0xAAAA // [-] Start frame definition for serial commands #define SERIAL_TIMEOUT 160 // [-] Serial timeout duration for the received data. 160 ~= 0.8 sec. Calculation: 0.8 sec / 0.005 sec #endif #if defined(FEEDBACK_SERIAL_USART2) || defined(CONTROL_SERIAL_USART2) || defined(DEBUG_SERIAL_USART2) #ifndef USART2_BAUD - #define USART2_BAUD 38400 // UART2 baud rate (long wired cable) + #define USART2_BAUD 38400 // UART2 baud rate (long wired cable) #endif #define USART2_WORDLENGTH UART_WORDLENGTH_8B // UART_WORDLENGTH_8B or UART_WORDLENGTH_9B #endif @@ -362,15 +365,18 @@ // ############################### APPLY DEFAULT SETTINGS ############################### +#ifndef RATE + #define RATE DEFAULT_RATE +#endif +#ifndef FILTER + #define FILTER DEFAULT_FILTER +#endif #ifndef SPEED_COEFFICIENT #define SPEED_COEFFICIENT DEFAULT_SPEED_COEFFICIENT #endif #ifndef STEER_COEFFICIENT #define STEER_COEFFICIENT DEFAULT_STEER_COEFFICIENT #endif -#ifndef FILTER - #define FILTER DEFAULT_FILTER -#endif // ########################### END OF APPLY DEFAULT SETTING ############################ diff --git a/README.md b/README.md index 8debc84..089b2e7 100644 --- a/README.md +++ b/README.md @@ -155,7 +155,7 @@ Most robust way for input is to use the ADC and potis. It works well even on 1m This firmware offers currently these variants (selectable in [platformio.ini](/platformio.ini) and / or [/Inc/config.h](/Inc/config.h)): - **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 on USART3 right sensor cable (short wired). The commands can be sent from an Arduino. Check out the [hoverserial.ino](/02_Arduino/hoverserial) as an example sketch. +- **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. @@ -170,7 +170,7 @@ Of course the firmware can be further customized for other needs or projects. Last but not least, I would like to acknowledge and thank the following people: - Original firmware: [@NiklasFauth](https://github.com/NiklasFauth) - Github: [@TomTinkering](https://github.com/TomTinkering), [@ced2c](https://github.com/ced2c), [@btsimonh](https://github.com/btsimonh), [@lalalandrus](https://github.com/lalalandrus), [@p-h-a-i-l](https://github.com/p-h-a-i-l) , [@AntumArk](https://github.com/AntumArk), [@juodumas](https://github.com/juodumas) +- Github: all the people that contributed via Pull Requests - ST Employee: [cedric H](https://community.st.com/s/question/0D50X0000B28qTDSQY/custom-foc-control-current-measurement-dma-timer-interrupt-needs-review) -for the very useful discussions, code snippets, and good suggestions to make this work possbile. diff --git a/platformio.ini b/platformio.ini index 3c7b025..a745180 100644 --- a/platformio.ini +++ b/platformio.ini @@ -11,7 +11,7 @@ src_dir = Src ; ;default_envs = VARIANT_ADC ; Variant for control via ADC input ;default_envs = VARIANT_USART ; Variant for Serial control via USART3 input -;default_envs = VARIANT_NUNCHUK ; Variant for Nunchuk controlled vehicle build +;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_IBUS ; Variant for RC-Remotes with FLYSKY IBUS ;default_envs = VARIANT_HOVERCAR ; Variant for HOVERCAR build