cancel
Showing results for 
Search instead for 
Did you mean: 

PWM Motor Control with STM32

Matyáš
Associate II

So, After many hours of suffering and understanding the basics of CubeIDE I got stuck and cant find any solution.

My main goal was to control bldc motor by RC controller and this is what i have achieved so far :

void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef* htim)
{
if (htim->Instance == TIM2)
{
if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
{
capture_value = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);
if (capture_value)
{
frequency = SystemCoreClock / capture_value;
duty_cycle = 10000 * HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2) / capture_value;
}
} }

The thought was to control the motors by the value of capture_value

 

then I tried to controll the motors by simple potenciometer which also worked really well. Then just withdrawing the potenciometer and implementing the first code.

uint16_t ADC_Data=0;
int ADC_Converted = 0;
long map(long x, long in_min, long in_max, long out_min, long out_max)
{
return (x - in_min)*(out_max - out_min + 1) / (in_max - in_min + 1 )+ out_min;
}

void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)
{
ADC_Converted = map(ADC_Data, 0, 2000, 50, 150);

TIM1->CCR1 = ADC_Converted;
}

I originally thought that I kinda just put those two codes together(of course with some modifications) but i think Im doing something really wrong. It feels like Im really close but missing something really important.

 

Please help me, I dont really know what else to do

 

1 REPLY 1
cedric H
ST Employee

Hello @Matyáš ,

This forum section is dedicated to the Motor control SDK downloadable here.

Reading your post, I understand you implemented your own motor control algorithm. 

In our MCSDK, if you use the inverter B-G431B-ESC1, you can activate the PWM input control:

 

cedricH_1-1705683354897.png

Doing so, the generated project will contain an esc.c file implementing this feature.

The generated IOC will allow you to understand the timer configuration we used.

Best regards

Cedric