Friday, October 8, 2021

Programming the BLDC Motor Controller

Here a picture of the BLDC motor jig that holds a 4108 hobby BLDC motor along with the magnetic encoder for servo control of the motor.

I completed programming the BLDC motor controller and now have a power dense closed loop BLDC servo! Here is the demo:


After getting STM32CubeIDE setup and building drivers for the motor PWM timer, ADC, Serial, and SPI peripherals I had a BLDC testbed that was capable of reading the motor position and phase currents then driving the H bridge to control the motor. 

The basic working principle of field oriented control of brushless motors is that the motor controller knows what position the rotor is in and drives currents through the stator to generate a magnetic field in the correct position to generate maximum torque. For this BLDC motor, that means the magnetic field generated by the stator should always be 90 degrees offset from the magnetic field of the rotor. 

To drive currents through the stator and make a magnetic field at a particular angle, two PID loops are used to control the currents in the stator: one in the 'X' direction and one in the 'Y'. The currents in the motor are measured, converted from a three phase to a two phase system using the Clarke transformation, then run through the PID loops which output a desired control voltage. The control voltages from each PID loop is then combined into a vector in which voltage should be applied to the motor which is then sent to an SVM algorithm that converts the control voltage vector to PWM values for the H bridge to execute.

With control over the magnetic field generated by the stator, the position of the rotor can be found by running a magnetic field in a known orientation across the stator and using the magnetic encoder to measure the position of the rotor. Knowing this offset the position of the rotor's magnetic field can be found.

The torque of the motor can be controlled by changing the magnitude of current sent through the stator, and the angle of the current in the stator is always perpendicular to the rotor magnetic field. One last PID loop is required for position control that takes a position setpoint and the current position and outputs the torque that should be applied to the rotor. 

Putting all of those control algorithms together, we have a functional BLDC servo motor with position control. There are a few more components that can be added to this system to improve reliability and make the system respond faster to disturbances that I do more work on in the future.
 

Monday, October 4, 2021

BLDC Motor Controller Hardware

After using the B-G431B-ESC1 development board as a testbed for BLDC motors, I decided to make a more purpose-built development board for me to experiment with servo control of hobby BLDC motors, and maybe try to run some sensorless control algorithms. 

The goal for this motor controller is to have a 3 phase 40V / 25A H bridge, 3 phase current sensors, and a magnetic encoder all feeding into an STM32 MCU that is capable of running the control algorithm as well as all of the debugging overhead that I will need to troubleshoot the system. If I can keep the overall price of this board low enough, it could become a great general purpose ESC for future robotics projects.


After looking at the STM32G431CB that I am currently using in the B-G431B-ESC1 development board and realizing that it does not have enough GPIO to add in a few UART communication channels and an SPI encoder, I browsed LCSC until I found the STM32F401CCU6. At ~$2/pc and offering most of the features of the STM32G431CB minus the second ADC, higher clock frequency, and CORDIC unit it is a good middle ground between spending a lot on an MCU and having enough features to run a single motor. 

Next I decided to use the Fortior FD6288 for an H bridge driver. I have seen this H bridge driver on several BLHeli32 ESCs and driving all six MOSFETs from a single chip seems to save a lot of board space. The documentation for this chip isn't the best as the only version I could find is in Chinese, but I was able to figure it out. The total footprint with all of the extra components is very small for what this driver does, and the chip was only $0.60/pc, keeping the overall cost of this project low.

I found the CSD18543Q3A MOSFETs from Texas Instruments for the H bridge, with a 60V drain-to-source maximum voltage I can get a little bit of voltage overhead in case of motor overspeed or over inductance events if the bus voltage is kept at 40V. Additionally it has a low gate charge and on state resistance so it will be easy for the FD6288 to drive this MOSFET and the board shouldn't be getting too hot from power losses in the H bridge. At the desired 25A phase current, this chip would be dissipating 5W, which I believe a well designed aluminum heatsink can handle.

A novel component of this development board that I have not seen before is that the three current sensors have a sample-and-hold circuit buffering the input to the microcontroller. This allows the microcontroller to precisely time when the phase currents are sampled and ensures that all three samples are simultaneous. With a single ADC on the STM32F401CCU6, the currents would otherwise have to be sequentially sampled and the H bridge would have to be held in a state where all three phases are being pulled to ground for the duration of the measurement because the current sensors are on the low side. The addition of this sample and hold circuit allows the MCU to sample the currents at the specified time then read them as the single ADC allows.

After drawing up the schematics and laying out the PCB, I sent this board to JLCPCB panelized as 3 boards with V groove cuts. This puts the board in a size range where the single side containing all of the components could be placed using the SMT service in the future if I like the design and want to use it for future projects.