cancel
Showing results for 
Search instead for 
Did you mean: 

I use a STM32F103 chip and I try to synchronize two DC motors using inputcapture, I read the time difference between the rising edge of each sensor hall of effect,how can I update the pwm of the two motors. please check with me the pg

kkhli.1
Associate III

void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)

{

if(htim->Instance==TIM1){

if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)

{

if (Is_First_Captured==0) // if the first rising edge is not captured

{

IC1_Val1 =TIM1->CNT; // read the first value of the first sensor

Is_First_Captured = 1; // set the first captured as true

}

}

if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_4)

{

if (Is_First_Captured1==0) // if the first rising edge is not captured

{

IC2_Val1 = TIM1->CNT; // read the first value of the seconde sensor

Is_First_Captured1 = 1; // set the first captured as true

}

Is_First_Captured1 = 0; // set it back to false

}

}

}

// synchronize tow motors

sync_motors(void){

time_diff = IC1_Val1 - IC2_Val1;

// Si le capteur 1 a été activé avant le capteur 2, on augmente le PWM du moteur 1

if (time_diff > 0)

{

  sync_error = time_diff;

  // Augmentation du PWM du moteur 1

  motor1_pwm += 0.1*sync_error;

  if (motor1_pwm > 1000)

  {

    motor1_pwm = 1000;

  }

  TIM3->CCR1 = motor1_pwm;

   //Réduction du PWM du moteur 2

  motor2_pwm -= 0.1*sync_error;

  if (motor2_pwm < 0)

  {

    motor2_pwm = 0;

  }

  TIM2->CCR1 = motor2_pwm;

}

// Si le capteur 2 a été activé avant le capteur 1, on augmente le PWM du moteur 2

else if (time_diff < 0)

{

  sync_error = -time_diff;

  // Augmentation du PWM du moteur 2

  motor2_pwm += 0.01*sync_error;

  if (motor2_pwm > 1000)

  {

    motor2_pwm = 1000;

  }

  TIM2->CCR1 = motor2_pwm;

  // Réduction du PWM du moteur 1

  motor1_pwm -= 0.1*sync_error;

  if (motor1_pwm < 0)

  {

    motor1_pwm = 0;

  }

  TIM3->CCR1 = motor1_pwm;

}

}

int main(void)

{......

while (1)

 {

  

 sync_motors();

   

 }

}

4 REPLIES 4
Sarra.S
ST Employee

Hello @kkhli.1​ and welcome to ST Community

First, it's important to make sure that you have configured the input capture correctly for both sensors.

In your code, it appears that you are only capturing the rising edge of the input signal on channel 1 and channel 4 of the same TIM1!

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

kkhli.1
Associate III

i used TIM1 to capture the  the rising edge of the input signal on channel 1 for the sensor 1 and the channel 4 to capture the  the rising edge of the input signal sensor2 , with live expression i can read the two sensors value0693W00000aJlHHQA0.png

Sarra.S
ST Employee

Hello @kkhli.1​,

I see..

The synchronization logic implemented here in you code assumes that the two DC motors are mechanically coupled in such a way that they should rotate at the same speed.. are they?

to ensure that the PWM updates are applied, the motor control loop should run at a higher frequency than the input capture interrupts.. So, you may want to consider implementing a separate motor control loop that runs at a higher frequency

Hope that helps!

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

kkhli.1
Associate III

yes exactly they should rotate at the same speed.. this is the configuration of the TIM1 used for incapture interrupts :(the clock frequency is 72 MHZ)

 /* USER CODE END TIM1_Init 1 */

 htim1.Instance = TIM1;

 htim1.Init.Prescaler = 72-1;

 htim1.Init.CounterMode = TIM_COUNTERMODE_UP;

 htim1.Init.Period = 0xffff;

 htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

 htim1.Init.RepetitionCounter = 0;

 htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;.............

this is the configuration for TIM2 (pwm1) and the same configuration for the TIM3(pwm2)

/* USER CODE END TIM2_Init 1 */

 htim2.Instance = TIM2;

 htim2.Init.Prescaler = 72-1;

 htim2.Init.CounterMode = TIM_COUNTERMODE_UP;

 htim2.Init.Period = 1000;

 htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

 htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;

what i can do ?