hoverboard-firmware-hack-fo.../README.md

194 lines
12 KiB
Markdown
Raw Normal View History

2019-10-06 13:09:15 +00:00
# hoverboard-firmware-hack-FOC
2019-10-06 20:57:52 +00:00
[![Build Status](https://travis-ci.com/EmanuelFeru/hoverboard-firmware-hack-FOC.svg?branch=master)](https://travis-ci.com/EmanuelFeru/hoverboard-firmware-hack-FOC)
2020-02-07 16:27:23 +00:00
[![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0)
[![paypal](https://www.paypalobjects.com/en_US/i/btn/btn_donate_SM.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=CU2SWN2XV9SCY&currency_code=EUR&source=url)
2019-10-06 13:09:15 +00:00
2019-10-06 21:09:28 +00:00
This repository implements Field Oriented Control (FOC) for stock hoverboards. Compared to the commutation method, this new FOC control method offers superior performance featuring:
2019-10-06 13:09:15 +00:00
- reduced noise and vibrations
2019-10-07 08:44:11 +00:00
- smooth torque output and improved motor efficiency. Thus, lower energy consumption
2019-10-06 13:09:15 +00:00
- field weakening to increase maximum speed range
2019-10-07 08:44:11 +00:00
2020-02-10 08:52:49 +00:00
#### For the hoverboard sideboard firmware see the following repositories:
- [hoverboard-sideboard-hack-GD](https://github.com/EmanuelFeru/hoverboard-sideboard-hack-GD)
- [hoverboard-sideboard-hack-STM](https://github.com/EmanuelFeru/hoverboard-sideboard-hack-STM)
2020-02-07 17:15:18 +00:00
2020-02-07 16:27:23 +00:00
---
## Hardware
![mainboard_pinout](pinout.png)
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
2020-02-07 17:15:18 +00:00
2020-02-07 16:27:23 +00:00
---
## FOC firmware
2019-10-07 08:44:11 +00:00
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".
2020-02-07 16:27:23 +00:00
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](https://nl.mathworks.com/solutions/embedded-code-generation.html) 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](/01_Matlab).
- A [webview](/01_Matlab/BLDC_controller_ert_rtw/html/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.
2019-10-06 13:09:15 +00:00
2020-02-07 16:27:23 +00:00
### Firmware architecture
2019-10-06 13:09:15 +00:00
2019-10-07 08:56:13 +00:00
The main firmware architecture includes:
2019-10-07 08:44:11 +00:00
- **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
2019-10-06 13:09:15 +00:00
2020-01-05 13:28:02 +00:00
![Firmware architecture](/docs/pictures/FW_architecture.png)
2019-10-06 13:09:15 +00:00
2019-10-06 14:12:35 +00:00
The FOC algorithm architecture is illustrated in the figure below:
2019-10-06 13:09:15 +00:00
2020-01-05 13:28:02 +00:00
![FOC algorithm](/docs/pictures/FOC_algorithm.png)
2019-10-06 13:09:15 +00:00
In this firmware 3 control types are available:
- Commutation
- SIN (Sinusoidal)
- FOC (Field Oriented Control)
2020-01-05 13:28:02 +00:00
![Schematic representation of the available control methods](/01_Matlab/02_Figures/control_methods.png)
2019-10-06 13:09:15 +00:00
2019-12-12 22:52:07 +00:00
### Field Weakening / Phase Advance
2019-10-06 13:09:15 +00:00
- By default the Field weakening is disabled. You can enable it in config.h file by setting the FIELD_WEAK_ENA = 1
2019-12-12 22:52:07 +00:00
- 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
2020-01-05 13:28:02 +00:00
![Field Weakening](/docs/pictures/FieldWeakening.png)
2019-12-12 22:52:07 +00:00
- If you re-calibrate the Field Weakening please take all the safety measures! The motors can spin very fast!
2019-10-06 13:09:15 +00:00
2019-10-06 13:54:24 +00:00
### Parameters
2019-10-06 13:09:15 +00:00
- 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](https://github.com/EmanuelFeru/FixedPointViewer) tool
2019-10-06 14:12:35 +00:00
- The parameters data Fixed-point types are given in the following table:
2019-10-06 13:09:15 +00:00
2020-01-05 13:28:02 +00:00
![Parameters table](/docs/pictures/paramTable.png)
2019-10-06 13:54:24 +00:00
2019-10-20 18:08:22 +00:00
### 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.
2019-10-20 18:08:22 +00:00
2020-02-07 16:27:23 +00:00
### Demo videos
2019-10-06 13:09:15 +00:00
2020-02-07 16:27:23 +00:00
[►Video: Commutation vs Advanced control (constant speed)](https://drive.google.com/open?id=1vC_kEkp2LE2lAaMCJcmK4z2m3jrPUoBD)
2019-10-06 13:09:15 +00:00
2020-02-07 16:27:23 +00:00
[►Video: Commutation vs Advanced control (variable speed)](https://drive.google.com/open?id=1rrQ4k5VLhhAWXQzDSCar_SmEdsbM-hq2)
2019-10-06 13:09:15 +00:00
2020-02-07 16:27:23 +00:00
[►Video: Reliable Serial Communication demo](https://drive.google.com/open?id=1mUM-p7SE6gmyTH7zhDHy5DUyczXvmy5d)
2019-10-06 13:09:15 +00:00
2020-02-07 16:27:23 +00:00
[►Video: HOVERCAR demo](https://drive.google.com/open?id=18IvRJVdQSsjTg1I0Wedlg19e0FuDjfdS)
2019-10-06 13:09:15 +00:00
---
## Flashing
2020-02-07 16:27:23 +00:00
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.
2020-02-07 17:15:18 +00:00
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](https://github.com/EmanuelFeru/hoverboard-firmware-hack-FOC/wiki/How-to-Unlock-MCU-flash).
2019-10-06 13:09:15 +00:00
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.
2020-02-07 16:27:23 +00:00
To build and flash choose one of the following methods:
2019-10-06 13:09:15 +00:00
2020-02-07 16:27:23 +00:00
### Method 1: Using Platformio
2019-10-06 13:09:15 +00:00
2020-02-07 16:27:23 +00:00
- 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](https://www.keil.com/download/product/), open the [mainboard-hack.uvproj](/MDK-ARM/)
- 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 Ubuntu
2020-02-07 16:27:23 +00:00
- prerequisites: install [ST-Flash utility](https://github.com/texane/stlink).
- open a terminal in the repo check-out folder and type:
2019-10-06 13:09:15 +00:00
```
2020-02-07 16:27:23 +00:00
make
2019-10-06 13:09:15 +00:00
```
2020-02-07 16:27:23 +00:00
- flash the firmware by typing:
2019-10-06 13:09:15 +00:00
```
2020-02-07 16:27:23 +00:00
make flash
2019-10-06 13:09:15 +00:00
```
2020-02-07 16:27:23 +00:00
- or
2019-10-06 13:09:15 +00:00
```
openocd -f interface/stlink-v2.cfg -f target/stm32f1x.cfg -c flash "write_image erase build/hover.bin 0x8000000"
```
---
## 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.
2020-01-18 00:05:06 +00:00
Nunchuk not working: Use the right one of the 2 types of nunchuks. Use i2c pullups.
2019-10-06 13:09:15 +00:00
2020-01-18 00:05:06 +00:00
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.
2019-10-06 13:09:15 +00:00
2020-01-18 00:05:06 +00:00
Recommendation: Nunchuk Breakout Board https://github.com/Jan--Henrik/hoverboard-breakout
2019-10-06 13:09:15 +00:00
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.
2020-02-07 17:15:18 +00:00
2019-10-06 13:09:15 +00:00
---
2020-02-07 16:27:23 +00:00
## Example Variants
2019-10-06 13:09:15 +00:00
2020-02-07 16:27:23 +00:00
This firmware offers currently these variants (selectable in [platformio.ini](/platformio.ini) or [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 (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.
2020-01-18 00:15:45 +00:00
- **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.
2020-01-05 12:08:18 +00:00
- **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.
- **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.
2020-01-05 12:08:18 +00:00
- **VARIANT_TRANSPOTTER**: This build is for transpotter which is a hoverboard based transportation system. For more details on how to build it check [here](https://github.com/NiklasFauth/hoverboard-firmware-hack/wiki/Build-Instruction:-TranspOtter) and [here](https://hackaday.io/project/161891-transpotter-ng).
Of course the firmware can be further customized for other needs or projects.
2020-02-07 17:15:18 +00:00
---
## Acknowledgements
Last but not least, I would like to acknowledge and thank the following people:
2019-10-06 21:06:06 +00:00
- Original firmware: [@NiklasFauth](https://github.com/NiklasFauth)
2019-10-06 21:02:19 +00:00
- 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
2019-10-06 21:02:19 +00:00
- ST Employee: [cedric H](https://community.st.com/s/question/0D50X0000B28qTDSQY/custom-foc-control-current-measurement-dma-timer-interrupt-needs-review)
2019-10-06 21:02:53 +00:00
2020-02-07 17:15:18 +00:00
2020-02-07 16:27:23 +00:00
---
## Contributions
2020-02-08 07:55:11 +00:00
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.
2020-02-07 16:27:23 +00:00
2020-02-07 17:25:57 +00:00
If you want to donate to keep this firmware updated, please use the link below:
2020-02-07 16:27:23 +00:00
[![paypal](https://www.paypalobjects.com/en_US/NL/i/btn/btn_donateCC_LG.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=CU2SWN2XV9SCY&currency_code=EUR&source=url)
---