cancel
Showing results for 
Search instead for 
Did you mean: 

How to controll esc with pwm signal from RC controller

Matyáš
Associate II

So i have this simple code for pwm input for the rc controller :

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

} }

How to take the value of  capture_value and controll an ESC with it?  i feel like i tried anything at this point and it just wont work. Here is my last attempt 

uint32_t mappedValue[0];
uint32_t pwmInput[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;
}

// Function to set PWM duty cycle for motors
void setMotorPWM(uint32_t motorIndex, uint32_t pwmValue)
{
// Ensure motorIndex is within bounds
if (motorIndex < 4)
{
// Set PWM duty cycle for the corresponding motor channel
if (motorIndex == 0)
__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, pwmValue);
else if (motorIndex == 1)
__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_2, pwmValue);
else if (motorIndex == 2)
__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_3, pwmValue);
else if (motorIndex == 3)
__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_4, pwmValue);

// Optionally, update the duty_cycle array
duty_cycle[motorIndex] = pwmValue;
}
}


void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
// Assuming this callback is triggered based on the PWM input from the RC controller
if (htim == &htim1)
{
// Read the PWM input value from your RC controller (you need to implement this part)
pwmInput[0] = duty_cycle[0];

// Map the PWM input to the desired range for your motors
mappedValue[0] = map(pwmInput[0], 0, 2000, 50, 150);

// Set the PWM duty cycle for the first motor
setMotorPWM(0, mappedValue[0]);
}
}

------------

int main(void)
{
/* USER CODE BEGIN 1 */
// Initialize your hardware peripherals (TIM, GPIO, etc.) and system

// Start PWM for all motor channels
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_2);
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_3);
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_4);

 

 

EDIT: I am using STM32F07G - disc1

8 REPLIES 8

> i tried anything at this point and it just wont work

What is "just wont work"? What are the expectations and what is the observed behaviour? Are the interrupts occuring?

JW

Matyáš
Associate II

When i tried to controll the esc potenciometer

uint16_t ADC_Data=0;
int ADC_Converted = 0;
volatile uint32_t myVariable = 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;
}

the esc calibrates and starts working completely fine, but when i try to use the previous code, the esc isn working at all

@waclawek.jan 

Hello @Matyáš ,

Why two posts for the same question ?

Did you look at the answer I did to your initial post here.

Regards

Cedric

Hello @cedric H 

Sorry , you said I put my question to the wrong section so i wanted to delte the previous but couldnt.

Anyway I downloaded the program, but I couldnt find my STM32F407G-disc1 there. Does that mean it doesnt have any motor controll unit?

Thank you for your time.

Matyáš

Hello,

The proposal was to look at how do we use the timer connected to the PWM input in order to control the speed of the motor. We did it for the ESC-G4 board, that's why I proposed you to generate this project and look at the esc.c file which contains the state machine you are looking for. Your discovery board is not considered as a motor control box, so not part of the MCSDK, but it does not mean it can not work. What is the power board you connected to it ? What kind of firmware do you use to drive your motor ?

Cedric

Hello,@cedric H 

Im usin CubeIDE and basic Lipo battery.

 I am quite new to this, but i thought that controlling the pwm generation by radio controller shouldnt be quite the problem. I tried to use simple potenciometer also by pwm generation and it worked completeley fine. My RC´s output signal is in pwm mode, so i dont see any problem with that. Also on the SWD, the value of rc changes and does whats told.

Could you possibly provide me with some example code for controlling esc by remote control if you have any? 

Matyáš

 

Ok, then I guess you are using a simple DC motor without any advance motor control algorithm. 

I am sorry, but the only example I have is the one I already proposed you, but now I realized it is too much complex for your use case.

Regards

Cedric

@cedric H 

Im using bldc motor with basic esc. It is a school project that I am already behind because of this matter, Could you direct me to someone who can help me with my problem?

Anyway, thank you for your time and patience with me.

Matyáš