# BLDC driver



# Working Environment

<H4>Tools:</H4>

* Enveronment: ``PlatformIO``

* Framework: ``Arduino``

* Code editor: ``Visual Studio Code``

* FOC: ``Arduino Simple FOC``

<H4>Hardware:</H4>

* Mcu: ``STM32G030C8T6``

# How to build and Flash

Setup Visual studio Code
--------------------------
The VS code setup Instructions are available in <a href="https://code.visualstudio.com/docs">Getting Started</a>.
* Setup Visual studio code according to the Host operating system. </a>
* Install <a href="https://docs.platformio.org/en/latest/integration/ide/vscode.html#ide-vscode">PlatformIo</a> extension inside VS code
* Install <a href="https://docs.platformio.org/en/latest/platforms/ststm32.html">STM32 Platform</a> in PlatformIo
* Install Board support packages for STM32G0C038T6
* Select the board and Framework as Arduino

Building and Flashing Application:
----------------------
To build the application, clone the `affbotics_software` repo and go to the folder `BLDC_driver`
* Pull the <a href="https://github.com/baseapp/affbotics_software">repository</a> locally
* Navigate to the directory ``affbotics_software/firmware/avg/BLDC_driver/``
* Select the COM port
* Click Upload

# Program Flow

Basic Workflow:
----------------

Firmware is present in ``affbotics_software/firmware/avg/BLDC_driver/src/main.cpp``
* BLDC driver board communicates using Modbus over RS485
* The Modbus I registers ``0x2000`` can be Writen with a value from -200 to 200 for setting the velocity of the motor.
* Where -ve values spin the motor in the opposite direction
* comment the line ``#define IAM_LEFT`` for Right motor or leave it uncommented for Left motor

Am a Geek:
------------
* Modbus Register address and ``SLAVE_ID`` can be changed by changing ``#define`` values 
* Motor PID values can be tweaked if needed using `` motor.PID_velocity.P,  motor.PID_velocity.I,  motor.PID_velocity.D``
* change the phase angle ``motor.P_angle.P`` and direction ``motor.initFOC(0, Direction::CW);`` if required.