Field oriented BLDC Servo motor control with the stm32g4 and Zig

July 09, 2022

I finally got closed loop BLDC motor control working using Zuric and a DRV8316 driver board I made (with Horizon-EDA).

The code is based on the awesome arduino-foc project, just is implemented in zig and uses a timer for the servo control loop.

The video shows closed loop velocity control but it also works with position control.

Since it's closed loop when a load is put on the motor the controller will increase the voltage up to a limit in order to try and achive the desired output.

It uses the stm32's hardware encoder timer mode with a 2000 cpr encoder. There are 3 pwm signals to set the phase voltages. Also shows the i2c driver running the LCD and a software encoder for the rotary encoder knob.

Just for kicks. Here's it using position/angle control.

The motor is a really old Pittman Elcom BLDC servo (model 4232B514) with a huge HP HEDS-6310 encoder. I think 3 motors with encoders were something like 30 yankee bucks shipped on ebay so I can't complain. They seem to work fine up to about 1.5A and can go higher just not continously (or the coating starts to melt and smell (oops)).

I couldn't find the exact datasheet for the encoder but found a similar model and this is the pinout (A). HP-HEDS-6130 Encoder Pinout

I want to say thanks to the authors of the arduino-foc project for making their library open source and providing docs and thanks to the Zig team for making an easy to use low level language.

Have a good day!