/hoverboard-firmware-maker

With Field Oriented Control (FOC)

Primary LanguageCGNU General Public License v3.0GPL-3.0

VARIANT_UARTCAR added

Variant for car with serial control, compatible to my Niklas fork.

speed is 10x km/h, so 60 will be 6 kmh. Check WHEEL_SIZE_INCHES in the config.h

VARIANT_UARTCAR has it's own control loop in main.c which allows for different left/right wheel speeds in a curve.

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:

  • reduced noise and vibrations
  • smooth torque output and improved motor efficiency. Thus, lower energy consumption
  • field weakening to increase maximum speed range

For the hoverboard sideboard firmware see the following repositories:


Hardware

mainboard_pinout

The original Hardware supports two 4-pin cables that originally were connected to the two sensor boards. They break out GND, 12/15V and USART2&3 of the Hoverboard mainboard. Both USART2 & 3 can be used for UART and I2C, PA2&3 can be used as 12bit ADCs.

The reverse-engineered schematics of the mainboard can be found here: http://vocke.tv/lib/exe/fetch.php?media=20150722_hoverboard_sch.pdf


FOC Firmware

This new firmware offers 3 control modes:

  • VOLTAGE MODE: in this mode the controller applies a constant Voltage to the motors
  • SPEED MODE: in this mode a closed-loop controller realizes the input target speed by rejecting any of the disturbance (resistive load) applied to the motor
  • TORQUE MODE: in this mode the target torque set by the user is realized. This mode enables motor "freewheeling" when the torque target is "0". Recommended for most applications with a sitting human driver. If you want the motor to brake instead of "freewheel" when torque target is "0", modify code to set torque target below "0" when speedAvgAbs > 0.

In all the 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.

  • The C code for the controller was auto-code generated using Matlab/Simulink from a model which I developed from scratch specifically for hoverboard control. For more details regarding the working principle of the controller please consult the Matlab/Simulink model.
  • 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.

Firmware Architecture

The main firmware architecture includes:

  • Estimations: estimates the rotor position, angle and motor speed based on Hall sensors signal
  • Diagnostics: implements error detection such as unconnected Hall sensor, motor blocked, MOSFET defective
  • Control Manager: manages the transitions between control modes (Voltage, Speed, Torque)
  • FOC Algorithm: implements the FOC strategy
  • Control Type Manager: Manages the transition between Commutation, Sinusoidal, and FOC control type

Firmware architecture

The FOC algorithm architecture is illustrated in the figure below:

FOC algorithm

In this firmware 3 control types are available:

  • Commutation
  • SIN (Sinusoidal)
  • FOC (Field Oriented Control) Schematic representation of the available control methods

Field Weakening / Phase Advance

  • By default the Field weakening is disabled. You can enable it in config.h file by setting the FIELD_WEAK_ENA = 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)
  • The Field Weakening starts engaging at FIELD_WEAK_LO and reaches the maximum value at FIELD_WEAK_HI
  • The figure below shows different possible calibrations for Field Weakening / Phase Advance Field Weakening
  • If you re-calibrate the Field Weakening please take all the safety measures! The motors can spin very fast!

Parameters

  • All the calibratable motor parameters can be found in the 'BLDC_controller_data.c'. I provided you with an already calibrated controller, but if you feel like fine tuning it feel free to do so
  • The parameters are represented in Fixed-point data type for a more efficient code execution
  • For calibrating the fixed-point parameters use the Fixed-Point Viewer tool
  • The parameters data Fixed-point types are given in the following table:

Parameters table

Diagnostics

Each motor is constantly monitored for errors. These errors are:

  • Error 001: Hall sensor not connected
  • Error 002: Hall sensor short circuit
  • Error 004: Motor NOT able to spin (Possible causes: motor phase disconnected, MOSFET defective, operational Amplifier defective, motor blocked)

The error codes above are reported for each motor in the variables rtY_Left.z_errCode and rtY_Right.z_errCode for Left motor (long wired motor) and Right motor (short wired motor), respectively. In case of error, the motor power is reduced to 0, while an audible (fast beep) can be heard to notify the user.

Demo Videos

►Video: HOVERCAR

►Video: Commutation vs Advanced control (constant speed)

►Video: Commutation vs Advanced control (variable speed)

►Video: Reliable Serial Communication demo


Example Variants

This firmware offers currently these variants (selectable in platformio.ini or 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 (e.g. on USART3 right sensor cable, the short wired cable). The commands can be sent from an Arduino. Check out the hoverserial.ino 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 an RC remote control with PPM Sum signal.
  • VARIANT_PWM: This is when you want to use an RC remote control with PWM signal.
  • VARIANT_IBUS: This is when you want to use an RC remote control with Flysky IBUS protocol connected to the Left sensor cable.
  • VARIANT_HOVERCAR: In this variant the motors are controlled by two pedals brake and throttle. Reverse is engaged by double tapping on the brake pedal at standstill. See HOVERCAR video.
  • VARIANT_HOVERBOARD: In this variant the mainboard reads the sideboards data. The sideboards need to be flashed with the hacked version. Only balancing controller is still to be implemented.
  • VARIANT_TRANSPOTTER: This build is for transpotter which is a hoverboard based transportation system. For more details on how to build it check here and here.

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


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

  • open the folder in the IDE of choice (vscode or Atom)
  • press the 'PlatformIO:Build' or the 'PlatformIO:Upload' button (bottom left in vscode).

Method 2: Using Keil uVision

  • in Keil uVision, open the mainboard-hack.uvproj
  • if you are asked to install missing packages, click Yes
  • click Build Target (or press F7) to build the firmware
  • click Load Code (or press F8) to flash the firmware.

Method 3: Using Linux CLI

  • prerequisites: install ST-Flash utility.
  • open a terminal in the repo check-out folder and if you have definded the variant in config.h type:
make

or you can set the variant like this

make -e VARIANT=VARIANT_####
  • flash the firmware by typing:
make flash
  • or
openocd -f interface/stlink-v2.cfg -f target/stm32f1x.cfg -c flash "write_image erase build/hover.bin 0x8000000"

Method 4: MacOS CLI

  • prerequisites: first get brew https://brew.sh
  • then install stlink ST-Flash utility

Using Make

brew install stlink
  • open a terminal in the repo check-out folder and if you have definded the variant in config.h type:
make

or you can set the variant like this

make -e VARIANT=VARIANT_####

If compiling fails because something is missing just install it with brew AND leave a comment to improve this howto or pull request ;-)

  • flash the firmware by typing:
make flash
  • if unlock is needed
make unlock

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.


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