# **Feeling FOC brushless drive** design/learning record
## Finished product display:
![IMG20221022104727.jpg]

![IMG20221022105616.jpg]

## Functional requirements:
- Able to realize single-channel motor foc control
- Able to adapt to a variety of position sensors
- Able to receive host computer instructions to realize passive control
- Able to independently perform multi-mode active control
- There is a screen to display some status parameters of motor operation
## Hardware design:
The main control board and the power board are separated, which is convenient for replacement and testing, and can be used for some expansion. The common signal pins of the main control board and the power board are connected through **pin/female** headers, and the two boards are reinforced by copper column assembly.
**Main control board:**
![Main control board.png]

MCU uses stm32F103CBT6. The connection interface with the power board includes three complementary PWM signal IOs, 4 adc channel IOs (two-phase current, bus current, power supply voltage), and GND/5V pins. The external connection interface includes the screen spi communication port, the magnetic encoder spi interface/IIC interface, the ABZ encoder interface, the reset button, the function button, the download and debug interface, and the host computer communication interface. The LDO is used to reduce the 5V power supply of the driver board to 3.3V, and the 5V is used to power the sensor.
- [x] MCU core circuit: crystal oscillator, boot, reset button
- [x] Public interface pin: three complementary pwmIO, 4 adc sampling IO, GND/5V/3.3V
- [x] spi magnetic encoder interface
- [x] TFT screen interface
- [x] Software IIC encoder interface, add an IO so that it can be reused as ABZ encoder interface
- [x] Function button
- [x] Download and debug interface: swd download, uart1 debug
- [x] One uart host computer communication interface
- [x] One LED system indicator, one power indicator
**Power board: **
! [Driver board schematic diagram.png]

Three-phase inverter, six-way pre-drive, power supply voltage division for mainboard acquisition, ab phase current acquisition (add bias voltage), bus current acquisition, DC-DC step-down to 5V for the main control board. In addition to power supply and output, the external interface also requires LED indicators and power switches.
- [x] Three H half-bridges
- [x] PWM pre-driver
- [x] Power supply voltage division for ADC acquisition
- [X] Bus current op amp, AB phase current op amp plus bias voltage
- [x] DC-DC step-down to 5V
- [x] LDO step-down 5V->3.3V
- [x] Common interface header
- [x] Power LED indicator, bus current LED indicator (indicates whether the three-phase inverter has output)
- [x] Power switch
**Three-phase inverter:**
![Three-phase inverter circuit.png]

It uses the pre-driver **FD6288** with integrated three-way half-bridge drive, the sampling resistor is placed in the lower half bridge, and a low-end sampling scheme is adopted. The mos can withstand 40V voltage and 120A current, but the PCB layout and wiring of the first-generation board cannot withstand too much current and is not suitable for testing high-power motors (because of the main drive separation design, it only needs to ensure that the interface is consistent, and consider redesigning a set of PCBs later).
**Current sampling op amp:**
![Current sampling.png]

uses a chip with 4 integrated op amps to reduce the size of the PCB. One op amp is used as a voltage follower to generate the bias voltage Vref required by the current sampling op amp. Two op amps collect AB phase current. Because of the bias voltage Vref, positive and negative currents can be collected. The last op amp collects the bus current.
**Encoder:**
The encoder uses the as5600 magnetic encoder chip. The IIC and pwm interfaces are both brought out. The program finally uses the IIC communication method. Personally, I don’t feel good about it. There will be occasional angle fluctuations.
**Hardware problems:**
- The output of the sampling op amp of the B phase circuit (regardless of whether it drives the motor) is a value higher than the theoretical value (0.01xN volts). After current calculation, it is equivalent to about 1A higher than the actual current value. The reason is not known yet (it is suspected to be a PCB design defect). Temporarily rely on program design to eliminate this deviation. In addition, the op amp has large interference and cannot meet the current loop design requirements.
- The bus current indicator light on the driver board is useless. When the actual current is small, the voltage is not enough and it will not light up at all (the original idea was that the brightness of the LED would change with the current size (⊙x⊙;)
- Because the main control board will be installed on the driver board, the power switch designed on the driver board will be blocked, making it very inconvenient to switch the power on and off. It should be designed as a side sliding switch X﹏X
## Software design:
I referred to many open source cases from big guys and **simplefoc**'s solution, used the peripheral driver configured with cubemx, and finally combined it with the board I made to write a set of code. Due to my relatively novice level, the code looks a bit messy as a whole, and there are potential unreasonable designs. In short, the level needs to be improved.
Due to the defects of hardware current sampling, the current loop was not successfully debugged in the end. Currently, only the speed and position closed loop through voltage control has been achieved.
## Summary:
The initial stage of foc learning has come to an end. I am very grateful for the blog tutorials of the masters on the Internet and the explanations of the masters in the technical exchange group. Otherwise, it would be difficult to learn alone. Now that I have done a set, I have a preliminary understanding of foc. Later, I will learn some foc theoretical knowledge in depth, and then redesign a set of software and hardware to further improve my foc design ability.
## References: