vamfun / Hoverboard-Hack-FOC-PID-Servo

This software will run the ADC variant with added PID closed loop control of the motors.
GNU General Public License v3.0
22 stars 14 forks source link

This hoverboard firmware is based upon the FOC firmware. It has GPIO modifications to match a motherboard that was taken from a Hover 1 H1 hoverboard. The GPIO changes are shown here: https://github.com/NiklasFauth/hoverboard-firmware-hack/issues/162#issuecomment-757374880. So you need to restore the GPIO changes to your board before running.
My repository uses a modified FOC ADC Variant that includes a closed loop PID controller that uses HALL effect sensor motor position to as feedback. The inputs are from ADC1 and ADC2. PID is parameterized to include KP,KD,KI, deadzone and KI integerator limit. Since the position is based upon HALL sensor feedback, the resolution is 4 degs mechanical position. A separate encoder is required for more precision. The default software only uses KP = 2... all other PID gains are 0. All input filters and limiters are effectively removed by setting their paramaters at extreme limits.

Since the main loop includes an input rate limit and filter I decided to regulate the main loop to be periodic at exactly the DELAY_IN_MAIN_LOOP. This is done with a main loop timer function that regulates the main period based upon the bldc in terrupt based buzzerTimer. If less than 32 characters are debug printed, then the print frequency can be every 5m. I have also replaced steering/speed variables with speed1 /speed2 variables.

The PID inputs and motor feedbacks are scaled to 2000 counts per rotation or +_1000 cnts. ie 1000 counts moves the motor 180 mechanical degrees. Varying the ADC from 0 to 4096 will cause one rotation. If using type2 ADC, starting the program with ADC at mid position will create a zero startup transient. If there is an uncenterd ADC, the motors will move to the commanded position slowly at 100 pwm speed for during first 5 seconds to slow fade the PID loop action. After 5 seconds the pwm command limit is restored to 1000.
Notice that these #Defines are required to run the PID. In config.h uncomment

Define VARIANT_ADC

and PID_CONTROL here: // ############################# PID CONTROL SETTINGS ################################

define PID_CONTROL //uncomment to use PID control

#ifdef PID_CONTROL 

 #define KP  2.
 #define KI  0. 
 #define PIDDZ 0
 #define PIDLIM  360
 typedef struct 
 {
     float Kp;
     float Ki;
     int dz ;
     int limit;
     int error;
     int cum_error;
     int input;
     int feedback;
     int output;

     }PID_DATA ;
     void PID(PID_DATA *p);
     void print_PID(PID_DATA p);
#endif

Good luck... The motors are very responsive....Motor response to a full rotation step input to the PID controller takes less than an 1/8 second reach a full 360 deg mechanical rotation. See plots and video in this issue... https://github.com/EmanuelFeru/hoverboard-firmware-hack-FOC/issues/139

hoverboard-firmware-hack-FOC

Build Status License: GPL v3 paypal

This repository implements Field Oriented Control (FOC) for stock hoverboards. Compared to the commutation method, this new FOC control method offers superior performance featuring:

Table of Contents

For the hoverboard sideboard firmware, see the following repositories:

For the FOC controller design, see the following repository:

Videos:


Hardware

mainboard_pinout

The original Hardware supports two 4-pin cables that originally were connected to the two sideboards. They break out GND, 12/15V and USART2&3 of the Hoverboard mainboard. Both USART2&3 support UART, PWM, PPM, and iBUS input. Additionally, the USART2 can be used as 12bit ADC, while USART3 can be used for I2C. Note that while USART3 (right sideboard cable) is 5V tolerant, USART2 (left sideboard cable) is not 5V tolerant.

Typically, the mainboard brain is an STM32F103RCT6, however some mainboards feature a GD32F103RCT6 which is also supported by this firmware.

For the reverse-engineered schematics of the mainboard, see 20150722_hoverboard_sch.pdf


FOC Firmware

In this firmware 3 control types are available:

Comparison between different control methods

