This example shows how to control motor speed, torque and angle of BLDC gimbal motors using the Field Oriented Control (FOC) algorithm (or not) in C++/Python.

"A Guide to Using BLDC Motors (Gimbals) with SaraKIT: A Comparison of Stepper Motors, Servos, and BLDC Motors"

BLDC motors have become increasingly popular due to their smooth operation, high torque, and precise control. In this guide, we will explore the use of BLDC motors using SaraKIT, and compare them to other commonly used motors such as stepper motors and servos.

In this video, I demonstrate how easy it is to control BLDC gimbal motors, and also discuss the advantages and disadvantages of solutions based on servos or stepper motors. I also show the difference in controlling gimbal motors that have a decoder and those that do not.

Gimbal motors with a decoder are controlled using the Field Oriented Control (FOC) algorithm. The condensed knowledge presented here is based on many materials from the internet, such as the "Simple Field Oriented Control BLDC driver" for Arduino systems. In future videos, we will show how to use gimbal control to create a self-balancing robot, RC car, precise Pan/Tilt camera control, or creating a voice assistant based on Amazon Alexa, Google Home, or ChatGPT that does not require continuous calling of the keyword "Alexa...", but just looking at it.

Code examples in Pascal and C++ is created for Raspberry Pi 4 in CM4 version. SaraKIT requires 12-24VDC min. 20W when you use motors in your project, you should use a correspondingly larger power supply. For mobile devices, we recommend a simple connection to the PowerBank (PD2.0 PD3.0 Fast Charging and QC4 with USB-C output) with a special PD 12v cable. Or by connecting a USB cable via USB-C Pd Trigger Module Pd 12v.




Some functions of bldc motors from lib/SaraKIT/devices.hpp:

Rotate the motor motorID by angle with speed and torque.
the angle can be entered in degrees where 360 is a full turn, or radians where a full turn is 2xPi (2x3.14=6.28):

BLDCMotor_MoveToAngle(uchar motorId, float angle, float speed, uchar torque, bool degrees);

 

Rotate the motor motorID by angle with speed and torque:

BLDCMotor_MoveToAngle(uchar motorId, float angle, float speed, uchar torque, bool degrees);

 

Knowing the diameter of the wheel, we can move the vehicle by a certain number of centimeters or meters:

BLDCMotor_DriveMeters(uchar motorId, float centimeters, float speed, uchar torque, float WhellDiameter);

 

We can also rotate the motor like a DC motor by adding only power (rotation depend on the added torque, the more torque the motor rotates faster):

BLDCMotor_MoveContinuousTorque(uchar motorId, int8_t direction, uchar torque);

 

We can also rotate the motor at a certain velocity:

BLDCMotor_MoveContinuousVelocity(uchar motorId, int8_t direction, uchar torque, float speed, bool degrees);

 

If you use motors without an encoder, it is worth making them go into sleep mode after a certain time when you do not move them.
Otherwise, they will get very hot unnecessarily. Determine after what time and to what torque they will reach with the function:

BLDCMotor_IdleTorque(uchar motorId, uchar torque, unsigned short torqueMs);

 

Retrieving the current engine position:

Encoder_Get(EncoderId).angle;

 

Current velocity:

Encoder_Get(EncoderId).velocity;

or without encoder:

printf("%.2f",BLDCMotor_GetInfo(motorId,2,false).fdata1);

 

more lib/SaraKIT/devices.hpp

Code Example:

 

#Attention, BLDC motors get very hot (this applies especially to motors without encoders that adjust power to the load) - do not leave them operating at too high power - risk of burns and fire!

 

You can find C++ and Python code for Raspberry Pi4 in the
SaraKIT Github repository:
https://github.com/SaraEye

Examples:

https://github.com/SaraEye/SaraKIT-BLDC-gimbal-motors-FOC-Raspberry-Pi-64bit

https://github.com/SaraEye/SaraKIT-AS5048A-AS5600-encoder-Raspberry-Pi

https://github.com/SaraEye/SaraKIT-Python-Examples (Python)