corrected nunchuck to nunchuk

This commit is contained in:
kai 2020-01-18 01:05:06 +01:00
parent fb362f04a9
commit cd01b73451
7 changed files with 72 additions and 72 deletions

View File

@ -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 <How to calibrate>.
@ -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)

View File

@ -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);

View File

@ -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.

View File

@ -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);
}

View File

@ -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);

View File

@ -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)
{

View File

@ -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
;================================================================