Tuesday, December 3, 2019

An ATmega brushless sensorless motor driver v02


Brushless electric motor (BLDC motors) are synchronous motors that are powered by a DC electric source via an integrated inverter/switching power supply, which produces an AC electric signal to drive the motor.
For an introduction to BLDC motors, you can take look at my sensored motor driver post, here: http://davidegironi.blogspot.it/2013/09/a-simple-brushless-sensored-motor.html or you can browse the internet, you will find there is a lot of litterature about BLDC motors.


This library implements a brushless sensorless motor dirver for AVR ATmega.
To write this library I take inspiration from the library above:

This library take inspiration from:
The code i propose is written, tested and compiled using avrgcc over an ATmega8 running at 16Mhz.
I've also tested a version of this code on ATmega8 running at 8Mhz.
The aim of is project is to build a fairly simple bldc driver one can embed and easly modify in order to meet their own needs.

There are of course driver that performs better than this one, so if you are looking for best performance and you are using an ATmega micro, i suggest you to take a look at the SimonK firmware (https://github.com/sim-/tgy) or the BLHeli firmware (https://github.com/bitdump/BLHeli).

My previos driver you can find on links above does not fully implements PWM, also the brushless sensorless version to detect Zero Crossing reads the ADC voltage insted of using the internal ADC comparer.

Like the previos driver, this one implements a speed and direction (clockwise and anti-clockwise) controller.

Speed and direction can be set using the proper function, the speed have to be set using a 0 to 100 interval. 0 will stop the motor, direction has defined macro available for CW and CCW set.

In the sample main file the speed is driven by an RC input library, binded to the external interrupt input PIN.
For the test project the RC signal generator is a servo tester.
The RC reader library can be found here: http://davidegironi.blogspot.com/2018/03/an-avr-atmega-rc-control-signal-reader.html
Settings for this library can be changed on the rcin2.h file.

The bldcsetup.h file containst the main settings. As example there you can find the RC input library enabler.

This driver implements 4 PWM schemes:
  • H PWM L ON: PWM is performed on the ON channel
  • H ON L PWW: PWM is performed on the OFF channel
  • H PWM ON: PWM is performed on UWV channels every 60degrees, starting from U ON-ON
  • H ON PWM: PWM is performed on UWV channels every 60degrees, starting from V OFF-NULL
The PWM scheme is the way the power gates bridge is controlled in order to drive the motor.
One can chose the PWM scheme in the bldcsetup.h file.

The main.c file is a sample running project that use this library to run a motor.

bldcsetupmicro.h file contains specific microcontroller definition, this file should allow the implementation of this library over others than the ATmega8 microcontroller. It contains ADC registry settings and TIMER settings.

This driver can run on many board pinout, input/output PIN can be changed in the bldcsetupboard.h file.
The adc mux schema represent the order of PIN chosen as Zero Crossing detection depending on the running step.

The commutation sequence is defined in the bldc.h file.
By default it is set as follow (first group is the U bridge switch, second is the V, third is the W):
  • Step 1: 10-01-00
  • Step 2: 10-00-01
  • Step 3: 00-10-01
  • Step 4: 01-10-00
  • Step 5: 01-00-10
  • Step 6: 00-01-10
The picture below represent the commutations:


The interface with the user consists in a bounch of function that performs:
  • initialization of the library
  • speed selection or retrive
  • direction selection or retrive
  • speed retrive in RPM
  • debug function to retrive the status of the driver

Two TIMER are implemented:
  • TIMER PWM: works as the PWM emitter, it emit the commutation to the power bridge according to the selected step
  • TIMER MAIN: select the step to emit, depending on speed and direction selection. It works in three modes:
    • startup mode: perform the startup sequence of step
    • emission mode: select the commution step to run and switch to non emission mode
    • non emission mode: check the BEMF for Zero Crossing detection, once the ZC is detected, the mission mode is triggered
Two error detection strategies are implemented, both the strategies can be enabled or disabled at compile time. ZC detection error and Max Ticks between steps detection. Both the strategies trigger a motor startup when the max number of errors is reached. The ZC detection errors makes a +1 increment whenever a ZC crossing should be detected but it was not. The Max Ticks between steps makes a +1 increment whenever a step happens.

The RPM speed estimation is performed assuming the TIMER A runs at a fixed frequency.

Further improvement can be made on the whole driver, expecially the startup stage.

Code

Notes
  • read risk disclaimer
  • excuse my bad english

13 comments:

  1. Thanks Davide for your kind help. I have found the schematic from this blog. But the pic is not clearly visible. Please send me the clear view of schematic if any. Eagerly waiting for your early response.

    ReplyDelete
    Replies
    1. Dear Abhisek, please do not multi/post. Find it here: https://file.io/1PxLhURs

      Delete
    2. Hello, that link was temporary. You can find the board published here: https://davidegironi.blogspot.com/2020/02/bldc-motor-driver-atmega-dev-board.html

      Delete
  2. Sir I've been trying to implement your eclipse project, but ive not been able to generate the hex file. the eclipse doesn't identify it as a C/C++ project.

    ReplyDelete
    Replies
    1. Dear Hassan, you have to build a new project, include the src code, then you can compile it. You can also use VSCode as an alternative. If you google for Eclipse AVR plugin tutorial you will find how to setup Eclipse to build and compile an avrgcc projeect.

      Delete

  3. Hi Engineer, it is possible to enter the password of the file
    Thanks

    ReplyDelete
    Replies
    1. Hello, there is no password to the provided source files.

      Delete
  4. hello sir can you share the hex code of your project
    because i dont know programing well
    but i completely build the hardware
    can you help me?

    ReplyDelete
    Replies
    1. Hello, find it here: https://file.re/2021/11/19/avrlibbldc02-board2sensorless/
      The file will be automatically deleted by November 20, 2021, at 07:13 PM (CET)

      Delete
  5. sir i wrote one of your hex file (avr_lib_bldc02-board2sensorless.hex) from release folder to my at mega 8A-8PU
    but the motor didn't run
    is there anyway to solve my problem
    how can i contact you

    ReplyDelete
    Replies
    1. Hello, please do not multi-post the same question like you have done 4 times for this.
      You can look at the I/O PIN, using a multi-channel oscilloscope. Consider building up your compiling enviroment, my hex is for ATmega running at 16Mhz, so you also have to set Fuse of the micro. Keep in mind that setting fuse in an uncorrect way may brick your micro.

      Delete