How to build the motor controller firmware?
mkdir -v build_bobbycar-firmware
cd build_bobbycar-firmware
cmake ../bobbycar-firmware
make unlock0 # needed only once
make flash
How to build the steering wheel board computer firmware?
cd boardcomputer/
platformio run --environment bobbycar --target upload
How to wire all the PCBs inside the bobbycar?
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
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".
- OPEN MODE: Just reduces the input to 0. Since it is in software it will be exact 0. It will charge the battery, it is basically a fast recharge phase.
NOTE: 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.
- for critical error (blocked motor, Hall sensor not connected), enable = 0. So, the MOSFETs get interrupted immediately, to avoid HW damage.
- for soft error (Serial timeout, ADC out of range), I request OPEN mode. The rate of opening is controlled by dV_openRate see here. Decrease this parameter if you want slower opening.
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
The FOC algorithm architecture is illustrated in the figure below:
In this firmware 3 control types are available:
- 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.
- By default the Field weakening is enabled. You can disable it in config.h file by setting the FIELD_WEAK_ENA = 0
- 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
- If you re-calibrate the Field Weakening please take all the safety measures! The motors can spin very fast!
- 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:
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 errCode_Left and errCode_Right 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.
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
TODO: photos
To build the firmware, just type "make flash". 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.
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 flash the STM32, use the ST-Flash utility (https://github.com/texane/stlink).
If you never flashed your mainboard before, the STM is probably locked. To unlock the flash, use the following OpenOCD command:
make unlock0
If that does not work:
make unlock1
make unlock2
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.
Last but not least, I would like to acknowledge and thank the following people:
- Original firmware: @NiklasFauth
- Github: @TomTinkering, @ced2c, @btsimonh, @lalalandrus, @p-h-a-i-l , @AntumArk, @juodumas
- ST Employee: cedric H
- Emanuel Feru: @EmanuelFeru
for the very useful discussions, code snippets, and good suggestions to make this work possbile.