Control method Complexity Efficiency Smoothness Field Weakening Freewheeling Standstill hold
Commutation - - ++ n.a. n.a. +
Sinusoidal + ++ ++ +++ n.a. +
FOC VOLTAGE ++ +++ ++ ++ n.a. +(2)
FOC SPEED +++ +++ + ++ n.a. +++
FOC TORQUE +++ +++ +++ ++ +++(1) n.a(2)

(1) By enabling ELECTRIC_BRAKE_ENABLE in config.h, the freewheeling amount can be adjusted using the ELECTRIC_BRAKE_MAX parameter.
(2) The standstill hold functionality can be forced by enabling STANDSTILL_HOLD_ENABLE in config.h.

In all FOC control modes, the controller features maximum motor speed and maximum motor current protection. This brings great advantages to fulfil the needs of many robotic applications while maintaining safe operation.

Field Weakening / Phase Advance

Parameters


Example Variants

This firmware offers currently these variants (selectable in platformio.ini or config.h):

Of course the firmware can be further customized for other needs or projects.


Dual Inputs

The firmware supports the input to be provided from two different sources connected to the Left and Right cable, respectively. To enable dual-inputs functionality uncomment #define DUAL_INPUTS in config.h for the respective variant. Various dual-inputs combinations can be realized as illustrated in the following table: Left Right Availability
ADC(0) UART(1) VARIANT_ADC
ADC(0) {PPM,PWM,iBUS}(1) VARIANT_{PPM,PWM,IBUS}
ADC(0) Sideboard(1*) VARIANT_HOVERCAR
UART(0) Sideboard(1*) VARIANT_UART
UART(1) Nunchuk(0) VARIANT_NUNCHUK

(0) Primary input: this input is used when the Auxilliary input is not available or not connected.
(1) Auxilliary input: this inputs is used when connected or enabled by a switch(*). If the Auxilliary input is disconnected, the firmware will automatically switch to the Primary input. Timeout is reported only on the Primary input.

With slight modifications in config.h, other dual-inputs combinations can be realized as: Left Right Possibility
Sideboard(1*) UART(0) VARIANT_UART
UART(0) {PPM,PWM,iBUS}(1) VARIANT_{PPM,PWM,IBUS}
{PPM,PWM,iBUS}(1) Nunchuk(0) VARIANT_{PPM,PWM,IBUS}

Flashing

Right to the STM32, there is a debugging header with GND, 3V3, SWDIO and SWCLK. Connect GND, SWDIO and SWCLK to your SWD programmer, like the ST-Link found on many STM devboards.

If you have never flashed your sideboard before, the MCU is probably locked. To unlock the flash, check-out the wiki page How to Unlock MCU flash.

Do not power the mainboard from the 3.3V of your programmer! This has already killed multiple mainboards.

Make sure you hold the powerbutton or connect a jumper to the power button pins while flashing the firmware, as the STM might release the power latch and switches itself off during flashing. Battery > 36V have to be connected while flashing.

To build and flash choose one of the following methods:

Method 1: Using Platformio IDE

Method 2: Using Keil uVision

Method 3: Using Linux CLI

Method 4: MacOS CLI

Using Make

brew install stlink

Using platformio CLI

brew install platformio
platformio run -e VARIANT_####
platformio run –target upload -e VARIANT_####

If you have set default_envs in platformio.ini you can ommit -e parameter


Troubleshooting

First, check that power is connected and voltage is >36V while flashing. 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.

Nunchuk not working: Use the right one of the 2 types of nunchuks. Use i2c pullups.

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


Diagnostics

The errors reported by the board are in the form of audible beeps:

For a more detailed troubleshooting connect an FTDI Serial adapter or a Bluetooth module to the DEBUG_SERIAL cable (Left or Right) and monitor the output data using the Hoverboard Web Serial Control tool developed by Candas.


Projects and Links


Contributions

Every contribution to this repository is highly appreciated! Feel free to create pull requests to improve this firmware as ultimately you are going to help everyone.

If you want to donate to keep this firmware updated, please use the link below:

paypal