how to subtract specific value from counter pulses in each instance in stm32
I am using stm32G431C8U6 microcontroller and I use TB6612 motor driver to control two motors with encoders. I have enabled timer 2 channel 3 and 4 for PWM generation and Timer3 and Timer 4 for combined channel encoder mode. Also I have used Timer15 for timer interrupt. I need to move the two motors for a specific counter pulses (eg:25,000) and then I need to stop them when timer3 counter reaches to specified counter pulses. In the code it will not exactly stop at 25,000(specified counter pulses) and it will deviated more than 25,000 (like 25,123,25,200) .Hence I have used PID function to mitigate this issue. I have enabled timer 15 for interrupt and in that interrupt function I have called back my PID function.
In the PID function it is supposed to be that counter starts from 0 and every 100ms it will increment by one. For an example initially timer 3 counter at 0 and then next 100ms it will reach to one. Then I have designed the pid function as below,
apply_pid(int error)
{
int Kp=0.01;
int16_t output =0;
error = 25,000 - CounterTicks;
output = (Kp)*error;
TIM2->CCR3 == output;
}
In here CounterTicks is the variable for counterpulses. Here I have used only proportional part for simplicity.
According to theory that interrupt function will callback the PID function and there
counterticks will increment by instance of every 100ms and each instance it will calculate error and assign that value to PWM CCR variable.Then error will be proportional to pwm and eventually motors should stop intended counterpulses which is 25,000.
The issue is code does not work as intended. Hence I need your help to configure this issue. Thank you.
